forked from coxmediagroup/Tic-Tac-Toe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tictactoeboardtestcase.py
383 lines (361 loc) · 14.8 KB
/
tictactoeboardtestcase.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
import copy
import random
import unittest
from tictactoeboard import TicTacToeBoard
"""A Tic-Tac-Toe board test case class.
Exported Classes:
TicTacToeBoardTestCase -- A tic-tac-toe board test case class.
"""
class TicTacToeBoardTestCase(unittest.TestCase):
def test_initial_board(self):
"""Test whether the default constructor generates an empty 3x3 board."""
board = TicTacToeBoard()
# Make sure the board is the traditional 3x3 board with no marks
self.assertEqual(board.column_count, 3)
self.assertEqual(board.row_count, 3)
self.assertEqual(len(board.matrix), board.row_count)
self.assertEqual(len(board.matrix[0]), board.column_count)
# Make sure board has no initial marks
self.assertEqual(board.CELL_NO_PLAYER, 0)
for row in xrange(board.row_count):
for col in xrange(board.column_count):
self.assertEqual(board.matrix[row][col], board.CELL_NO_PLAYER)
def test_get_other_player_function(self):
"""Test whether the get_other_player_id function provides the id of the other player."""
board = TicTacToeBoard()
self.assertEqual(board.get_other_player_id(1), 2)
self.assertEqual(board.get_other_player_id(2), 1)
def test_get_lines_on_empty_square_board(self):
"""Test whether the _get_lines function provides the correct lines for an empty square board."""
board = TicTacToeBoard()
lines = board._get_lines()
# Make sure there are the correct number of lines
self.assertEqual(len(lines), board.column_count + board.row_count + 2)
# Make sure all lines are empty
emptyLine = [board.CELL_NO_PLAYER for i in xrange(board.column_count)]
for line in lines:
self.assertEqual(line, emptyLine)
def test_get_lines_on_board(self):
"""Test whether the _get_lines function provides the correct lines for a non empty square board."""
# Create non-empty square tic-tac-toe game board
board = TicTacToeBoard()
board.matrix[0] = [1, 2, 0]
board.matrix[1] = [0, 1, 0]
board.matrix[2] = [0, 2, 0]
lines = board._get_lines()
# Make sure there are the correct number of lines
self.assertEqual(board.column_count + board.row_count + 2, 8)
self.assertEqual(len(lines), 8)
# Make sure the lines are correct
self.assertEqual(lines.count([0,0,0]), 1)
self.assertEqual(lines.count([1,0,0]), 1)
self.assertEqual(lines.count([1,2,0]), 1)
self.assertEqual(lines.count([0,1,0]), 2)
self.assertEqual(lines.count([1,1,0]), 1)
self.assertEqual(lines.count([0,2,0]), 1)
self.assertEqual(lines.count([2,1,2]), 1)
def test_get_winner_on_empty_square_board(self):
"""Test whether get_winner function provides game not over status for an empty square board."""
board = TicTacToeBoard()
# Make sure that the game is not over when it begins with an empty board
winner = board.get_winner()
self.assertEqual(winner, board.GAME_WINNER_GAME_NOT_OVER)
def test_get_winner_on_non_empty_game_not_over_square_board(self):
"""Test whether get_winner function provides game not over status for a non-empty square board."""
# Create non-empty game-not-over tic-tac-toe game board
board = TicTacToeBoard()
board.matrix[0] = [1, 2, 0]
board.matrix[1] = [0, 1, 0]
board.matrix[2] = [0, 2, 0]
winner = board.get_winner()
# Make sure that the game is not over
self.assertEqual(winner, board.GAME_WINNER_GAME_NOT_OVER)
def test_get_winner_on_non_empty_tied_game_square_board(self):
"""Test whether get_winner function provides game tied status for a non-empty square board."""
# Create non-empty tied tic-tac-toe game board
board = TicTacToeBoard()
board.matrix[0] = [1, 2, 1]
board.matrix[1] = [1, 2, 1]
board.matrix[2] = [2, 1, 2]
winner = board.get_winner()
self.assertEqual(board.GAME_WINNER_TIED, -1)
self.assertEqual(winner, board.GAME_WINNER_TIED)
def test_get_winner_on_non_empty_player_1_wins_square_board(self):
"""Test whether get_winner function provides player 1 wins for a non-empty square board that player 1 won."""
# Create non-empty tic-tac-toe game board that player 1 has won
board = TicTacToeBoard()
board.matrix[0] = [1, 0, 0]
board.matrix[1] = [2, 1, 0]
board.matrix[2] = [0, 2, 1]
winner = board.get_winner()
self.assertEqual(winner, 1)
def test_get_winner_on_non_empty_player_2_wins_square_board(self):
"""Test whether get_winner function provides player 2 wins for a non-empty square board that player 2 won."""
# Create non-empty tic-tac-toe game board that player 2 has won
board = TicTacToeBoard()
board.matrix[0] = [2, 0, 0]
board.matrix[1] = [1, 2, 0]
board.matrix[2] = [1, 1, 2]
winner = board.get_winner()
self.assertEqual(winner, 2)
def test_get_next_move_boards_on_non_empty_game_not_over_square_board(self):
"""Test whether _get_next_move_boards function provides all next move boards for a non-empty game-not-over square board."""
# Create non-empty game-not-over tic-tac-toe game board
board = TicTacToeBoard()
board.matrix[0] = [2, 0, 0]
board.matrix[1] = [1, 2, 0]
board.matrix[2] = [1, 1, 2]
next_boards = board._get_next_move_boards(1)
# Initialize valid next boards
valid_next_boards = []
# Add valid next board 1 to list
valid_next_board = TicTacToeBoard()
valid_next_board.matrix = copy.deepcopy(board.matrix)
valid_next_board.matrix[0][1] = 1
valid_next_boards.append(valid_next_board)
# Add valid next board 2 to list
valid_next_board = TicTacToeBoard()
valid_next_board.matrix = copy.deepcopy(board.matrix)
valid_next_board.matrix[0][2] = 1
valid_next_boards.append(valid_next_board)
# Add valid next board 3 to list
valid_next_board = TicTacToeBoard()
valid_next_board.matrix = copy.deepcopy(board.matrix)
valid_next_board.matrix[1][2] = 1
valid_next_boards.append(valid_next_board)
# Make sure there are the correct number of next boards for player 1
next_board_matrices = [next_board.matrix for next_board in next_boards]
self.assertEqual(len(next_board_matrices), 3)
# Make sure that each valid next board for player 1 was found
for valid_next_board in valid_next_boards:
self.assertTrue(valid_next_board.matrix in next_board_matrices)
def test_can_win_on_non_empty_square_board_that_you_won(self):
"""Test whether _can_win function returns that a player can win a non-empty square board that they have won."""
# Create non-empty tic-tac-toe game board that player 1 has won
board = TicTacToeBoard()
board.matrix[0] = [1, 0, 0]
board.matrix[1] = [2, 1, 0]
board.matrix[2] = [0, 2, 1]
self.assertTrue(board._can_win(1))
# Test another similar board
board = TicTacToeBoard()
board.matrix[0] = [1, 0, 0]
board.matrix[1] = [1, 2, 0]
board.matrix[2] = [1, 2, 0]
self.assertTrue(board._can_win(1))
# Test another similar board
board = TicTacToeBoard()
board.matrix[0] = [1, 1, 1]
board.matrix[1] = [0, 2, 0]
board.matrix[2] = [0, 2, 0]
self.assertTrue(board._can_win(1))
# Test another similar board
board = TicTacToeBoard()
board.matrix[0] = [0, 2, 1]
board.matrix[1] = [0, 1, 0]
board.matrix[2] = [1, 2, 0]
self.assertTrue(board._can_win(1))
# Create non-empty tic-tac-toe game board that player 2 has won
board = TicTacToeBoard()
board.matrix[0] = [2, 0, 0]
board.matrix[1] = [1, 2, 0]
board.matrix[2] = [0, 1, 2]
self.assertTrue(board._can_win(2))
# Test another similar board
board = TicTacToeBoard()
board.matrix[0] = [2, 0, 0]
board.matrix[1] = [2, 1, 0]
board.matrix[2] = [2, 1, 0]
self.assertTrue(board._can_win(2))
# Test another similar board
board = TicTacToeBoard()
board.matrix[0] = [2, 2, 2]
board.matrix[1] = [0, 1, 0]
board.matrix[2] = [0, 1, 0]
self.assertTrue(board._can_win(2))
# Test another similar board
board = TicTacToeBoard()
board.matrix[0] = [0, 1, 2]
board.matrix[1] = [0, 2, 0]
board.matrix[2] = [2, 1, 0]
self.assertTrue(board._can_win(2))
def test_can_win_on_non_empty_square_board_you_lost(self):
"""Test whether _can_win function returns that a player cannot win a non-empty square board that they lost."""
# Create non-empty tic-tac-toe game board that player 1 has won
board = TicTacToeBoard()
board.matrix[0] = [1, 0, 0]
board.matrix[1] = [2, 1, 0]
board.matrix[2] = [0, 2, 1]
self.assertFalse(board._can_win(2))
# Test another similar board
board = TicTacToeBoard()
board.matrix[0] = [1, 0, 0]
board.matrix[1] = [1, 2, 0]
board.matrix[2] = [1, 2, 0]
self.assertFalse(board._can_win(2))
# Test another similar board
board = TicTacToeBoard()
board.matrix[0] = [1, 1, 1]
board.matrix[1] = [0, 2, 0]
board.matrix[2] = [0, 2, 0]
self.assertFalse(board._can_win(2))
# Test another similar board
board = TicTacToeBoard()
board.matrix[0] = [0, 2, 1]
board.matrix[1] = [0, 1, 0]
board.matrix[2] = [1, 2, 0]
self.assertFalse(board._can_win(2))
# Create non-empty tic-tac-toe game board that player 2 has won
board = TicTacToeBoard()
board.matrix[0] = [2, 0, 0]
board.matrix[1] = [1, 2, 0]
board.matrix[2] = [0, 1, 2]
self.assertFalse(board._can_win(1))
# Test another similar board
board = TicTacToeBoard()
board.matrix[0] = [2, 0, 0]
board.matrix[1] = [2, 1, 0]
board.matrix[2] = [2, 1, 0]
self.assertFalse(board._can_win(1))
# Test another similar board
board = TicTacToeBoard()
board.matrix[0] = [2, 2, 2]
board.matrix[1] = [0, 1, 0]
board.matrix[2] = [0, 1, 0]
self.assertFalse(board._can_win(1))
# Test another similar board
board = TicTacToeBoard()
board.matrix[0] = [0, 1, 2]
board.matrix[1] = [0, 2, 0]
board.matrix[2] = [2, 1, 0]
self.assertFalse(board._can_win(1))
def test_can_tie_on_non_empty_square_board_that_you_won(self):
"""Test whether _can_tie function returns that a player can tie a non-empty square board that they have won."""
# Create non-empty tic-tac-toe game board that player 1 has won
board = TicTacToeBoard()
board.matrix[0] = [1, 0, 0]
board.matrix[1] = [2, 1, 0]
board.matrix[2] = [0, 2, 1]
self.assertTrue(board._can_tie(1))
# Test another similar board
board = TicTacToeBoard()
board.matrix[0] = [1, 0, 0]
board.matrix[1] = [1, 2, 0]
board.matrix[2] = [1, 2, 0]
self.assertTrue(board._can_tie(1))
# Test another similar board
board = TicTacToeBoard()
board.matrix[0] = [1, 1, 1]
board.matrix[1] = [0, 2, 0]
board.matrix[2] = [0, 2, 0]
self.assertTrue(board._can_tie(1))
# Test another similar board
board = TicTacToeBoard()
board.matrix[0] = [0, 2, 1]
board.matrix[1] = [0, 1, 0]
board.matrix[2] = [1, 2, 0]
self.assertTrue(board._can_tie(1))
# Create non-empty tic-tac-toe game board that player 2 has won
board = TicTacToeBoard()
board.matrix[0] = [2, 0, 0]
board.matrix[1] = [1, 2, 0]
board.matrix[2] = [0, 1, 2]
self.assertTrue(board._can_tie(2))
# Test another similar board
board = TicTacToeBoard()
board.matrix[0] = [2, 0, 0]
board.matrix[1] = [2, 1, 0]
board.matrix[2] = [2, 1, 0]
self.assertTrue(board._can_tie(2))
# Test another similar board
board = TicTacToeBoard()
board.matrix[0] = [2, 2, 2]
board.matrix[1] = [0, 1, 0]
board.matrix[2] = [0, 1, 0]
self.assertTrue(board._can_tie(2))
# Test another similar board
board = TicTacToeBoard()
board.matrix[0] = [0, 1, 2]
board.matrix[1] = [0, 2, 0]
board.matrix[2] = [2, 1, 0]
self.assertTrue(board._can_tie(2))
def test_can_tie_on_non_empty_square_board_you_lost(self):
"""Test whether _can_tie function returns that a player cannot tie a non-empty square board that they lost."""
# Create non-empty tic-tac-toe game board that player 1 has won
board = TicTacToeBoard()
board.matrix[0] = [1, 0, 0]
board.matrix[1] = [2, 1, 0]
board.matrix[2] = [0, 2, 1]
self.assertFalse(board._can_tie(2))
# Test another similar board
board = TicTacToeBoard()
board.matrix[0] = [1, 0, 0]
board.matrix[1] = [1, 2, 0]
board.matrix[2] = [1, 2, 0]
self.assertFalse(board._can_tie(2))
# Test another similar board
board = TicTacToeBoard()
board.matrix[0] = [1, 1, 1]
board.matrix[1] = [0, 2, 0]
board.matrix[2] = [0, 2, 0]
self.assertFalse(board._can_tie(2))
# Test another similar board
board = TicTacToeBoard()
board.matrix[0] = [0, 2, 1]
board.matrix[1] = [0, 1, 0]
board.matrix[2] = [1, 2, 0]
self.assertFalse(board._can_tie(2))
# Create non-empty tic-tac-toe game board that player 2 has won
board = TicTacToeBoard()
board.matrix[0] = [2, 0, 0]
board.matrix[1] = [1, 2, 0]
board.matrix[2] = [0, 1, 2]
self.assertFalse(board._can_tie(1))
# Test another similar board
board = TicTacToeBoard()
board.matrix[0] = [2, 0, 0]
board.matrix[1] = [2, 1, 0]
board.matrix[2] = [2, 1, 0]
self.assertFalse(board._can_tie(1))
# Test another similar board
board = TicTacToeBoard()
board.matrix[0] = [2, 2, 2]
board.matrix[1] = [0, 1, 0]
board.matrix[2] = [0, 1, 0]
self.assertFalse(board._can_tie(1))
# Test another similar board
board = TicTacToeBoard()
board.matrix[0] = [0, 1, 2]
board.matrix[1] = [0, 2, 0]
board.matrix[2] = [2, 1, 0]
self.assertFalse(board._can_tie(1))
def test_can_win_on_empty_square_board(self):
"""Test whether _can_win function returns that a player can always win on an empty square board."""
# Create empty tic-tac-toe game board
board = TicTacToeBoard()
# A perfect player is not guaranteed to win on an empty board
self.assertFalse(board._can_win(1))
self.assertFalse(board._can_win(2))
def test_can_tie_on_empty_square_board(self):
"""Test whether _can_tie function returns that a player can tie on an empty square board."""
# Create empty tic-tac-toe game board
board = TicTacToeBoard()
# A perfect player is guaranteed to tie on an empty board
self.assertTrue(board._can_tie(1))
self.assertTrue(board._can_tie(2))
def test_can_win_on_non_empty_square_board_they_cannot_win(self):
"""Test whether _can_win function returns that a player can win on a non-empty square board they cannot win."""
# Create non-empty tic-tac-toe game board that player 2 cannot win
board = TicTacToeBoard()
board.matrix[0] = [1, 2, 1]
board.matrix[1] = [2, 1, 0]
board.matrix[2] = [0, 0, 2]
self.assertFalse(board._can_win(2))
# Create non-empty tic-tac-toe game board that player 1 cannot win
board = TicTacToeBoard()
board.matrix[0] = [2, 1, 2]
board.matrix[1] = [1, 2, 0]
board.matrix[2] = [0, 0, 1]
self.assertFalse(board._can_win(1))
if __name__ == '__main__':
unittest.main()