-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLexiGuess.java
More file actions
1498 lines (1341 loc) · 74.7 KB
/
LexiGuess.java
File metadata and controls
1498 lines (1341 loc) · 74.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.nio.file.*;
import java.util.*;
import java.util.List;
import javax.sound.sampled.*;
import javax.swing.*;
import javax.swing.Timer;
public class LexiGuess extends JFrame {
// ── Word Entry (loaded from JSON) ─────────────────────────────────────────
static class WordEntry {
String word;
String clue;
String difficulty;
WordEntry(String word, String clue, String difficulty) {
this.word = word.toUpperCase().trim();
this.clue = clue;
this.difficulty = difficulty.toUpperCase().trim();
}
}
// ── JSON Loader ───────────────────────────────────────────────────────────
private static List<WordEntry> loadJson(String filePath) {
List<WordEntry> entries = new ArrayList<>();
try {
String raw = new String(Files.readAllBytes(Paths.get(filePath)));
String[] objects = raw.split("\\{");
for (String obj : objects) {
obj = obj.trim();
if (obj.isEmpty()) continue;
obj = obj.replaceAll("[}\\]]+\\s*$", "").trim();
String word = extractJsonValue(obj, "words");
if (word == null || word.isEmpty())
word = extractJsonValue(obj, "word");
String clue = extractJsonValue(obj, "clue");
String difficulty = extractJsonValue(obj, "difficulty");
if (word != null && !word.isEmpty()
&& clue != null && difficulty != null) {
entries.add(new WordEntry(word, clue, difficulty));
}
}
} catch (IOException e) {
System.err.println("Could not load " + filePath + ": " + e.getMessage());
}
return entries;
}
private static String extractJsonValue(String obj, String key) {
String pattern = "\"" + key + "\"\\s*:\\s*\"([^\"]+)\"";
java.util.regex.Matcher m =
java.util.regex.Pattern.compile(pattern).matcher(obj);
return m.find() ? m.group(1).trim() : null;
}
// ── Word Banks ────────────────────────────────────────────────────────────
private List<WordEntry> easyWords = new ArrayList<>();
private List<WordEntry> mediumWords = new ArrayList<>();
private List<WordEntry> hardWords = new ArrayList<>();
private void loadAllWords() {
easyWords = loadJson("easy.json");
mediumWords = loadJson("medium.json");
hardWords = loadJson("hard.json");
if (easyWords.isEmpty()) {
for (String[] e : new String[][]{
{"CODE", "Instructions written for a computer to execute."},
{"LOOP", "A structure that repeats a block of code."},
{"BYTE", "A unit of data equal to 8 bits."},
{"TYPE", "A classification specifying the kind of value a variable holds."},
{"VOID", "A return type indicating a function returns nothing."}
}) easyWords.add(new WordEntry(e[0], e[1], "EASY"));
}
if (mediumWords.isEmpty()) {
for (String[] e : new String[][]{
{"ARRAY", "A collection of elements stored at contiguous memory locations."},
{"STACK", "A linear data structure following Last-In-First-Out order."},
{"QUEUE", "A linear data structure following First-In-First-Out order."},
{"PARSE", "To analyse a string of symbols according to formal grammar rules."},
{"CACHE", "Temporary storage that speeds up future data requests."}
}) mediumWords.add(new WordEntry(e[0], e[1], "MEDIUM"));
}
if (hardWords.isEmpty()) {
for (String[] e : new String[][]{
{"MUTEX", "A synchronisation primitive that prevents simultaneous resource access."},
{"PRAGMA", "A compiler directive that provides additional information to the compiler."},
{"LAMBDA", "An anonymous function defined without a name."},
{"DAEMON", "A background process that runs without direct user interaction."},
{"ENDIAN", "Describes the byte order used to represent multi-byte data."}
}) hardWords.add(new WordEntry(e[0], e[1], "HARD"));
}
}
// ── Colours ───────────────────────────────────────────────────────────────
private static final Color BG_DARK = new Color(8, 10, 18);
private static final Color BG_PANEL = new Color(14, 17, 28);
private static final Color GOLD = new Color(212, 175, 95);
private static final Color GOLD_DIM = new Color(140, 110, 50);
private static final Color SILVER = new Color(180, 185, 200);
private static final Color TEXT_WHITE = new Color(235, 232, 225);
private static final Color TEXT_MUTED = new Color(120, 118, 130);
private static final Color TILE_EMPTY_BG = new Color(22, 26, 42);
private static final Color TILE_EMPTY_BD = new Color(55, 55, 75);
private static final Color GREEN_CORRECT = new Color(60, 140, 90);
private static final Color YELLOW_PRESENT = new Color(190, 155, 40);
private static final Color GRAY_ABSENT = new Color(45, 45, 58);
private static final Color RED_DANGER = new Color(180, 60, 60);
private static final Color EASY_CLR = new Color(70, 170, 100);
private static final Color MEDIUM_CLR = new Color(190, 155, 40);
private static final Color HARD_CLR = new Color(180, 70, 70);
private static final Color CLUE_CLR = new Color(140, 190, 255);
// ── Sound ─────────────────────────────────────────────────────────────────
private static final String SOUND_CORRECT = "chrisiex1-correct-156911 (1).wav";
private static final String SOUND_LOSE = "freesound_community-080047_lose_funny_re.wav";
private static final String SOUND_KEYCLICK = "creatorshome-keyboard-click-327728.wav";
private static final String SOUND_START = "creatorshome-keyboard-click-327728.wav";
// ── Game State ────────────────────────────────────────────────────────────
private WordEntry currentEntry;
private String targetWord = "";
private String currentClue = "";
private String difficulty = "EASY";
private int maxAttempts = 6;
private int currentAttempt = 0;
private int sessionScore = 0;
private int currentStage = 1;
private boolean gameOver = false;
private boolean gameWon = false;
private List<String> guesses = new ArrayList<>();
private List<int[]> feedbackList = new ArrayList<>();
private Set<Character> correctLetters = new HashSet<>();
private Set<Character> presentLetters = new HashSet<>();
private Set<Character> absentLetters = new HashSet<>();
private Set<String> usedWords = new HashSet<>();
private int currentLevel = 1;
private int totalLevels = 0;
private int wordLength;
// ── Per-difficulty level progress (tracks highest unlocked level per diff) ─
private Map<String, Integer> difficultyProgress = new HashMap<>();
private List<LeaderboardEntry> leaderboard = new ArrayList<>();
private String currentPlayerName = "Player";
private final DatabaseManager db = new DatabaseManager();
// ── UI ────────────────────────────────────────────────────────────────────
private CardLayout cardLayout;
private JPanel cardPanel;
private JLabel[][] tiles;
private JPanel gridPanel;
private HangmanPanel hangmanPanel;
private JTextField inputField;
private JButton submitBtn;
private JLabel messageLabel;
private JLabel clueLabel;
private JLabel difficultyLabel;
private JLabel inGameScoreLabel;
private JLabel sessionScoreLabel;
private JLabel stageLabel;
private Map<Character, JLabel> keyLabels = new HashMap<>();
private JPanel keyboardPanel;
private JPanel leaderboardListPanel;
private JPanel levelSelectPanel;
// ── Sound ─────────────────────────────────────────────────────────────────
private void playSound(String filePath) {
new Thread(() -> {
try {
File f = new File(filePath);
if (!f.exists()) return;
AudioInputStream ai = AudioSystem.getAudioInputStream(f);
Clip clip = AudioSystem.getClip();
clip.open(ai);
clip.start();
clip.addLineListener(ev -> { if (ev.getType() == LineEvent.Type.STOP) clip.close(); });
} catch (Exception ignored) {}
}).start();
}
// ── Constructor ───────────────────────────────────────────────────────────
public LexiGuess() {
loadAllWords();
db.connect();
Runtime.getRuntime().addShutdownHook(new Thread(db::disconnect));
// Initialise progress: level 1 unlocked for each difficulty
difficultyProgress.put("EASY", 1);
difficultyProgress.put("MEDIUM", 1);
difficultyProgress.put("HARD", 1);
setTitle("LexiGuess - Programming Enhancement Game");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
getContentPane().setBackground(BG_DARK);
cardLayout = new CardLayout();
cardPanel = new JPanel(cardLayout);
cardPanel.setBackground(BG_DARK);
cardPanel.add(buildStartScreen(), "START");
cardPanel.add(buildMenuScreen(), "MENU");
cardPanel.add(buildLevelSelectScreen(), "LEVELSELECT");
cardPanel.add(buildGameScreen(), "GAME");
cardPanel.add(buildInstructionsScreen(), "INSTRUCTIONS");
cardPanel.add(buildLeaderboardScreen(), "LEADERBOARD");
add(cardPanel);
setSize(960, 820);
setLocationRelativeTo(null);
setVisible(true);
cardLayout.show(cardPanel, "START");
}
// ── Helpers ───────────────────────────────────────────────────────────────
static class GradientPanel extends JPanel {
private Color top, bottom;
GradientPanel(Color top, Color bottom) { this.top = top; this.bottom = bottom; setOpaque(false); }
@Override protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setPaint(new GradientPaint(0, 0, top, 0, getHeight(), bottom));
g2.fillRect(0, 0, getWidth(), getHeight());
super.paintComponent(g);
}
}
private JLabel makeOrnamentLabel(String text) {
JLabel lbl = new JLabel(text, SwingConstants.CENTER);
lbl.setFont(new Font("Serif", Font.PLAIN, 14));
lbl.setForeground(GOLD_DIM);
lbl.setAlignmentX(CENTER_ALIGNMENT);
return lbl;
}
private JButton makeFancyButton(String text, Color bgColor, Color borderColor) {
JButton btn = new JButton(text) {
@Override protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(bgColor);
g2.fillRoundRect(0, 0, getWidth(), getHeight(), 8, 8);
super.paintComponent(g);
}
};
btn.setFont(new Font("Serif", Font.BOLD, 13));
btn.setForeground(TEXT_WHITE);
btn.setContentAreaFilled(false);
btn.setBorderPainted(false);
btn.setFocusPainted(false);
btn.setOpaque(false);
btn.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
btn.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(borderColor, 1),
BorderFactory.createEmptyBorder(6, 16, 6, 16)));
return btn;
}
private JButton makeLinkButton(String text) {
JButton btn = new JButton(text);
btn.setFont(new Font("Serif", Font.PLAIN, 13));
btn.setForeground(GOLD_DIM);
btn.setContentAreaFilled(false);
btn.setBorderPainted(false);
btn.setFocusPainted(false);
btn.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
btn.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e) { btn.setForeground(GOLD); }
public void mouseExited(MouseEvent e) { btn.setForeground(GOLD_DIM); }
});
return btn;
}
// ── End Game Dialog ───────────────────────────────────────────────────────
private void showEndGameDialog(String title, String message, String word,
String subMessage, Color accentColor, String retryLabel) {
JDialog dialog = new JDialog(this, title, true);
dialog.setUndecorated(true);
dialog.setSize(450, 380);
dialog.setLocationRelativeTo(this);
JPanel panel = new JPanel() {
@Override protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(BG_PANEL);
g2.fillRoundRect(0, 0, getWidth(), getHeight(), 24, 24);
g2.setStroke(new BasicStroke(2f));
g2.setColor(accentColor);
g2.drawRoundRect(1, 1, getWidth()-3, getHeight()-3, 24, 24);
}
};
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setBorder(BorderFactory.createEmptyBorder(28, 40, 26, 40));
panel.setOpaque(false);
JLabel ornament = new JLabel("--- \u2726 ---", SwingConstants.CENTER);
ornament.setFont(new Font("Serif", Font.PLAIN, 14));
ornament.setForeground(GOLD_DIM);
ornament.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(ornament);
panel.add(Box.createVerticalStrut(10));
JLabel titleLabel = new JLabel(title, SwingConstants.CENTER);
titleLabel.setFont(new Font("Serif", Font.BOLD | Font.ITALIC, 28));
titleLabel.setForeground(accentColor.equals(RED_DANGER) ? RED_DANGER : GOLD);
titleLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(titleLabel);
panel.add(Box.createVerticalStrut(10));
JLabel msgLabel = new JLabel(message, SwingConstants.CENTER);
msgLabel.setFont(new Font("Serif", Font.PLAIN, 14));
msgLabel.setForeground(TEXT_MUTED);
msgLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(msgLabel);
panel.add(Box.createVerticalStrut(5));
JLabel wordLabel = new JLabel(word, SwingConstants.CENTER);
wordLabel.setFont(new Font("Monospaced", Font.BOLD, 32));
wordLabel.setForeground(TEXT_WHITE);
wordLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(wordLabel);
panel.add(Box.createVerticalStrut(8));
JLabel clueDialogLabel = new JLabel(
"<html><div style='text-align:center;width:320px'>" + currentClue + "</div></html>",
SwingConstants.CENTER);
clueDialogLabel.setFont(new Font("Serif", Font.ITALIC, 12));
clueDialogLabel.setForeground(CLUE_CLR);
clueDialogLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(clueDialogLabel);
panel.add(Box.createVerticalStrut(8));
JLabel subLabel = new JLabel(subMessage, SwingConstants.CENTER);
subLabel.setFont(new Font("Serif", Font.ITALIC, 13));
subLabel.setForeground(GOLD_DIM);
subLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(subLabel);
panel.add(Box.createVerticalStrut(22));
JPanel btnPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 12, 0));
btnPanel.setOpaque(false);
Color retryBg = accentColor.equals(RED_DANGER) ? new Color(50,20,20) : new Color(20,40,20);
JButton retryBtn = makeFancyButton(retryLabel, retryBg, accentColor);
retryBtn.setFont(new Font("Serif", Font.BOLD, 14));
retryBtn.addActionListener(e -> { dialog.dispose(); startGame(); });
// "Level Select" button so player can pick a different level after winning/losing
JButton lvlBtn = makeFancyButton("Level Select", new Color(20,25,45), GOLD_DIM);
lvlBtn.setFont(new Font("Serif", Font.BOLD, 14));
lvlBtn.addActionListener(e -> {
dialog.dispose();
updateLevelSelectScreen();
cardLayout.show(cardPanel, "LEVELSELECT");
});
JButton exitBtn = makeFancyButton("Exit", new Color(30,28,35), GOLD_DIM);
exitBtn.setFont(new Font("Serif", Font.BOLD, 14));
exitBtn.addActionListener(e -> System.exit(0));
btnPanel.add(retryBtn);
btnPanel.add(lvlBtn);
btnPanel.add(exitBtn);
btnPanel.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(btnPanel);
dialog.setContentPane(panel);
dialog.getContentPane().setBackground(BG_DARK);
dialog.setVisible(true);
}
// ── Start Screen ──────────────────────────────────────────────────────────
private JPanel buildStartScreen() {
JPanel root = new GradientPanel(BG_DARK, new Color(12, 15, 28));
root.setLayout(new BorderLayout());
JPanel center = new JPanel();
center.setOpaque(false);
center.setLayout(new BoxLayout(center, BoxLayout.Y_AXIS));
center.setBorder(BorderFactory.createEmptyBorder(100, 80, 100, 80));
center.add(makeOrnamentLabel("--- * ---"));
center.add(Box.createVerticalStrut(20));
JLabel title = new JLabel("LexiGuess", SwingConstants.CENTER);
title.setFont(new Font("Serif", Font.BOLD | Font.ITALIC, 72));
title.setForeground(GOLD);
title.setAlignmentX(CENTER_ALIGNMENT);
center.add(title);
JLabel sub = new JLabel("--- Programming Enhancement Game ---", SwingConstants.CENTER);
sub.setFont(new Font("Serif", Font.ITALIC, 18));
sub.setForeground(GOLD_DIM);
sub.setAlignmentX(CENTER_ALIGNMENT);
center.add(sub);
center.add(Box.createVerticalStrut(40));
center.add(makeOrnamentLabel("* o *"));
center.add(Box.createVerticalStrut(50));
JLabel nameLbl = new JLabel("Enter Your Name:", SwingConstants.CENTER);
nameLbl.setFont(new Font("Serif", Font.BOLD, 18));
nameLbl.setForeground(TEXT_WHITE);
nameLbl.setAlignmentX(CENTER_ALIGNMENT);
center.add(nameLbl);
center.add(Box.createVerticalStrut(10));
JTextField nameField = new JTextField("Player", 15);
nameField.setFont(new Font("Serif", Font.PLAIN, 18));
nameField.setForeground(TEXT_WHITE);
nameField.setBackground(new Color(22, 26, 42));
nameField.setCaretColor(GOLD);
nameField.setHorizontalAlignment(SwingConstants.CENTER);
nameField.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(GOLD_DIM, 1),
BorderFactory.createEmptyBorder(8, 15, 8, 15)));
nameField.setMaximumSize(new Dimension(250, 45));
nameField.setAlignmentX(CENTER_ALIGNMENT);
center.add(nameField);
center.add(Box.createVerticalStrut(40));
JButton startBtn = new JButton("START GAME") {
@Override protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setPaint(new GradientPaint(0, 0, GOLD, 0, getHeight(), GOLD_DIM));
g2.fillRoundRect(0, 0, getWidth(), getHeight(), 12, 12);
super.paintComponent(g);
}
};
startBtn.setFont(new Font("Serif", Font.BOLD, 24));
startBtn.setForeground(BG_DARK);
startBtn.setContentAreaFilled(false);
startBtn.setBorderPainted(false);
startBtn.setFocusPainted(false);
startBtn.setOpaque(false);
startBtn.setPreferredSize(new Dimension(250, 60));
startBtn.setMaximumSize(new Dimension(250, 60));
startBtn.setAlignmentX(CENTER_ALIGNMENT);
startBtn.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
startBtn.addActionListener(e -> {
String name = nameField.getText().trim();
if (!name.isEmpty()) currentPlayerName = name;
System.out.println("[TEST] Player name is: " + currentPlayerName);
System.out.println("[TEST] DB connected: " + db.isConnected());
// Restore saved progress for this player from the database
if (db.isConnected()) {
java.util.Map<String, Integer> saved = db.loadAllProgress(currentPlayerName);
difficultyProgress.put("EASY", saved.get("EASY"));
difficultyProgress.put("MEDIUM", saved.get("MEDIUM"));
difficultyProgress.put("HARD", saved.get("HARD"));
}
playSound(SOUND_START);
cardLayout.show(cardPanel, "MENU");
});
startBtn.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e) { startBtn.setForeground(TEXT_WHITE); }
public void mouseExited(MouseEvent e) { startBtn.setForeground(BG_DARK); }
});
center.add(startBtn);
center.add(Box.createVerticalStrut(30));
JButton leaderBtn = makeFancyButton("View Leaderboard", new Color(25,22,12), GOLD_DIM);
leaderBtn.setAlignmentX(CENTER_ALIGNMENT);
leaderBtn.addActionListener(e -> { updateLeaderboardDisplay(); cardLayout.show(cardPanel, "LEADERBOARD"); });
center.add(leaderBtn);
center.add(Box.createVerticalStrut(15));
JButton exitBtn = makeFancyButton("Exit Game", new Color(50,20,20), RED_DANGER);
exitBtn.setAlignmentX(CENTER_ALIGNMENT);
exitBtn.addActionListener(e -> System.exit(0));
center.add(exitBtn);
root.add(center, BorderLayout.CENTER);
JLabel bot = new JLabel("* o *", SwingConstants.CENTER);
bot.setFont(new Font("Serif", Font.PLAIN, 14));
bot.setForeground(GOLD_DIM);
bot.setBorder(BorderFactory.createEmptyBorder(0, 0, 30, 0));
root.add(bot, BorderLayout.SOUTH);
return root;
}
// ── Menu Screen ───────────────────────────────────────────────────────────
private JPanel buildMenuScreen() {
JPanel root = new GradientPanel(BG_DARK, new Color(12, 15, 28));
root.setLayout(new BorderLayout());
JPanel topBar = new JPanel(new FlowLayout(FlowLayout.RIGHT, 20, 12));
topBar.setOpaque(false);
JButton instrBtn = makeLinkButton("How to Play");
instrBtn.addActionListener(e -> cardLayout.show(cardPanel, "INSTRUCTIONS"));
JButton leaderBtn = makeLinkButton("Leaderboard");
leaderBtn.addActionListener(e -> { updateLeaderboardDisplay(); cardLayout.show(cardPanel, "LEADERBOARD"); });
JButton exitBtn = makeLinkButton("Exit");
exitBtn.addActionListener(e -> System.exit(0));
topBar.add(instrBtn); topBar.add(leaderBtn); topBar.add(exitBtn);
root.add(topBar, BorderLayout.NORTH);
JPanel center = new JPanel();
center.setOpaque(false);
center.setLayout(new BoxLayout(center, BoxLayout.Y_AXIS));
center.setBorder(BorderFactory.createEmptyBorder(10, 80, 20, 80));
center.add(makeOrnamentLabel("--- * ---"));
center.add(Box.createVerticalStrut(10));
JLabel title = new JLabel("LexiGuess", SwingConstants.CENTER);
title.setFont(new Font("Serif", Font.BOLD | Font.ITALIC, 58));
title.setForeground(GOLD); title.setAlignmentX(CENTER_ALIGNMENT);
center.add(title);
JLabel sub = new JLabel("--- Programming Enhancement Game ---", SwingConstants.CENTER);
sub.setFont(new Font("Serif", Font.ITALIC, 15));
sub.setForeground(GOLD_DIM); sub.setAlignmentX(CENTER_ALIGNMENT);
center.add(sub);
center.add(Box.createVerticalStrut(8));
center.add(makeOrnamentLabel("* o *"));
center.add(Box.createVerticalStrut(28));
JLabel chooseLbl = new JLabel("Choose Your Challenge", SwingConstants.CENTER);
chooseLbl.setFont(new Font("Serif", Font.BOLD, 22));
chooseLbl.setForeground(TEXT_WHITE); chooseLbl.setAlignmentX(CENTER_ALIGNMENT);
center.add(chooseLbl);
center.add(Box.createVerticalStrut(20));
JPanel diffRow = new JPanel(new FlowLayout(FlowLayout.CENTER, 22, 0));
diffRow.setOpaque(false);
diffRow.add(makeDiffCard("EASY", "Novice", "Common concepts", EASY_CLR, "*", easyWords.size()));
diffRow.add(makeDiffCard("MEDIUM", "Scholar", "Intermediate terms", MEDIUM_CLR, "**", mediumWords.size()));
diffRow.add(makeDiffCard("HARD", "Mastermind", "Advanced jargon", HARD_CLR, "***", hardWords.size()));
center.add(diffRow);
center.add(Box.createVerticalStrut(30));
sessionScoreLabel = new JLabel("Session Score: 0 | Stage: 1", SwingConstants.CENTER);
sessionScoreLabel.setFont(new Font("Serif", Font.BOLD, 16));
sessionScoreLabel.setForeground(GOLD); sessionScoreLabel.setAlignmentX(CENTER_ALIGNMENT);
center.add(sessionScoreLabel);
root.add(center, BorderLayout.CENTER);
JLabel bot = new JLabel("* o *", SwingConstants.CENTER);
bot.setFont(new Font("Serif", Font.PLAIN, 14));
bot.setForeground(GOLD_DIM);
bot.setBorder(BorderFactory.createEmptyBorder(0, 0, 16, 0));
root.add(bot, BorderLayout.SOUTH);
return root;
}
// ── Difficulty Card ───────────────────────────────────────────────────────
private JPanel makeDiffCard(String diff, String rankTitle, String descriptor,
Color accent, String stars, int wordCount) {
JPanel card = new JPanel() {
@Override protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setPaint(new GradientPaint(0, 0,
new Color(accent.getRed()/7, accent.getGreen()/7, accent.getBlue()/7),
0, getHeight(),
new Color(accent.getRed()/11, accent.getGreen()/11, accent.getBlue()/11)));
g2.fillRoundRect(0, 0, getWidth(), getHeight(), 16, 16);
g2.setColor(accent);
g2.setStroke(new BasicStroke(1.5f));
g2.drawRoundRect(1, 1, getWidth()-3, getHeight()-3, 15, 15);
}
};
card.setLayout(new BoxLayout(card, BoxLayout.Y_AXIS));
card.setPreferredSize(new Dimension(208, 230));
card.setOpaque(false);
card.setBorder(BorderFactory.createEmptyBorder(18, 16, 18, 16));
card.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
JLabel starsLbl = new JLabel(stars, SwingConstants.CENTER);
starsLbl.setFont(new Font("Serif", Font.PLAIN, 13));
starsLbl.setForeground(accent); starsLbl.setAlignmentX(CENTER_ALIGNMENT);
JLabel titleLbl = new JLabel(diff, SwingConstants.CENTER);
titleLbl.setFont(new Font("Serif", Font.BOLD, 24));
titleLbl.setForeground(accent); titleLbl.setAlignmentX(CENTER_ALIGNMENT);
JLabel rankLbl = new JLabel(rankTitle, SwingConstants.CENTER);
rankLbl.setFont(new Font("Serif", Font.ITALIC, 13));
rankLbl.setForeground(SILVER); rankLbl.setAlignmentX(CENTER_ALIGNMENT);
JPanel sep = new JPanel() {
@Override protected void paintComponent(Graphics g) {
g.setColor(new Color(accent.getRed(), accent.getGreen(), accent.getBlue(), 80));
g.drawLine(10, 0, getWidth()-10, 0);
}
};
sep.setOpaque(false); sep.setPreferredSize(new Dimension(160, 4));
sep.setMaximumSize(new Dimension(160, 4)); sep.setAlignmentX(CENTER_ALIGNMENT);
JLabel descLbl = new JLabel(descriptor, SwingConstants.CENTER);
descLbl.setFont(new Font("SansSerif", Font.PLAIN, 12));
descLbl.setForeground(TEXT_MUTED); descLbl.setAlignmentX(CENTER_ALIGNMENT);
JLabel countLbl = new JLabel("6 attempts | " + wordCount + " levels", SwingConstants.CENTER);
countLbl.setFont(new Font("SansSerif", Font.PLAIN, 12));
countLbl.setForeground(TEXT_MUTED); countLbl.setAlignmentX(CENTER_ALIGNMENT);
JLabel clueLbl = new JLabel("Clue included \u2139", SwingConstants.CENTER);
clueLbl.setFont(new Font("SansSerif", Font.ITALIC, 11));
clueLbl.setForeground(CLUE_CLR); clueLbl.setAlignmentX(CENTER_ALIGNMENT);
JButton playBtn = new JButton("Select Level ->") {
@Override protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(accent);
g2.fillRoundRect(0, 0, getWidth(), getHeight(), 8, 8);
super.paintComponent(g);
}
};
playBtn.setFont(new Font("Serif", Font.BOLD, 12));
playBtn.setForeground(BG_DARK);
playBtn.setContentAreaFilled(false); playBtn.setBorderPainted(false);
playBtn.setFocusPainted(false); playBtn.setOpaque(false);
playBtn.setMaximumSize(new Dimension(160, 32)); playBtn.setAlignmentX(CENTER_ALIGNMENT);
playBtn.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
// ── Goes to Level Select, not directly to game ──
playBtn.addActionListener(e -> {
difficulty = diff;
// Restore saved progress for this difficulty
currentLevel = difficultyProgress.getOrDefault(diff, 1);
updateLevelSelectScreen();
cardLayout.show(cardPanel, "LEVELSELECT");
});
card.add(starsLbl);
card.add(Box.createVerticalStrut(4));
card.add(titleLbl);
card.add(rankLbl);
card.add(Box.createVerticalStrut(8));
card.add(sep);
card.add(Box.createVerticalStrut(8));
card.add(descLbl);
card.add(countLbl);
card.add(Box.createVerticalStrut(4));
card.add(clueLbl);
card.add(Box.createVerticalGlue());
card.add(playBtn);
card.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e) {
card.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(accent, 2),
BorderFactory.createEmptyBorder(17,15,17,15)));
card.repaint();
}
public void mouseExited(MouseEvent e) {
card.setBorder(BorderFactory.createEmptyBorder(18,16,18,16));
card.repaint();
}
});
return card;
}
// ── Level Select Screen ───────────────────────────────────────────────────
private JPanel buildLevelSelectScreen() {
JPanel root = new GradientPanel(BG_DARK, new Color(12, 15, 28));
root.setLayout(new BorderLayout());
// Top bar
JPanel topBar = new JPanel(new BorderLayout());
topBar.setOpaque(false);
topBar.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createMatteBorder(0, 0, 1, 0, GOLD_DIM),
BorderFactory.createEmptyBorder(12, 18, 12, 18)));
JButton backBtn = makeFancyButton("<- Back", new Color(25,22,12), GOLD_DIM);
backBtn.addActionListener(e -> cardLayout.show(cardPanel, "MENU"));
topBar.add(backBtn, BorderLayout.WEST);
JLabel titleLbl = new JLabel("Select Level", SwingConstants.CENTER);
titleLbl.setFont(new Font("Serif", Font.BOLD | Font.ITALIC, 28));
titleLbl.setForeground(GOLD);
topBar.add(titleLbl, BorderLayout.CENTER);
root.add(topBar, BorderLayout.NORTH);
// Scrollable content area — populated by updateLevelSelectScreen()
levelSelectPanel = new JPanel();
levelSelectPanel.setOpaque(false);
levelSelectPanel.setBorder(BorderFactory.createEmptyBorder(28, 50, 28, 50));
JScrollPane scroll = new JScrollPane(levelSelectPanel);
scroll.setOpaque(false);
scroll.getViewport().setOpaque(false);
scroll.setBorder(null);
scroll.getVerticalScrollBar().setUnitIncrement(16);
root.add(scroll, BorderLayout.CENTER);
// Bottom ornament
JLabel bot = new JLabel("* o *", SwingConstants.CENTER);
bot.setFont(new Font("Serif", Font.PLAIN, 14));
bot.setForeground(GOLD_DIM);
bot.setBorder(BorderFactory.createEmptyBorder(0, 0, 14, 0));
root.add(bot, BorderLayout.SOUTH);
return root;
}
// ── Populate / refresh Level Select ───────────────────────────────────────
private void updateLevelSelectScreen() {
levelSelectPanel.removeAll();
levelSelectPanel.setLayout(new BoxLayout(levelSelectPanel, BoxLayout.Y_AXIS));
List<WordEntry> bank = difficulty.equals("EASY") ? easyWords
: difficulty.equals("MEDIUM") ? mediumWords : hardWords;
totalLevels = bank.size();
Color accent = difficulty.equals("EASY") ? EASY_CLR
: difficulty.equals("MEDIUM") ? MEDIUM_CLR : HARD_CLR;
// How far the player has progressed in this difficulty
int highestUnlocked = difficultyProgress.getOrDefault(difficulty, 1);
// ── Difficulty title ──────────────────────────────────────────────
JLabel diffLbl = new JLabel(difficulty + " — " + totalLevels + " Levels",
SwingConstants.CENTER);
diffLbl.setFont(new Font("Serif", Font.BOLD | Font.ITALIC, 24));
diffLbl.setForeground(accent);
diffLbl.setAlignmentX(CENTER_ALIGNMENT);
levelSelectPanel.add(diffLbl);
// Progress text
int cleared = highestUnlocked - 1;
JLabel progressLbl = new JLabel(
cleared + " of " + totalLevels + " levels cleared",
SwingConstants.CENTER);
progressLbl.setFont(new Font("SansSerif", Font.PLAIN, 13));
progressLbl.setForeground(TEXT_MUTED);
progressLbl.setAlignmentX(CENTER_ALIGNMENT);
levelSelectPanel.add(Box.createVerticalStrut(6));
levelSelectPanel.add(progressLbl);
levelSelectPanel.add(Box.createVerticalStrut(24));
// ── Level grid: 5 columns ─────────────────────────────────────────
int cols = 5;
int rows = (int) Math.ceil((double) totalLevels / cols);
JPanel grid = new JPanel(new GridLayout(rows, cols, 16, 16));
grid.setOpaque(false);
grid.setMaximumSize(new Dimension(660, rows * 110));
grid.setAlignmentX(CENTER_ALIGNMENT);
for (int i = 1; i <= totalLevels; i++) {
final int levelNum = i;
boolean cleared2 = (i < highestUnlocked);
boolean unlocked = (i <= highestUnlocked);
// ── Cell panel ────────────────────────────────────────────────
JPanel cell = new JPanel() {
@Override protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
Color bg;
if (cleared2) bg = new Color(accent.getRed()/6, accent.getGreen()/6, accent.getBlue()/6);
else if (unlocked) bg = new Color(22, 26, 42);
else bg = new Color(15, 17, 26);
g2.setColor(bg);
g2.fillRoundRect(0, 0, getWidth(), getHeight(), 14, 14);
g2.setStroke(new BasicStroke(cleared2 ? 2f : 1.5f));
g2.setColor(cleared2 ? accent : unlocked ? GOLD_DIM : new Color(40,42,55));
g2.drawRoundRect(1, 1, getWidth()-3, getHeight()-3, 13, 13);
}
};
cell.setLayout(new BoxLayout(cell, BoxLayout.Y_AXIS));
cell.setOpaque(false);
cell.setPreferredSize(new Dimension(110, 95));
cell.setBorder(BorderFactory.createEmptyBorder(10, 8, 10, 8));
if (unlocked) {
cell.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
}
// Level number or lock icon
String numText = unlocked ? String.valueOf(levelNum) : "\uD83D\uDD12";
JLabel numLbl = new JLabel(numText, SwingConstants.CENTER);
numLbl.setFont(new Font("Serif", Font.BOLD, unlocked ? 28 : 20));
numLbl.setForeground(cleared2 ? accent : unlocked ? TEXT_WHITE : new Color(55,55,70));
numLbl.setAlignmentX(CENTER_ALIGNMENT);
// Status label
String statusText = cleared2 ? "\u2605 Cleared" : unlocked ? "Play" : "Locked";
JLabel statusLbl = new JLabel(statusText, SwingConstants.CENTER);
statusLbl.setFont(new Font("SansSerif", Font.PLAIN, 11));
statusLbl.setForeground(cleared2 ? accent : unlocked ? GOLD_DIM : new Color(55,55,70));
statusLbl.setAlignmentX(CENTER_ALIGNMENT);
cell.add(Box.createVerticalGlue());
cell.add(numLbl);
cell.add(Box.createVerticalStrut(5));
cell.add(statusLbl);
cell.add(Box.createVerticalGlue());
// Click handler — only for unlocked levels
if (unlocked) {
cell.addMouseListener(new MouseAdapter() {
@Override public void mouseClicked(MouseEvent e) {
currentLevel = levelNum;
usedWords.clear();
startGame();
}
@Override public void mouseEntered(MouseEvent e) {
cell.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(accent, 2),
BorderFactory.createEmptyBorder(9,7,9,7)));
cell.repaint();
}
@Override public void mouseExited(MouseEvent e) {
cell.setBorder(BorderFactory.createEmptyBorder(10,8,10,8));
cell.repaint();
}
});
}
grid.add(cell);
}
levelSelectPanel.add(grid);
levelSelectPanel.revalidate();
levelSelectPanel.repaint();
}
// ── Instructions Screen ───────────────────────────────────────────────────
private JPanel buildInstructionsScreen() {
JPanel root = new GradientPanel(BG_DARK, new Color(12, 15, 28));
root.setLayout(new BorderLayout());
root.setBorder(BorderFactory.createEmptyBorder(30, 70, 30, 70));
JLabel title = new JLabel("How to Play", SwingConstants.CENTER);
title.setFont(new Font("Serif", Font.BOLD | Font.ITALIC, 36));
title.setForeground(GOLD);
title.setBorder(BorderFactory.createEmptyBorder(0, 0, 18, 0));
root.add(title, BorderLayout.NORTH);
String html = "<html><body style='background:#0e1118;color:#b8b9c8;font-family:SansSerif;padding:18px;'>"
+ "<h2 style='color:#d4af5f;'>Objective</h2>"
+ "<p>Decipher the hidden programming word within 6 attempts to claim victory and advance to the next level.</p><br>"
+ "<h2 style='color:#d4af5f;'>Levels</h2>"
+ "<p>Each difficulty has its own set of levels. Complete a level to unlock the next one. "
+ "You can replay any unlocked level from the Level Select screen.</p><br>"
+ "<h2 style='color:#d4af5f;'>Clue System</h2>"
+ "<p>Every word comes with a <span style='color:#8cbeff;'>clue</span> displayed beneath the grid "
+ "and shown again in the result screen.</p><br>"
+ "<h2 style='color:#d4af5f;'>Difficulty — Based on Concept Complexity</h2>"
+ "<p>Difficulty is determined by how <b>obscure or advanced</b> the programming concept is.</p>"
+ "<table border='0' cellpadding='6'>"
+ "<tr><td style='color:#46aa64;font-weight:bold;'>Easy</td><td>Everyday concepts any beginner knows</td></tr>"
+ "<tr><td style='color:#be9b28;font-weight:bold;'>Medium</td><td>Intermediate terms encountered during learning</td></tr>"
+ "<tr><td style='color:#b44646;font-weight:bold;'>Hard</td><td>Advanced / niche jargon for seasoned developers</td></tr>"
+ "</table><br>"
+ "<h2 style='color:#d4af5f;'>Colour Feedback</h2>"
+ "<table border='0' cellpadding='6'>"
+ "<tr><td style='color:#3c8c5a;font-weight:bold;'>GREEN</td><td>Correct letter, correct position</td></tr>"
+ "<tr><td style='color:#be9b28;font-weight:bold;'>AMBER</td><td>Letter exists but wrong position</td></tr>"
+ "<tr><td style='color:#787682;font-weight:bold;'>GREY</td><td>Letter not in the word at all</td></tr>"
+ "</table><br>"
+ "<h2 style='color:#d4af5f;'>Scoring</h2>"
+ "<table border='0' cellpadding='6'>"
+ "<tr><td style='color:#46aa64;font-weight:bold;'>Easy</td><td>100 pts base + 10 \u00d7 remaining attempts</td></tr>"
+ "<tr><td style='color:#be9b28;font-weight:bold;'>Medium</td><td>200 pts base + 20 \u00d7 remaining attempts</td></tr>"
+ "<tr><td style='color:#b44646;font-weight:bold;'>Hard</td><td>300 pts base + 30 \u00d7 remaining attempts</td></tr>"
+ "</table><br>"
+ "<h2 style='color:#d4af5f;'>The Stickman</h2>"
+ "<p>Each wrong guess removes a body part. 6 wrong guesses = game over!</p>"
+ "</body></html>";
JEditorPane ep = new JEditorPane("text/html", html);
ep.setEditable(false);
ep.setBackground(new Color(14, 17, 24));
JScrollPane scroll = new JScrollPane(ep);
scroll.setBorder(BorderFactory.createLineBorder(GOLD_DIM, 1));
scroll.getViewport().setBackground(new Color(14, 17, 24));
root.add(scroll, BorderLayout.CENTER);
JButton back = makeFancyButton("<- Return to Hall", new Color(25,20,10), GOLD_DIM);
back.addActionListener(e -> cardLayout.show(cardPanel, "MENU"));
JPanel bot = new JPanel(new FlowLayout(FlowLayout.CENTER));
bot.setOpaque(false);
bot.setBorder(BorderFactory.createEmptyBorder(16, 0, 0, 0));
bot.add(back);
root.add(bot, BorderLayout.SOUTH);
return root;
}
// ── Leaderboard Screen ────────────────────────────────────────────────────
private JPanel buildLeaderboardScreen() {
JPanel root = new GradientPanel(BG_DARK, new Color(12, 15, 28));
root.setLayout(new BorderLayout());
root.setBorder(BorderFactory.createEmptyBorder(30, 70, 30, 70));
JLabel title = new JLabel("* Leaderboard *", SwingConstants.CENTER);
title.setFont(new Font("Serif", Font.BOLD | Font.ITALIC, 36));
title.setForeground(GOLD);
title.setBorder(BorderFactory.createEmptyBorder(0, 0, 18, 0));
root.add(title, BorderLayout.NORTH);
leaderboardListPanel = new JPanel();
leaderboardListPanel.setLayout(new BoxLayout(leaderboardListPanel, BoxLayout.Y_AXIS));
leaderboardListPanel.setOpaque(false);
leaderboardListPanel.setBorder(BorderFactory.createEmptyBorder(10, 20, 10, 20));
JScrollPane scroll = new JScrollPane(leaderboardListPanel);
scroll.setBorder(BorderFactory.createLineBorder(GOLD_DIM, 1));
scroll.getViewport().setBackground(new Color(14, 17, 24));
scroll.setOpaque(false);
root.add(scroll, BorderLayout.CENTER);
JPanel bot = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 0));
bot.setOpaque(false);
bot.setBorder(BorderFactory.createEmptyBorder(16, 0, 0, 0));
JButton backBtn = makeFancyButton("<- Return", new Color(25,20,10), GOLD_DIM);
backBtn.addActionListener(e -> cardLayout.show(cardPanel, "MENU"));
JButton clearBtn = makeFancyButton("Clear Leaderboard", new Color(50,20,20), RED_DANGER);
clearBtn.addActionListener(e -> {
leaderboard.clear();
if (db.isConnected()) db.clearLeaderboard();
updateLeaderboardDisplay();
});
bot.add(backBtn); bot.add(clearBtn);
root.add(bot, BorderLayout.SOUTH);
return root;
}
private void updateLeaderboardDisplay() {
leaderboardListPanel.removeAll();
if (db.isConnected()) {
leaderboard.clear();
for (DatabaseManager.LeaderboardRow row : db.getTopScores(10)) {
leaderboard.add(new LeaderboardEntry(row.name, row.score, row.stage));
}
}
leaderboard.sort((a, b) -> Integer.compare(b.score, a.score));
if (leaderboard.isEmpty()) {
JLabel e = new JLabel("No entries yet. Play to get on the board!", SwingConstants.CENTER);
e.setFont(new Font("Serif", Font.ITALIC, 16));
e.setForeground(TEXT_MUTED); e.setAlignmentX(CENTER_ALIGNMENT);
leaderboardListPanel.add(Box.createVerticalStrut(50));
leaderboardListPanel.add(e);
} else {
JPanel header = new JPanel(new GridLayout(1, 4, 10, 0));
header.setOpaque(false); header.setMaximumSize(new Dimension(600, 35));
header.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, GOLD_DIM));
for (String h : new String[]{"Rank","Player","Score","Stage"}) {
JLabel lbl = new JLabel(h, SwingConstants.CENTER);
lbl.setFont(new Font("Serif", Font.BOLD, 14)); lbl.setForeground(GOLD);
header.add(lbl);
}
leaderboardListPanel.add(header);
leaderboardListPanel.add(Box.createVerticalStrut(5));
int rank = 1;
for (LeaderboardEntry entry : leaderboard) {
if (rank > 10) break;
JPanel row = new JPanel(new GridLayout(1, 4, 10, 0));
row.setOpaque(false); row.setMaximumSize(new Dimension(600, 35));
row.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, new Color(50,50,60)));
Color rc = rank == 1 ? GOLD : rank == 2 ? SILVER
: rank == 3 ? new Color(205,127,50) : TEXT_WHITE;
for (String val : new String[]{"#"+rank, entry.name, String.valueOf(entry.score), String.valueOf(entry.stage)}) {
JLabel lbl = new JLabel(val, SwingConstants.CENTER);
lbl.setFont(new Font("Serif", Font.BOLD, 14)); lbl.setForeground(rc);
row.add(lbl);
}
leaderboardListPanel.add(row);
leaderboardListPanel.add(Box.createVerticalStrut(3));
rank++;
}
}
leaderboardListPanel.revalidate();
leaderboardListPanel.repaint();
}
// ── Game Screen ───────────────────────────────────────────────────────────
private JPanel buildGameScreen() {
JPanel root = new GradientPanel(BG_DARK, new Color(10, 13, 22));
root.setLayout(new BorderLayout());
JPanel topBar = new JPanel(new BorderLayout());
topBar.setOpaque(false);
topBar.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createMatteBorder(0, 0, 1, 0, GOLD_DIM),
BorderFactory.createEmptyBorder(10, 18, 10, 18)));
JButton backBtn = makeFancyButton("<- Levels", new Color(25,22,12), GOLD_DIM);
backBtn.addActionListener(e -> {
sessionScoreLabel.setText("Session Score: " + sessionScore + " | Stage: " + currentStage);
updateLevelSelectScreen();
cardLayout.show(cardPanel, "LEVELSELECT");
});
topBar.add(backBtn, BorderLayout.WEST);
JPanel titleGroup = new JPanel();
titleGroup.setOpaque(false);
titleGroup.setLayout(new BoxLayout(titleGroup, BoxLayout.Y_AXIS));
JLabel gTitle = new JLabel("LexiGuess", SwingConstants.CENTER);
gTitle.setFont(new Font("Serif", Font.BOLD | Font.ITALIC, 26));
gTitle.setForeground(GOLD); gTitle.setAlignmentX(CENTER_ALIGNMENT);
difficultyLabel = new JLabel("", SwingConstants.CENTER);
difficultyLabel.setFont(new Font("Serif", Font.ITALIC, 12));