def test_captured_black(self): expected_result = [ Piece(c.PieceType.pawn, c.Color.black, order=7), Piece(c.PieceType.pawn, c.Color.black, order=5), ] self.assertEqual(self.game.captured_black, expected_result)
def test_less_equals(self): p1 = Piece(c.PieceType.queen, c.Color.black) p2 = Piece(c.PieceType.king, c.Color.white) self.assertLessEqual(p1, p2) p1 = Piece(c.PieceType.king, c.Color.white) p2 = Piece(c.PieceType.king, c.Color.white) self.assertLessEqual(p1, p2)
def test_greater_equals(self): p1 = Piece(c.PieceType.king, c.Color.black) p2 = Piece(c.PieceType.queen, c.Color.white) self.assertGreaterEqual(p1, p2) p1 = Piece(c.PieceType.king, c.Color.white) p2 = Piece(c.PieceType.king, c.Color.white) self.assertGreaterEqual(p1, p2)
def test_not_equals(self): mv = Move(Piece(c.PieceType.pawn, c.Color.black), Square('b2'), Square('b3')) expected_result = Move(Piece(c.PieceType.pawn, c.Color.black), Square('a2'), Square('a4')) self.assertNotEqual(mv, expected_result)
def test_code(self): expected_result = ['p', 'n', 'b', 'r', 'q', 'k'] pieces = [Piece(p, c.Color.black) for p in c.PieceType] codes = [p.code for p in pieces] self.assertEqual(codes, expected_result)
def test_move_hint_2(self): b = Board() b.move(Square('e2'), Square('e4')) b.move(Square('d7'), Square('d5')) expected_hints = { Square('d1'): [ (Square('e2'), None), (Square('f3'), None), (Square('g4'), None), (Square('h5'), None), ], Square('e4'): [ ( Square('d5'), Piece(c.PieceType.pawn, c.Color.black, 3), ), (Square('e5'), None), ], Square('c8'): [ (Square('d7'), None), (Square('e6'), None), (Square('f5'), None), (Square('g4'), None), (Square('h3'), None), ], } for s, hints in expected_hints.items(): self.assertEqual(b.move_hint(s), hints)
def test_name(self): expected_result = ['pawn', 'knight', 'bishop', 'rook', 'queen', 'king'] pieces = [Piece(p, c.Color.black) for p in c.PieceType] names = [p.name for p in pieces] self.assertEqual(names, expected_result)
def test_worth(self): expected_result = [1, 3, 3, 5, 9, 10] pieces = [Piece(p, c.Color.black) for p in c.PieceType] worths = [p.worth for p in pieces] self.assertEqual(worths, expected_result)
def test_nb_pieces(self): expected_result = [8, 2, 2, 2, 1, 1] pieces = [Piece(p, c.Color.black) for p in c.PieceType] nb_pieces = [p.nb_pieces for p in pieces] self.assertEqual(nb_pieces, expected_result)
def test_reset(self): b = Board() # First move some pieces around b.move(Square('e2'), Square('e4')) b.move(Square('e7'), Square('e5')) expected_piece = Piece( c.PieceType.pawn, c.Color.white, order=4, ) self.assertEqual( b.get_piece(Square('e4')), expected_piece, ) expected_piece = Piece( c.PieceType.pawn, c.Color.black, order=4, ) self.assertEqual( b.get_piece(Square('e5')), expected_piece, ) # Now let's reset the board b.reset() for square, piece in b.data.items(): if b.is_empty(square): continue expected_piece = self.data[square] self.assertEqual(piece, expected_piece)
def test_order(self): nb_pieces = { c.PieceType.pawn: 8, c.PieceType.knight: 2, c.PieceType.bishop: 2, c.PieceType.rook: 2, c.PieceType.queen: 1, c.PieceType.king: 1, } for piece_type in nb_pieces.keys(): pieces = [] orders = [] for i in range(nb_pieces[piece_type]): pieces.append(Piece(piece_type, c.Color.black, order=i)) orders = [p.order for p in pieces] expected_result = list(range(nb_pieces[piece_type])) self.assertEqual(orders, expected_result)
def test_hash(self): import itertools piece_data = [d for d in itertools.product(c.PieceType, c.Color)] expected_results = [ (d[0].value * 100) + (d[1].value * 10) + (0) # default order for d in piece_data ] pieces = [Piece(piece_type=d[0], color=d[1]) for d in piece_data] for piece, expected_result in zip(pieces, expected_results): try: self.assertEqual(hash(piece), expected_result) except AssertionError: print(f'{piece} != {expected_result}') raise
def test_sort_pieces(self): import itertools pieces = sorted([ Piece(pd[0], pd[1]) for pd in itertools.product(c.PieceType, c.Color) ]) expected_result = [ '<Piece(black pawn 0)>', '<Piece(white pawn 0)>', '<Piece(black knight 0)>', '<Piece(white knight 0)>', '<Piece(black bishop 0)>', '<Piece(white bishop 0)>', '<Piece(black rook 0)>', '<Piece(white rook 0)>', '<Piece(black queen 0)>', '<Piece(white queen 0)>', '<Piece(black king 0)>', '<Piece(white king 0)>', ] self.assertEqual([repr(p) for p in pieces], expected_result)
def setUp(self): self.data = { # Rooks Square('a1'): Piece(c.PieceType.rook, c.Color.white, order=0), Square('h1'): Piece(c.PieceType.rook, c.Color.white, order=1), Square('a8'): Piece(c.PieceType.rook, c.Color.black, order=0), Square('h8'): Piece(c.PieceType.rook, c.Color.black, order=1), # Kinghts Square('b1'): Piece(c.PieceType.knight, c.Color.white, order=0), Square('g1'): Piece(c.PieceType.knight, c.Color.white, order=1), Square('b8'): Piece(c.PieceType.knight, c.Color.black, order=0), Square('g8'): Piece(c.PieceType.knight, c.Color.black, order=1), # Bishops Square('c1'): Piece(c.PieceType.bishop, c.Color.white, order=0), Square('f1'): Piece(c.PieceType.bishop, c.Color.white, order=1), Square('c8'): Piece(c.PieceType.bishop, c.Color.black, order=0), Square('f8'): Piece(c.PieceType.bishop, c.Color.black, order=1), # Queens Square('d1'): Piece(c.PieceType.queen, c.Color.white), Square('d8'): Piece(c.PieceType.queen, c.Color.black), # Kings Square('e1'): Piece(c.PieceType.king, c.Color.white), Square('e8'): Piece(c.PieceType.king, c.Color.black), # White Pawns Square('a2'): Piece(c.PieceType.pawn, c.Color.white, order=0), Square('b2'): Piece(c.PieceType.pawn, c.Color.white, order=1), Square('c2'): Piece(c.PieceType.pawn, c.Color.white, order=2), Square('d2'): Piece(c.PieceType.pawn, c.Color.white, order=3), Square('e2'): Piece(c.PieceType.pawn, c.Color.white, order=4), Square('f2'): Piece(c.PieceType.pawn, c.Color.white, order=5), Square('g2'): Piece(c.PieceType.pawn, c.Color.white, order=6), Square('h2'): Piece(c.PieceType.pawn, c.Color.white, order=7), # Black Pawns Square('a7'): Piece(c.PieceType.pawn, c.Color.black, order=0), Square('b7'): Piece(c.PieceType.pawn, c.Color.black, order=1), Square('c7'): Piece(c.PieceType.pawn, c.Color.black, order=2), Square('d7'): Piece(c.PieceType.pawn, c.Color.black, order=3), Square('e7'): Piece(c.PieceType.pawn, c.Color.black, order=4), Square('f7'): Piece(c.PieceType.pawn, c.Color.black, order=5), Square('g7'): Piece(c.PieceType.pawn, c.Color.black, order=6), Square('h7'): Piece(c.PieceType.pawn, c.Color.black, order=7), } self.reverse = {v: k for k, v in self.data.items()}
def test_color_code(self): p = Piece(c.PieceType.pawn, color=c.Color.black) self.assertEqual(p.color_code, 'b') p = Piece(c.PieceType.pawn, color=c.Color.white) self.assertEqual(p.color_code, 'w')
def test_pieces_checking_black(self): expected_result = [Piece(c.PieceType.queen, c.Color.white)] self.assertEqual(self.game.pieces_checking_black, expected_result)
def test_equals(self): p1 = Piece(c.PieceType.pawn, c.Color.black) p2 = Piece(c.PieceType.pawn, c.Color.black) self.assertTrue(p1 is not p2) self.assertEqual(p1, p2)
def test_not_equals(self): p1 = Piece(c.PieceType.rook, c.Color.black) p2 = Piece(c.PieceType.rook, c.Color.black, order=1) self.assertNotEqual(p1, p2)
def setUpClass(cls): # Some short hands for fitting one move in one line p = c.PieceType.pawn b = c.PieceType.bishop r = c.PieceType.rook q = c.PieceType.queen wt = c.Color.white bl = c.Color.black s = Square cls.expected_move_history = [] # A simple gameplay (even a noob would not play so bad ;) cls.game = Game() # Illegal Move - White rook trying to move diagonally cls.game.move(('a1h8', None)) _check_win(cls.game) # Move 1 - White opening with d4 cls.game.move(('d2d4', None)) _check_win(cls.game) cls.expected_move_history.append( Move(Piece(p, wt, 3), s('d2'), s('d4')) ) # Move 2 - Black opening with c6 cls.game.move(('c7c6', None)) _check_win(cls.game) cls.expected_move_history.append( Move(Piece(p, bl, 2), s('c7'), s('c6')) ) # Illegal Move - Trying to capture white pawn at d4 by white queen cls.game.move(('d1d4', None)) _check_win(cls.game) # Move 3 - White Queen to d3 cls.game.move(('d1d3', None)) _check_win(cls.game) cls.expected_move_history.append( Move(Piece(q, wt, 0), s('d1'), s('d3')) ) # Illegal Move - Queen trying to jump over white pawn at d4 # nothing will happen cls.game.move(('d3d5', None)) _check_win(cls.game) # Move 4 - Black moves pawn a6 cls.game.move(('a7a6', None)) _check_win(cls.game) cls.expected_move_history.append( Move(Piece(p, bl, 0), s('a7'), s('a6')) ) # Move 5 - Black pawn at h7 captured by the white queen cls.game.move(('d3h7', None)) _check_win(cls.game) cls.expected_move_history.append( Move(Piece(q, wt, 0), s('d3'), s('h7')) ) # Move 6 - Black pawn to a5 cls.game.move(('a6a5', None)) _check_win(cls.game) cls.expected_move_history.append( Move(Piece(p, bl, 0), s('a6'), s('a5')) ) # Move 7 - White queen to f5 cls.game.move(('h7f5', None)) _check_win(cls.game) cls.expected_move_history.append( Move(Piece(q, wt, 0), s('h7'), s('f5')) ) # Move 8 - Black rook to h6, where it comes under attack # by the black bishop at c1 cls.game.move(('h8h6', None)) _check_win(cls.game) cls.expected_move_history.append( Move(Piece(r, bl, 1), s('h8'), s('h6')) ) # Move 9 - White pawn to e4 cls.game.move(('e2e4', None)) _check_win(cls.game) cls.expected_move_history.append( Move(Piece(p, wt, 4), s('e2'), s('e4')) ) # Move 10 - Black pawn to a4 cls.game.move(('a5a4', None)) _check_win(cls.game) cls.expected_move_history.append( Move(Piece(p, bl, 0), s('a5'), s('a4')) ) # Move 11 - White rook to c4 cls.game.move(('f1c4', None)) _check_win(cls.game) cls.expected_move_history.append( Move(Piece(b, wt, 1), s('f1'), s('c4')) ) # Move 12 - White rook to h5 attacking the queen cls.game.move(('h6h5', None)) _check_win(cls.game) cls.expected_move_history.append( Move(Piece(r, bl, 1), s('h6'), s('h5')) ) # Move 13 - White queen to f7, it's a mate! cls.game.move(('f5f7', None)) _check_win(cls.game) cls.expected_move_history.append( Move(Piece(q, wt, 0), s('f5'), s('f7')) )
def test_color(self): p = Piece(c.PieceType.pawn, color=c.Color.black) self.assertEqual(p.color, c.Color.black) p = Piece(c.PieceType.pawn, color=c.Color.white) self.assertEqual(p.color, c.Color.white)
def test_capturables(self): expected_result = { c.Color.white: { Piece(c.PieceType.king, c.Color.black): [ Piece(c.PieceType.queen, c.Color.white) ], Piece(c.PieceType.rook, c.Color.black, 1): [ Piece(c.PieceType.pawn, c.Color.white, 7) ] }, c.Color.black: { Piece(c.PieceType.queen, c.Color.white): [ Piece(c.PieceType.pawn, c.Color.black, 4), Piece(c.PieceType.king, c.Color.black), Piece(c.PieceType.bishop, c.Color.black, 1), Piece(c.PieceType.pawn, c.Color.black, 6), Piece(c.PieceType.knight, c.Color.black, 1), Piece(c.PieceType.rook, c.Color.black, 1), ] } } self.assertEqual(self.game.capturables, expected_result)
def test_type(self): pieces = [Piece(p, c.Color.black) for p in c.PieceType] types = [p.type for p in pieces] self.assertEqual(types, [t for t in c.PieceType])