-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHangmanGame.java
295 lines (258 loc) · 10.8 KB
/
HangmanGame.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
/**
* This code contains the Hangman Game Class and recreates in text-based form the Classic Hangman Game.
*
*
* @author Carlos M. Muniz
*
*/
import java.util.Scanner;
import java.util.Set;
import java.io.File;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
/**
* The HangmanGame class contains all necessary methods for the Hangman Game
*/
public class HangmanGame {
// Introduction to the Hangman Game that collects the initial word size
public static int startInput(Scanner objectInput) {
System.out.println("Welcome to Hangman!");
System.out.print("Think of a word and enter the number of letters in that word: ");
// Takes in the size of the word.
final int wordSize = objectInput.nextInt();
objectInput.nextLine();
return wordSize;
}
// Loads and constrains the data corpus based on the size of the word.
public static List<String> loadData(final int wordSize) {
final String path = "words.csv";
BufferedReader br = null;
List<String> words = null;
try {
// Loads the data corpis
br = new BufferedReader(new FileReader(path));
words = new ArrayList<String>();
String line;
while ((line = br.readLine()) != null) {
final String[] rowData = line.split(",");
final int size = Integer.parseInt(rowData[2]);
// Constrains by word size
if (size == wordSize) {
words.add(rowData[1]);
}
}
br.close();
} catch (final FileNotFoundException e) {
e.printStackTrace();
final File tmpDir = new File(path);
final boolean exists = tmpDir.exists();
System.out.println("File Exists: " + exists);
} catch (final IOException e) {
e.printStackTrace();
}
return words;
}
// "Guesses" the next letter based on the letters in the word data set, and the letters already picked.
public static char guessLetter(final List<String> words, final Set<Character> letters) {
char c = ' ';
final Map<Character, Integer> charCount = new HashMap<Character, Integer>();
// Counts the letters in each word.
for (int i = 0; i < words.size(); i++) {
for (int j = 0; j < words.get(i).length(); j++) {
final char ch = words.get(i).charAt(j);
if (!charCount.containsKey(ch)) {
charCount.put(ch, 0);
}
charCount.put(ch, charCount.get(ch) + 1);
}
}
int max = 0;
// Chooses the most common letter, that hasn't already been picked.
for (final char key : charCount.keySet()) {
final int val = charCount.get(key);
if ((val > max) & (!letters.contains(key))) {
max = val;
c = key;
}
}
return c;
}
// Constrains the dataset based on the guess and its locations, using regex.
public static void constrainDataset(final List<String> words, final String[] places, final char letter,
final int wordSize) {
// generating the regex pattern using the inputted places and letter
final String[] regexPattern = new String[wordSize];
final String neg = "[^" + String.valueOf(letter) + "]";
Arrays.fill(regexPattern, neg);
for (int i = 0; i < places.length; i++) {
final int j = Integer.parseInt(places[i]) - 1;
if (j >= 0) {
regexPattern[j] = String.valueOf(letter);
}
}
// Constraining the dataset in place
final Pattern pattern = Pattern.compile(String.join("", regexPattern));
Matcher matcher;
for (final Iterator<String> iter = words.listIterator(); iter.hasNext();) {
matcher = pattern.matcher(iter.next());
if (!matcher.find()) {
iter.remove();
}
}
}
// While loop that handles the main game mechanic of gussing letters until the word is guessed.
public static void guessWords(int wordSize, List<String> wordSelection, Scanner objectInput) {
final String mess = "The rules are simple. I'll guess a letter that is in the word. "
+ " If I'm right, you tell me 'yes', and where that letter is. "
+ "If I'm wrong you tell me 'no'. I have 5 guesses to get it right.";
System.out.println(mess);
final char[] letters = new char[wordSize];
Arrays.fill(letters, '_');
String indices = " ";
String space = " ";
for (int i = 0; i < wordSize; i++) {
if (i + 1 > 9) {
space = " ";
}
indices = indices + space + String.valueOf(i + 1) + " ";
}
// Set of letters that have been picked regardless if they are in the word or not.
final Set<Character> lettersPicked = new HashSet<Character>();
for (final char c : letters) {
lettersPicked.add(c);
}
int count = 6;
boolean wordIncomplete = true;
// While Loop for Main Game Mechanic
while ((count > 0) & (wordIncomplete)) {
// Guesses the letter based on Word Dataset
final char guess = guessLetter(wordSelection, lettersPicked);
System.out.println("My guess: " + guess);
System.out.println("Did I guess right? (yes or no)");
final String answer = objectInput.nextLine();
String[] places = { "0" };
// If guess was right, it asks where to put it.
if (answer.equals("yes")) {
System.out.println(String.valueOf(letters).replace("", " "));
System.out.println(indices);
System.out.println("Where does the letter go? (Use commas (,) to separate multiple answers.)");
places = objectInput.nextLine().split(",");
for (int i = 0; i < places.length; i++) {
final int j = Integer.parseInt(places[i]) - 1;
letters[j] = guess;
}
System.out.println(String.valueOf(letters).replace("", " "));
count++;
}
// Constrains dataset based on the guess.
constrainDataset(wordSelection, places, guess, wordSize);
lettersPicked.add(guess);
count--;
// Checks if the word is complete or not.
wordIncomplete = false;
for (int i=0; i < letters.length; i ++) {
if (letters[i] == '_') {
wordIncomplete = true;
}
}
hangmanDrawer(count);
}
// End of Game Messages
if (count == 0) {
System.out.println("I lost...");
}
if (!wordIncomplete) {
System.out.println("I won! The word is: " + String.valueOf(letters));
}
System.out.println("Good Game!");
}
// Prints out the Classic Hangman Drawing based on the number of turns used.
public static void hangmanDrawer(int num) {
String hangman = "";
switch (num) {
case 0: hangman = " __________ \n"+
" | | \n"+
" \\O/ | \n"+
" | | \n"+
"_/ \\_ | \n"+
" | \n"+
" | \n"+
" ______|_\n";
break;
case 1: hangman = " __________ \n"+
" | | \n"+
" \\O/ | \n"+
" | | \n"+
"_/ | \n"+
" | \n"+
" | \n"+
" ______|_\n";
break;
case 2: hangman = " __________ \n"+
" | | \n"+
" \\O/ | \n"+
" | | \n"+
" | \n"+
" | \n"+
" | \n"+
" ______|_\n";
break;
case 3: hangman = " __________ \n"+
" | | \n"+
" \\O/ | \n"+
" | \n"+
" | \n"+
" | \n"+
" | \n"+
" ______|_\n";
break;
case 4: hangman = " __________ \n"+
" | | \n"+
" \\O | \n"+
" | \n"+
" | \n"+
" | \n"+
" | \n"+
" ______|_\n";
break;
case 5: hangman = " __________ \n"+
" | | \n"+
" O | \n"+
" | \n"+
" | \n"+
" | \n"+
" | \n"+
" ______|_\n";
break;
case 6: hangman = " __________ \n"+
" | | \n"+
" | \n"+
" | \n"+
" | \n"+
" | \n"+
" | \n"+
" ______|_\n";
break;
}
System.out.println(hangman);
}
// Main method for calling all other methods
public static void main(final String[] args) {
Scanner objectInput = new Scanner(System.in);
int wordSize = startInput(objectInput);
final List<String> wordSelection = loadData(wordSize);
guessWords(wordSize, wordSelection, objectInput);
objectInput.close();
}
}