-
Notifications
You must be signed in to change notification settings - Fork 0
/
game_test.py
192 lines (177 loc) · 8.26 KB
/
game_test.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
import unittest
import game
import game
class TestBoard(unittest.TestCase):
def setUp(self):
self.initial_state = game.init_state()
self.borders = game.init_borders()
self.b_piece = game.Piece.BLACK
self.w_piece = game.Piece.WHITE
self.b_king = game.Piece.BLACK_KING
self.empty = game.Piece.EMPTY
self.move = game.make_move
def tearDown(self):
del self.initial_state
del self.borders
del self.b_piece
del self.w_piece
del self.b_king
del self.empty
del self.move
def test_init_state_method_with_initial_board(self):
actual_output = set(self.initial_state)
expected = {
(1 + 0j, self.w_piece,),
(3 + 0j, self.w_piece,),
(5 + 0j, self.w_piece,),
(7 + 0j, self.w_piece,),
(0 + 1j, self.w_piece,),
(2 + 1j, self.w_piece,),
(4 + 1j, self.w_piece,),
(6 + 1j, self.w_piece,),
(1 + 2j, self.w_piece,),
(3 + 2j, self.w_piece,),
(5 + 2j, self.w_piece,),
(7 + 2j, self.w_piece,),
(0 + 5j, self.b_piece,),
(2 + 5j, self.b_piece,),
(4 + 5j, self.b_piece,),
(6 + 5j, self.b_piece,),
(1 + 6j, self.b_piece,),
(3 + 6j, self.b_piece,),
(5 + 6j, self.b_piece,),
(7 + 6j, self.b_piece,),
(0 + 7j, self.b_piece,),
(2 + 7j, self.b_piece,),
(4 + 7j, self.b_piece,),
(6 + 7j, self.b_piece,),
(0 + 3j, self.empty,),
(2 + 3j, self.empty,),
(4 + 3j, self.empty,),
(6 + 3j, self.empty,),
(1 + 4j, self.empty,),
(3 + 4j, self.empty,),
(5 + 4j, self.empty,),
(7 + 4j, self.empty,),
}
self.assertEqual(actual_output, expected)
def test_valid_move_method_with_initial_board_state(self):
board_obj = game.Board(self.initial_state, self.borders)
expected = {
(self.b_piece, (5j, 1 + 4j), ()),
(self.b_piece, (2 + 5j, 1 + 4j), ()),
(self.b_piece, (2 + 5j, 3 + 4j), ()),
(self.b_piece, (4 + 5j, 3 + 4j), ()),
(self.b_piece, (4 + 5j, 5 + 4j), ()),
(self.b_piece, (6 + 5j, 5 + 4j), ()),
(self.b_piece, (6 + 5j, 7 + 4j), ()),
}
actual_output = board_obj.find_valid_moves(self.b_piece)
self.assertEqual(expected, actual_output)
def test_valid_move_method_with_one_valid_capture(self):
state = self.move((self.b_piece, (5j, 1 + 4j), ()), self.initial_state)
state = self.move((self.w_piece, (3 + 2j, 2 + 3j), ()), state)
expected = {(self.b_piece, (1 + 4j, 3 + 2j), (2 + 3j,))}
self.valid_paths_helper(state, expected, self.b_piece)
def test_valid_move_method_with_one_valid_capture_2(self):
state = self.move((self.b_piece, (2 + 5j, 3 + 4j), ()), self.initial_state)
state = self.move((self.w_piece, (3 + 2j, 4 + 3j), ()), state)
state = self.move((self.b_piece, (1 + 6j, 2 + 5j), ()), state)
state = self.move((self.w_piece, (5 + 2j, 6 + 3j), ()), state)
state = self.move((self.b_piece, (3 + 4j, 5 + 2j), (4 + 3j,)), state)
expected = {(self.w_piece, (6 + 1j, 4 + 3j), (5 + 2j,))}
self.valid_paths_helper(state, expected, self.w_piece)
def test_valid_move_method_with_2_valid_captures(self):
state = self.move((self.b_piece, (4 + 5j, 5 + 4j), ()), self.initial_state)
state = self.move((self.w_piece, (5 + 2j, 6 + 3j), ()), state)
state = self.move((self.b_piece, (5 + 6j, 4 + 5j), ()), state)
state = self.move((self.w_piece, (3 + 2j, 4 + 3j), ()), state)
state = self.move((self.b_piece, (5 + 4j, 3 + 2j), (4 + 3j,)), state)
expected = {
(self.w_piece, (2 + 1j, 4 + 3j), (3 + 2j,)),
(self.w_piece, (4 + 1j, 2 + 3j), (3 + 2j,)),
}
self.valid_paths_helper(state, expected, self.w_piece)
def test_valid_paths_method_with_double_capture(self):
state = self.move((self.b_piece, (5j, 1 + 4j), ()), self.initial_state)
state = self.move((self.w_piece, (5 + 2j, 4 + 3j), ()), state)
state = self.move((self.b_piece, (6 + 5j, 7 + 4j), ()), state)
state = self.move((self.w_piece, (6 + 1j, 5 + 2j), ()), state)
state = self.move((self.b_piece, (1 + 6j, 5j), ()), state)
state = self.move((self.w_piece, (4 + 3j, 3 + 4j), ()), state)
expected = {(self.b_piece, (2 + 5j, 4 + 3j, 6 + 1j), (3 + 4j, 5 + 2j))}
self.valid_paths_helper(state, expected, self.b_piece)
def test_valid_paths_method_with_double_capture_2(self):
state = self.move((self.b_piece, (5j, 1 + 4j), ()), self.initial_state)
state = self.move((self.w_piece, (5 + 2j, 4 + 3j), ()), state)
state = self.move((self.b_piece, (6 + 5j, 7 + 4j), ()), state)
state = self.move((self.w_piece, (6 + 1j, 5 + 2j), ()), state)
state = self.move((self.b_piece, (1 + 6j, 5j), ()), state)
state = self.move((self.w_piece, (5 + 0j, 6 + 1j), ()), state)
state = self.move((self.b_piece, (7j, 1 + 6j), ()), state)
state = self.move((self.w_piece, (3 + 2j, 2 + 3j), ()), state)
expected = {(self.b_piece, (1 + 4j, 3 + 2j, 5 + 0j), (2 + 3j, 4 + 1j))}
self.valid_paths_helper(state, expected, self.b_piece)
def test_valid_paths_with_king_double_capture(self):
state = self.move((self.b_piece, (6 + 5j, 7 + 4j), ()), self.initial_state)
state = self.move((self.w_piece, (1 + 2j, 2 + 3j), ()), state)
state = self.move((self.b_piece, (5 + 6j, 6 + 5j), ()), state)
state = self.move((self.w_piece, (1j, 1 + 2j), ()), state)
state = self.move((self.b_piece, (4 + 7j, 5 + 6j), ()), state)
state = self.move((self.w_piece, (2 + 3j, 3 + 4j), ()), state)
state = self.move((self.b_piece, (4 + 5j, 2 + 3j, 1j), ( 3 + 4j, 1 + 2j)), state)
state = self.move((self.w_piece, (2 + 1j, 1 + 2j), ()), state)
state = self.move((self.b_piece, (5j, 1 + 4j), ()), state)
state = self.move((self.w_piece, (1 + 0j, 2 + 1j), ()), state)
state = self.move((self.b_piece, (1j, 1 + 0j), ()), state) # black piece promoted
state = self.move((self.w_piece, (3 + 2j, 4 + 3j), ()), state)
expected = {(self.b_king, (1 + 0j, 3 + 2j, 5 + 4j), (2 + 1j, 4 + 3j))}
self.valid_paths_helper(state, expected, self.b_piece)
def test_valid_paths_white_mid_game_state(self):
state = {
(1 + 0j, self.empty,),
(3 + 0j, self.w_piece,),
(5 + 0j, self.empty,),
(7 + 0j, self.w_piece,),
(0 + 1j, self.empty,),
(2 + 1j, self.empty,),
(4 + 1j, self.w_piece,),
(6 + 1j, self.w_piece,),
(1 + 2j, self.w_piece,),
(3 + 2j, self.w_piece,),
(5 + 2j, self.w_piece,),
(7 + 2j, self.w_piece,),
(0 + 5j, self.b_piece,),
(2 + 5j, self.b_piece,),
(4 + 5j, self.b_piece,),
(6 + 5j, self.empty,),
(1 + 6j, self.b_piece,),
(3 + 6j, self.b_piece,),
(5 + 6j, self.b_piece,),
(7 + 6j, self.empty,),
(0 + 7j, self.empty,),
(2 + 7j, self.b_piece,),
(4 + 7j, self.empty,),
(6 + 7j, self.b_piece,),
(0 + 3j, self.w_piece,),
(2 + 3j, self.w_piece,),
(4 + 3j, self.empty,),
(6 + 3j, self.w_piece,),
(1 + 4j, self.b_piece,),
(3 + 4j, self.empty,),
(5 + 4j, self.b_piece,),
(7 + 4j, self.b_piece,),
}
expected = {
(self.w_piece, (3+0j, 2+1j), ()),
(self.w_piece, (3+2j, 4+3j), ()),
(self.w_piece, (5+2j, 4+3j), ()),
(self.w_piece, (2+3j, 3+4j), ()),
}
self.valid_paths_helper(state, expected, self.w_piece)
def valid_paths_helper(self, state, expected, player):
board_state = game.Board(state, self.borders)
actual_output = board_state.find_valid_moves(player)
self.assertEqual(expected, actual_output)
if __name__ == "__main__":
unittest.main()