Ejemplo n.º 1
0
    def test_update_castle(self):
        self.game.half_move_clock = 0
        self.game.full_move_clock = 10
        self.game.count = 20
        self.game.empty_cell = Empty(None, Color.EMPTY, -1, -1)
        self.game.history = [{"fen": "r3k3/8/8/8/Pp6/8/8/4K2R b Kq a3 0 10",
                              "movement": {"src": "a2", "tar": "a4"}}]
        self.game.turn = Color.BLACK
        for row in range(8):
            for col in range(8):
                self.game.board[row][col] = self.game.empty_cell
        self.game.board[7][4] = King(self.game, Color.BLACK, 7, 4)
        self.game.board[7][4].firstMove = True
        self.game.board[7][0] = Rook(self.game, Color.BLACK, 7, 0)
        self.game.board[7][0].firstMove = True
        self.game.board[0][4] = King(self.game, Color.WHITE, 0, 4)
        self.game.board[0][4].firstMove = True
        self.game.board[0][7] = Rook(self.game, Color.WHITE, 0, 7)
        self.game.board[0][7].firstMove = True
        self.game.board[3][0] = Pawn(self.game, Color.WHITE, 3, 0)
        self.game.board[3][1] = Pawn(self.game, Color.BLACK, 3, 1)
        expected_1 = self.game.board[7][0]
        expected_2 = self.game.board[0][7]

        self.game.update_castle((7, 4), (7, 2))
        self.assertEqual(self.game.board[7][3], expected_1)

        self.game.update_castle((0, 4), (0, 6))
        self.assertEqual(self.game.board[0][5], expected_2)
Ejemplo n.º 2
0
 def setUp(self) -> None:
     fen = "8/8/8/8/8/8/8/8 w KQkq - 0 1"
     self.chess_game = ChessGame(fen=fen)  # empty board
     self.chess_game.board[4][4] = King(self.chess_game, Color.WHITE, 4, 4)
     self.chess_game.board[4][1] = King(self.chess_game, Color.BLACK, 4, 1)
     self.chess_game.kings_coordinate = [(4, 4), (4, 1)]
     self.chess_game.board[3][3] = Rook(self.chess_game, Color.BLACK, 3, 3)
     self.chess_game.board[3][2] = Rook(self.chess_game, Color.WHITE, 3, 2)
Ejemplo n.º 3
0
 def setUp(self) -> None:
     fen = "8/8/8/8/8/8/8/8 w KQkq - 0 1"
     self.chess_game = ChessGame(fen=fen)  # empty board
     self.chess_game.board[0][4] = King(self.chess_game, Color.WHITE, 0, 4)
     self.chess_game.board[0][7] = Rook(self.chess_game, Color.WHITE, 0, 7)
     self.chess_game.board[0][0] = Rook(self.chess_game, Color.WHITE, 0, 0)
     self.chess_game.board[7][4] = King(self.chess_game, Color.BLACK, 7, 4)
     self.chess_game.board[7][0] = Rook(self.chess_game, Color.BLACK, 7, 0)
Ejemplo n.º 4
0
 def setUp(self) -> None:
     fen = "8/8/8/8/8/8/8/8 w KQkq - 0 1"
     self.chess_game = ChessGame(fen=fen)  # empty board
     self.chess_game.board[0][0] = Rook(self.chess_game, Color.WHITE, 0, 0)
     self.chess_game.board[2][3] = Rook(self.chess_game, Color.WHITE, 2, 3)
     self.chess_game.board[0][3] = King(self.chess_game, Color.WHITE, 0, 3)
     self.chess_game.board[3][3] = Rook(self.chess_game, Color.BLACK, 3, 3)
     self.chess_game.kings_coordinate = [(0, 3), (None, None)]
Ejemplo n.º 5
0
 def test_get_checked_moves_include_castling(self):
     fen = "8/8/8/8/8/8/8/8 w KQkq - 0 1"
     chess_game2 = ChessGame(fen=fen)
     chess_game2.board[0][4] = King(chess_game2, Color.WHITE, 0, 4)
     chess_game2.board[7][4] = King(chess_game2, Color.BLACK, 7, 4)
     chess_game2.board[0][7] = Rook(chess_game2, Color.WHITE, 0, 7)
     chess_game2.board[0][0] = Rook(chess_game2, Color.WHITE, 0, 0)
     actual = chess_game2.board[0][4].get_checked_moves()
     expected = {
         'moves': [(1, 4), (1, 5), (0, 5), (0, 3), (1, 3), (0, 6), (0, 2)]
     }
     self.assertEqual(actual, expected)
