-
Notifications
You must be signed in to change notification settings - Fork 1
/
board_test.py
209 lines (161 loc) · 5.98 KB
/
board_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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import unittest
import board
import constants
class TestBoard(unittest.TestCase):
def test_single_static_piece(self):
b = board.Board(["2,5;B5"])
p = b.state[1][4]
self.assertEqual(p.color, constants.BLACK)
self.assertEqual(p.pos, "B5")
self.assertEqual(p.type, constants.KING)
self.assertFalse(b.moving[constants.BLACK][1][4])
def test_single_sleeping_piece(self):
b = board.Board(["1,6;S,1518694394.674937,B4"])
p = b.state[1][3]
self.assertEqual(p.color, constants.WHITE)
self.assertEqual(p.pos, "B4")
self.assertEqual(p.type, constants.PAWN)
self.assertLess(abs(1518694394.674937 - p.end_time), 1e-6)
self.assertFalse(b.moving[constants.WHITE][1][3])
def test_single_moving_piece(self):
b = board.Board(["1,6;M,1518694394.674937,B4"])
self.assertIs(b.state[1][4], None)
self.assertTrue(b.moving[constants.WHITE][1][3])
class TestIsValidMove(unittest.TestCase):
def test_ok(self):
b = board.Board(["2,5;B5"])
self.assertTrue(b.is_valid_move("B5", "B6"))
def test_invalid_pos(self):
b = board.Board(["2,5;B5"])
self.assertFalse(b.is_valid_move("B5", "B6s"))
self.assertFalse(b.is_valid_move("B55", "B6"))
self.assertFalse(b.is_valid_move("Z5", "B6"))
def test_same_pos(self):
b = board.Board(["2,5;B5"])
self.assertFalse(b.is_valid_move("B5", "B5"))
def test_no_piece(self):
b = board.Board(["2,5;B5"])
self.assertFalse(b.is_valid_move("A1", "A2"))
def test_sleeping(self):
b = board.Board(["2,5;S,1518694394.674937,B5"])
self.assertFalse(b.is_valid_move("B5", "B6"))
def test_moving(self):
b = board.Board(["2,5;M,1518694394.674937,B5"])
self.assertFalse(b.is_valid_move("B5", "B6"))
def test_rook_ok(self):
b = board.Board(["2,1;B5"])
self.assertTrue(b.is_valid_move("B5", "B8"))
def test_rook_diagonal(self):
b = board.Board(["2,1;B5"])
self.assertFalse(b.is_valid_move("B5", "C6"))
def test_rook_block(self):
b = board.Board(["2,1;B5", "2,1;B7"])
self.assertFalse(b.is_valid_move("B5", "B8"))
def test_bishop_ok(self):
b = board.Board(["2,3;B5"])
self.assertTrue(b.is_valid_move("B5", "D7"))
def test_bishop_straight(self):
b = board.Board(["2,3;B5"])
self.assertFalse(b.is_valid_move("B5", "B7"))
def test_bishop_block(self):
b = board.Board(["2,3;B5", "2,1;C6"])
self.assertFalse(b.is_valid_move("B5", "D7"))
def test_queen_ok_diagonal(self):
b = board.Board(["2,4;B5"])
self.assertTrue(b.is_valid_move("B5", "D7"))
def test_queen_ok_straight(self):
b = board.Board(["2,4;B5"])
self.assertTrue(b.is_valid_move("B5", "B7"))
def test_queen_irregular(self):
b = board.Board(["2,4;B5"])
self.assertFalse(b.is_valid_move("B5", "C7"))
def test_queen_block(self):
b = board.Board(["2,4;B5", "2,1;C6"])
self.assertFalse(b.is_valid_move("B5", "D7"))
def test_king_ok(self):
b = board.Board(["2,5;D5"])
self.assertTrue(b.is_valid_move("D5", "D6"))
self.assertTrue(b.is_valid_move("D5", "D4"))
self.assertTrue(b.is_valid_move("D5", "C4"))
self.assertTrue(b.is_valid_move("D5", "E6"))
def test_king_too_far(self):
b = board.Board(["2,5;D5"])
self.assertFalse(b.is_valid_move("D5", "D7"))
def test_knight_ok(self):
b = board.Board(["2,2;D5"])
self.assertTrue(b.is_valid_move("D5", "C7"))
self.assertTrue(b.is_valid_move("D5", "E7"))
self.assertTrue(b.is_valid_move("D5", "F4"))
self.assertTrue(b.is_valid_move("D5", "F6"))
def test_knight_invalid(self):
b = board.Board(["2,2;D5"])
self.assertFalse(b.is_valid_move("D5", "D7"))
def test_white_pawn_move_start(self):
b = board.Board(["1,6;D2"])
self.assertTrue(b.is_valid_move("D2", "D3"))
self.assertTrue(b.is_valid_move("D2", "D4"))
self.assertFalse(b.is_valid_move("D2", "D1"))
self.assertFalse(b.is_valid_move("D2", "E3"))
def test_black_pawn_move_start(self):
b = board.Board(["2,6;D7"])
self.assertTrue(b.is_valid_move("D7", "D6"))
self.assertTrue(b.is_valid_move("D7", "D5"))
self.assertFalse(b.is_valid_move("D7", "D8"))
self.assertFalse(b.is_valid_move("D7", "E6"))
def test_white_pawn_capture(self):
b = board.Board(["1,6;D2", "1,6;E3"])
self.assertFalse(b.is_valid_move("D2", "E3"))
def test_black_pawn_capture(self):
b = board.Board(["2,6;D7", "2,6;E6"])
self.assertFalse(b.is_valid_move("D7", "E6"))
def test_invalid_type(self):
b = board.Board(["2,12;D5"])
self.assertFalse(b.is_valid_move("D5", "D6"))
class TestPossibleMoves(unittest.TestCase):
def test_empty(self):
b = board.Board(["1,6;D2"])
self.assertEqual(b.get_moves("A2"), [])
def test_pawn_start(self):
b = board.Board(["1,6;D2"])
self.assertCountEqual(b.get_moves("D2"), ["D3", "D4"])
def test_pawn_capture(self):
b = board.Board(["1,6;D3", "1,6;E4", "2,6;C4"])
self.assertCountEqual(b.get_moves("D3"), ["D4", "C4"])
def test_black_pawn_(self):
b = board.Board(["2,6;D2"])
self.assertCountEqual(b.get_moves("D2"), ["D1"])
def test_rook(self):
b = board.Board(["1,1;D3", "2,6;D5", "1,6;D1"])
self.assertCountEqual(
b.get_moves("D3"),
["D2", "D4", "D5", "A3", "B3", "C3", "E3", "F3", "G3", "H3"])
def test_knight(self):
b = board.Board(["1,2;D3"])
self.assertCountEqual(
b.get_moves("D3"),
["F2", "F4", "B2", "B4", "C1", "E1", "C5", "E5"])
def test_bishop(self):
b = board.Board(["2,3;B2"])
self.assertCountEqual(
b.get_moves("B2"),
["A1", "C3", "D4", "E5", "F6", "G7", "H8", "A3", "C1"])
def test_queen(self):
b = board.Board(["2,4;B2", "1,6;C2", "1,6;B4"])
self.assertCountEqual(
b.get_moves("B2"), [
"A1", "C3", "D4", "E5", "F6", "G7", "H8", "A3", "C1", "A2",
"B1", "C2", "B3", "B4"
])
def test_king(self):
b = board.Board(["2,5;B1"])
self.assertCountEqual(
b.get_moves("B1"), ["A1", "A2", "B2", "C2", "C1"])
def test_all(self):
b = board.Board(["2,5;B1", "2,3;B2"])
moves = {key: val for key, val in b.get_possible_moves(2)}
self.assertCountEqual(moves["B1"], ["A1", "A2", "C2", "C1"])
self.assertCountEqual(
moves["B2"],
["A1", "C3", "D4", "E5", "F6", "G7", "H8", "A3", "C1"])
if __name__ == '__main__':
unittest.main()