-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_minesweeper.py
523 lines (430 loc) · 14.4 KB
/
test_minesweeper.py
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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
# coding=utf-8
import unittest
from Minesweeper_tdd import Game, Board
__author__ = 'JacobAMason'
class TileTests(unittest.TestCase):
def setUp(self):
self.tile = Board.Tile(0, 0)
def test_default_values(self):
self.assertEqual(self.tile.position, (0, 0))
self.assertEqual(self.tile.nearbyMines, 0)
self.assertEqual(self.tile.isMine, False)
self.assertEqual(self.tile.isHidden, True)
self.assertEqual(self.tile.isFlagged, False)
def test_reveal_tile(self):
self.tile.reveal()
self.assertFalse(self.tile.isHidden)
def test_flag_tile(self):
self.tile.toggle_flag()
self.assertTrue(self.tile.isHidden)
def test_unflag_tile(self):
self.tile.toggle_flag()
self.tile.toggle_flag()
self.assertTrue(self.tile.isHidden)
def test_set_number(self):
self.tile.set_number_of_nearby_mines(4)
self.assertEqual(self.tile.nearbyMines, 4)
def test_set_mine(self):
self.tile.set_as_mine()
self.assertTrue(self.tile.isMine)
class Board2x3Tests(unittest.TestCase):
def setUp(self):
self.board = Board(width=2, height=3)
self.tile = self.board.grid[0][0]
def test_initialize_blank_board(self):
visibilityGrid = [[tile.isHidden for tile in row] for row in
self.board.grid]
self.assertEqual(
visibilityGrid,
[[True, True],
[True, True],
[True, True]]
)
def test_show_blank_board(self):
self.assertEqual(
str(self.board),
(
" a b\n"
"a ████ a\n"
"b ████ b\n"
"c ████ c\n"
" a b\n"
)
)
def test_initialize_0_mines(self):
mineGrid = [[tile.isMine for tile in row] for row in self.board.grid]
self.assertEqual(
mineGrid,
[[False, False],
[False, False],
[False, False]]
)
def test_initialize_1_mine(self):
self.board.place_mines(1, self.tile)
mineGrid = [[tile.isMine for tile in row] for row in self.board.grid]
self.assertTrue(
any([any(mineGrid[0]),
any(mineGrid[1]),
any(mineGrid[2])])
)
def test_initialize_full_mines(self):
self.board.place_mines(5, self.tile)
mineGrid = [[tile.isMine for tile in row] for row in self.board.grid]
self.assertEqual(
mineGrid,
[[False, True],
[True, True],
[True, True]]
)
def test_number_of_nearby_mines_update_when_mine_is_added(self):
self.tile.set_as_mine()
self.board.increment_tiles_around_mine(self.tile)
numberGrid = [[tile.nearbyMines for tile in row] for row in
self.board.grid]
self.assertEqual(
numberGrid,
[[0, 1],
[1, 1],
[0, 0]]
)
def test_reveal(self):
self.tile.set_as_mine()
self.board.increment_tiles_around_mine(self.tile)
self.board.reveal(0, 2)
visibilityGrid = [[tile.isHidden for tile in row] for row in
self.board.grid]
self.assertEqual(
visibilityGrid,
[[True, True],
[False, False],
[False, False]]
)
def test_reveal_print(self):
self.tile.set_as_mine()
self.board.increment_tiles_around_mine(self.tile)
self.board.reveal(0, 2)
self.assertEqual(
str(self.board),
(
" a b\n"
"a ████ a\n"
"b 1 1 b\n"
"c c\n"
" a b\n"
)
)
def test_can_recursively_reveal_if_flags_set(self):
self.tile.set_as_mine()
extraMinePreventsGameWin = self.board.convert_coordinate_to_tile(0, 2)
extraMinePreventsGameWin.set_as_mine()
self.board.increment_tiles_around_mine(self.tile)
self.board.increment_tiles_around_mine(extraMinePreventsGameWin)
self.board.toggle_flag(self.tile.position[0], self.tile.position[1])
self.board.reveal(1, 0)
self.board.reveal(1, 0)
visibilityGrid = [[tile.isHidden for tile in row] for row in
self.board.grid]
self.assertEqual(
visibilityGrid,
[[True, False],
[False, False],
[True, True]]
)
def test_cannot_recursively_reveal_tiles_near_mine(self):
self.tile.set_as_mine()
self.board.increment_tiles_around_mine(self.tile)
self.board.reveal(1, 1)
visibilityGrid = [[tile.isHidden for tile in row] for row in
self.board.grid]
self.assertEqual(
visibilityGrid,
[[True, True],
[True, False],
[True, True]]
)
def test_cannot_recursively_reveal_tile_near_mine_print(self):
self.tile.set_as_mine()
self.board.increment_tiles_around_mine(self.tile)
self.board.reveal(1, 1)
self.assertEqual(
str(self.board),
(
" a b\n"
"a ████ a\n"
"b ██1 b\n"
"c ████ c\n"
" a b\n"
)
)
def test_flag_tile(self):
self.board.toggle_flag(0, 1)
flaggedGrid = [[tile.isFlagged for tile in row] for row in
self.board.grid]
self.assertEqual(
flaggedGrid,
[[False, False],
[True, False],
[False, False]]
)
def test_flag_tile_print(self):
self.board.toggle_flag(0, 1)
self.assertEqual(
str(self.board),
(
" a b\n"
"a ████ a\n"
"b ⚑ ██ b\n"
"c ████ c\n"
" a b\n"
)
)
def test_cannot_reveal_flagged_tile(self):
self.board.toggle_flag(0, 1)
self.board.reveal(0, 1)
visibilityGrid = [[tile.isHidden for tile in row] for row in
self.board.grid]
self.assertEqual(
visibilityGrid,
[[True, True],
[True, True],
[True, True]]
)
def test_cannot_recursively_reveal_flagged_tile(self):
self.board.toggle_flag(0, 1)
self.board.reveal(1, 1)
visibilityGrid = [[tile.isHidden for tile in row] for row in
self.board.grid]
self.assertEqual(
visibilityGrid,
[[False, False],
[True, False],
[False, False]]
)
def test_cannot_flag_revealed_tile(self):
self.board.reveal(0, 0)
self.board.toggle_flag(0, 0)
flaggedGrid = [[tile.isFlagged for tile in row] for row in
self.board.grid]
self.assertEqual(
flaggedGrid,
[[False, False],
[False, False],
[False, False]]
)
def test_mine_triggered(self):
self.board.grid[1][0].set_as_mine()
self.board.reveal(0, 1)
self.assertTrue(self.board.is_mine_triggered)
def test_mine_triggered_print(self):
self.board.grid[1][0].set_as_mine()
self.board.reveal(0, 1)
self.assertEqual(
str(self.board),
(
" a b\n"
"a a\n"
"b X b\n"
"c c\n"
" a b\n"
)
)
def test_reveal_all_print(self):
self.board.place_mines(5, self.tile)
self.board.reveal_whole_board()
self.assertEqual(
str(self.board),
(
" a b\n"
"a 3 X a\n"
"b X X b\n"
"c X X c\n"
" a b\n"
)
)
def test_loss_reveals_whole_board(self):
self.board.place_mines(5, self.tile)
self.board.reveal(0, 1)
self.assertEqual(
str(self.board),
(
" a b\n"
"a 3 X a\n"
"b X X b\n"
"c X X c\n"
" a b\n"
)
)
def test_flagged_mine_shows_checkmark_after_end_game_loss(self):
self.board.place_mines(5, self.tile)
self.board.toggle_flag(1, 0)
self.board.reveal(0, 1)
self.assertEqual(
str(self.board),
(
" a b\n"
"a 3 ⚐ a\n"
"b X X b\n"
"c X X c\n"
" a b\n"
)
)
def test_flagged_mine_shows_checkmark_after_end_game_win(self):
self.board.place_mines(5, self.tile)
self.board.toggle_flag(0, 1)
self.board.toggle_flag(0, 2)
self.board.toggle_flag(1, 0)
self.board.toggle_flag(1, 1)
self.board.toggle_flag(1, 2)
self.board.reveal(0, 0)
self.assertEqual(
str(self.board),
(
" a b\n"
"a 3 ⚐ a\n"
"b ⚐ ⚐ b\n"
"c ⚐ ⚐ c\n"
" a b\n"
)
)
class GameTests(unittest.TestCase):
def setUp(self):
self.game = Game()
self.game.generate_board(2, 3, mines=1)
def test_show_welcome_message(self):
self.assertEqual(
self.game.show_welcome_message(),
(
"Welcome to Minesweeper!\n"
"What size minefield do you wish to play on?\n"
"Say 1 for small, 2 for medium, and 3 for large.\n"
)
)
def test_create_empty_board(self):
self.assertEqual(self.game.mines, 1)
def test_show_board(self):
self.assertEqual(
self.game.show_board(),
(
"Mines Left: 1\n"
"\n"
" a b\n"
"a ████ a\n"
"b ████ b\n"
"c ████ c\n"
" a b\n"
"\n"
)
)
def test_show_command_does_not_throw_error_and_therefore_shows_board(self):
self.game.process_input("show")
self.assertEqual(len(self.game.errorQueue), 0)
def test_reveal_tile(self):
self.game.process_input("a b")
visibilityGrid = [[tile.isHidden for tile in row] for row in
self.game.board.grid]
self.assertEqual(
visibilityGrid,
[[True, True],
[False, True],
[True, True]]
)
def test_reveal_returns_one_tile_revealed(self):
self.game.generate_board(2, 3, 5)
tile = self.game.reveal(0, 0)
self.assertEqual(tile, [self.game.board.grid[0][0]])
def test_reveal_returns_all_tiles_revealed(self):
self.game.generate_board(2, 3, 0)
tiles = self.game.reveal(0, 0)
self.assertItemsEqual(tiles, [tile for row in self.game.board.grid for tile in row])
def test_toggle_flag_mode(self):
self.game.toggle_flag_mode()
self.assertEqual(
self.game.show_prompt(),
(
"Enter an 'f' to exit flag mode\n"
"Enter a coordinate to toggle the flag\n"
)
)
def test_toggle_flag(self):
self.game.toggle_flag_mode()
self.game.process_input("a b")
flaggedGrid = [[tile.isFlagged for tile in row] for row in
self.game.board.grid]
self.assertEqual(
flaggedGrid,
[[False, False],
[True, False],
[False, False]]
)
def test_user_enter_flag_mode(self):
self.game.process_input("f")
self.assertTrue(self.game.isInFlagMode)
def test_error_queue(self):
self.game.process_input("FOO BAR")
self.assertEqual(
list(self.game.show_errors())[0],
"This isn't valid input. Try something like \"a a\""
)
def test_error_queue_gets_popped(self):
self.game.process_input("FOO BAR")
list(self.game.show_errors())
self.assertEqual(
len(list(self.game.show_errors())), 0
)
def test_start_game(self):
self.game.start_game(0, 1)
visibilityGrid = [[tile.isHidden for tile in row] for row in
self.game.board.grid]
self.assertEqual(
visibilityGrid,
[[True, True],
[False, True],
[True, True]]
)
def test_start_game_reveal_callback_changes(self):
self.assertEqual(self.game.reveal_callback, self.game.start_game)
self.game.process_input("a a")
self.assertEqual(self.game.reveal_callback, self.game.board.reveal)
def test_check_end_game_none(self):
self.game.generate_board(2, 3, 5)
self.game.reveal(0, 0)
self.assertFalse(self.game.board.check_end_game_loss())
self.assertFalse(self.game.board.check_end_game_win())
def test_check_end_game_loss(self):
self.lose_a_game()
self.assertTrue(self.game.board.check_end_game_loss())
self.assertFalse(self.game.board.check_end_game_win())
def test_check_end_game_win(self):
self.win_a_game()
self.assertFalse(self.game.board.check_end_game_loss())
self.assertTrue(self.game.board.check_end_game_win())
def test_end_game_win_prompt(self):
self.win_a_game()
self.assertEqual(
self.game.show_prompt(),
(
"Congrats, you've won!"
)
)
def test_end_game_lose_prompt(self):
self.lose_a_game()
self.assertEqual(
self.game.show_prompt(),
(
"Ouch."
)
)
def lose_a_game(self):
self.game.generate_board(2, 3, 5)
self.game.reveal(0, 0)
self.game.reveal(0, 1)
def win_a_game(self):
self.game.generate_board(2, 3, 5)
self.game.reveal(0, 0)
self.game.board.toggle_flag(0, 1)
self.game.board.toggle_flag(0, 2)
self.game.board.toggle_flag(1, 0)
self.game.board.toggle_flag(1, 1)
self.game.board.toggle_flag(1, 2)
if __name__ == '__main__':
unittest.main()