-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathtest_pieces.py
More file actions
336 lines (249 loc) · 8.62 KB
/
test_pieces.py
File metadata and controls
336 lines (249 loc) · 8.62 KB
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
from chessington.engine.board import Board
from chessington.engine.data import Player, Square
from chessington.engine.pieces import Pawn
class TestPawns:
@staticmethod
def test_white_pawns_can_move_up_one_square():
# Arrange
board = Board.empty()
pawn = Pawn(Player.WHITE)
square = Square.at(1, 4)
board.set_piece(square, pawn)
# Act
moves = pawn.get_available_moves(board)
# Assert
assert Square.at(2, 4) in moves
@staticmethod
def test_black_pawns_can_move_down_one_square():
# Arrange
board = Board.empty()
pawn = Pawn(Player.BLACK)
square = Square.at(6, 4)
board.set_piece(square, pawn)
# Act
moves = pawn.get_available_moves(board)
# Assert
assert Square.at(5, 4) in moves
@staticmethod
def test_white_pawn_can_move_up_two_squares_if_not_moved():
# Arrange
board = Board.empty()
pawn = Pawn(Player.WHITE)
square = Square.at(1, 4)
board.set_piece(square, pawn)
# Act
moves = pawn.get_available_moves(board)
# Assert
assert Square.at(3, 4) in moves
@staticmethod
def test_black_pawn_can_move_down_two_squares_if_not_moved():
# Arrange
board = Board.empty()
pawn = Pawn(Player.BLACK)
square = Square.at(6, 4)
board.set_piece(square, pawn)
# Act
moves = pawn.get_available_moves(board)
# Assert
assert Square.at(4, 4) in moves
@staticmethod
def test_white_pawn_cannot_move_up_two_squares_if_already_moved():
# Arrange
board = Board.empty()
pawn = Pawn(Player.WHITE)
starting_square = Square.at(1, 4)
board.set_piece(starting_square, pawn)
intermediate_square = Square.at(2, 4)
pawn.move_to(board, intermediate_square)
# Act
moves = pawn.get_available_moves(board)
# Assert
assert Square.at(3, 4) in moves
assert Square.at(4, 4) not in moves
@staticmethod
def test_black_pawn_cannot_move_down_two_squares_if_already_moved():
# Arrange
board = Board.empty()
pawn = Pawn(Player.BLACK)
starting_square = Square.at(6, 4)
board.set_piece(starting_square, pawn)
intermediate_square = Square.at(5, 4)
board.current_player = Player.BLACK
pawn.move_to(board, intermediate_square)
# Act
moves = pawn.get_available_moves(board)
# Assert
assert Square.at(4, 4) in moves
assert Square.at(3, 4) not in moves
@staticmethod
def test_white_pawn_cannot_move_if_piece_in_front():
# Arrange
board = Board.empty()
pawn = Pawn(Player.WHITE)
pawn_square = Square.at(4, 4)
board.set_piece(pawn_square, pawn)
obstructing_square = Square.at(5, 4)
obstruction = Pawn(Player.BLACK)
board.set_piece(obstructing_square, obstruction)
# Act
moves = pawn.get_available_moves(board)
# Assert
assert len(moves) == 0
@staticmethod
def test_black_pawn_cannot_move_if_piece_in_front():
# Arrange
board = Board.empty()
pawn = Pawn(Player.BLACK)
pawn_square = Square.at(4, 4)
board.set_piece(pawn_square, pawn)
obstructing_square = Square.at(3, 4)
obstruction = Pawn(Player.WHITE)
board.set_piece(obstructing_square, obstruction)
# Act
moves = pawn.get_available_moves(board)
# Assert
assert len(moves) == 0
@staticmethod
def test_white_pawn_cannot_move_two_squares_if_piece_two_in_front():
# Arrange
board = Board.empty()
pawn = Pawn(Player.WHITE)
pawn_square = Square.at(1, 4)
board.set_piece(pawn_square, pawn)
obstructing_square = Square.at(3, 4)
obstruction = Pawn(Player.BLACK)
board.set_piece(obstructing_square, obstruction)
# Act
moves = pawn.get_available_moves(board)
# Assert
assert obstructing_square not in moves
@staticmethod
def test_black_pawn_cannot_move_two_squares_if_piece_two_in_front():
# Arrange
board = Board.empty()
pawn = Pawn(Player.BLACK)
pawn_square = Square.at(6, 4)
board.set_piece(pawn_square, pawn)
obstructing_square = Square.at(4, 4)
obstruction = Pawn(Player.WHITE)
board.set_piece(obstructing_square, obstruction)
# Act
moves = pawn.get_available_moves(board)
# Assert
assert obstructing_square not in moves
@staticmethod
def test_white_pawn_cannot_move_two_squares_if_piece_one_in_front():
# Arrange
board = Board.empty()
pawn = Pawn(Player.WHITE)
pawn_square = Square.at(1, 4)
board.set_piece(pawn_square, pawn)
obstructing_square = Square.at(2, 4)
obstruction = Pawn(Player.BLACK)
board.set_piece(obstructing_square, obstruction)
# Act
moves = pawn.get_available_moves(board)
# Assert
assert Square.at(3, 4) not in moves
@staticmethod
def test_black_pawn_cannot_move_two_squares_if_piece_one_in_front():
# Arrange
board = Board.empty()
pawn = Pawn(Player.BLACK)
pawn_square = Square.at(6, 4)
board.set_piece(pawn_square, pawn)
obstructing_square = Square.at(5, 4)
obstruction = Pawn(Player.WHITE)
board.set_piece(obstructing_square, obstruction)
# Act
moves = pawn.get_available_moves(board)
# Assert
assert Square.at(4, 4) not in moves
@staticmethod
def test_white_pawn_cannot_move_at_top_of_board():
# Arrange
board = Board.empty()
pawn = Pawn(Player.WHITE)
square = Square.at(7, 4)
board.set_piece(square, pawn)
# Act
moves = pawn.get_available_moves(board)
# Assert
assert len(moves) == 0
@staticmethod
def test_black_pawn_cannot_move_at_bottom_of_board():
# Arrange
board = Board.empty()
pawn = Pawn(Player.BLACK)
square = Square.at(0, 4)
board.set_piece(square, pawn)
# Act
moves = pawn.get_available_moves(board)
# Assert
assert len(moves) == 0
@staticmethod
def test_white_pawns_can_capture_diagonally():
# Arrange
board = Board.empty()
pawn = Pawn(Player.WHITE)
pawn_square = Square.at(3, 4)
board.set_piece(pawn_square, pawn)
enemy1 = Pawn(Player.BLACK)
enemy1_square = Square.at(4, 5)
board.set_piece(enemy1_square, enemy1)
enemy2 = Pawn(Player.BLACK)
enemy2_square = Square.at(4, 3)
board.set_piece(enemy2_square, enemy2)
# Act
moves = pawn.get_available_moves(board)
# Assert
assert enemy1_square in moves
assert enemy2_square in moves
@staticmethod
def test_black_pawns_can_capture_diagonally():
# Arrange
board = Board.empty()
pawn = Pawn(Player.BLACK)
pawn_square = Square.at(3, 4)
board.set_piece(pawn_square, pawn)
enemy1 = Pawn(Player.WHITE)
enemy1_square = Square.at(2, 5)
board.set_piece(enemy1_square, enemy1)
enemy2 = Pawn(Player.WHITE)
enemy2_square = Square.at(2, 3)
board.set_piece(enemy2_square, enemy2)
# Act
moves = pawn.get_available_moves(board)
# Assert
assert enemy1_square in moves
assert enemy2_square in moves
@staticmethod
def test_white_pawns_cannot_move_diagonally_except_to_capture():
# Arrange
board = Board.empty()
pawn = Pawn(Player.WHITE)
pawn_square = Square.at(3, 4)
board.set_piece(pawn_square, pawn)
friendly = Pawn(Player.WHITE)
friendly_square = Square.at(4, 5)
board.set_piece(friendly_square, friendly)
# Act
moves = pawn.get_available_moves(board)
# Assert
assert Square.at(4, 3) not in moves
assert Square.at(4, 5) not in moves
@staticmethod
def test_black_pawns_cannot_move_diagonally_except_to_capture():
# Arrange
board = Board.empty()
pawn = Pawn(Player.BLACK)
pawn_square = Square.at(3, 4)
board.set_piece(pawn_square, pawn)
friendly = Pawn(Player.BLACK)
friendly_square = Square.at(2, 5)
board.set_piece(friendly_square, friendly)
# Act
moves = pawn.get_available_moves(board)
# Assert
assert Square.at(2, 3) not in moves
assert Square.at(2, 5) not in moves