-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathWordPuzzleTester.java
More file actions
451 lines (381 loc) · 15.3 KB
/
WordPuzzleTester.java
File metadata and controls
451 lines (381 loc) · 15.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
package il.ac.tau.cs.sw1.ex4;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.Scanner;
public class WordPuzzleTester {
public static void main(String[] args) {
// puzzle1 = {w,h,_,i,_}
char[] puzzle1 = WordPuzzle.createPuzzleFromTemplate("while",
new boolean[] { false, false, true, false, true });
if (!Arrays.equals(puzzle1, new char[] { 'w', 'h', WordPuzzle.HIDDEN_CHAR, 'l', WordPuzzle.HIDDEN_CHAR })) {
System.err.println("Error 1.1");
}
char[] puzzle2 = WordPuzzle.createPuzzleFromTemplate("a",
new boolean[] { true });
if (!Arrays.equals(puzzle2, new char[] { WordPuzzle.HIDDEN_CHAR })) {
System.err.println("Error 1.2");
}
char[] puzzle3 = WordPuzzle.createPuzzleFromTemplate("abcd",
new boolean[] { false, false, false, false });
if (!Arrays.equals(puzzle3, new char[] { 'a','b','c','d' })) {
System.err.println("Error 1.3");
}
char[] puzzle4 = WordPuzzle.createPuzzleFromTemplate("abcd",
new boolean[] { true, false, false, false });
if (!Arrays.equals(puzzle4, new char[] { WordPuzzle.HIDDEN_CHAR,'b','c','d' })) {
System.err.println("Error 1.4");
}
char[] puzzle5 = WordPuzzle.createPuzzleFromTemplate("abcd",
new boolean[] { true, true, true, true });
if (!Arrays.equals(puzzle5, new char[] { WordPuzzle.HIDDEN_CHAR, WordPuzzle.HIDDEN_CHAR, WordPuzzle.HIDDEN_CHAR, WordPuzzle.HIDDEN_CHAR })) {
System.err.println("Error 1.4");
}
boolean[] template1 = { true, true, true, false, true, true };
boolean[] template2 = { true, true, false, false, true, true };
boolean[] template3 = { true, true, true, true, true, true };
boolean[] template4 = { false };
boolean[] template5 = { true };
boolean[] template6 = { false, true };
boolean[] template7 = { true, false };
boolean[] template8 = { true, true, false };
boolean[] template9 = { true, true, true, true, false, false };
boolean[] template10 = { false, false, true, true, false, false };
boolean[] template11 = { false, false, false, false, false, false };
boolean[] template12 = { false, true };
boolean[] template13 = { true, false };
boolean[] template14 = { true, true, false, false };
// puzzle = {_,_,_,e,_,_}
if (WordPuzzle.checkLegalTemplate("wheeps", template1)) {
System.err.println("Error 2.1");
}
// puzzle = {_,_,e,e,_,_}
if (!WordPuzzle.checkLegalTemplate("wheeps", template2)) {
System.err.println("Error 2.2");
}
// puzzle = {_,_,_,_,_,_}
if (WordPuzzle.checkLegalTemplate("wheeps", template3)) {
System.err.println("Error 2.3");
}
// puzzle = {'e'}
if (WordPuzzle.checkLegalTemplate("e", template4)) {
System.err.println("Error 2.4");
}
// puzzle = {'_'}
if (WordPuzzle.checkLegalTemplate("e", template5)) {
System.err.println("Error 2.5");
}
// puzzle = {'e','_'}
if (!WordPuzzle.checkLegalTemplate("ep", template6)) {
System.err.println("Error 2.6");
}
// puzzle = {'_','e'}
if (!WordPuzzle.checkLegalTemplate("pe", template7)) {
System.err.println("Error 2.7");
}
// puzzle = {'_','_','e'}
if (WordPuzzle.checkLegalTemplate("wee", template8)) {
System.err.println("Error 2.8");
}
// puzzle = {'_','_','_','_','p','s'}
if (!WordPuzzle.checkLegalTemplate("wheeps", template9)) {
System.err.println("Error 2.9");
}
// puzzle = {'w','h','_','_','p','s'}
if (!WordPuzzle.checkLegalTemplate("wheeps", template10)) {
System.err.println("Error 2.10");
}
// puzzle = {'w','h','e','e','p','s'}
if (WordPuzzle.checkLegalTemplate("wheeps", template11)) {
System.err.println("Error 2.11");
}
// puzzle = {'e','_'}
if (WordPuzzle.checkLegalTemplate("ee", template12)) {
System.err.println("Error 2.12");
}
// puzzle = {'_','e'}
if (WordPuzzle.checkLegalTemplate("ee", template13)) {
System.err.println("Error 2.13");
}
// puzzle = {'','_','e','l'}
if (WordPuzzle.checkLegalTemplate("elel", template14)) {
System.err.println("Error 2.14");
}
boolean[][] templates = WordPuzzle.getAllLegalTemplates("look", 1);
if (templates.length != 2) {
System.err.println("Error 3.1");
}
if (!Arrays.equals(templates[0],(new boolean[] { false, false, false, true }))) {
System.err.println("Error 3.2");
}
boolean[][] templates2 = WordPuzzle.getAllLegalTemplates("look", 4);
if (templates2.length != 0) {
System.err.println("Error 3.3");
}
boolean[][] templates3 = WordPuzzle.getAllLegalTemplates("look", 0);
if (templates3.length != 0) {
System.err.println("Error 3.4");
}
boolean[][] templates4 = WordPuzzle.getAllLegalTemplates("oooo", 1);
if (templates4.length != 0) {
System.err.println("Error 3.5");
}
boolean[][] templates5 = WordPuzzle.getAllLegalTemplates("oo", 1);
if (templates5.length != 0) {
System.err.println("Error 3.6");
}
boolean[][] templates6 = WordPuzzle.getAllLegalTemplates("oe", 1);
if (templates6.length != 2) {
System.err.println("Error 3.7");
}
boolean[][] templates7 = WordPuzzle.getAllLegalTemplates("abcdefg", 2);
if (templates7.length != 21) {
System.err.println("Error 3.8");
}
boolean[] res7 = new boolean[7];
res7[0] = true;
res7[1] = true;
if (!Arrays.equals(templates7[20], res7)) {
System.err.println("Error 3.9");
}
// UNCOMMENT IF YOU FAIL 3.10 to check yourself
// for (boolean[] template: templates7) {
// System.out.println(Arrays.toString(template));
// }
res7[0] = false;
res7[2] = true;
if (!Arrays.equals(templates7[14], res7)) {
System.err.println("Error 3.10");
}
boolean[][] templates8 = WordPuzzle.getAllLegalTemplates("nylcpeodce", 3); // 10 letters long
if (templates8.length != 32) {
System.err.println("Error 3.11");
}
// UNCOMMENT IF YOU FAIL 3.12 to check yourself
// for (boolean[] template: templates8) {
// System.out.println(Arrays.toString(template));
// }
if (!Arrays.equals(templates8[0], new boolean[] {false, false, false, false, false, true, false, true, false, true})) {
System.err.println("Error 3.12");
}
// puzzle = {w,_,_,_,_s}
char[] puzzle = WordPuzzle.createPuzzleFromTemplate("wheeps",
new boolean[] { false, true, true, true, true, false });
int numOfChangedLetters = WordPuzzle.applyGuess('h', "wheeps", puzzle);
if (numOfChangedLetters != 1) {
System.err.println("Error 4.1");
}
numOfChangedLetters = WordPuzzle.applyGuess('e', "wheeps", puzzle);
if (numOfChangedLetters != 2) {
System.err.println("Error 4.2");
}
numOfChangedLetters = WordPuzzle.applyGuess('s', "wheeps", puzzle);
if (numOfChangedLetters != 0) {
System.err.println("Error 4.3");
}
numOfChangedLetters = WordPuzzle.applyGuess('j', "wheeps", puzzle);
if (numOfChangedLetters != 0) {
System.err.println("Error 4.4");
}
numOfChangedLetters = WordPuzzle.applyGuess('e', "wheeps", puzzle);
if (numOfChangedLetters != 0) {
System.err.println("Error 4.5");
}
boolean[] already_guessed = new boolean[26];
// already guessed [a,b,c,d]
for (int i = 0; i <4; i++){
already_guessed[i] = true;
}
boolean[] copyAlreadyGuessed = Arrays.copyOf(already_guessed, 26);
// puzzle = {w,_,_,_,_s}
puzzle = WordPuzzle.createPuzzleFromTemplate("wheeps",
new boolean[] { false, true, true, true, true, false });
char[] puzzleCopy = Arrays.copyOf(puzzle, puzzle.length);
for (int j = 0; j < 100; j++){
char[] hint = WordPuzzle.getHint("wheeps", puzzle, already_guessed);
/*
* correctGuesses - all those letters are a "correct" hint.
* incorrectGuesses - none of those letters should be the "misleading" hint. Some
* of them have been guessed already (a,b,c,d), and the rest appear in the word
*/
String correctGuesses = "hep";
String incorrectGuesses = "whepsabcd";
boolean case1 = (correctGuesses.contains("" + hint[0]) &&
!incorrectGuesses.contains("" + hint[1])); //first char is correct
boolean case2 = (correctGuesses.contains("" + hint[1]) &&
!incorrectGuesses.contains("" + hint[0])); //first char is incorrect
// already_guessed shouldn't be changed
boolean case3 = Arrays.equals(already_guessed, copyAlreadyGuessed);
// puzzle shouldn't be changed
boolean case4 = Arrays.equals(puzzle, puzzleCopy);
if ( !case1 && !case2 && !case3 && !case4){
System.err.println("Error 5.1");
}
if ( !case3 ){
System.err.println("Error 5.3");
}
if ( !case4 ){
System.err.println("Error 5.4");
}
}
/*
* In order for these tests to run properly, your scanner should only use: nextInt(), nextChar() and next()
* to read integerts, characters and strings. Alternatively, you should only use nextLine() to read each line.
* mixing nextLine() with the rest of the functions will fail this test (and is more problematic in general).
*/
testMainTemplate();
testMainGame();
System.out.println("Done!");
}
public static void testMainTemplate(){
PrintStream sysOut = System.out; //backup System.in
try{
redirectOutput(); // no need to save output
Scanner inputScanner1 = getInputScanner("2", "_,X,X,_,_", "someOtherString");
char[] puzzle = WordPuzzle.mainTemplateSettings("print", inputScanner1);
System.setOut(sysOut);
if (!Arrays.equals(puzzle, new char[] {'_', 'r', 'i', '_', '_'})){
System.err.println("Error 6.1");
}
checkScannerIsNotClosed(inputScanner1, "6");
redirectOutput();
Scanner inputScanner2 = getInputScanner("2", "_,X,X,_,_,_", "2", "_,X,X,_,_", "someOtherString");
char[] puzzle_2 = WordPuzzle.mainTemplateSettings("print", inputScanner2);
System.setOut(sysOut);
// UNCOMMENT IF YOU FAIL to check yourself
// System.out.println(puzzle_2);
// System.out.println(puzzle_2.length);
if (!Arrays.equals(puzzle_2, new char[] {'_', 'r', 'i', '_', '_'})){
System.err.println("Error 6.2");
}
checkScannerIsNotClosed(inputScanner2, "6");
redirectOutput();
Scanner inputScanner3 = getInputScanner("1", "as", "2", "_,X,X", "2", "_,X,X,_,_", "someOtherString");
char[] puzzle_3 = WordPuzzle.mainTemplateSettings("print", inputScanner3);
System.setOut(sysOut);
// UNCOMMENT IF YOU FAIL to check yourself
// System.out.println(puzzle_3);
// System.out.println(puzzle_3.length);
if (!Arrays.equals(puzzle_3, new char[] {'_', 'r', 'i', '_', '_'})){
System.err.println("Error 6.3");
}
checkScannerIsNotClosed(inputScanner3, "6");
redirectOutput();
Scanner inputScanner4 = getInputScanner("1", "2", "1", "0","2","_,X,X,_", "2","_,X,x,_", "2","_,X,X,X", "2", "_,_,_,X", "someOtherString");
char[] puzzle_4 = WordPuzzle.mainTemplateSettings("oooa", inputScanner4);
System.setOut(sysOut);
// UNCOMMENT IF YOU FAIL to check yourself
// System.out.println(puzzle_4);
// System.out.println(puzzle_4.length);
if (!Arrays.equals(puzzle_4, new char[] {'_', '_', '_', 'a'})){
System.err.println("Error 6.4");
}
checkScannerIsNotClosed(inputScanner4, "6");
redirectOutput();
Scanner inputScanner5 = getInputScanner("2","_,_,X,X", "2", "X,X,X,X","2", "_,_,_,_", "2", "_,_,_,X", "someOtherString");
char[] puzzle_5 = WordPuzzle.mainTemplateSettings("oooa", inputScanner5);
System.setOut(sysOut);
// UNCOMMENT IF YOU FAIL to check yourself
// System.out.println(puzzle_5);
// System.out.println(puzzle_5.length);
if (!Arrays.equals(puzzle_5, new char[] {'_', '_', '_', 'a'})){
System.err.println("Error 6.5");
}
checkScannerIsNotClosed(inputScanner5, "6");
}
finally{
System.setOut(sysOut); //restore System.out
}
}
public static void testMainGame(){
PrintStream sysOut = System.out; //backup System.in
try{
ByteArrayOutputStream baos = redirectOutput();
Scanner inputScanner = getInputScanner("p", "n", "t", "someOtherString");
WordPuzzle.mainGame("print", new char[] {'_', 'r', 'i', '_', '_'}, inputScanner);
System.out.flush();
System.setOut(sysOut);
if (! baos.toString().contains("Congratulations")){
System.err.println("Error 7.1");
}
checkScannerIsNotClosed(inputScanner, "7");
ByteArrayOutputStream baos2 = redirectOutput();
Scanner inputScanner2 = getInputScanner("a", "b", "c", "d", "e", "f", "g", "h", "i", "H", "H", "H", "H", "H","H","H","H","H","H","H","H","H","H","a", "b", "someOtherString");
WordPuzzle.mainGame("printttttt", new char[] {'_', 'r', 'i', '_', '_', '_', '_', '_', '_', '_'}, inputScanner2);
System.out.flush();
System.setOut(sysOut);
// UNCOMMENT IF YOU FAIL 7.2 - checks if you don't give a hint on an already guessed char
// System.out.println(baos2.toString());
if (baos2.toString().contains("Here's a hint for you: choose either a") ||
baos2.toString().contains("Here's a hint for you: choose either b") ||
baos2.toString().contains("Here's a hint for you: choose either c") ||
baos2.toString().contains("Here's a hint for you: choose either d") ||
baos2.toString().contains("Here's a hint for you: choose either e") ||
baos2.toString().contains("Here's a hint for you: choose either f") ||
baos2.toString().contains("Here's a hint for you: choose either g") ||
baos2.toString().contains("Here's a hint for you: choose either h") ||
baos2.toString().contains("Here's a hint for you: choose either i")){
System.err.println("Error 7.2");
}
checkScannerIsNotClosed(inputScanner2, "7");
ByteArrayOutputStream baos3 = redirectOutput();
Scanner inputScanner3 = getInputScanner("p", "g", "x", "y", "k", "l", "n","t", "someOtherString");
WordPuzzle.mainGame("print", new char[] {'_', 'r', 'i', '_', '_'}, inputScanner3);
System.out.flush();
System.setOut(sysOut);
// System.out.println(baos3.toString());
if (! baos3.toString().contains("Game over!")){
System.err.println("Error 7.3");
}
checkScannerIsNotClosed(inputScanner3, "7");
ByteArrayOutputStream baos4 = redirectOutput();
Scanner inputScanner4 = getInputScanner("e", "someOtherString");
WordPuzzle.mainGame("eriee", new char[] {'_', 'r', 'i', '_', '_'}, inputScanner4);
System.out.flush();
System.setOut(sysOut);
// System.out.println(baos4.toString());
if (baos4.toString().contains("Game over!")){
System.err.println("Error 7.4");
}
checkScannerIsNotClosed(inputScanner4, "7");
ByteArrayOutputStream baos5 = redirectOutput();
Scanner inputScanner5 = getInputScanner("a", "H", "H","H", "H", "H", "H", "e", "someOtherString");
WordPuzzle.mainGame("eriee", new char[] {'_', 'r', 'i', '_', '_'}, inputScanner5);
System.out.flush();
System.setOut(sysOut);
// System.out.println(baos5.toString());
if (!baos5.toString().contains("Congratulations")){
System.err.println("Error 7.5");
}
checkScannerIsNotClosed(inputScanner5, "7");
}
finally{
System.setOut(sysOut); //restore System.out
}
}
private static void checkScannerIsNotClosed(Scanner s, String errorPrefix){
try{
if (!s.hasNext()){
System.err.println(String.format("Error %s.2", errorPrefix));
}
}
catch (IllegalStateException exp){
// scanner should not be closed
System.err.println(String.format("Error %s.2", errorPrefix));
}
}
private static ByteArrayOutputStream redirectOutput(){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos);
System.setOut(ps);
return baos;
}
private static Scanner getInputScanner(String... arr){
String inputString = String.join(System.lineSeparator(), arr);
InputStream inStream = new ByteArrayInputStream(inputString.getBytes());
Scanner inputScanner = new Scanner(inputString);
return inputScanner;
}
}