Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/testcases/piecetests.py
blob: ffa619f0b98da27904c5ec3bf30a81783f1d781d (plain)
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
#
#    Ceibal Chess - A chess activity for Sugar.
#    Copyright (C) 2009 Alejandro Segovia <asegovi@gmail.com>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
#
import unittest
from board import Board, RIGHT, LEFT
from piece import Move, EnPassant, Castling, Crowning, Pawn, Knight, Queen, Rook, King

class PawnTest(unittest.TestCase):
	'''Class for testing a pawn's moves'''

	def test_basic_white_moves(self):
		'''Test basic white pawn movement (step and double-step).'''
		board = Board(10, 10)
		pawn = Pawn(board.white)

		i, j = 1, 6
		board[i, j].piece = pawn

		moves = pawn.get_moves((i, j), board)

		self.assertEquals(len(moves), 2)
		self.assertTrue(Move((i,j), (i,j-1)) in moves)
		self.assertTrue(Move((i,j), (i,j-2)) in moves)

	def test_en_passant_right(self):
		'''Test en passant for black pawn.'''
		board = Board(10, 10)
		white_pawn = Pawn(board.white)
		black_pawn = Pawn(board.black)

		board[4, 4].piece = black_pawn
		board[5, 6].piece = white_pawn

		board.perform_move(Move((5,6), (5,4)))
		moves = black_pawn.get_moves((4, 4), board)

		self.assertEquals(2, len(moves))
		self.assertTrue(Move((4,4),(4,5)) in moves)
		self.assertTrue(EnPassant((4,4), RIGHT, board.black) in moves)

	def test_en_passant_left(self):
		'''Test en passant for black pawn.'''
		board = Board(10, 10)
		white_pawn = Pawn(board.white)
		black_pawn = Pawn(board.black)

		board[4, 4].piece = black_pawn
		board[3, 6].piece = white_pawn

		board.perform_move(Move((3,6), (3,4)))
		moves = black_pawn.get_moves((4, 4), board)

		self.assertEquals(2, len(moves))
		self.assertTrue(Move((4,4),(4,5)) in moves)
		self.assertTrue(EnPassant((4,4), LEFT, board.black) in moves)

	def test_white_attack_move(self):
		'''Test attack move for white pawn.'''
		board = Board(10, 10)
		white_pawn = Pawn(board.white)
		black_pawn = Pawn(board.black)

		i, j = 1, 6
		board[i, j].piece = white_pawn
		board[i-1, j-1].piece = black_pawn
		board[i+1, j-1].piece = black_pawn

		moves = white_pawn.get_moves((i, j), board)

		self.assertEquals(4, len(moves))
		self.assertTrue(Move((i,j),(i-1,j-1)) in moves)
		self.assertTrue(Move((i,j),(i+1,j-1)) in moves)

	def test_white_crowning(self):
		'''Test white crowning.'''
		board = Board(10, 10)
		pawn = Pawn(board.white)

		i, j = 1, 1
		board[i, j].piece = pawn

		moves = pawn.get_moves((i, j), board)

		self.assertEquals(len(moves), 1)
		self.assertTrue(moves[0].type == "Crowning")

	def test_white_crowning_with_type(self):
		'''Test white crowning.'''
		board = Board(10, 10)
		pawn = Pawn(board.white)

		i, j = 1, 1
		board[i, j].piece = pawn

		moves = pawn.get_moves((i, j), board, type="R")

		self.assertEquals(len(moves), 1)
		self.assertTrue(moves[0].type == "Crowning")
		self.assertTrue(moves[0].type == "Crowning")
		self.assertTrue(isinstance(moves[0].piece, Rook))

	def test_white_crowning_attack(self):
		'''Test white crowning attack.'''
		board = Board(10, 10)
		pawn = Pawn(board.white)
		rook = Rook(board.black)

		i, j = 1, 1
		board[i, j].piece = pawn
		board[0, 0].piece = rook

		moves = pawn.get_moves((i, j), board)

		self.assertEquals(2, len(moves))
		self.assertTrue(Crowning((1,1),(1,0),Queen(board.white)) in moves)
		self.assertTrue(Crowning((1,1),(0,0),Queen(board.white)) in moves)

