-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_tictactoe.py
188 lines (162 loc) · 5.92 KB
/
test_tictactoe.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
import unittest
import tictactoe
class TestTicTacToe(unittest.TestCase):
def test_player(self):
board = [
[tictactoe.EMPTY, tictactoe.EMPTY, tictactoe.EMPTY],
[tictactoe.EMPTY, tictactoe.EMPTY, tictactoe.EMPTY],
[tictactoe.EMPTY, tictactoe.EMPTY, tictactoe.EMPTY],
]
# Check first player
result = tictactoe.player(board)
self.assertEqual(result, tictactoe.X)
# Check second player
board[0][0] = tictactoe.X
result = tictactoe.player(board)
self.assertEqual(result, tictactoe.O)
# Check third player
board[0][1] = tictactoe.O
result = tictactoe.player(board)
self.assertEqual(result, tictactoe.X)
def test_actions(self):
board = [
[tictactoe.X, tictactoe.O, tictactoe.EMPTY],
[tictactoe.O, tictactoe.O, tictactoe.X],
[tictactoe.X, tictactoe.EMPTY, tictactoe.EMPTY],
]
# Check three actions
result = tictactoe.actions(board)
self.assertEqual(result, [(0, 2), (2, 1), (2, 2)])
# Check two actions
board[2][2] = tictactoe.X
result = tictactoe.actions(board)
self.assertEqual(result, [(0, 2), (2, 1)])
# Check one actions
board[0][2] = tictactoe.O
result = tictactoe.actions(board)
self.assertEqual(result, [(2, 1)])
def test_result(self):
board = [
[tictactoe.X, tictactoe.O, tictactoe.EMPTY],
[tictactoe.O, tictactoe.O, tictactoe.X],
[tictactoe.X, tictactoe.EMPTY, tictactoe.EMPTY],
]
# Check first move
result = tictactoe.result(board, (0, 2))
self.assertEqual(
result,
[
[tictactoe.X, tictactoe.O, tictactoe.X],
[tictactoe.O, tictactoe.O, tictactoe.X],
[tictactoe.X, tictactoe.EMPTY, tictactoe.EMPTY],
],
)
# Check second move
board[0][2] = tictactoe.X
result = tictactoe.result(board, (2, 1))
self.assertEqual(
result,
[
[tictactoe.X, tictactoe.O, tictactoe.X],
[tictactoe.O, tictactoe.O, tictactoe.X],
[tictactoe.X, tictactoe.O, tictactoe.EMPTY],
],
)
# Check third move
board[2][1] = tictactoe.O
result = tictactoe.result(board, (2, 2))
self.assertEqual(
result,
[
[tictactoe.X, tictactoe.O, tictactoe.X],
[tictactoe.O, tictactoe.O, tictactoe.X],
[tictactoe.X, tictactoe.O, tictactoe.X],
],
)
def test_winner(self):
# Check no winner
board = [
[tictactoe.X, tictactoe.O, tictactoe.EMPTY],
[tictactoe.O, tictactoe.O, tictactoe.X],
[tictactoe.X, tictactoe.X, tictactoe.EMPTY],
]
result = tictactoe.winner(board)
self.assertIsNone(result)
# Check vertical match
board = [
[tictactoe.X, tictactoe.O, tictactoe.EMPTY],
[tictactoe.O, tictactoe.O, tictactoe.X],
[tictactoe.X, tictactoe.O, tictactoe.X],
]
result = tictactoe.winner(board)
self.assertEqual(result, tictactoe.O)
# Check horizontal match
board = [
[tictactoe.X, tictactoe.O, tictactoe.O],
[tictactoe.O, tictactoe.O, tictactoe.X],
[tictactoe.X, tictactoe.X, tictactoe.X],
]
result = tictactoe.winner(board)
self.assertEqual(result, tictactoe.X)
# Check main diagonal match
board = [
[tictactoe.X, tictactoe.O, tictactoe.O],
[tictactoe.X, tictactoe.X, tictactoe.EMPTY],
[tictactoe.O, tictactoe.EMPTY, tictactoe.X],
]
result = tictactoe.winner(board)
self.assertEqual(result, tictactoe.X)
# Check anti diagonal match
board = [
[tictactoe.X, tictactoe.O, tictactoe.O],
[tictactoe.X, tictactoe.O, tictactoe.EMPTY],
[tictactoe.O, tictactoe.X, tictactoe.X],
]
result = tictactoe.winner(board)
self.assertEqual(result, tictactoe.O)
def test_terminal(self):
# Game not finished
board = [
[tictactoe.EMPTY, tictactoe.EMPTY, tictactoe.EMPTY],
[tictactoe.EMPTY, tictactoe.EMPTY, tictactoe.EMPTY],
[tictactoe.EMPTY, tictactoe.EMPTY, tictactoe.EMPTY],
]
self.assertFalse(tictactoe.terminal(board))
# Game has a winner
board = [
[tictactoe.X, tictactoe.EMPTY, tictactoe.EMPTY],
[tictactoe.O, tictactoe.X, tictactoe.O],
[tictactoe.EMPTY, tictactoe.EMPTY, tictactoe.X],
]
self.assertTrue(tictactoe.terminal(board))
# Game is a tie
board = [
[tictactoe.X, tictactoe.O, tictactoe.O],
[tictactoe.O, tictactoe.X, tictactoe.X],
[tictactoe.X, tictactoe.X, tictactoe.O],
]
self.assertTrue(tictactoe.terminal(board))
def test_utility(self):
# Game is a tie
board = [
[tictactoe.X, tictactoe.O, tictactoe.O],
[tictactoe.O, tictactoe.X, tictactoe.X],
[tictactoe.X, tictactoe.X, tictactoe.O],
]
self.assertEqual(tictactoe.utility(board), 0)
# X has won
board = [
[tictactoe.X, tictactoe.EMPTY, tictactoe.EMPTY],
[tictactoe.O, tictactoe.X, tictactoe.O],
[tictactoe.EMPTY, tictactoe.EMPTY, tictactoe.X],
]
self.assertEqual(tictactoe.utility(board), 1)
# O has won
board = [
[tictactoe.X, tictactoe.EMPTY, tictactoe.EMPTY],
[tictactoe.O, tictactoe.O, tictactoe.O],
[tictactoe.X, tictactoe.EMPTY, tictactoe.X],
]
self.assertEqual(tictactoe.utility(board), -1)
if __name__ == "__main__":
unittest.main()