This repository was archived by the owner on Mar 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoggle2.java
More file actions
1033 lines (873 loc) · 37.6 KB
/
Boggle2.java
File metadata and controls
1033 lines (873 loc) · 37.6 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.util.*;
import java.util.Timer;
import javax.sound.sampled.*;
import javax.swing.*;
//Added the customisation of difficulty
public class Boggle2 extends JFrame {
// declare static variables
static boolean playerAI = false;
static boolean allowPause = true;
static int score1 = 0;
static int score2 = 0;
static int goal = 15;
static Dice dice = new Dice();
static char[][] map = dice.getDice();
static JLabel[][] showMap = new JLabel[map.length][map[0].length];
static int counter = 0;
static ArrayList<String> wordsAI = new ArrayList<String>();
static boolean player1;
static int player = 1;
static boolean played = false;
static ArrayList<String> wordGuessedValid = new ArrayList<String>();
static boolean paused;
public static int[] row = { -1, -1, -1, 0, 1, 0, 1, 1 };
public static int[] col = { -1, 1, 0, -1, -1, 1, 0, 1 };
public static int intStartR = 0;
public static int intStartC = 0;
public static boolean pathFound;
public static ArrayList<int[]> coordinates = new ArrayList<>();
public static ArrayList<int[]> aiCoordinates = new ArrayList<>();
public static int difficulty;
public static Color color1 = Color.WHITE;
public static Color color1f = Color.BLACK;
public static Color color2 = Color.ORANGE;
public static Color color2f = Color.BLACK;
private GameTimer t; // create the game timer (a count down timer)
// INSTRUCTION PANEL COMPONENTS
JPanel panInstruction = new JPanel();
JLabel instructionLabel0 = new JLabel("INSTRUCTION: ");
JLabel instructionLabel1 = new JLabel("Boggle overview");
JLabel instructionLabel2 = new JLabel("Boggle is a Hasbro word game that requires 16 letter cubes, ");
JLabel instructionLabel3 = new JLabel("the cube grid with dome, and a 3-minute sand timer, ");
JLabel instructionLabel4 = new JLabel("all of which should be included with your game. ");
JLabel instructionLabel5 = new JLabel("The game requires a minimum of two players with no maximum. ");
JLabel instructionLabel6 = new JLabel("It is recommended for ages 8 and up.");
JLabel instructionLabel7 = new JLabel("The goal of the game is have the highest point total. ");
JLabel instructionLabel8 = new JLabel(
"To gain points, players must create words from the randomly assorted letters in the cube grid. ");
JLabel instructionLabel9 = new JLabel(
"The longer the word, the higher the point value of the word, according to Boggle rules.");
JLabel instructionLabel10 = new JLabel("However, the word must have at least 3 characters. ");
JLabel instructionLabel11 = new JLabel("Noted that if you choose single player mode, you will always be the person who goes first. ");
JLabel instructionLabel12 = new JLabel("************************************************** The scoring rules:**************************************************");
JLabel instructionLabel13 = new JLabel(" Word Length | 3 | 4 | 5 | 6 | 7 | 8 or more ");
JLabel instructionLabel14 = new JLabel("--------------------------------------------------");
JLabel instructionLabel15 = new JLabel(" Score | 1 | 1 | 2 | 3 | 5 | 11 ");
// GAME OVER PANEL
JPanel panOver = new JPanel(new FlowLayout());
JLabel winnerLabel = new JLabel();
JButton closeBtn = new JButton("Quit application");
// PAUSE PANEL
JPanel panPause = new JPanel();
JLabel pauseLabel = new JLabel("Game paused");
JButton resumeBtn = new JButton("Resume");
// PLAYING PANELS
// declare components for playing panels
JPanel panGuess = new JPanel(new FlowLayout());
static JPanel panBoggle = new JPanel(new GridLayout(map.length, map[0].length));
JPanel panAIGuess = new JPanel(new FlowLayout());
JPanel panScore = new JPanel(new FlowLayout());
JPanel panTimer = new JPanel(new FlowLayout());
JPanel panError = new JPanel(new FlowLayout());
JLabel guessLabel = new JLabel("What is your answer? ");
JTextField guessIn = new JTextField("Please enter your answer here", 20);
JButton guessBtn = new JButton("Enter");
JLabel computerLabel0 = new JLabel("The answer from computer is... ");
static JLabel computerLabel1 = new JLabel("still thinking... ");
JLabel scoreLabel0 = new JLabel("Current score of player 1:");
JLabel scoreLabel1 = new JLabel("0");
JLabel scoreLabel2 = new JLabel(" Current score of player 2:");
static JLabel scoreLabel3 = new JLabel("0");
JButton homeBtn = new JButton("Back to homepage");
JLabel timerLabel = new JLabel("Timer: 00:15");
JLabel errorLabel = new JLabel();
// CUSTOMISATION PANEL
JPanel panCustomisation = new JPanel(new GridLayout(9, 2));
JLabel customisationLabel0 = new JLabel("CUSTOMISATION");
JLabel playersMode = new JLabel("Which mdoe would you like to choose? ");
JButton singlePlayerBtn = new JButton("Single Player");
JButton twoPlayerBtn = new JButton("Two Player");
JLabel difficultyLabel = new JLabel("Choose the difficulty for the bot. Please enter 1-5. ");
JTextField difficultyIn = new JTextField("Please enter 1-5");
JButton difficultyBtn = new JButton("Enter");
JLabel allowPauseLabel = new JLabel("Will this game allow pausing? ");
JButton yesBtn = new JButton("Yes");
JButton noBtn = new JButton("No");
JTextField ptIn = new JTextField("Enter points to play");
JLabel targetScore = new JLabel("Tournament scroe? Please enter an integer. ");
JButton ptBtn = new JButton("Enter");
// MENU BAR
JMenuBar menuBar = new JMenuBar();
// menu
JMenu menu = new JMenu("Menu"); // restart, shake up button, instruction, customisation
JMenu pauseMenu = new JMenu("Pause"); // pause, resume
JMenu theme = new JMenu("Theme"); // change theme
// menu menu items
JMenuItem instruction = new JMenuItem("Instruction");
JMenuItem restart = new JMenuItem("Home/ Restart");
JMenuItem shakeUp = new JMenuItem("Shake-up");
JMenuItem customisation = new JMenuItem("Customisation");
// pause menu item
JMenuItem pauseOp = new JMenuItem("Pause");
JMenuItem resume = new JMenuItem("Resume");
// theme menu items
JMenuItem thDefault = new JMenuItem("Default");
JMenuItem thDark = new JMenuItem("Dark");
JMenuItem thClassy = new JMenuItem("Classy");
JMenuItem thModern = new JMenuItem("Modern");
JMenuItem thGalaxy = new JMenuItem("Galaxy");
JMenuItem thCandy = new JMenuItem("Candy");
JMenuItem thSynthwave = new JMenuItem("Synthwave");
JMenuItem thNight = new JMenuItem("Night Owl");
// HOME PAGE PANEL
JPanel panStart = new JPanel();
JButton playBtn = new JButton("Play");
JLabel tips = new JLabel("Remember to check the menu for more features and intruction. ");
public Boggle2() { // constructor
setTitle("boggle game");// set frame title
setSize(750, 680);// set frame size
setVisible(true);
// INSTRUCTION PANEL
panInstruction.add(instructionLabel0);
panInstruction.add(instructionLabel1);
panInstruction.add(instructionLabel2);
panInstruction.add(instructionLabel3);
panInstruction.add(instructionLabel4);
panInstruction.add(instructionLabel5);
panInstruction.add(instructionLabel6);
panInstruction.add(instructionLabel7);
panInstruction.add(instructionLabel8);
panInstruction.add(instructionLabel9);
panInstruction.add(instructionLabel10);
panInstruction.add(instructionLabel11);
panInstruction.add(instructionLabel12);
panInstruction.add(instructionLabel13);
panInstruction.add(instructionLabel14);
panInstruction.add(instructionLabel15);
// set visible of panInstruction to true
panInstruction.setVisible(false);
// PAUSE PANEL
// add components to panPause
pauseLabel.setFont(new Font("Bradley Hand", Font.BOLD, 40)); // set font of the JLabel
panPause.add(pauseLabel);
panPause.add(resumeBtn);
// set visible to false
panPause.setVisible(false);
// GAME OVER PANEL
panOver.add(winnerLabel, BorderLayout.NORTH);
panOver.add(homeBtn, BorderLayout.CENTER);
panOver.add(closeBtn, BorderLayout.SOUTH);
panOver.setVisible(false);
// PLAY PANEL
panScore.setPreferredSize(new Dimension(3, 550));
panBoggle.setPreferredSize(new Dimension(600, 550));
initialisePanBoggle();
panGuess.add(guessLabel);
panGuess.add(guessIn);
panGuess.add(guessBtn);
panAIGuess.add(computerLabel0);
panAIGuess.add(computerLabel1);
panScore.add(scoreLabel0);
panScore.add(scoreLabel1);
panScore.add(scoreLabel2);
panScore.add(scoreLabel3);
panTimer.add(timerLabel);
panError.add(errorLabel);
panScore.setVisible(false);
panBoggle.setVisible(false);
panGuess.setVisible(false);
panAIGuess.setVisible(false);
panCustomisation.setPreferredSize(new Dimension(650, 550));
panCustomisation.add(customisationLabel0);
panCustomisation.add(new JLabel(""));
panCustomisation.add(playersMode);
panCustomisation.add(new JLabel(""));
panCustomisation.add(singlePlayerBtn);
panCustomisation.add(twoPlayerBtn);
panCustomisation.add(allowPauseLabel);
panCustomisation.add(new JLabel(""));
panCustomisation.add(yesBtn);
panCustomisation.add(noBtn);
panCustomisation.add(targetScore);
panCustomisation.add(new JLabel(""));
panCustomisation.add(ptIn);
panCustomisation.add(ptBtn);
panCustomisation.setVisible(false);
// MENU BAR
menuBar.add(menu);
menuBar.add(pauseMenu);
menuBar.add(theme);
setJMenuBar(menuBar);
// MENU - add menu items
menu.add(restart);
menu.add(instruction);
menu.add(customisation);
// PAUSE -- add menu items
pauseMenu.add(pauseOp);
pauseMenu.add(resume);
// THEME -- add menu items
theme.add(thDefault);
theme.add(thDark);
theme.add(thClassy);
theme.add(thGalaxy);
theme.add(thCandy);
theme.add(thSynthwave);
theme.add(thNight);
// START PANEL
GridLayout gl1 = new GridLayout(2, 1);
panStart.setLayout(gl1);
panStart.setPreferredSize(new Dimension(650, 50));
panStart.add(playBtn);
panStart.add(tips);
add(panStart, BorderLayout.NORTH);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
t = new GameTimer();
closeBtn.addActionListener(e -> {
dispose();
});
ptBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
goal = Integer.parseInt(ptIn.getText());
System.out.println("ptBtnPressed pressed, goal stored");
}
});
singlePlayerBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
playerAI = true;
System.out.println("singlePlayerBtn pressed, playerAI to true");
if (!difficultyLabel.isShowing())
{
panCustomisation.add(difficultyLabel);
panCustomisation.add(difficultyIn);
panCustomisation.add(difficultyBtn);
panCustomisation.repaint();
repaint();
}
}
});
twoPlayerBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
playerAI = false;
System.out.println("twoPlayerBtn pressed, playerAI to false");
if (difficultyLabel.isShowing())
{
panCustomisation.remove(difficultyLabel);
panCustomisation.remove(difficultyIn);
panCustomisation.remove(difficultyBtn);
}
}
});
difficultyBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
difficulty = Integer.parseInt(guessIn.getText());
}
});
yesBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!allowPause) {
pauseMenu.add(pauseOp);
}
allowPause = true;
}
});
noBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
allowPause = false;
pauseMenu.remove(pauseOp);
}
});
instruction.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("instruction invoked");
getContentPane().removeAll();
repaint();
revalidate();
add(panInstruction);
panInstruction.setVisible(true);
validate();
}
});
restart.addActionListener(restartAction);
homeBtn.addActionListener(restartAction);
shakeUp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("shakeUp invoked");
changeColor(coordinates, false);
randomizeBoggle();
}
});
customisation.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("customisation invoked");
t.pause();
getContentPane().removeAll();
repaint();
revalidate();
add(panCustomisation);
panCustomisation.setVisible(true);
validate();
}
});
pauseOp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("pauseOp invoked");
getContentPane().removeAll();
repaint();
revalidate();
paused = true;
t.pause();
add(panPause);
panPause.setVisible(true);
}
});
thDefault.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("thDefault invoked");
color1 = Color.WHITE;
color1f = Color.BLACK;
color2 = Color.ORANGE;
color2f = Color.BLACK;
changeGridColor();
panGuess.setBackground(color2);
guessLabel.setForeground(color2f);
}
});
thDark.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("thDark invoked");
color1 = Color.BLACK;
color1f = Color.WHITE;
color2 = Color.GRAY;
color2f = Color.BLACK;
changeGridColor();
panGuess.setBackground(color2);
guessLabel.setForeground(color2f);
}
});
thClassy.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("thClassy invoked");
color1 = Color.WHITE;
color1f = Color.BLACK;
color2 = Color.BLACK;
color2f = Color.WHITE;
changeGridColor();
panGuess.setBackground(color2);
guessLabel.setForeground(color2f);
}
});
thGalaxy.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("thGalaxy invoked");
color1 = new Color(0, 102, 102);
color1f = Color.BLACK;
color2 = new Color(204, 255, 255);
color2f = Color.BLACK;
changeGridColor();
panGuess.setBackground(color2);
guessLabel.setForeground(color2f);
}
});
thCandy.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("thCandy invoked");
color1 = Color.PINK;
color1f = Color.BLACK;
color2 = new Color(194, 194, 214);
color2f = Color.BLACK;
changeGridColor();
panGuess.setBackground(color2);
guessLabel.setForeground(color2f);
}
});
thSynthwave.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("thSynthwave invoked");
color1 = new Color(102, 0, 102);
color1f = Color.WHITE;
color2 = new Color(0, 255, 255);
color2f = Color.BLACK;
changeGridColor();
panGuess.setBackground(color2);
guessLabel.setForeground(color2f);
}
});
thNight.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("thNight invoked");
color1 = new Color(0, 0, 51);
color1f = Color.WHITE;
color2 = new Color(102, 102, 255);
color2f = Color.WHITE;
changeGridColor();
panGuess.setBackground(color2);
guessLabel.setForeground(color2f);
}
});
resume.addActionListener(resumeAction);
resumeBtn.addActionListener(resumeAction);
playBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("playPressed");
try {
t.setTime();
} catch (UnsupportedAudioFileException | IOException | LineUnavailableException e1) {
e1.printStackTrace();
}
t.reset();
played = true;
menu.remove(customisation);
menu.remove(instruction);
getContentPane().removeAll();
repaint();
revalidate();
// Player 1
add(panGuess, BorderLayout.NORTH);
panGuess.setVisible(true);
randomizeBoggle();
changeColor(coordinates, false);
add(panTimer, BorderLayout.EAST);
add(panBoggle, BorderLayout.SOUTH);
add(panScore, BorderLayout.CENTER);
add(panError, BorderLayout.WEST);
panTimer.setVisible(true);
panScore.setVisible(true);
panBoggle.setVisible(true);
panError.setVisible(true);
if (!playerAI)
{
player = (int) (Math.random() * 2) + 1;
} else {
player =1;
}
errorLabel.setText("Player " + player + " goes first");
if (counter == 4) {
menu.add(shakeUp);
}
}
});
guessBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("guessBtn invoked");
System.out.println();
String guess = guessIn.getText();
System.out.println("The player is " + player);
changeColor(coordinates, false); // turn the previous cell back to white
coordinates.clear();
pathFound = false;
System.out.println("after cleared");
for (int i = 0; i < coordinates.size(); i++) {
System.out.println(coordinates.get(i)[0] + " " + coordinates.get(i)[1]);
}
if (guess.length() < 3) {
errorLabel.setText("Enter 3 characters or above!");
} else {
errorLabel.setText("");
if (findWord(guess, map) && !wordGuessedValid.contains(guess)) {
System.out.println("Valid answer");
for (int i = 0; i < coordinates.size(); i++) {
System.out.println(coordinates.get(i)[0] + " " + coordinates.get(i)[1]);
}
if (player == 1) {
score1 = score1 + score(guess);
System.out.println("The score1 is " + score1);
scoreLabel1.setText(Integer.toString(score1));
} else {
score2 = score2 + score(guess);
System.out.println("The score2 is " + score2);
scoreLabel3.setText(Integer.toString(score2));
// scoreLabel1.setText("I am dead");
}
changeColor(coordinates, true);
} else {
errorLabel.setText("Invalid answer");
System.out.println("Invalid answer");
}
}
wordGuessedValid.add(guess);
}
});
}
ActionListener resumeAction = new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("resume invoked");
getContentPane().removeAll();
repaint();
revalidate();
paused = false;
t.unpause();
add(panGuess, BorderLayout.NORTH);
panGuess.setVisible(true);
add(panTimer, BorderLayout.EAST);
panTimer.setVisible(true);
add(panScore, BorderLayout.CENTER);
panScore.setVisible(true);
add(panBoggle, BorderLayout.SOUTH);
panBoggle.setVisible(true);
add(panError, BorderLayout.WEST);
panError.setVisible(true);
}
};
ActionListener restartAction = new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("restart invoked");
getContentPane().removeAll();
repaint();
revalidate();
if (!customisation.isShowing()) {
menu.add(customisation);
menu.add(instruction);
}
wordGuessedValid.clear();
score1 = 0;
score2 = 0;
scoreLabel1.setText("0");
scoreLabel3.setText("0");
if (played) {
changeColor(coordinates, false);
played = false;
}
add(panStart);
panStart.setVisible(true);
validate();
t.pause();
}
};
public static void initialisePanBoggle() {
System.out.println("Initialise panBoggle invoked");
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[i].length; j++) {
showMap[i][j] = new JLabel("");
panBoggle.add(showMap[i][j]);
showMap[i][j].setBackground(color1);
showMap[i][j].setForeground(color1f);
showMap[i][j].setOpaque(true);
}
}
}
public static void main(String args[]) {
Boggle2 frame1 = new Boggle2(); // start gui
}
public static void changeGridColor() {
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[i].length; j++) {
showMap[i][j].setBackground(color1);
showMap[i][j].setForeground(color1f);
}
}
}
public static void changeColor(ArrayList<int[]> path, boolean showFinal) {
System.out.println("Change color invoked");
if (path.size() != 0) {
System.out.println("path size does not equal to zero");
for (int i = 0; i < path.size(); i++) // for loop
{
// String rowCol = path.get(i); // get x y coordinate
int row = path.get(i)[0]; // get y coordinate
int col = path.get(i)[1]; // get x coordinate
if (showFinal) {
System.out.println(" colorToOrange invoked");
showMap[row][col].setBackground(color2);
showMap[row][col].setForeground(color2f);
} else {
System.out.println(" colorToWhite invoked");
showMap[row][col].setBackground(color1);
showMap[row][col].setForeground(color1f);
showMap[row][col].setOpaque(true);
}
}
} else {
System.out.println("path size equal to zero");
}
}
public static void randomizeBoggle() {
System.out.println("randomizeBoggle invoked");
dice.shuffle();
map = dice.getDice();
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[i].length; j++) {
showMap[i][j].setText(Character.toString(map[i][j]));
}
}
}
public static ArrayList<String> aiGuess() {
System.out.println("aiGuess invoked");
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[i].length; j++) {
System.out.print(map[i][j] + " ");
}
System.out.println();
}
int wordsToGuess = difficulty + 2;
ArrayList<String> words = getWords(map, wordsToGuess);
return words;
}
public static int score(String x) {
int score = 0;
if (x.length() == 3 || x.length() == 4) {
score += 1;
} else if (x.length() == 5) {
score += 2;
} else if (x.length() == 6) {
score += 3;
} else if (x.length() == 7) {
score += 4;
} else if (x.length() >= 8) {
score += 11;
}
return score;
}
public static boolean isSafe(int x, int y, boolean[][] processed, char[][] board, String word, String path) {
boolean isSafe = false;
if ((x >= 0 && x < processed.length) && (y >= 0 && y < processed[0].length) && !processed[x][y]) {
if (word.charAt(path.length()) == board[x][y]) {
isSafe = true;
} else {
isSafe = false;
}
}
return isSafe;
}
// A recursive function to generate and print all possible words in a boggle
public static void searchBoggle(char[][] board, String word, ArrayList<String> result, boolean[][] processed, int i,
int j, String path) {
// Mark the current node as processed
processed[i][j] = true;
int[] point = new int[2];
int count = -1;
// Update the path with the current character and insert it into the set
path += board[i][j];
count++;
point[0] = i;
point[1] = j;
if (!pathFound) {
coordinates.add(point);
}
// Check whether the path is present in the input set
if (word.equals(path)) {
pathFound = true;
result.add(path);
return;
}
// Check for all eight possible movements from the current cell
for (int k = 0; k < row.length; k++) {
if (isSafe(i + row[k], j + col[k], processed, board, word, path)) {
searchBoggle(board, word, result, processed, i + row[k], j + col[k], path);
}
}
// Mark the current node as unprocessed
processed[i][j] = false;
int index = coordinates.indexOf(point);
for (int k = coordinates.size() - 1; k >= index; k--) {
if (!pathFound) {
coordinates.remove(k);
}
}
}
public static ArrayList<String> searchBoggle(char[][] board, String word) {
ArrayList<String> result = new ArrayList<>();
if (board.length == 0) {
return result;
}
boolean[][] processed = new boolean[board.length][board[0].length];
searchBoggle(board, word, result, processed, intStartR, intStartC, "");
if (result.size() > 0) {
return result;
}
return result;
}
public static ArrayList<String> getWords(char[][] board, int limit) {
Map<String, ArrayList<String>> mapDic = new HashMap<String, ArrayList<String>>();
ArrayList<String> dicWords = new ArrayList<String>();
// Put dictionary into Map
File fileDictionary = new File("dictionary.txt");
try {
Scanner inputDic = new Scanner(fileDictionary);
while (inputDic.hasNext()) {
String strNext = inputDic.next();
if (strNext.length() > 2) {
String strFirstLetter = strNext.substring(0, 1);
if (mapDic.get(strFirstLetter) != null) {
mapDic.get(strFirstLetter).add(strNext);
} else {
ArrayList<String> arrTemp = new ArrayList<String>();
arrTemp.add(strNext);
mapDic.put(strFirstLetter, arrTemp);
}
}
}
inputDic.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
intStartR = i;
intStartC = j;
ArrayList<String> arrWordList = mapDic.get(Character.toString(board[i][j]));
for (int k = 0; k < arrWordList.size(); k++) {
ArrayList<String> arrWords = new ArrayList<String>();
String words = arrWordList.get(k);
for (int l = 0; l < words.length(); l++) {
arrWords.add(Character.toString(words.charAt(l)));
}
ArrayList<String> validWords = searchBoggle(board, words);
if (!validWords.isEmpty()) {
if (!(dicWords.contains(validWords.get(0).toString()))) {
dicWords.add(validWords.get(0).toString());
if (dicWords.size() >= limit) {
return dicWords;
}
}
}
}
}
}
return dicWords;
}
public static boolean findWord(String word, char[][] board) {
ArrayList<String> arrWords = new ArrayList<>();
for (int i = 0; i < word.length(); i++) {
arrWords.add(Character.toString(word.charAt(i)));
}
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
if (board[i][j] == word.charAt(0)) {
intStartR = i;
intStartC = j;
ArrayList<String> validWords = searchBoggle(board, word);
if (validWords.indexOf(word) > -1) {
return true;
}
}
}
}
return false;
}
public class GameTimer extends Timer {
Task t; // The task to run
private long startTime; // What time the game started
private long pauseStart;
// Sets up the timer for moving flights
public GameTimer() {
super();
t = new Task();
scheduleAtFixedRate(t, 0, 1000);
startTime = System.currentTimeMillis();
}
// Restart the timer
public void reset() {
startTime = System.currentTimeMillis();
}
// Update the timer label
public void setTime() throws UnsupportedAudioFileException, IOException, LineUnavailableException {
if (!paused) {
long playingTime = System.currentTimeMillis() - startTime;
long timeLeft = 15000 - playingTime;
long sec = (timeLeft) / 1000L;
if (timeLeft < 0) // 15 seconds passed
{
counter ++;
if (player == 1) {
player = 2;
System.out.println();
System.out.println("PLAYER 1 --> PLAYER 2");
System.out.println();
if (playerAI) {
changeColor(coordinates, false);
remove(panGuess);
add(panAIGuess, BorderLayout.NORTH);
panAIGuess.setVisible(true);
// Let ai guess work first
wordsAI.clear();
aiCoordinates.clear();
wordsAI = aiGuess();
for (int i = 0; i < wordsAI.size(); i++) {
score2 = score2 + score(wordsAI.get(i));
if (i == 0) {
computerLabel1.setText(wordsAI.get(i));
} else {
computerLabel1.setText(computerLabel1.getText() + ", " + wordsAI.get(i));
}
}
scoreLabel3.setText(Integer.toString(score2));
}
reset();
setTime();
return;
} else {
System.out.println();
System.out.println("PLAYER 2 --> PLAYER 1");
player = 1;
System.out.println();
if (panAIGuess.isShowing()) {
System.out.println("panAIGuess is showing");
getContentPane().remove(panAIGuess);
panAIGuess.setVisible(false);
add(panGuess, BorderLayout.NORTH);
panGuess.setVisible(true);
revalidate();
repaint();
}
reset();
setTime();
return;
}
}
String s = "Time: ";
if (sec >= 10)
s += Integer.toString((int) sec);
else
s += "0" + Integer.toString((int) sec);
if (score1 >= goal || score2 >= goal) {
pause();
getContentPane().removeAll();
repaint();
revalidate();
if (score1 == goal) {
winnerLabel.setText("Player 1 won! ");
} else {
if (playerAI) {
winnerLabel.setText("The AI won! ");
} else {
winnerLabel.setText("Player 2 won! ");
}
}
add(panOver);
panOver.setVisible(true);
File file = new File("Cheers10.wav");
AudioInputStream audioStream = AudioSystem.getAudioInputStream(file);
Clip clip = AudioSystem.getClip();
clip.open(audioStream);
clip.start();
clip.loop(Clip.LOOP_CONTINUOUSLY);