class KnightTest(unittest.TestCase):
	'''Class for testing the knight's moves'''

	def test_basic_knight_moves(self):
		'''Test a knight's basic moves'''
		board = Board(10, 10)
		knight = Knight(board.white)

		i, j = 3, 4
		board[i, j].piece = knight

		moves = knight.get_moves((i, j), board)

		self.assertEquals(len(moves), 8)

	def test_knight_near_edge(self):
		'''Test a knight's moves when near an edge of the board'''
		board = Board(10, 10)
		knight = Knight(board.white)

		i, j = 0, 4
		board[i, j].piece = knight

		moves = knight.get_moves((i, j), board)

		self.assertEquals(len(moves), 4)

class EqualityTest(unittest.TestCase):
	'''Test for move equality'''

	def test_move_equality(self):
		'''Test Move instance equality'''
		m1 = Move((0, 0), (1, 1))
		m2 = Move((0, 0), (1, 1))
		self.assertEquals(m1, m2)

	def test_castling_equality(self):
		'''Test Castling instance equality'''
		board = Board()
		m1 = Castling("left", board.white)
		m2 = Castling("left", board.white)
		self.assertEquals(m1, m2)
	
	def test_crowning(self):
		'''Test Crowning instance equality'''
		board = Board()
		queen = Queen(board.white)
		m1 = Crowning((0,1),(0,0), queen)
		m2 = Crowning((0,1),(0,0), queen)
		self.assertEquals(m1, m2)

class MovementTest(unittest.TestCase):
	'''Test piece movements'''

	def test_move_instance(self):
		'''Test Move instance's data storage'''
		fro = 1,2
		to = 3,4
		m = Move(fro, to)

		self.assertEquals(m.fro, fro)
		self.assertEquals(m.to, to)

	def test_queen_horizontal_move_blocked(self):
		'''Test the Queen's horizontal move when blocked by a piece from the
		same owner.'''
		board = Board(10, 10)
		queen = Queen(board.white)
		pawn = Pawn(board.white)

		i, j = 0, 4
		board[i, j].piece = queen
		board[i+2, j].piece = pawn

		moves = queen.get_moves((i, j), board)

		self.assertTrue(Move((i,j), (i-1,j)) not in moves)
		self.assertTrue(Move((i,j), (i+1,j)) in moves)
		self.assertTrue(Move((i,j), (i+2,j)) not in moves)
		self.assertTrue(Move((i,j), (i+3,j)) not in moves)

	def test_queen_moves(self):
		'''Test the rook's moves unblocked'''
		board = Board(10, 10)
		queen = Queen(board.white)

		i, j = 4, 4
		board[i, j].piece = queen
		moves = queen.get_moves((i, j), board)

		self.assertTrue(Move((i,j),(i,0)) in moves)
		self.assertTrue(Move((i,j),(i,7)) in moves)
		self.assertTrue(Move((i,j),(0,j)) in moves)
		self.assertTrue(Move((i,j),(7,j)) in moves)
		self.assertTrue(Move((i,j),(i+1,j+1)) in moves)
		self.assertTrue(Move((i,j),(i+1,j-1)) in moves)
		self.assertTrue(Move((i,j),(i-1,j+1)) in moves)
		self.assertTrue(Move((i,j),(i-1,j-1)) in moves)

	def test_rook_moves(self):
		'''Test the rook's moves unblocked'''
		board = Board(10, 10)
		rook = Rook(board.white)

		i, j = 4, 4
		board[i, j].piece = rook
		moves = rook.get_moves((i, j), board)

		self.assertTrue(Move((i,j),(i,0)) in moves)
		self.assertTrue(Move((i,j),(i,7)) in moves)
		self.assertTrue(Move((i,j),(0,j)) in moves)
		self.assertTrue(Move((i,j),(7,j)) in moves)

