-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordTarget.java
More file actions
424 lines (420 loc) · 15.6 KB
/
Copy pathWordTarget.java
File metadata and controls
424 lines (420 loc) · 15.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
import java.util.Random;
import java.util.Scanner;
import java.io.*;
import java.util.*;
public class WordTarget {
public static boolean newGame = false;
//declare variables word set and word list and the word
public Scanner scanner;
public static Set<String> words;
public static ArrayList<String> nineLetterWords;
public String theWord;
public String theScrambledWord;
private ArrayList<String> answers = new ArrayList<>();
public void updateAnswers(String s){
answers.add(s);
}
//methods for creating word set and list
public static void initiateNineLetterWords(){
nineLetterWords = new ArrayList<>();
ReadFile r = new ReadFile();
nineLetterWords = new ArrayList<>();
nineLetterWords = r.createList();
}
public void Welcome(){
System.out.println();
System.out.println("Welcome to Word Target!");
System.out.println();
}
public void instructions(){
System.out.println("Come up with as many words as possible, using " +
"the letter in the grid, containing FOUR letters or more.");
System.out.println("There is at least one nine letter word");
System.out.println("Words MUST contain the MIDDLE letter.");
System.out.println();
System.out.println("Type your answers then press [enter]");
System.out.println("To see the nine letter word type 9");
System.out.println("To see your answers so far type: 1");
System.out.println("To see all possible answers type: 5");
System.out.println("To start a new game, type \"new\"" +
"your current game will be lost \n" +
" for ever");
System.out.println("Enter * to quit");
}
//create "words"-a list of all english words in the main method
public static void initiateWords(){
ReadFile r = new ReadFile();
words = new HashSet<>();
words = r.createSet();
}
public void initiateTheWord(){
theWord = getWord();
}
public void scrambleTheWord(){
theScrambledWord = scramble(theWord);
}
public Set<String>getWords(){
return words;
}
//main
public static void main(String []args) {
int lineCount = 0;
WordTarget t = new WordTarget();
Scanner scan = new Scanner(System.in);
initiateWords();
initiateNineLetterWords();
t.initiateTheWord();
t.Welcome();
t.instructions();
System.out.println();
t.scrambleTheWord();
System.out.println();
t.gridify(t.theScrambledWord);
System.out.println();
ArrayList<String> answers = t.allAnswers();
//get user input
String input = scan.next();
input = input.toLowerCase();
//validate etc
while(!(input.equals("*"))){
if(input.equals("new")){
newGame = true;
System.out.println("Are you sure you want to start a new game?");
System.out.println("type \"yes\" to confirm new game," +
" or anything else to resume");
input = scan.next();
if(input.equals("yes")){
t.initiateTheWord();
t.Welcome();
t.instructions();
t.scrambleTheWord();
System.out.println();
t.gridify(t.theScrambledWord);
answers = t.allAnswers();
}
//if anything other than new is entered, game resumes
}
if(input.equals("5")){
System.out.println((answers));
System.out.println();
System.out.println("There are " + answers.size() +
" possible answers");
t.gridify(t.theScrambledWord);
System.out.println();
}
else if(input.equals("1")){
System.out.println(t.answers);
lineCount+=2;
}
else if(input.equals("9")){
System.out.println(t.theWord);
lineCount+=2;
}
else if(t.answers.contains(input)){
System.out.println("you already gave that answer");
lineCount+=2;
}
else if(input.equals(t.theWord)){
System.out.println("WOOHOO! you are AMAZING," +
" YOU FOUND THE NINE LETTER WORD");
}
else if(t.check(input)){
//random int between 0 and 4
int n = (int) Math.floor(Math.random() * 5);
if(n == 0){
System.out.println("nice!");
}
else if(n == 1){
System.out.println("great!");
}
else if(n == 2){
System.out.println("good word!");
}
else if(n == 3){
System.out.println("nice one!");
}
else if(n == 4){
System.out.println("wow nice job!");
}
t.updateAnswers(input);
lineCount +=3;
}
//the following is an else statement to add to
// line count when an invalid word is entered
else{
lineCount+=3;
}
if(lineCount > 20){
System.out.println();
t.gridify(t.theScrambledWord);
System.out.println();
lineCount = 0;
}
System.out.println();
input = scan.next();
input = input.toLowerCase();
}
}
public Set<String> createSet(){
Set<String> words = new HashSet<>();
try{
scanner = new Scanner(new File("words.txt"));
}
catch(Exception e){
System.out.println("oops, couldn't find my dictionary!");
}
while(scanner.hasNext()){
String a = scanner.next();
words.add(a);
}
return words;
}
//create a List out of txt file
//this will be used to generate the initial word
public ArrayList<String> createList(){
ArrayList<String> nineLetterWords = new ArrayList<>();
try{
scanner = new Scanner(new File("words.txt"));
}
catch(Exception e){
System.out.println("oops, couldn't find my dictionary!");
}
while(scanner.hasNext()){
String a = scanner.next();
if(a.length() == 9){
nineLetterWords.add(a);
}
}
return nineLetterWords;
}
public String getWord(){
ReadFile d = new ReadFile();
ArrayList<String> wordz = new ArrayList<>();
wordz = d.createList();
//create a random number between 0 and the length of the list
Random rand = new Random();
int num = rand.nextInt(wordz.size());
theWord = wordz.get(num);
return theWord;
}
public String scramble(String w){
String scrambledWord = "";
//scramble word
ArrayList<Character> chars = new ArrayList<Character>(w.length());
for ( char c : w.toCharArray() ) {
chars.add(c);
}
Collections.shuffle(chars);
char[] shuffled = new char[chars.size()];
for ( int i = 0; i < shuffled.length; i++ ) {
shuffled[i] = chars.get(i);
}
scrambledWord = new String(shuffled);
return scrambledWord;
}
//the following method displays a nine letter word in a grid-like fashion
public void gridify(String word){
String wordCaps = word.toUpperCase();
//we know word will be nine letters
System.out.println(wordCaps.charAt(0) + " | " + wordCaps.charAt(1)
+ " | " + wordCaps.charAt(2));
System.out.println("----------");
System.out.println(wordCaps.charAt(3) + " | "
+ wordCaps.charAt(4) + " | " + wordCaps.charAt(5));
System.out.println("----------");
System.out.println(wordCaps.charAt(6) + " | "
+ wordCaps.charAt(7) + " | " + wordCaps.charAt(8));
}
public boolean check(String word) {
//create a boolean to indicate if the word is valid or not
//the check method is currently incomplete
//it gives a false negative(shows invalid answers as valid,
// when a given word repeats a letter that only exists
// once on the grid
//the following will attempt to solve this
//create a Character arrayList to store letters of the Word
int answerOccurances = 0;
int wordoccurances = 0;
//
boolean tooMany = false;
boolean isWord = true;
boolean isValid = true;
boolean containsMiddle = false;
boolean fourOrMore = true;
boolean tooLong = false;
boolean isAll = isWord && isValid && containsMiddle && fourOrMore && !tooLong && !tooMany;
//create an array of characters to see if any characters in the word entered by the user
//are not in the word target
ArrayList<Character> wordChars = new ArrayList<>();
for(int i = 0; i<theWord.length();i++){
wordChars.add(theWord.charAt(i));
}
// create arraylist of answer letters
ArrayList<Character> answerChars = new ArrayList<>();
for(int i = 0; i< word.length();i++){
answerChars.add(word.charAt(i));
}
//find out if any letters occur more often in the answer than in the grid
// while(!tooMany){
for(int i = 0; i <answerChars.size(); i++){
answerOccurances = Collections.frequency(answerChars,
answerChars.get(i));
wordoccurances = Collections.frequency(wordChars,
answerChars.get(i));
if(answerOccurances > wordoccurances){
tooMany = true;
}
}
char[] letters = new char[word.length()];
//fill array using loop;
for (int i = 0; i < word.length(); i++) {
letters[i] = word.charAt(i);
}
//compare letters in user word the the word
for (int i = 0; i < letters.length; i++) {
if (theWord.indexOf(letters[i]) == -1) {
isValid = false;
}
}
if(word.length() > 9){
tooLong = true;
}
if (!(words.contains(word))) {
isWord = false;
}
for(int k = 0; k < letters.length; k++){
if(letters[k] == theScrambledWord.charAt(4)){
containsMiddle = true;
}
}
if(newGame){
newGame = false;
return noOutputCheck(word);
}
if(!isValid){
System.out.println(word + " contains letters not on the grid");
}
else if(tooMany){
System.out.println("You have used a letter too many times");
}
else if(!isWord){
System.out.println(word + " is not a word");
}
else if(word.length() < 4){
fourOrMore = false;
System.out.println("words must contain at least four letters");
}
else if(!containsMiddle){
System.out.println(word + " does not contain the middle letter");
}
isAll = isWord && isValid && containsMiddle && fourOrMore && !tooLong && !tooMany;
return isAll;
}
public boolean noOutputCheck(String word) {
int answerOccurances = 0;
int wordoccurances = 0;
//create a boolean to indicate if the word is valid or not
boolean tooMany = false;
boolean isWord = true;
boolean isValid = true;
boolean containsMiddle = false;
boolean fourOrMore = true;
boolean tooLong = false;
boolean isAll = isWord && isValid && containsMiddle && fourOrMore && !tooLong && !tooMany;
//create an array of characters to see if any charactes in the word entered by the user
//are not in the word target
ArrayList<Character> wordChars = new ArrayList<>();
for(int i = 0; i<theWord.length();i++){
wordChars.add(theWord.charAt(i));
}
// create arraylist of answer letters
ArrayList<Character> answerChars = new ArrayList<>();
for(int i = 0; i< word.length();i++){
answerChars.add(word.charAt(i));
}
//find out if any letters occur more often in the answer than in the grid
for(int i = 0; i <answerChars.size(); i++){
answerOccurances = Collections.frequency(answerChars, answerChars.get(i));
wordoccurances = Collections.frequency(wordChars, answerChars.get(i));
if(answerOccurances > wordoccurances){
tooMany = true;
}
}
//
char[] letters = new char[word.length()];
//fill array using loop;
for (int i = 0; i < word.length(); i++) {
letters[i] = word.charAt(i);
}
//compare letters in user word the the word
for (int i = 0; i < letters.length; i++) {
if (theWord.indexOf(letters[i]) == -1) {
isValid = false;
}
}
if(word.length() > 9){
tooLong = true;
}
if (!(words.contains(word))) {
isWord = false;
}
for(int k = 0; k < letters.length; k++){
if(letters[k] == theScrambledWord.charAt(4)){
containsMiddle = true;
}
}
if(!isValid){
// System.out.println(word + " contains letters not on the grid");
}
else if(!isWord){
// System.out.println(word + " is not a word");
}
else if(word.length() < 4){
fourOrMore = false;
// System.out.println("words must contain at least four letters");
}
else if(!containsMiddle){
// System.out.println(word + " does not contain the middle letter");
}
isAll = isWord && isValid && containsMiddle && fourOrMore && !tooLong && !tooMany;
return isAll;
}
public ArrayList<String> allAnswers(){
ArrayList<String> allPossible = new ArrayList<>();
//use an iterator to iterate through Hashset
Iterator<String> it = words.iterator();
while(it.hasNext()){
String a = it.next();
if(noOutputCheck(a)){
allPossible.add(a);
}
}
// System.out.println(allPossible.size());
return allPossible;
}
//function to solve unknown word word target
public void solveUnknown(){
//create scanner to get letters
boolean isWord = false;
String newArrangement = "";
Scanner scan = new Scanner(System.in);
System.out.println("enter the letters as a single \"word\" with no spaces ");
String scrambled = scan.next();
ArrayList<Character> scrammedList = new ArrayList<>();
for(int i = 0; i < scrambled.length();i++){
scrammedList.add(scrambled.charAt(i));
}
while(!isWord){
Collections.shuffle(scrammedList);
char[] lettersArray = new char[scrammedList.size()];
for( int i = 0; i < lettersArray.length; i++ ){
lettersArray[i] = scrammedList.get(i);
}
newArrangement = new String(lettersArray);
if(words.contains(newArrangement)){
isWord = true;
}
}
System.out.println(newArrangement);
}
}