-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboggleWithGUI.java
719 lines (627 loc) · 28.2 KB
/
boggleWithGUI.java
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
/*
* Names: Rohan Ravindran, Kevin Xu, Mandana Emam, Nicholas Tao
* Date: June 12, 2019
* Assignment Title: Boggle
*
*/
//import needed libraries
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.sound.sampled.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;
import java.util.Timer;
import java.util.TimerTask;
public class frame extends JFrame implements ActionListener {
//declare global variables
static ArrayList<String> wordsEntered = new ArrayList<String>();
static String [] die = {"AAAFRS", "AAEEEE", "AAFIRS", "ADENNN", "AEEEEM", "AEEGMU", "AEGMNN", "AFIRSY", "BJKQXZ", "CCNSTW", "CEIILT", "CEILPT", "CEIPST", "DDLNOR", "DHHLOR", "DHHNOT", "DHLNOR", "EIIITT", "EMOTTT", "ENSSSU", "FIPRSY", "GORRVW", "HIPRRY", "NOOTUW", "OOOTTU"}; //array storing the 25 die
static String [] wordList; //stores dictionary words
static int scoreToWin; //stores score to win
static String board[][]; //board of letters
static int minWordLen; //minimum word length
static boolean found = false;
static boolean gameRunning = true;
static Scanner sc = new Scanner(System.in);
static JLabel[][] boardLabelGrid = new JLabel[5][5];
static int playerNum; //number of players
static String[] name; //names of players
static int playerTurn = 0; //keep track of turn
static int[] playerScore; //scores of players
static int [] passCounter; //counts times passed
static boolean hasWon = false; //keeps track of whether game is paused
static int onePlayerTimeInterval; //time limit for one player mode
static int interval = 15; //for 15s timer (2P mode), it is one second higher than 15 because the timer stops at 1s, so starting from 16 ensures a 15s timer
static Timer timer; //declare timer object
//GUI Components
static JPanel gridPan = new JPanel();
JPanel totalPan = new JPanel();
JPanel bottomPanel = new JPanel();
JPanel infoPanel = new JPanel();
JPanel bottomButtons = new JPanel();
static JPanel validWordPanel = new JPanel();
JPanel timeRemainingPanel = new JPanel();
JPanel scorePanel = new JPanel();
JLabel timeRemainingTitle = new JLabel("Time Remaining: ", JLabel.CENTER);
static JLabel timeRemaining = new JLabel("0", JLabel.CENTER); //displays time remaining
JLabel scoreTitle = new JLabel("Your Score: ", JLabel.CENTER);
static JLabel score = new JLabel("0", JLabel.CENTER); //displays player's score
static JLabel validWord = new JLabel("Word is ", JLabel.CENTER); //indicates whether word is valid
//Other Components
JButton enterButton = new JButton("Enter");
static JLabel introLabel = new JLabel("Welcome To Boggle", SwingConstants.CENTER); //labels
static JTextField enterField = new JTextField ("", 30);// blank text field
static Font font = new Font("Sans Serif", Font.BOLD, 20);
static Font biggerFont = new Font ("Sans Serif", Font.BOLD, 30);
static Font smallerFont = new Font("Sans Serif", Font.BOLD, 15);
static Border labelBorder = BorderFactory.createEtchedBorder();
Border upperBorder = BorderFactory.createDashedBorder(Color.BLUE, 4, 3); //a border
//Bottom Buttons (options to randomize board, restart, exit, pass)
JButton shakeBoard = new JButton("Randomize Board!");
JButton restartGame = new JButton("Restart"); //switch text to play when game state is paused
JButton exitGame = new JButton("Exit");
JButton pass = new JButton("Pass");
JPanel playerTurnPanel = new JPanel();
JLabel playerTurnTitle = new JLabel("Player Turn:", JLabel.CENTER);
static JLabel playerTurnLabel = new JLabel("", JLabel.CENTER);
/*
* Method sets up the game and prompts the user to enter game setup questions regarding minimum word length and score to win
*/
public static void setupQuestions() {
Object[] minLengthValues = { "2", "3", "4" }; //getting the min length of a word
Object selectedValue2 = JOptionPane.showInputDialog(null,
"Please choose a minimum word length", "Input",
JOptionPane.INFORMATION_MESSAGE, null,
minLengthValues, minLengthValues[0]);
minWordLen = Integer.parseInt(selectedValue2.toString());
scoreToWin = 0;
//do-while loop for input validation to get score to win from user
do {
String scoreToWinInput = JOptionPane.showInputDialog("Please choose a score to win");//getting the score limit
try {
scoreToWin = Integer.valueOf(scoreToWinInput);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "You have to an appropriate value", "ERROR", JOptionPane.ERROR_MESSAGE);
continue;
}
if (scoreToWin < 1) {
JOptionPane.showMessageDialog(null, "You have to enter a value bigger than 1", "ERROR", JOptionPane.ERROR_MESSAGE);
}
} while(scoreToWin < 1);
//if one player, they choose how long they get to reach the score to win
if (playerNum == 1) {
//do-while loop for input validation
do {
String input = JOptionPane.showInputDialog(null, "Enter time (secs) available for input", "Enter time");
try {
onePlayerTimeInterval = Integer.valueOf(input);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "You have to an appropriate value", "ERROR", JOptionPane.ERROR_MESSAGE);
continue;
}
if (onePlayerTimeInterval < 5) {
JOptionPane.showMessageDialog(null, "You have to enter a value bigger than 5", "ERROR", JOptionPane.ERROR_MESSAGE);
}
} while(onePlayerTimeInterval < 5);
}
}
/*
* Constructor method for the frame of the game
*/
public frame() {
//Intro Questions and Game Rules
String gameRules = "Welcome to Boggle. Here's how to play: \n\n You must find words on the board that that are:\n" +
" - Touching each other, either horizontally vertically, or diagonally\n" +
" - Not repeating the same letter in a single word\n" +
" - Have more than or the same number of letters as the minimum word length\n" +
" - Exist in the English dictionary \n\n" +
"You must find all the words you can within the time limit and you will gain points by how many letters are in\nthe words you find with each letter earning you 1 point. " +
"If two payers are playing and they both pass twice or if\nit is one player and the player passes, the board will be shaken up. " +
"The players are able to restart and exit\nthe game at any given time. They can also continue playing another round after the game has finished.\n" +
"\nIn the end, the player to pass the score limit first wins the game and there are a few things to note:\n" +
" - A word can’t be repeated in the same round\n" +
" - A word can’t be counted twice even if it has 2 different meanings\n" +
" - NOTE: For 2 player mode, if a player passes, the next player's turn starts IMMEDIATELY after the current player's turn. Be ready!";
JOptionPane.showMessageDialog(null, gameRules, "Game Rules", JOptionPane.INFORMATION_MESSAGE);
Object[] possibleValues = {"One", "Two"};
Object selectedValue = JOptionPane.showInputDialog(null,"How many players are there?", "Input",JOptionPane.INFORMATION_MESSAGE, null,possibleValues, possibleValues[0]);
if(selectedValue.equals("One")) playerNum=1; //indicates 1 player mode
else playerNum=2; //2 player mode
name = new String[playerNum];
setupQuestions(); //call method to ask setup questions
//getting the name of players
for(int i = 0; i < playerNum; i++) {
name[i] = JOptionPane.showInputDialog(null, "Enter name of Player "+(i+1)+".", "Enter name");
}
//MARK: Frame Setup
setTitle("Boggle");
setSize(800, 700);
//Set panel layouts
totalPan.setLayout(new BoxLayout(totalPan, BoxLayout.PAGE_AXIS));
gridPan.setLayout(new GridLayout(5,5));
bottomPanel.setLayout(new FlowLayout());
infoPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
//Intro Label
introLabel.setFont(font);
introLabel.setAlignmentX(CENTER_ALIGNMENT);
totalPan.add(introLabel);
//Time remaining panel
timeRemainingPanel.setLayout(new BoxLayout(timeRemainingPanel, BoxLayout.PAGE_AXIS)); //setting to flow layout
timeRemainingTitle.setFont(font);
timeRemaining.setFont(font);
timeRemainingTitle.setAlignmentX(CENTER_ALIGNMENT);
timeRemaining.setAlignmentX(CENTER_ALIGNMENT);
timeRemainingPanel.add(timeRemainingTitle);
timeRemainingPanel.add(timeRemaining);
timeRemainingPanel.setBorder(upperBorder);
//Score Panel
scorePanel.setLayout(new BoxLayout(scorePanel, BoxLayout.PAGE_AXIS));
scoreTitle.setFont(font);
score.setFont(font);
scoreTitle.setAlignmentX(CENTER_ALIGNMENT);
score.setAlignmentX(CENTER_ALIGNMENT);
scorePanel.add(scoreTitle);
scorePanel.add(score);
scorePanel.setBorder(upperBorder);
//ValidWord Panel
validWord.setFont(smallerFont);
validWord.setAlignmentX(CENTER_ALIGNMENT);
validWordPanel.add(validWord);
validWordPanel.setVisible(false);
//Bottom buttons (restart, exit)
restartGame.setPreferredSize(new Dimension(165, 25));
exitGame.setPreferredSize(new Dimension(165, 25));
bottomButtons.add(restartGame);
restartGame.addActionListener(this);
bottomButtons.add(exitGame);
exitGame.addActionListener(this);
validWordPanel.setPreferredSize(new Dimension(400, 30));
//Unique components if 2 player mode
if (playerNum == 2) {
playerTurnPanel.setLayout(new BoxLayout(playerTurnPanel, BoxLayout.PAGE_AXIS));
playerTurnTitle.setFont(font);
playerTurnLabel.setFont(font);
playerTurnTitle.setAlignmentX(CENTER_ALIGNMENT);
playerTurnLabel.setAlignmentX(CENTER_ALIGNMENT);
playerTurnPanel.add(playerTurnTitle);
playerTurnPanel.add(playerTurnLabel);
playerTurnPanel.setBorder(upperBorder);
playerTurnPanel.setPreferredSize(new Dimension(240, 65));
scorePanel.setPreferredSize(new Dimension(240, 65));
timeRemainingPanel.setPreferredSize(new Dimension(240, 65));
infoPanel.add(playerTurnPanel);
pass.setPreferredSize(new Dimension(165, 25));
pass.addActionListener(this);
bottomButtons.add(pass);
playerScore = new int[2];
passCounter = new int [2];
playerTurnLabel.setText(name[playerTurn]);
} else { //components for 1 player mode
timeRemainingPanel.setPreferredSize(new Dimension(350, 65));
scorePanel.setPreferredSize(new Dimension(350, 65));
shakeBoard.setPreferredSize(new Dimension(165, 25));
shakeBoard.addActionListener(this);
bottomButtons.add(shakeBoard);
playerScore = new int[1];
}
//add panels to panels
infoPanel.add(timeRemainingPanel);
infoPanel.add(scorePanel);
bottomPanel.add(enterField);
bottomPanel.add(enterButton);
bottomPanel.add(validWordPanel);
gridPan.setPreferredSize(new Dimension(600, 300));
//adding panels to totalPan
totalPan.add(infoPanel);
totalPan.add(gridPan);
totalPan.add(bottomPanel);
totalPan.add(bottomButtons); //adding panels
enterButton.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(totalPan);
setVisible(true);
setLocationRelativeTo(null); //centering the frame
}
/*
* Method restarts the game by calling appropriate methods and reseting variables
*/
public static void resartGame() {
setupQuestions();
randomizeBoard(board);
updateBoard(board);
enterField.setText("");
validWordPanel.setVisible(false);
wordsEntered.removeAll(wordsEntered);
hasWon = false;
if (playerNum == 1) {
playerScore[0] = 0;
score.setText(Integer.toString(playerScore[0]));
interval = onePlayerTimeInterval;
startTimer();
} else {
playerScore[0] = 0;
playerScore[1] = 0;
playerTurn = 0;
passCounter[0] = 0;
passCounter[1] = 0;
score.setText(Integer.toString(playerScore[playerTurn]));
playerTurnLabel.setText(name[playerTurn]);
interval = 15;
startTimer();
}
}
/*
* Method determines the winner by comparing users' score with score limit
*/
public static void checkWon() {
if (playerScore[playerTurn] >= scoreToWin) { //if player has reached score limit
timer.cancel(); //cancel timer
hasWon = true;
Object[] resartGameValues = {"Yes", "No"};
Object selectedValue = JOptionPane.showInputDialog(null, "Congrats, " + name[playerTurn] +". You have finished the game by reaching " + scoreToWin + " points! \nWould you like to play again?", "Game Finished!",JOptionPane.INFORMATION_MESSAGE, null,resartGameValues, resartGameValues[1]);
//try-catch for input validation
try {
if (selectedValue.equals("Yes")) {
resartGame();
} else {
System.exit(0);
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
/*
* Method that calls appropriate methods when actions are performed
*/
public void actionPerformed(ActionEvent event) { //GUI ACTION RESPONSES
if (event.getSource() == enterButton) { //Pressed Enter Button
String word = enterField.getText();
validWordPanel.setVisible(true);
if (validate(word, minWordLen, wordList, wordsEntered, board)) { //call validate method to check if word is valid
playerScore[playerTurn] += word.length();
score.setText(Integer.toString(playerScore[playerTurn]));
wordsEntered.add(word); //add word to list of entered words
validWord.setText("Word is VALID!");
validWordPanel.setBorder(BorderFactory.createDashedBorder(Color.GREEN, 4, 3));
enterField.setText(""); //clear text field
checkWon(); //checks if player has won
} else {
validWord.setText("Word is INVALID!");
validWordPanel.setBorder(BorderFactory.createDashedBorder(Color.RED, 4, 3));
}
} else if (event.getSource() == shakeBoard) { //Pressed Randomize Board Button
randomizeBoard(board); //randomize the board
updateBoard(board); //update board in GUI
System.out.println("Random Board");
} else if (event.getSource() == exitGame) { //Pressed Exit Game Button
timer.cancel(); //cancel timer to make sure it doesn't run while they are trying to exit
String exitMessage = "";
//if 2 players, must get other player to confirm that they would like to exit game as well
if (playerNum == 2) {
if (playerTurn == 0) {
exitMessage = name[playerTurn+1] + ", are you sure you would like to exit?";
} else {
exitMessage = name[playerTurn-1] + ", are you sure you would like to exit?";
}
} else {
exitMessage = "Are you sure you would like to exit?";
}
//pause timer
Object[] exitGameValues = {"Yes", "No"};
Object selectedValue = JOptionPane.showInputDialog(null, exitMessage, "Exit Game",JOptionPane.INFORMATION_MESSAGE, null, exitGameValues, exitGameValues[1]);
//try-catch for input validation
try {
if (selectedValue.equals("Yes")) {
System.exit(0);
} else {
startTimer();
}
} catch (Exception e) {
startTimer();
System.out.println(e.getMessage());
}
} else if (event.getSource() == restartGame) {
timer.cancel(); //cancel timer to make sure it doesn't run while they are trying to restart
//ensure that other player wants to restart as well
String exitMessage = "";
if (playerNum == 2) {
if (playerTurn == 0) {
exitMessage = name[playerTurn+1] + ", are you sure you would like to restart?";
} else {
exitMessage = name[playerTurn-1] + ", are you sure you would like to restart?";
}
} else {
exitMessage = "Are you sure you would like to restart?";
}
Object[] resartGameValues = {"Yes", "No"};
Object selectedValue = JOptionPane.showInputDialog(null, exitMessage, "Restart Game",JOptionPane.INFORMATION_MESSAGE, null, resartGameValues, resartGameValues[1]);
//try-catch for input validation
try {
if (selectedValue.equals("Yes")) {
resartGame();
} else {
startTimer();
}
} catch (Exception e) {
startTimer();
System.out.println(e.getMessage());
}
} else if (event.getSource() == pass) { //if player passes
passCounter[playerTurn]++; //add one to counter counting number of times passed
if (playerTurn == 0) playerTurn = 1; //switch to next player
else playerTurn = 0;
score.setText(Integer.toString(playerScore[playerTurn]));
//Resetting timer
timer.cancel();
interval = 15;
startTimer();
playerTurnLabel.setText(name[playerTurn]);
if (passCounter[0] >=2 && passCounter[1] >= 2) { //both players have to pass twice to randomize the board
passCounter[0] = 0;
passCounter[1] = 0;
randomizeBoard(board);
updateBoard(board);
}
}
}
/*
* Main method to run GUI and setup game
*/
public static void main(String[] args) throws Exception {
frame frame1 =new frame();
//startMusic("sound.aiff"); //start music
final int BOARD_SIZE = 5; //board size is 5
board = new String[BOARD_SIZE][BOARD_SIZE]; //declare a 2D array for the board
wordList = readFromFile(); //wordList stores dictionary words by calling the method
randomizeBoard(board); //randomize the board
updateBoard(board); //update the board in the GUI
if (playerNum == 1) interval = onePlayerTimeInterval; //if one player mode, set the timer to the time they inputted
if (playerNum==2) playerTurn = (int)(Math.random()*2); // if 2 player, "flip a coin" (generate a random number from 0 to 1) to see who goes first
getReady();
startTimer(); //start the timer
}
/*
* Method lets user know that their turn is starting and counts down
*/
public static void getReady () {
introLabel.setText(name[playerTurn] +", your turn starts in...");
playerTurnLabel.setText(name[playerTurn]);
timeDelays(1500);
introLabel.setText("3....");
timeDelays(1000);
introLabel.setText("2.....");
timeDelays(1000);
introLabel.setText("1.....");
timeDelays(1000);
introLabel.setText("The Timer has Started!!");
}
/*
* Method sets time delays which are used for countdown messages in getReady()
*/
public static void timeDelays (int time) { // method to add time delays between print statements.
try {
Thread.sleep(time);
} catch (Exception e) {}
//timeDelays(1000);
} // end of time delays
/*
* Method starts a timer and runs for certain time interval.
* The time interval is called by setTimerInterval method
*/
public static void startTimer() {
int delay = 1000;
int period = 1000;
timer = new Timer();
timeRemaining.setText(Integer.toString(interval));
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
//System.out.println(setTimerInterval());
timeRemaining.setText(Integer.toString(setTimerInterval()));
}
}, delay, period);
}
/*
* Method sets the interval of a timer
*/
public static int setTimerInterval() {
if (interval == 0 && playerNum == 2) { //once timer is finished
if (playerTurn == 0) { playerTurn = 1; }
else { playerTurn = 0; }
playerTurnLabel.setText(name[playerTurn]);
score.setText(Integer.toString(playerScore[playerTurn]));
timer.cancel();
checkWon();
if (!hasWon) {
interval = 16;
startTimer();
}
} else if (interval == 0 && playerNum == 1) {
if (playerScore[playerTurn] < scoreToWin) {
timer.cancel();
hasWon = true;
Object[] resartGameValues = {"Yes", "No"};
Object selectedValue = JOptionPane.showInputDialog(null,
"Sorry, " + name[playerTurn] +". You have lost! \nWould you like to play again?", "Game Over!",
JOptionPane.INFORMATION_MESSAGE, null,
resartGameValues, resartGameValues[1]);
try {
if (selectedValue.equals("Yes")) {
resartGame();
} else {
System.exit(0);
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
return --interval;
}
/*
* Method updates the board by removing everything and setting the border and font
*/
public static void updateBoard(String[][] board) {
gridPan.removeAll();
for (int i = 0; i < boardLabelGrid.length; i++) {
for (int j = 0; j< boardLabelGrid.length; j++) {
boardLabelGrid[i][j] = new JLabel(board[i][j], JLabel.CENTER);
boardLabelGrid[i][j].setBorder(labelBorder);
boardLabelGrid[i][j].setFont(biggerFont);
gridPan.add(boardLabelGrid[i][j]);
}
}
gridPan.revalidate();
gridPan.repaint();
}
/*
* Method checks if the word is valid by calling methods to check a certain characteristic. If all methods return true,
* then validate returns true; otherwise, it returns false.
*/
public static boolean validate(String word, int wordLen, String[] wordList, ArrayList<String> wordsEntered, String[][] board) {
int min = 0;
int max = wordList.length-1;
String wordLowerCase = word.toLowerCase();
if (checkDict(wordList, wordLowerCase, min, max) > -1 && checkLength(word, wordLen) && checkAdjacent(board, word) && checkDuplicateWord(wordsEntered, word)) {
return true;
}
return false;
}
/*
* Method checks if the word is in the dictionary by using recursive binary search
* if the word is in the dictionary, then method returns the index of the word; otherwise, it returns -1
*/
public static int checkDict(String[] wordList, String word, int min, int max) {
int middle = (max + min)/2;
if (max < min) {
return -1;
}
if (word.compareTo(wordList[middle])==0) {
return middle;
} else if (word.compareTo(wordList[middle]) > 0) {
return checkDict(wordList, word, middle+1, max);
} else if (word.compareTo(wordList[middle]) < 0) {
return checkDict(wordList, word, min, middle-1);
}
return -1;
}
/*
* Method returns true if word length is at least the minimum word length. Otherwise,
* it returns false.
*/
public static boolean checkLength(String word, int wordLen) {
if (word.length() >= wordLen) {
return true;
}
return false;
}
/*
* Method returns true if word entered has not been entered before. Otherwise, it
* returns false.
*/
public static boolean checkDuplicateWord(ArrayList<String> usedWords, String word) {
if (usedWords.contains(word)) {
return false;
}
return true;
}
/*
* Method checks if the word's letters are horizontally, vertically and diagonally adjacent to each other.
* If the word is found, it returns true; otherwise it returns false
*/
public static boolean checkAdjacent(String[][] board, String word) {
word = word.toUpperCase(); //Setting the word to upper case as string comparison is case sensitive
for (int i=0; i<board.length; i++) { //nested for-loop looping over the board 2d array
for (int j=0; j<board[i].length; j++) {
if (board[i][j].equals(Character.toString(word.charAt(0)))) { //if the board at index (i, j) equals to the first character of the word
gridSearch(board, i, j, -1, -1, word, 0, word.length()-1); //run the recursive gridSearch method with (i, j) as the initial coordinates
}
if (found) { //if global variable found is true
found = false; //set true to false
return true; //return true
}
}
}
return false; //otherwise return false
}
/*
* Method returns true if a pair of coordinates (x, y) are of valid
* position on the board of characters. Otherwise, returns false.
*/
public static boolean indexValid(String[][] board, int row, int col, int prevRow, int prevCol) {
int len = board.length; //initlizing variable len to be the length of the board (grid of characters
if ((row >= 0 && col >= 0 && row < len && col < len) && !(prevRow == row && prevCol == col)) { //checking if the index (row, col) is valid
return true;
} else {
return false;
}
}
/*
* Method recursively searches in 8 directions around the character until it is
* able to find all the characters of the word on the grid.
*/
public static void gridSearch(String[][] board, int row, int col, int prevRow, int prevCol, String word, int index, int wordLen) {
int[] x = {-1, -1, -1, 0, 0, 1, 1, 1}; //initializing an array of x positions to search around the previous character
int[] y = {-1, 0, 1, -1, 1, -1, 0, 1}; //initializing an array of y positions to search around the previous character
if (index > wordLen || !board[row][col].equals(Character.toString(word.charAt(index)))) { //checking if the current index is larger than the length of
return; //the word or if the board at index (row, col) does not equal
} //the word at current index. If true, return and end search.
if (index == wordLen) { //base case: if index equals the length of the word, that means that the search has successfully found all characters in the
found = true; //word on the board.
return; //if true: Set found equals to true, return and end search.
}
for (int i=0; i < 8; i++) { //for loop for searching in 8 directions around current character
if (indexValid(board, (row + x[i]), (col + y[i]), prevRow, prevCol)) { //if the index (row, col) is valid
gridSearch(board, row + x[i], col + y[i], row, col, word, index+1, wordLen); //run recursive method gridSearch to search around current character
}
}
}
/*
* Method generates a random board of letters
*/
public static void randomizeBoard(String [][] board) {
ArrayList <Integer> ranNums = new ArrayList <Integer>();
for (int i = 0; i < die.length; i++) {
ranNums.add(i); //initialize arraylist with values from 0 to die.length-1 (inclusive)
}
Collections.shuffle(ranNums); //randomize order of values
int count = 0;
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
board[i][j] = die[ranNums.get(count)]; //set board[i][j] to a previously generated random indexed String from die array
int randIndex = (int)(Math.random() * (board[i][j]).length()); //generate random number from 0 to length of String
board[i][j] = Character.toString(board[i][j].charAt(randIndex)); //set board[i][j] to a random character within the String
count++; //add one to counter so that next previously generated random number gets used
}
}
}
/*
* Method reads from a dictionary file and adds them to an arraylist.
* It returns the arraylist converted to an array
*/
public static String [] readFromFile() throws Exception {
Scanner readFile = new Scanner(new File("wordlist.txt"),"UTF-8"); //declare scanner to read text file
ArrayList<String> wordArrayList = new ArrayList<String>(); //arraylist to store words from file
while(readFile.hasNext()) {
wordArrayList.add(readFile.nextLine());
}
readFile.close();
return wordArrayList.toArray(new String [wordArrayList.size()]); //convert arraylist to array
}
/*
* Method plays music on a continuous loop
*/
public static void startMusic(String filepath) throws UnsupportedAudioFileException, IOException, LineUnavailableException {
AudioInputStream audioInputStream;
audioInputStream = AudioSystem.getAudioInputStream(new File(filepath).getAbsoluteFile());
Clip clip = AudioSystem.getClip();
clip.open(audioInputStream);
clip.loop(Clip.LOOP_CONTINUOUSLY);
clip.start();
}
}