class CastlingTest(unittest.TestCase):
	'''Test the king's castling move.'''

	def test_castling_no_moves(self):
		'''Test castling when neither the king nor the rook's have moved.'''
		board = Board(10, 10)
		king = King(board.white)
		rook_l = Rook(board.white)
		rook_r = Rook(board.white)

		board[0, 7].piece = rook_l
		board[7, 7].piece = rook_r
		board[4, 7].piece = king

		moves = king.get_moves((4, 7), board)

		self.assertTrue(Castling("left",board.white) in moves)
		self.assertTrue(Castling("right",board.white) in moves)

	def test_castling_forbidden_king_moved(self):
		'''Test that castling is forbidden once the king moved.'''
		board = Board(10, 10)
		king = King(board.white)
		rook_l = Rook(board.white)
		rook_r = Rook(board.white)

		board[0, 7].piece = rook_l
		board[7, 7].piece = rook_r
		board[4, 7].piece = king

		moves = king.get_moves((4, 7), board)
		move = Move((4,7),(5,7))
		self.assertTrue(move in moves)
		move.perform(board)

		moves = king.get_moves((4, 7), board)
		self.assertFalse(Castling("left",board.white) in moves)
		self.assertFalse(Castling("right",board.white) in moves)

	def test_castling_forbidden_rook_moved(self):
		'''Test that castling is forbidden once the rook moved.'''
		board = Board(10, 10)
		king = King(board.white)
		rook_l = Rook(board.white)
		rook_r = Rook(board.white)

		board[0, 7].piece = rook_l
		board[7, 7].piece = rook_r
		board[4, 7].piece = king

		moves = king.get_moves((4, 7), board)
		self.assertTrue(Castling("left",board.white) in moves)
		self.assertTrue(Castling("right",board.white) in moves)

		move = Move((0,7),(1,7))
		self.assertTrue(move in rook_l.get_moves((0,7),board))
		move.perform(board)

		moves = king.get_moves((4, 7), board)
		self.assertFalse(Castling("left",board.white) in moves)
		self.assertTrue(Castling("right",board.white) in moves)

		move = Move((7,7),(6,7))
		self.assertTrue(move in rook_r.get_moves((7,7),board))
		move.perform(board)

		moves = king.get_moves((4, 7), board)
		self.assertFalse(Castling("left",board.white) in moves)
		self.assertFalse(Castling("right",board.white) in moves)

	def test_castling_permitted_after_undo(self):
		board = Board(10, 10)
		king = King(board.white)
		rook_l = Rook(board.white)
		rook_r = Rook(board.white)

		board[0, 7].piece = rook_l
		board[7, 7].piece = rook_r
		board[4, 7].piece = king

		moves = king.get_moves((4, 7), board)
		self.assertTrue(Castling("left",board.white) in moves)
		self.assertTrue(Castling("right",board.white) in moves)

		move = Move((0,7),(1,7))
		self.assertTrue(move in rook_l.get_moves((0,7),board))
		move.perform(board)
		move.undo(board)

		moves = king.get_moves((4, 7), board)
		self.assertTrue(Castling("left",board.white) in moves)
		self.assertTrue(Castling("right",board.white) in moves)

