-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordSearch.java
More file actions
505 lines (464 loc) · 13.3 KB
/
WordSearch.java
File metadata and controls
505 lines (464 loc) · 13.3 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
/* Name: Charlie LaWarne
Maddie Herrmann
Assignment: Lab #10
Title: Word search
Course: CS 144
Class section: Section 2
Lab Section: Section 1
Semester: Spring 2019
Instructor: Dr.Cao
Date: 5-17-19
Sources consulted: docs.oracle.com for the Dialog window for the instructions
Known Bugs: findMissedWords does not work
Program description: Creates a game using a GUI that takes player answers and
gives a score for the correct answers.
Creativity: Created instructions button where a new window pops up with the
instructions before the game begins.
Instructions: Press new game to be given a word and play to check the player
answers once everything is typed in. For specific instructions click the
instructions button.
*/
import java.util.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
public class WordSearch extends JFrame
{
/**
* South panel.
*/
private JPanel sPanel;
/**
* North Panel
*/
private JPanel nPanel;
/**
* Play Button
*/
private JButton play;
/**
* New Game Button.
*/
private JButton newGame;
private JButton instructionsDisp;
/**
* Word Given to Player by Dictionary.
*/
private JTextField givenWord;
/**
* Title "Word Search" on GUI.
*/
private JLabel title;
/**
* How to play the game shown under the title.
*/
private JLabel instructions;
/**
* Given Word Box
*/
private String wordGiven = "";
/**
* Word Given.
*/
private static String[] dictionary;
/**
* Random Word instantiation
*/
private Random rand = new Random();
/**
* Player Input text box
*/
private JTextArea showText;
/**
* Answers player provides.
*/
private String[] answers;
public WordSearch() throws IOException
{
// instantiate the method
super("Word Search");
readDictionary();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//panel setup
JPanel mainPanel = new JPanel();
mainPanel.setLayout( new BorderLayout() );
nPanel = new JPanel();
nPanel.setLayout( new BorderLayout() );
sPanel = new JPanel();
//formatting for GUI
play = new JButton("Play");
newGame = new JButton("New Game");
instructionsDisp = new JButton("Instructions");
title = new JLabel("Word Search");
title.setHorizontalAlignment(SwingConstants.CENTER);
title.setVerticalAlignment(SwingConstants.CENTER);
title.setFont(new Font("Verdana", Font.BOLD, 48));
title.setForeground(Color.RED);
instructions = new JLabel("Enter as many words as you can find using only the letters in:");
instructions.setHorizontalAlignment(SwingConstants.CENTER);
instructions.setVerticalAlignment(SwingConstants.CENTER);
showText = new JTextArea(20, 65);
showText.setLineWrap(true);
JScrollPane scroll = new JScrollPane ( showText );
scroll.setVerticalScrollBarPolicy( ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS );
givenWord = new JTextField(20);
givenWord.setFont(new Font("Verdana", Font.BOLD, 24));
givenWord.setForeground(Color.BLUE);
givenWord.setBackground(Color.WHITE);
answers = new String[10000];
showText.setEditable(false);
play.addActionListener(new playButtonListener());
newGame.addActionListener(new gameButtonListener());
instructionsDisp.addActionListener(new instructionsButtonListener());
mainPanel.add(scroll);
nPanel.add(title, BorderLayout.NORTH);
nPanel.add(instructions, BorderLayout.CENTER);
nPanel.add(givenWord, BorderLayout.SOUTH);
mainPanel.add(scroll);
sPanel.add(play);
sPanel.add(newGame);
sPanel.add(instructionsDisp);
add( sPanel, BorderLayout.SOUTH );
add( nPanel, BorderLayout.NORTH );
add( mainPanel, BorderLayout.CENTER);
this.setSize(750, 500);
this.setVisible(true);
givenWord.setEditable(false);
}
/**
* This method reads the file .
* <p>
* File is scanned in using new scanner. This file
* is the dictionary
* </p>
* Dictionary is read using while loop and the file is then closed
* for the next game.
* @param inputFile .
* @return fileScan
*/
public void readDictionary () throws IOException
{
//create scanner object
Scanner keyboard = new Scanner(System.in);
dictionary = new String[172824];
int i = 0;
//read file name
File inputFile = new File ("words.txt");
Scanner fileScan = new Scanner (inputFile);
fileScan.nextLine();
while( fileScan.hasNext() )
{
dictionary[i] = fileScan.next(); //Read word
i++;
}
fileScan.close(); //close the file connection
}
/**
* This method creates new game button. (1)
* <p>
* Action listnere used to set game button.
* </p>
* Text on game button reads new game.
*
* @param givenWord the word that is given by the dictionary.
* @return givenWord.
*/
private class gameButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
showText.setEditable(true);
wordGiven = dictionary[rand.nextInt(172824)];
givenWord.setText("" + wordGiven);
showText.setText("");
}
}
/**
* This method creates the play button.
* <p>
* Action listener implemented to scan in words given and given
* appropriate feedback
* </p>
* Feedback given comes from the method checkPlayerWords.
*
* @param answers words the player gives.
* @return answers.
*/
private class playButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
answers = getPlayerWords();
checkPlayerWords(answers);
showText.setEditable(false);
findMissedWords(answers);
}
}
private class instructionsButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(null, "The instructions are: The words must be four or more letters. Proper nouns are not used. Slang words are not used. The words must appear in the dictionary and may not be the entire original word.", "Instructions", JOptionPane.INFORMATION_MESSAGE);
}
}
/**
* This method returns player words to lower case.
* <p>
* Lower case words are checked by words in the dcitionary .
* </p>
* And even more explanations to follow in consecutive
* paragraphs separated by HTML paragraph breaks.
*
* @param fullText.
* @return fullText.
*/
private String[] getPlayerWords()
{
String fullText = showText.getText().toLowerCase();
return fullText.split("\\s+");
}
/**
* This method checks the player words to see if they are in dictionary.
* <p>
* Words not in dictionary
* Words too short
* Search word
* Not in search word
* </p>
* Player feedback is given with score.
*
* @param totalScore. (3)
* @return totalScore.
*/
private void checkPlayerWords(String[] answers)
{
int totalScore = answers.length;
for (int i = 0; i < answers.length; i++)
{
if(searchDictionary(answers[i]) == false)
{
answers[i] = answers[i] + " not in dictionary";
totalScore--;
}
else if(answers[i].length() <= 3)
{
answers[i] = answers[i] + " Too short";
totalScore--;
}
else if(inSearchWord(answers[i]) == false)
{
answers[i] = answers[i] + " Not in search word";
totalScore--;
}
else if (sameWord(answers[i]) == true)
{
answers[i] = answers[i] + " Search word";
totalScore--;
}
for(int j = 0; j < i; j++)
{
if(answers[i].equals(answers[j]))
{
answers[i] = answers[i] + " Answer has already been given";
totalScore--;
}
}
for(int k = 0; k < dictionary.length; k++)
{
if(answers[i].length() == 4 && answers[i].substring(0,2).equals(dictionary[k]) && answers[i].charAt(3) == 's')
{
answers[i] = answers[i] + " Answer acquires 4 letters by adding an s to the end";
totalScore--;
}
}
}
showText.setText("");
for(int j = 0; j < answers.length; j++)
{
showText.append(answers[j] + "\n");
}
showText.append("You got " + totalScore + " correct");
}
/**
* This method searches the dictionary.
* <p>
* Counts characters in dictionary
* </p>
* If statments given for individual letters.
*
* @param charCountOutput output of characters in string .
* @return
*/
private boolean searchDictionary(String word)
{
boolean trueFalse = false;
for(int i = 0; i < dictionary.length; i++)
{
if(word.equals(dictionary[i]))
trueFalse = true;
}
return trueFalse;
}
private int[] charCount(String s)
{
int[] charCountOutput;
charCountOutput = new int[26];
charCountOutput[0] = 0;
charCountOutput[1] = 0;
charCountOutput[2] = 0;
charCountOutput[3] = 0;
charCountOutput[4] = 0;
charCountOutput[5] = 0;
charCountOutput[6] = 0;
charCountOutput[7] = 0;
charCountOutput[8] = 0;
charCountOutput[9] = 0;
charCountOutput[10] = 0;
charCountOutput[11] = 0;
charCountOutput[12] = 0;
charCountOutput[13] = 0;
charCountOutput[14] = 0;
charCountOutput[15] = 0;
charCountOutput[16] = 0;
charCountOutput[17] = 0;
charCountOutput[18] = 0;
charCountOutput[19] = 0;
charCountOutput[20] = 0;
charCountOutput[21] = 0;
charCountOutput[22] = 0;
charCountOutput[23] = 0;
charCountOutput[24] = 0;
charCountOutput[25] = 0;
for (int i = 0;i < s.length();i++)
{
if(s.charAt(i) == 'a' || s.charAt(i) == 'A')
charCountOutput[0]++;
if(s.charAt(i) == 'b' || s.charAt(i) == 'B')
charCountOutput[1]++;
if(s.charAt(i) == 'c' || s.charAt(i) == 'C')
charCountOutput[2]++;
if(s.charAt(i) == 'd' || s.charAt(i) == 'D')
charCountOutput[3]++;
if(s.charAt(i) == 'e' || s.charAt(i) == 'E')
charCountOutput[4]++;
if(s.charAt(i) == 'f' || s.charAt(i) == 'F')
charCountOutput[5]++;
if(s.charAt(i) == 'g' || s.charAt(i) == 'G')
charCountOutput[6]++;
if(s.charAt(i) == 'h' || s.charAt(i) == 'H')
charCountOutput[7]++;
if(s.charAt(i) == 'i' || s.charAt(i) == 'I')
charCountOutput[8]++;
if(s.charAt(i) == 'j' || s.charAt(i) == 'J')
charCountOutput[9]++;
if(s.charAt(i) == 'k' || s.charAt(i) == 'K')
charCountOutput[10]++;
if(s.charAt(i) == 'l' || s.charAt(i) == 'L')
charCountOutput[11]++;
if(s.charAt(i) == 'm' || s.charAt(i) == 'M')
charCountOutput[12]++;
if(s.charAt(i) == 'n' || s.charAt(i) == 'N')
charCountOutput[13]++;
if(s.charAt(i) == 'o' || s.charAt(i) == 'O')
charCountOutput[14]++;
if(s.charAt(i) == 'p' || s.charAt(i) == 'P')
charCountOutput[15]++;
if(s.charAt(i) == 'q' || s.charAt(i) == 'Q')
charCountOutput[16]++;
if(s.charAt(i) == 'r' || s.charAt(i) == 'R')
charCountOutput[17]++;
if(s.charAt(i) == 's' || s.charAt(i) == 'S')
charCountOutput[18]++;
if(s.charAt(i) == 't' || s.charAt(i) == 'T')
charCountOutput[19]++;
if(s.charAt(i) == 'u' || s.charAt(i) == 'U')
charCountOutput[20]++;
if(s.charAt(i) == 'v' || s.charAt(i) == 'V')
charCountOutput[21]++;
if(s.charAt(i) == 'w' || s.charAt(i) == 'W')
charCountOutput[22]++;
if(s.charAt(i) == 'x' || s.charAt(i) == 'X')
charCountOutput[23]++;
if(s.charAt(i) == 'y' || s.charAt(i) == 'Y')
charCountOutput[24]++;
if(s.charAt(i) == 'z' || s.charAt(i) == 'Z')
charCountOutput[25]++;
}
return charCountOutput;
}
/**
* This method compares search word to character count.
* <p>
* @param searchWordTemp
* @return
*/
private boolean inSearchWord(String word)
{
int[] searchWordTemp;
searchWordTemp = new int[26];
searchWordTemp = charCount(word);
int[] searchWordTemp2;
searchWordTemp2 = new int[26];
searchWordTemp2 = charCount(wordGiven);
boolean trueFalse = true;
for (int i = 0; i < 26; i++)
{
if (searchWordTemp[i] > searchWordTemp2[i] )
{
trueFalse = false;
}
}
return trueFalse;
}
/**
* This method searches for words that are the same as the search word .
* <p>
* @param searchWordTemp2.
* @return
*/
private boolean sameWord(String word)
{
int[] searchWordTemp;
searchWordTemp = new int[26];
searchWordTemp = charCount(word);
int[] searchWordTemp2;
searchWordTemp2 = new int[26];
searchWordTemp2 = charCount(wordGiven);
boolean trueFalse = true;
for (int i = 0; i < 26;i++ )
{
if (searchWordTemp[i] != searchWordTemp2[i] )
{
trueFalse = false;
}
}
return trueFalse;
}
/**
* This method find the words that are missed.
* <p>
* Missed words are displayed at the bottom of the screen.
* </p>
* @param missedWords.
* @return
*/
private void findMissedWords(String [] words)
{
String missedWords = "";
for(int i = 0; i < dictionary.length; i++)
{
for (int j = 0; j < words.length; j++)
{
if(words[j].equals(dictionary[i]))
missedWords = missedWords + words[i] + ", ";
}
}
showText.append("\n" + missedWords);
}
// main method
public static void main( String[] args ) throws IOException
{
WordSearch frame = new WordSearch();
}
}