Ejemplo n.º 6
0
    def test_get_value_returns_correct_value(self):
        simple_ai = SimpleAI(ChessGame())
        self.assertEqual(simple_ai.get_value(simple_ai.game.board), -39)

        simple_ai.game.board[6][0] = simple_ai.game.empty_cell
        self.assertEqual(simple_ai.get_value(simple_ai.game.board), -38)

        simple_ai.game.board[6][0] = Rook(simple_ai.game, Color.BLACK, 6, 0)
        self.assertEqual(simple_ai.get_value(simple_ai.game.board), -43)
Ejemplo n.º 7
0
 def test_initial_start(self):
     self.assertEqual(self.game.turn, Color.WHITE)
     self.assertEqual(type(self.game.empty_cell), Empty)
     self.assertEqual(self.game.history, [])
     self.assertEqual(self.game.count, 0)
     self.assertEqual(self.game.half_move_clock, 0)
     self.assertEqual(self.game.full_move_clock, 1)
     self.assertEqual(self.game.en_passant_target_notation, "-")
     for col in range(8):
         self.game.board[1][col] = Pawn(self.game, Color.WHITE, 1, col)
         self.game.board[6][col] = Pawn(self.game, Color.BLACK, 6, col)
     for row, color in [(0, Color.WHITE), (7, Color.BLACK)]:
         self.game.board[row][0] = Rook(self.game, color, row, 0)
         self.game.board[row][7] = Rook(self.game, color, row, 7)
         self.game.board[row][1] = Knight(self.game, color, row, 1)
         self.game.board[row][6] = Knight(self.game, color, row, 6)
         self.game.board[row][2] = Bishop(self.game, color, row, 2)
         self.game.board[row][5] = Bishop(self.game, color, row, 5)
         self.game.board[row][3] = Queen(self.game, color, row, 3)
         self.game.board[row][4] = King(self.game, color, row, 4)
Ejemplo n.º 8
0
 def update_promotion(self, src: tuple, tar: tuple, role: str) -> None:
     """
     If the Pawn reached the button, then it would be promoted as promotion piece which passed through argument.
     :param: src: tuple, tar: tuple
     :return: None
     """
     src_row, src_col = src
     src_piece = self.board[src_row][src_col]
     tar_row, tar_col = tar
     if type(self.board[src_row][src_col]) == Pawn and tar_row in [0, 7]:
         color = self.board[src_row][src_col].color
         if role == "Rook":
             piece = Rook(self, color, tar_row, tar_row)
             piece.firstMove = False
         elif role == "Knight":
             piece = Knight(self, color, tar_row, tar_row)
         elif role == "Queen":
             piece = Queen(self, color, tar_row, tar_row)
         else:
             piece = Bishop(self, color, tar_row, tar_row)
         self.board[src_row][src_col] = self.empty_cell
         # noinspection PyTypeChecker
         self.board[tar_row][tar_col] = piece
         self.switch_turn()
     else:
         self.board[tar_row][tar_col] = src_piece
         src_piece.update_coordinate(tar_row, tar_col)
         self.board[src_row][src_col] = self.empty_cell
         if type(src_piece) == King:
             if self.turn == Color.WHITE:
                 self.kings_coordinate[0] = (tar_row, tar_col)
             else:
                 self.kings_coordinate[1] = (tar_row, tar_col)
         self.switch_turn()
     return None
Ejemplo n.º 9
0
 def test_is_being_checked_after_move_during_game_false(self):
     empty = Empty(None, Color.EMPTY, -1, -1)
     for row in range(8):
         for col in range(8):
             self.game.board[row][col] = empty
     self.game.board[0][4] = King(self.game, Color.WHITE, 0, 4)
     self.game.board[7][3] = King(self.game, Color.BLACK, 7, 3)
     self.game.kings_coordinate = [(0, 4), (7, 3)]
     self.game.board[1][0] = Pawn(self.game, Color.BLACK, 1, 0)
     self.game.board[4][5] = Pawn(self.game, Color.WHITE, 4, 5)
     self.game.board[4][1] = Rook(self.game, Color.WHITE, 4, 1)
     self.game.board[4][2] = Bishop(self.game, Color.BLACK, 4, 2)
     self.assertFalse(self.game.is_being_checked_after_move((4, 2), (2, 4)))
