-
Notifications
You must be signed in to change notification settings - Fork 0
/
WordFinderGame.java
421 lines (339 loc) · 14 KB
/
WordFinderGame.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
/**
WordFinderGame.java
Choose the number of letters to use for your game ( from 2-15, inclusive ). The game then selects that many random
letters and displays them. You can also choose to allow for random letters, '?' tiles, also know as 'blank' tiles,
such as in Scrabble, which can be any letters of the alphabet. Enter any words as you find them, and attempt to find
as many as you can. You can choose to display the words you have found thus far, and you can also choose to find all
the words possible.
This game uses the Dictionary.java file for processing and uses the Collin's 15th Edition Dictionary (2015).
@author Peter Olson
@version 4/1/18
@see Dictionary.java
*/
import java.util.Scanner;
import java.util.Random;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.io.File;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.util.Collections;
public class WordFinderGame {
public static ArrayList<String> currentList;
public static Dictionary dct;
public static int score = 0;
public static final String RESULT_TXT = "result_word_finder_game.txt";
public static final String CURRENT_TXT = "current_words_found_finder_game.txt";
private static String letterList = "";
public static void main( String[] args ) {
Scanner scanner = new Scanner( System.in );
currentList = new ArrayList<String>();
dct = new Dictionary();
dct.setCurrentDirectory( dct.DIRECTORY_PATH );
dct.clearFile( CURRENT_TXT );
dct.clearFile( RESULT_TXT );
SOPln("Welcome to the Word Finder Game!\n\nThis game is similar to many word games where you try and" +
" find as many words as you can with the letters given.\n" +
"This game uses Collin's 15th Edition Dictionary (2015).");
SOPln("\nHow many letters would you like to play with?");
int numLetters = scanner.nextInt();
scanner.nextLine();
SOPln("Would you like to use random letters / blanks?");
boolean useBlanks = false;
if( isPositiveResponse() )
useBlanks = true;
letterList = getLetters( numLetters, useBlanks );
letterList = letterList.toUpperCase();
dct.descramble( letterList, RESULT_TXT );
boolean willContinue = true;
do {
SOPln("Letters:\t\t" + letterList + "\n");
SOPln("Enter a word to add it to your list. Enter the below single letters to do the following:\n" +
"\tp - print your current list\n\ts - scramble the letters\n\th - see how many words you are missing of each length\n" +
"\tf - end the game, and see what words you missed and your final score\n" + "\tq - quit");
String response = scanner.nextLine();
int lengthResponse = response.length();
if( lengthResponse == 1 )
willContinue = assessResponse( response );
else if( lengthResponse > 0 && lengthResponse <= numLetters )
assessWord( response );
else
SOPln("\nPlease enter a word greater than 0 letters long and less than " + numLetters + " long.");
} while( willContinue );
scanner.close();
}
/**
Gets a random list of letters dependent on:
1. The number of letters the user wants
2. The weights of each letter, taken from Scrabble's distribution
3. Whether or not the user has chosen to use blank letters
@param numLetters The total number of letters in the list
@param useBlanks True if blanks ('?') are being used, false otherwise (aka variable letters)
@return String A String of random weighted letters
*/
public static String getLetters( int numLetters, boolean useBlanks ) {
Random random = new Random();
String letterList = "";
final String[] ALPHABET_LIST = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","?"};
int[] weights = { 9, 2, 2, 4, 12, 2, 3, 2, 9, 1, 1, 4, 2, 6, 8, 2, 1, 6, 4, 6, 4, 2, 2, 1, 2, 1, 2};
//Used to shut out blank letters if they aren't being used
int VAR_BUFFER = 1;
if( useBlanks )
VAR_BUFFER = 0;
//Set the cumulativeWeights, based on the weights, as well as the total weight
int[] cumulativeWeights = new int[ weights.length ];
int totalWeight = 0;
for( int i = 0; i < weights.length; i++ ) {
totalWeight += weights[i];
if( i != 0 )
cumulativeWeights[i] = weights[i] + cumulativeWeights[ i - 1 ];
else
cumulativeWeights[i] = weights[i];
}
//Add random letters based on the weights until the set number of letters is reached
int count = 0;
while( count++ < numLetters ) {
int letterScore = random.nextInt( totalWeight - (VAR_BUFFER * weights[ weights.length - 1 ]) );
int letterIndex = 0;
while( letterScore > cumulativeWeights[ letterIndex ] ) {
letterIndex++;
}
letterList += ALPHABET_LIST[ letterIndex ];
}
return letterList;
}
/**
Check if the response is a word. If it is, add it to the list and update the score.
@param response The String the user believes is a word
@param letterList The list of letters that can be used for finding words from
@see Dictionary.isWord(..)
*/
private static void assessWord( String response ) {
response = response.toUpperCase();
if( dct.isWord( response ) && dct.fileContains( RESULT_TXT, response ) ) {
if( !currentList.contains( response ) ) {
currentList.add( response );
score += response.length();
SOPln( "\n" + response + " is a word! +" + response.length() + " points!\t\tScore: " + score + "\n" );
} else {
SOPln( "\n" + response + " is a word! But you have already found this word." );
}
} else {
SOPln("\nThat's not a valid word!\n");
}
}
/**
Handles the responses for menu selections such as printing the current words found, printing hints,
and ending the game and showing scores, or simply quickly quitting the game.
@param response The String single-letter response
@return boolean True if the game is to be continued, false otherwise
*/
private static boolean assessResponse( String response ) {
response = response.toUpperCase();
if( response.equals("P") )
printCurrentList();
else if( response.equals("S") )
scramble( );
else if( response.equals("H") )
printHints();
else if( response.equals("F") ) {
scores();
return false;
} else
return false;
return true;
}
/**
Scramble the letters to a different orientation
@see assessResponse( String response, String letterList )
*/
private static void scramble( ) {
ArrayList<String> list = new ArrayList<String>();
for( int i = 0; i < letterList.length(); i++ ) {
list.add( String.valueOf( letterList.charAt(i) ) );
}
Collections.shuffle( list );
String newList = "";
for( String letter : list )
newList += letter;
letterList = newList;
}
/**
Print the current list of words and see the current score
@see Dictionary.write(..)
@see Dictionary.removeDuplicates(..)
@see Dictionary.orderIncreasing(..)
@see Dictionary.alphabetizeSets(..)
@see Dictionary.printFile(..)
*/
private static void printCurrentList() {
SOPln("\nWords found:\n");
dct.write( currentList, CURRENT_TXT );
dct.removeDuplicates( CURRENT_TXT );
dct.orderIncreasing( CURRENT_TXT );
dct.alphabetizeSets( CURRENT_TXT );
dct.printFile( CURRENT_TXT );
SOPln( "Score: " + score + "\n");
}
/**
Print hints to what words are still missing from each word length.
@see getMaxScore();
@see getWordsPerTier();
*/
private static void printHints() {
dct.write( currentList, CURRENT_TXT );
dct.removeDuplicates( CURRENT_TXT );
dct.orderIncreasing( CURRENT_TXT );
dct.alphabetizeSets( CURRENT_TXT );
int maxScore = getMaxScore();
int[] numWordsPerTierCurrent = getWordsPerTier( CURRENT_TXT );
int[] numWordsPerTierResult = getWordsPerTier( RESULT_TXT );
SOPln("");
for( int i = 0; i < numWordsPerTierResult.length; i++ ) {
int wordsMissing = numWordsPerTierResult[i];
if( numWordsPerTierCurrent.length > i && numWordsPerTierCurrent[i] != 0 ) {
wordsMissing = numWordsPerTierResult[i] - numWordsPerTierCurrent[i];
if( wordsMissing != 0 )
SOPln( (i + 2) + " letter words missing: " + wordsMissing );
else
SOPln( (i + 2) + " letter words missing: None!" );
} else {
SOPln( (i + 2) + " letter words missing: All of them! (" + wordsMissing + " total)" );
}
}
SOPln("\nYour score: " + score + "\nMax score: " + maxScore + "\n");
}
/**
Returns a list of the total number of words found of a given length for all lengths found in the text file
@param fileName The text file to be processed
@return int[] An array of numbers, each of which correlates to the next total number of words found in the text file,
each adjacent element being words one letter longer than the previous one
@see printHints();
@see getScanner();
@see toIntArray( Integer[] intArray );
@see Arrays.copyOf(..)
*/
private static int[] getWordsPerTier( String fileName ) {
Scanner scanner = getScanner( fileName );
ArrayList<Integer> numList = new ArrayList<Integer>();
int counter = 0;
int wordSize = -1;
while( scanner.hasNextLine() ) {
String word = scanner.nextLine();
if( wordSize == -1 ) {
wordSize = word.length();
counter++;
} else if( word.length() != wordSize ) {
numList.add( counter );
while( ++wordSize != word.length() ) {
counter = 0;
numList.add( counter );
}
counter = 1;
} else {
counter++;
}
}
numList.add( counter );
scanner.close();
return toIntArray( Arrays.copyOf( numList.toArray(), numList.size(), Integer[].class ) );
}
/**
Convert an Integer array to an int array
@param list The array to be converted to an int array
@return int[] The resulting int array
@see getWordsPerTier()
*/
private static int[] toIntArray( Integer[] list ){
int[] intArray = new int[ list.length ];
for( int i = 0; i < intArray.length; i++ )
intArray[i] = list[i];
return intArray;
}
/**
Finds the max score of a text file of words
@return int The total score of all the words in a file
@see getScanner();
@see printHints();
@see scores();
*/
private static int getMaxScore() {
Scanner scanner = getScanner( RESULT_TXT );
int maxScore = 0;
while( scanner.hasNextLine() ) {
String word = scanner.nextLine();
maxScore += word.length();
}
scanner.close();
return maxScore;
}
/**
Print your findings and what you missed and the final scores
@see printCurrentList()
@see Dictionary.printList()
@see getMaxScore()
*/
private static void scores() {
int maxScore = getMaxScore();
SOPln("\nHere's what you found: ");
printCurrentList();
SOPln("Here's what you missed: ");
removeOverlap();
dct.printFile( RESULT_TXT );
SOPln("Your final score: " + score + "\nMax possible score: " + maxScore );
SOPln("\nGoodbye nerd.\n");
}
/**
Remove all words found in the current list from the results list
@see Dictionary.removeWordsContainingX(..)
@see scores()
*/
private static void removeOverlap() {
for( String word : currentList )
dct.removeWords( RESULT_TXT, word );
}
/**
Given a text file name, attach a reader and a scanner to that reader to read the text file.
Throws a FileNotFoundException and return "File not found" is the text file cannot be found.
@param fileName The text file to be read
@return Scanner The scanner object now attached to the text file
@see File.getAbsoluteFile()
*/
private static Scanner getScanner( String fileName ) {
FileReader fr = null;
Scanner scan = null;
try {
fr = new FileReader( fileName );
scan = new Scanner( fr );
} catch( FileNotFoundException e ) {
try {
fr = new FileReader( new File( fileName ).getAbsoluteFile() );
scan = new Scanner( fr );
} catch( FileNotFoundException e2 ) {
System.out.println("File not found");
return null;
}
}
return scan;
}
/**
Assuming the user will input some form of yes or no, assess the response and return a boolean interpretation
@return boolean True if yes, false if no
*/
private static boolean isPositiveResponse( ) {
Scanner scanner = new Scanner( System.in );
String response = scanner.nextLine();
response = response.toUpperCase();
if( response.contains("Y") || response.contains("1") || response.contains("SURE") || response.contains("OK") )
return true;
return false;
}
/**
Replaces System.out.println(..) because laziness.
@param message The message to print
*/
private static void SOPln( String message ) {
System.out.println( message );
}
}