def test_king(): piece = King(Color.BLACK, Position(x=3, y=3)) assert piece.can_move_to(Position(x=4, y=3)) assert piece.can_move_to(Position(x=3, y=4)) assert piece.can_move_to(Position(x=2, y=2)) assert piece.can_move_to(Position(x=4, y=4)) assert piece.can_move_to(Position(x=2, y=4)) # Queen assert not piece.can_move_to(Position(x=7, y=3)) assert not piece.can_move_to(Position(x=3, y=7)) assert not piece.can_move_to(Position(x=1, y=1)) assert not piece.can_move_to(Position(x=5, y=5)) assert not piece.can_move_to(Position(x=1, y=5)) # Knight assert not piece.can_move_to(Position(x=4, y=5)) assert not piece.can_move_to(Position(x=2, y=5)) assert not piece.can_move_to(Position(x=4, y=1)) assert not piece.can_move_to(Position(x=2, y=1)) assert not piece.can_move_to(Position(x=1, y=2)) assert not piece.can_move_to(Position(x=1, y=4)) assert not piece.can_move_to(Position(x=5, y=2)) assert not piece.can_move_to(Position(x=5, y=4)) assert not piece.was_move piece.move_to(Position(x=4, y=3)) assert piece.was_move # Castling piece = King(Color.BLACK, Position(x=4, y=7)) assert piece.can_move_to(Position(x=6, y=7)) assert piece.can_move_to(Position(x=2, y=7)) assert not piece.can_move_to(Position(x=7, y=7)) assert not piece.can_move_to(Position(x=1, y=7))
def test_stalemate(): # Opposite col test omitted as player color doesn't effect this test. __ = BlankPiece() wk = King("white") r = Rook("black") q = Queen("black") bk = King("black") # 0 1 2 3 4 5 6 7 board = [ [__, __, __, __, __, __, __, __], # 0 [__, __, __, __, __, __, __, __], # 1 [__, __, __, __, __, __, __, __], # 2 [__, r, __, __, __, __, __, __], # 3 [__, __, __, __, __, __, __, bk], # 4 [__, __, __, __, __, __, __, __], # 5 [__, __, __, __, q, __, __, __], # 6 [wk, __, __, __, __, __, __, __] # 7 ] assert_false("White should NOT be a check state", is_being_checked(board, "white")) assert_true("White should be in stalemate", is_stalemate(board, "white", [])) assert_false("Black should NOT be in stalemate state", is_stalemate(board, "black", []))
def test_raise_exception_when_player_doesnt_get_out_of_check(self): board = Board({ 'e1': King('black'), 'f8': Rook('white'), 'a1': King('white') }) self.assertIsNone(board.check) board.move('f8', 'e8') self.assertEqual('black', board.check) self.assertRaises(ImpossibleMove, board.move, 'e1', 'e2')
def create(): b = [[ Rook("black"), Knight("black"), Bishop("black"), Queen("black"), King("black"), Bishop("black"), Knight("black"), Rook("black") ], [ Pawn("black"), Pawn("black"), Pawn("black"), Pawn("black"), Pawn("black"), Pawn("black"), Pawn("black"), Pawn("black") ]] for i in range(2, 6): b.append([ BlankPiece(), BlankPiece(), BlankPiece(), BlankPiece(), BlankPiece(), BlankPiece(), BlankPiece(), BlankPiece() ]) b.append([ Pawn("white"), Pawn("white"), Pawn("white"), Pawn("white"), Pawn("white"), Pawn("white"), Pawn("white"), Pawn("white") ]) b.append([ Rook("white"), Knight("white"), Bishop("white"), Queen("white"), King("white"), Bishop("white"), Knight("white"), Rook("white") ]) return b
def test_player_should_to_get_out_of_check(): board = Board({ 'e1': King('black'), 'f8': Rook('white'), 'a1': King('white') }) assert board.check is None board.move('f8', 'e8') assert board.check == 'black' with pytest.raises(ImpossibleMove): board.move('e1', 'e2')
def test_check_if_player_is_not_in_check_anymore(self): board = Board({ 'e1': King('black'), 'f8': Rook('white'), 'a1': King('white') }) self.assertIsNone(board.check) board.move('f8', 'e8') self.assertEqual('black', board.check) board.move('e1', 'f1') self.assertIsNone(board.check)
def test_king_can_moves(): king = King('white') # # I have to pass a second king because the board look for a second # king when you set the first one. Maybe a have to add an attribute # to the board class to say wether the match is tutorial or not. # board = Board(initial_pieces={'f5': king, 'h1': King('black')}) board.move('f5', 'e5') assert board.get_piece('e5') is king assert board.get_piece('f5') is None
def test_players_can_get_out_of_check(): board = Board({ 'e1': King('black'), 'f8': Rook('white'), 'a1': King('white') }) assert board.check is None board.move('f8', 'e8') assert board.check == 'black' board.move('e1', 'f1') assert board.check is None
def test_fail_castling_when_king_already_moved(): board = Board(initial_pieces={ 'a8': King('black'), 'a7': Pawn('black'), 'e1': King('white'), 'h1': Rook('white')}) board.move('e1', 'f1') # king moves board.move('a7', 'a6') # pawn moves board.move('f1', 'e1') # king moves back board.move('a6', 'a5') # pawn moves again with pytest.raises(ImpossibleMove): board.move('e1', 'g1')
def test_fail_castling_when_king_already_moved(self): board = Board( initial_pieces={ 'a8': King('black'), 'a7': Pawn('black'), 'e1': King('white'), 'h1': Rook('white') }) board.move('e1', 'f1') board.move('a7', 'a6') # pawn moves board.move('f1', 'e1') board.move('a6', 'a5') # pawn moves self.assertRaises(ImpossibleMove, board.move, 'e1', 'g1')
def test_cozio_s_mate(): """""" game = Game([ [Empty() for _ in range(8)], [Empty() for _ in range(8)], [Empty() for _ in range(8)], [Empty() for _ in range(8)], [Empty() for _ in range(6)] + [Pawn('black'), Empty()], [Empty() for _ in range(5)] + [Queen('black'), King('black'), Empty()], [Empty() for _ in range(7)] + [Queen('white')], [Empty() for _ in range(6)] + [King('white'), Empty()], ]) assert game.is_check('black'), print(game) assert game.is_checkmate('black'), print(game)
def test_fail_castling_when_some_piece_is_between_king_rook(self): board = Board(initial_pieces={ 'e1': King('white'), 'f1': Queen('white'), 'h1': Rook('white') }) self.assertRaises(ImpossibleMove, board.move, 'e1', 'g1')
def test_box_mate(): """""" game = Game([ [Tower('white'), Empty(), Empty()] + [King('black')] + [Empty() for _ in range(4)], [Empty() for _ in range(8)], [Empty() for _ in range(3)] + [King('white')] + [Empty() for _ in range(4)], [Empty() for _ in range(8)], [Empty() for _ in range(8)], [Empty() for _ in range(8)], [Empty() for _ in range(8)], [Empty() for _ in range(8)], ]) assert game.is_check('black'), print(game) assert game.is_checkmate('black'), print(game)
def test_anderssen_s_mate(): """""" game = Game([ [Empty() for _ in range(6)] + [King('black'), Tower('white')], [Empty() for _ in range(6)] + [Pawn('white'), Empty()], [Empty() for _ in range(5)] + [King('white'), Empty(), Empty()], [Empty() for _ in range(8)], [Empty() for _ in range(8)], [Empty() for _ in range(8)], [Empty() for _ in range(8)], [Empty() for _ in range(8)], ]) assert game.is_check('black'), print(game) assert game.is_checkmate('black'), print(game)
def test_anastasia_s_mate(): """""" game = Game([ [Empty() for _ in range(8)], [Empty() for _ in range(4)] + [Horse('white'), Empty(), Pawn('black'), King('black')], [Empty() for _ in range(8)], [Empty() for _ in range(7)] + [Tower('white')], [Empty() for _ in range(8)], [Empty() for _ in range(8)], [Empty() for _ in range(8)], [King('white')] + [Empty() for _ in range(7)], ]) assert game.is_check('black'), print(game) assert game.is_checkmate('black'), print(game)
def test_bishop_and_knight_mate(): """""" game = Game([ [Empty() for _ in range(7)] + [King('black')], [Empty() for _ in range(8)], [Empty() for _ in range(5)] + [Bishop('white'), King('white'), Horse('white')], [Empty() for _ in range(8)], [Empty() for _ in range(8)], [Empty() for _ in range(8)], [Empty() for _ in range(8)], [Empty() for _ in range(8)], ]) assert game.is_check('black'), print(game) assert game.is_checkmate('black'), print(game)
def test_damiano_s_bishop_mate(): """""" game = Game([ [Empty() for _ in range(5)] + [King('black'), Empty(), Empty()], [Empty() for _ in range(5)] + [Queen('white'), Empty(), Empty()], [Empty() for _ in range(6)] + [Bishop('white'), Empty()], [Empty() for _ in range(8)], [Empty() for _ in range(8)], [Empty() for _ in range(8)], [Empty() for _ in range(8)], [Empty() for _ in range(7)] + [King('white')], ]) assert game.is_check('black'), print(game) assert game.is_checkmate('black'), print(game)
def __init__(self, initial_pieces=None): self.check = None self.__turn = 'white' self.__squares = {} self.__initial_pieces = initial_pieces or { 'a1': Rook('white'), 'b1': Knight('white'), 'c1': Bishop('white'), 'd1': Queen('white'), 'e1': King('white'), 'f1': Bishop('white'), 'g1': Knight('white'), 'h1': Rook('white'), 'a2': Pawn('white'), 'b2': Pawn('white'), 'c2': Pawn('white'), 'd2': Pawn('white'), 'e2': Pawn('white'), 'f2': Pawn('white'), 'g2': Pawn('white'), 'h2': Pawn('white'), 'a8': Rook('black'), 'b8': Knight('black'), 'c8': Bishop('black'), 'd8': Queen('black'), 'e8': King('black'), 'f8': Bishop('black'), 'g8': Knight('black'), 'h8': Rook('black'), 'a7': Pawn('black'), 'b7': Pawn('black'), 'c7': Pawn('black'), 'd7': Pawn('black'), 'e7': Pawn('black'), 'f7': Pawn('black'), 'g7': Pawn('black'), 'h7': Pawn('black'), } for _x in x: for _y in y: self.__squares[_x + _y] = None if _x + _y in self.__initial_pieces: self.__squares[_x + _y] = self.__initial_pieces[_x + _y]
def test_king_can_do_castling_to_left(): board = Board(initial_pieces={'e1': King('white'), 'a1': Rook('white')}) king = board.get_piece('e1') rook = board.get_piece('a1') board.move('e1', 'c1') assert board.get_piece('c1') == king assert board.get_piece('d1') == rook assert board.get_piece('e1') is None assert board.get_piece('a1') is None
def test_conducting_col_h_castle_move(): board = get_castling_board() col_h_castle = Move(MoveType.CASTLING, [7, 4], [7, 6]) board, move_history_element = conduct_move(board, col_h_castle, "white") _ = BlankPiece() expected_row = [Rook("white"), _, _, _, _, Rook("white"), King("white"), _] actual_row = board[7] assert_row_contain_same_type_elements(expected_row, actual_row)
def __init__(self): self.classic = ([ Rook.Rook(0, "White", 0, 0), Knight.Knight(1, "White", 1, 0), Bishop.Bishop(2, "White", 2, 0), Queen.Queen(3, "White", 3, 0), King.King(4, "White", 4, 0), Bishop.Bishop(5, "White", 5, 0), Knight.Knight(6, "White", 6, 0), Rook.Rook(7, "White", 7, 0), Pawn.Pawn(8, "White", 0, 1), Pawn.Pawn(9, "White", 1, 1), Pawn.Pawn(10, "White", 2, 1), Pawn.Pawn(11, "White", 3, 1), Pawn.Pawn(12, "White", 4, 1), Pawn.Pawn(13, "White", 5, 1), Pawn.Pawn(14, "White", 6, 1), Pawn.Pawn(15, "White", 7, 1) ], [ Pawn.Pawn(16, "Black", 0, 6), Pawn.Pawn(17, "Black", 1, 6), Pawn.Pawn(18, "Black", 2, 6), Pawn.Pawn(19, "Black", 3, 6), Pawn.Pawn(20, "Black", 4, 6), Pawn.Pawn(21, "Black", 5, 6), Pawn.Pawn(22, "Black", 6, 6), Pawn.Pawn(23, "Black", 7, 6), Rook.Rook(24, "Black", 0, 7), Knight.Knight(25, "Black", 1, 7), Bishop.Bishop(26, "Black", 2, 7), King.King(27, "Black", 4, 7), Queen.Queen(28, "Black", 3, 7), Bishop.Bishop(29, "Black", 5, 7), Knight.Knight(30, "Black", 6, 7), Rook.Rook(31, "Black", 7, 7) ]) self.table = Table.Table(self.classic[0], self.classic[1]) self.shift = "White" self.player1 = ("Player 1", "White") self.player2 = ("Player 2", "Black") self.winner = None self.finish = False
def get_fifty_move_rule_board(): """ Helper function to run through the first 3 moves in a "four move rule" (use a smaller deque for sanity here -- 50 is a lot), """ # conducted_move_history = deque([], 4) __ = BlankPiece() wk = King("white") wp = Pawn("white") br = Rook("black") bk = King("black") # 0 1 2 3 4 5 6 7 board = [ [__, __, __, bk, __, __, __, __], # 0 [__, __, __, __, __, __, __, __], # 1 [__, __, __, __, __, __, __, __], # 2 [__, br, __, __, __, __, __, __], # 3 [__, __, __, __, __, __, __, __], # 4 [__, __, __, __, __, __, __, __], # 5 [__, wp, __, __, __, __, __, __], # 6 [__, __, __, __, wk, __, __, __] # 7 ] msg = "Should NOT be 50-move rule draw" board, move_history_element = conduct_move( board, Move(MoveType.NORMAL, [7, 4], [7, 1]), "white") conducted_move_history.append(move_history_element) assert_false(msg, is_fifty_move_rule_draw(conducted_move_history)) board, move_history_element = conduct_move( board, Move(MoveType.NORMAL, [7, 1], [7, 2]), "white") conducted_move_history.append(move_history_element) assert_false(msg, is_fifty_move_rule_draw(conducted_move_history)) board, move_history_element = conduct_move( board, Move(MoveType.NORMAL, [7, 2], [7, 3]), "white") conducted_move_history.append(move_history_element) assert_false(msg, is_fifty_move_rule_draw(conducted_move_history)) return board, conducted_move_history
def back_row(self, color): return [ Tower(color, self), Horse(color, self), Bishop(color, self), King(color, self), Queen(color, self), Bishop(color, self), Horse(color, self), Tower(color, self), ]
def test_pieces_can_capture_opponent_pieces(): board = Board(initial_pieces={ 'a8': King('black'), 'e5': Pawn('black'), 'f3': Knight('white') }) assert board.pieces_quantity() == 3 knight = board.get_piece('f3') board.move('f3', 'e5') assert board.get_piece('e5') is knight assert board.pieces_quantity() == 2
def test_castling_left_white(self): board = Board(initial_pieces={ 'e1': King('white'), 'a1': Rook('white') }) king = board.squares['e1'] rook = board.squares['a1'] board.move('e1', 'c1') self.assertIs(king, board.squares['c1']) self.assertIs(rook, board.squares['d1']) self.assertIsNone(board.squares['e1']) self.assertIsNone(board.squares['a1'])
def test_serialization(): __ = BlankPiece() wp = Pawn("white") bp = Pawn("black") br = Rook("black") bh = Knight("black") bb = Bishop("black") bq = Queen("black") bk = King("black") # 0 1 2 3 4 5 6 7 board = [ [br, bh, bb, bq, bk, __, __, __], # 0 [bp, __, __, __, __, __, __, __], # 1 [__, __, __, __, __, __, __, __], # 2 [__, __, __, __, __, __, __, __], # 3 [__, __, __, __, __, __, __, __], # 4 [__, __, __, __, __, __, __, __], # 5 [wp, __, __, __, __, __, __, __], # 6 [__, __, __, __, __, __, __, __] # 7 ] serializable_gameboard = convert_to_string_gameboard(board) expected_gameboard = [ ['br', 'bh', 'bb', 'bq', 'bk', '_', '_', '_'], ['bp', '_', '_', '_', '_', '_', '_', '_'], ['_', '_', '_', '_', '_', '_', '_', '_'], ['_', '_', '_', '_', '_', '_', '_', '_'], ['_', '_', '_', '_', '_', '_', '_', '_'], ['_', '_', '_', '_', '_', '_', '_', '_'], ['wp', '_', '_', '_', '_', '_', '_', '_'], ['_', '_', '_', '_', '_', '_', '_', '_'] ] assert_length(serializable_gameboard, 8) assert_length(serializable_gameboard[3], 8) if expected_gameboard != serializable_gameboard: raise AssertionError("Expected gameboard" + str(expected_gameboard) + " but was " + str(serializable_gameboard)) new_board = convert_from_string_gameboard(expected_gameboard) for i in range(0, len(board)): for j in range(0, len(board[0])): expected_piece = board[i][j] actual_piece = new_board[i][j] if expected_piece.type != actual_piece.type: raise AssertionError("Expected piece type at " + str(i) + ", " + str(j) + " was " + expected_piece.type + ", but got " + actual_piece.type) if expected_piece.col != actual_piece.col: raise AssertionError("Expected piece col at " + str(i) + ", " + str( j) + " was " + expected_piece.col + " but got " + actual_piece.col)
def test_castling_left_black(self): board = Board(initial_pieces={ 'a2': Pawn('white'), 'e8': King('black'), 'a8': Rook('black') }) king = board.squares['e8'] rook = board.squares['a8'] board.move('a2', 'a3') # just because white have to move first board.move('e8', 'c8') self.assertIs(king, board.squares['c8']) self.assertIs(rook, board.squares['d8']) self.assertIsNone(board.squares['e8']) self.assertIsNone(board.squares['a8'])
def test_blackburne_s_mate(): """""" game = Game([ [Empty() for _ in range(5)] + [Tower('black'), King('black'), Empty()], [Empty() for _ in range(7)] + [Bishop('white')], [Empty() for _ in range(8)], [Empty() for _ in range(6)] + [Horse('white'), Empty()], [Empty() for _ in range(8)], [Empty() for _ in range(8)], [Empty(), Bishop('white')] + [Empty() for _ in range(6)], [Empty() for _ in range(8)], ]) assert game.is_check('black'), print(game) assert game.is_checkmate('black'), print(game)
def test_arabian_mate(): """""" game = Game([ [Empty() for _ in range(7)] + [King('black')], [Empty() for _ in range(7)] + [Tower('white')], [Empty() for _ in range(5)] + [Horse('white'), Empty(), Empty()], [Empty() for _ in range(8)], [Empty() for _ in range(8)], [Empty() for _ in range(8)], [Empty() for _ in range(8)], [Empty() for _ in range(8)], ]) assert game.is_check('black'), print(game) assert game.is_checkmate('black'), print(game)
def test_black_swine_mate(): """""" game = Game([ [Empty() for _ in range(5)] + [Tower('black'), King('black'), Empty()], [Empty() for _ in range(6)] + [Tower('white'), Tower('white')], [Empty() for _ in range(8)], [Empty() for _ in range(8)], [Empty() for _ in range(8)], [Empty() for _ in range(8)], [Empty() for _ in range(8)], [Empty() for _ in range(8)], ]) assert game.is_check('black'), print(game) assert game.is_checkmate('black'), print(game)