Ejemplo n.º 10
0
 def test_is_being_checked_during_game_true(self):
     empty = Empty(None, Color.EMPTY, -1, -1)
     for row in range(8):
         for col in range(8):
             self.game.board[row][col] = empty
     self.game.board[0][0] = King(self.game, Color.WHITE, 0, 0)
     self.game.board[7][3] = King(self.game, Color.BLACK, 7, 3)
     self.game.kings_coordinate = [(0, 0), (7, 3)]
     self.game.board[1][0] = Pawn(self.game, Color.BLACK, 1, 0)
     self.game.board[4][5] = Pawn(self.game, Color.WHITE, 4, 5)
     self.game.board[4][1] = Rook(self.game, Color.WHITE, 4, 1)
     self.game.board[3][3] = Bishop(self.game, Color.BLACK, 3, 3)
     self.assertTrue(self.game.is_being_checked())
Ejemplo n.º 11
0
 def test_check_game_status_with_check_continue(self):
     empty = Empty(None, Color.EMPTY, -1, -1)
     for row in range(8):
         for col in range(8):
             self.game.board[row][col] = empty
     self.game.board[0][0] = King(self.game, Color.WHITE, 0, 0)
     self.game.board[7][3] = King(self.game, Color.BLACK, 7, 3)
     self.game.kings_coordinate = [(0, 0), (7, 3)]
     self.game.board[1][0] = Pawn(self.game, Color.BLACK, 1, 0)
     self.game.board[4][5] = Pawn(self.game, Color.WHITE, 4, 5)
     self.game.board[4][1] = Rook(self.game, Color.WHITE, 4, 1)
     self.game.board[3][3] = Bishop(self.game, Color.BLACK, 3, 3)
     self.assertEqual(self.game.check_game_status(), "Continue")
Ejemplo n.º 12
0
 def test_check_game_status_half_move_draw(self):
     empty = Empty(None, Color.EMPTY, -1, -1)
     for row in range(8):
         for col in range(8):
             self.game.board[row][col] = empty
     self.game.board[0][0] = King(self.game, Color.WHITE, 0, 0)
     self.game.board[7][3] = King(self.game, Color.BLACK, 7, 3)
     self.game.kings_coordinate = [(0, 0), (7, 3)]
     self.game.board[1][0] = Pawn(self.game, Color.BLACK, 1, 0)
     self.game.board[3][5] = Pawn(self.game, Color.WHITE, 3, 5)
     self.game.board[4][1] = Rook(self.game, Color.WHITE, 4, 1)
     self.game.board[2][0] = Bishop(self.game, Color.BLACK, 2, 0)
     self.game.board[2][1] = Bishop(self.game, Color.BLACK, 2, 1)
     self.game.half_move_clock = 50
     self.assertEqual(self.game.check_game_status(), "Draw")
Ejemplo n.º 13
0
    def test_get_checked_moves_checkmate2(self):
        self.game.board[0][3] = King(self.game, Color.WHITE, 0, 3)
        self.game.board[7][4] = King(self.game, Color.BLACK, 7, 4)
        self.game.board[1][2] = Pawn(self.game, Color.BLACK, 1, 2)
        self.game.board[4][1] = Rook(self.game, Color.WHITE, 4, 1)
        self.game.board[3][4] = Bishop(self.game, Color.BLACK, 3, 4)
        self.game.turn = Color.WHITE

        checked_moves = self.game.get_checked_moves((0, 3))
        moves = checked_moves["moves"]
        for move in moves:
            self.assertTrue(move in ["c1", "d2", "e1", "e2"])

        checked_moves = self.game.get_checked_moves((4, 1))
        moves = checked_moves["moves"]
        for move in moves:
            self.assertTrue(move in [])
Ejemplo n.º 14
0
 def test_get_fen_during_game(self):
     empty = Empty(None, Color.EMPTY, -1, -1)
     for row in range(8):
         for col in range(8):
             self.game.board[row][col] = empty
     self.game.board[0][0] = King(self.game, Color.WHITE, 0, 0)
     self.game.board[7][3] = King(self.game, Color.BLACK, 7, 3)
     self.game.board[1][0] = Pawn(self.game, Color.BLACK, 1, 0)
     self.game.board[4][5] = Pawn(self.game, Color.WHITE, 4, 5)
     self.game.board[4][1] = Rook(self.game, Color.WHITE, 4, 1)
     self.game.board[3][3] = Bishop(self.game, Color.BLACK, 3, 3)
     self.game.history = [{"src": (0, 4)}, {"src": (7, 4)}]
     self.game.en_passant_target_notation = "-"
     self.game.half_move_clock = 0
     self.game.full_move_clock = 20
     self.assertEqual(self.game.get_fen(),
                      "3k4/8/8/1R3P2/3b4/8/p7/K7 w - - 0 20")