class PerformUndoMoveTest(unittest.TestCase):
	'''Test performing moves and undoing them on a board.'''

	def test_move_perform_undo(self):
		'''Test performing a move and then undoing it.'''
		#set up:
		board = Board(10, 10)

		pawn = Pawn(board.white)
		i, j = 0, 6
		board[i, j].piece = pawn

		move = pawn.get_moves((i, j), board)[0]

		#initial conditions:
		self.assertTrue(not board[i, j-1].piece)
		self.assertTrue(board[i, j].piece)

		#test performing the move:
		move.perform(board)
		self.assertTrue(board[i, j-1].piece)
		self.assertTrue(not board[i, j].piece)

		self.assertTrue(not move.dst_piece)
		self.assertTrue(move.performed)

		#test undoing the move:
		move.undo(board)
		self.assertTrue(not board[i, j-1].piece)
		self.assertTrue(board[i, j].piece)
		self.assertTrue(not move.performed)

	def test_castling_white_left(self):
		'''Test performing a castling move to the left and then undoing it.'''
		board = Board(10, 10)
		board.current_turn = board.white
		king = King(board.white)
		rook = Rook(board.white)

		ki, ri, j = 4, 0, 7

		board[ki, j].piece = king
		board[ri, j].piece = rook

		#Preconditions:
		self.assertTrue(board[ki, j].piece)
		self.assertTrue(board[ri, j].piece)
		self.assertTrue(not board[ki-1, j].piece)
		self.assertTrue(not board[ki-2, j].piece)

		#Perform castling:
		move = Castling("left", board.white)
		move.perform(board)

		#Postconditions after castling
		self.assertTrue(not board[ki, j].piece)
		self.assertTrue(not board[ri, j].piece)
		self.assertTrue(board[ki-1, j].piece)
		self.assertTrue(board[ki-2, j].piece)

		#Undo castling:
		move.undo(board)

		#Undo postconditions:
		self.assertTrue(board[ki, j].piece)
		self.assertTrue(board[ri, j].piece)
		self.assertTrue(not board[ki-1, j].piece)
		self.assertTrue(not board[ki-2, j].piece)

	def test_castling_white_right(self):
		'''Test performing a castling move to the right and then undoing it.'''
		board = Board(10, 10)
		board.current_turn = board.white
		king = King(board.white)
		rook = Rook(board.white)

		ki, ri, j = 4, 7, 7

		board[ki, j].piece = king
		board[ri, j].piece = rook

		#Preconditions:
		self.assertTrue(board[ki, j].piece)
		self.assertTrue(board[ri, j].piece)
		self.assertTrue(not board[ki+1, j].piece)
		self.assertTrue(not board[ki+2, j].piece)

		#Perform castling:
		move = Castling("right", board.white)
		move.perform(board)

		#Postconditions after castling
		self.assertTrue(not board[ki, j].piece)
		self.assertTrue(not board[ri, j].piece)
		self.assertTrue(board[ki+1, j].piece)
		self.assertTrue(board[ki+2, j].piece)

		#Undo castling:
		move.undo(board)

		#Undo postconditions:
		self.assertTrue(board[ki, j].piece)
		self.assertTrue(board[ri, j].piece)
		self.assertTrue(not board[ki+1, j].piece)
		self.assertTrue(not board[ki+2, j].piece)

	def test_crowning_perform_undo(self):
		'''Test performing a crowning move and then undoing it'''
		#init:
		board = Board(10, 10)
		pawn = Pawn(board.white)

		i, j = 0, 1
		board[i, j].piece = pawn
		src_type = str(pawn).split(" ")[0].replace("<", "")

		move = filter(lambda x: x.type == "Crowning", pawn.get_moves((i, j), board))[0]

		#Preconditions:
		self.assertTrue(not board[i, j-1].piece)
		self.assertTrue(board[i, j].piece)

		move.perform(board)

		self.assertTrue(board[i, j-1].piece)
		self.assertTrue(not board[i, j].piece)
		self.assertTrue(move.performed)

		#compare piece types (should differ if crowning succeeded):
		dst_type = str(board[i, j-1].piece).split(" ")[0].replace("<","")
		self.assertTrue(src_type != dst_type)

		#undo move:
		move.undo(board)
		self.assertTrue(not board[i, j-1].piece)
		self.assertTrue(board[i, j].piece)
		self.assertTrue(not move.performed)

		#compare piece types (should be both pawns):
		dst_type = str(board[i, j].piece).split(" ")[0].replace("<","")
		self.assertTrue(src_type == dst_type)

if __name__ == "__main__":
	unittest.main()