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)
def __init__(self, fen="start"): """ Initial game default values. :param fen: Fen string notation """ self.board = [] self.movement = [] self.history = [] self.fen = fen self.count = 0 self.half_move_clock = 0 self.full_move_clock = 1 self.en_passant_target_notation = "-" self.castling_notation = "KQkq" self.passed_fen = fen self.empty_cell = Empty(None, Color.EMPTY, -1, -1) self.kings_coordinate = [(None, None), (None, None)] self.piece_mapping = { "P": Pawn, "R": Rook, "N": Knight, "B": Bishop, "Q": Queen, "K": King } for row in range(8): row_list = [] for col in range(8): row_list.append(self.empty_cell) self.board.append(row_list) self.turn = Color.WHITE self.load_fen(fen)
def setUp(self) -> None: self.game = ChessGame(fen="start") self.game.half_move_clock = 20 self.game.full_move_clock = 100 self.game.count = 200 for row in range(8): for col in range(8): self.game.board[row][col] = Empty(None, Color.EMPTY, row, col)
def test_check_game_status_without_check_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[2][0] = Bishop(self.game, Color.BLACK, 2, 0) self.game.board[2][1] = Bishop(self.game, Color.BLACK, 2, 1) self.assertEqual(self.game.check_game_status(), "Draw")
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")
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)))
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())
def test_update_ended_without_continue(self): self.game.half_move_clock = 49 self.game.full_move_clock = 100 self.game.count = 200 self.game.en_passant_target_notation = "-" self.game.castling_notation = "-" self.game.turn = Color.WHITE 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[7][3] = King(self.game, Color.BLACK, 7, 3) self.game.board[0][5] = King(self.game, Color.WHITE, 0, 5) self.assertTrue(self.game.update((0, 5), (0, 4), "Queen")) self.assertFalse(self.game.update((7, 3), (7, 2), "Queen"))
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")
def test_update_implement_en_passant(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": "3q4/8/8/8/Pp6/8/8/5K2 b - a3 0 10", "movement": {"src": "a2", "tar": "a4"}}] self.game.castling_notation = "-" 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][3] = King(self.game, Color.BLACK, 7, 3) self.game.board[0][5] = King(self.game, Color.WHITE, 0, 5) self.game.board[3][0] = Pawn(self.game, Color.WHITE, 3, 0) self.game.board[3][1] = Pawn(self.game, Color.BLACK, 3, 1) self.game.update_en_passant((3, 1), (2, 0)) self.assertEqual(self.game.board[3][0], self.game.empty_cell)
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)
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[3][3] = Empty(self.chess_game, Color.EMPTY, 3, 3)
def test_rook_has_moved_not_exist(self): self.chess_game.board[0][7] = Empty(self.chess_game, Color.EMPTY, 0, 7) self.assertTrue( self.chess_game.board[0][4].rook_has_moved(king_side=True))