Ejemplo n.º 15
0
    def test_update_movement_clock(self):
        self.game.update_movement_clock((1, 0), (3, 0))
        self.assertEqual(self.game.count, 1)
        self.assertEqual(self.game.full_move_clock, 1)
        self.assertEqual(self.game.half_move_clock, 0)

        self.game.update_movement_clock((6, 0), (4, 0))
        self.assertEqual(self.game.count, 2)
        self.assertEqual(self.game.full_move_clock, 2)
        self.assertEqual(self.game.half_move_clock, 0)

        self.game.update_movement_clock((0, 6), (2, 5))
        self.assertEqual(self.game.count, 3)
        self.assertEqual(self.game.full_move_clock, 2)
        self.assertEqual(self.game.half_move_clock, 1)

        self.game.update_movement_clock((6, 3), (5, 3))
        self.assertEqual(self.game.count, 4)
        self.assertEqual(self.game.full_move_clock, 3)
        self.assertEqual(self.game.half_move_clock, 0)

        self.game.update_movement_clock((0, 1), (2, 2))
        self.assertEqual(self.game.count, 5)
        self.assertEqual(self.game.full_move_clock, 3)
        self.assertEqual(self.game.half_move_clock, 1)

        self.game.update_movement_clock((7, 0), (5, 0))
        self.game.update_movement_clock((1, 4), (3, 4))
        self.game.update_movement_clock((7, 1), (5, 2))
        self.assertEqual(self.game.count, 8)
        self.assertEqual(self.game.full_move_clock, 5)
        self.assertEqual(self.game.half_move_clock, 1)

        self.game.board[5][0] = Rook(self, Color.BLACK, 5, 0)
        self.game.update_movement_clock((0, 5), (5, 0))
        self.assertEqual(self.game.count, 9)
        self.assertEqual(self.game.full_move_clock, 5)
        self.assertEqual(self.game.half_move_clock, 0)
Ejemplo n.º 16
0
    def test_update_implement_promotion(self):
        self.game.half_move_clock = 0
        self.game.full_move_clock = 10
        self.game.count = 20
        self.game.empty_cell = Empty(None, Color.EMPTY, -1, -1)
        self.game.history = [{"fen": "3bk3/PP6/8/8/8/8/6pp/4KB2 w - - 0 10",
                              "movement": {"src": "a6", "tar": "a7"}}]
        self.game.turn = Color.WHITE
        for row in range(8):
            for col in range(8):
                self.game.board[row][col] = self.game.empty_cell
        self.game.board[7][4] = King(self.game, Color.BLACK, 7, 4)
        self.game.board[7][3] = Bishop(self.game, Color.BLACK, 7, 3)
        self.game.board[7][4].firstMove = True
        self.game.board[0][4] = King(self.game, Color.WHITE, 0, 4)
        self.game.board[0][5] = Rook(self.game, Color.WHITE, 0, 5)
        self.game.board[0][4].firstMove = True
        self.game.board[6][0] = Pawn(self.game, Color.WHITE, 6, 0)
        self.game.board[1][7] = Pawn(self.game, Color.BLACK, 1, 7)
        self.game.board[6][1] = Pawn(self.game, Color.WHITE, 6, 1)
        self.game.board[1][6] = Pawn(self.game, Color.BLACK, 1, 6)

        self.game.update_promotion((6, 0), (7, 0), "Queen")
        self.assertEqual(type(self.game.board[7][0]), Queen)
        self.assertEqual(self.game.board[7][0].color, Color.WHITE)

        self.game.update_promotion((1, 7), (0, 7), "Rook")
        self.assertEqual(type(self.game.board[0][7]), Rook)
        self.assertEqual(self.game.board[0][7].color, Color.BLACK)

        self.game.update_promotion((6, 1), (7, 1), "Bishop")
        self.assertEqual(type(self.game.board[7][1]), Bishop)
        self.assertEqual(self.game.board[7][0].color, Color.WHITE)

        self.game.update_promotion((1, 6), (0, 6), "Knight")
        self.assertEqual(type(self.game.board[0][6]), Knight)
        self.assertEqual(self.game.board[0][6].color, Color.BLACK)
Ejemplo n.º 17
0
 def test_castling_being_checked(self):
     king = self.chess_game.board[0][4]
     self.chess_game.kings_coordinate = [(0, 4), (7, 4)]
     self.chess_game.board[6][4] = Rook(self.chess_game, Color.BLACK, 6, 4)
     self.assertFalse(king.castling(king_side=True))
Ejemplo n.º 18
0
 def test_into_checked(self):
     king = self.chess_game.board[0][4]
     self.chess_game.board[7][3] = Rook(self.chess_game, Color.BLACK, 7, 3)
     self.assertTrue(king.into_checked(king_side=False))