class TestGetFen(unittest.TestCase):
    def setUp(self) -> None:
        self.game = ChessGame(fen="start")

    def test_get_fen_start(self):
        self.assertEqual(self.game.get_fen(),
                         "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1")

    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")
示例#2
0
 def create_game(self, username: str, mode: str) -> dict:
     """
     Create a new chess game in put it in the dictionary, store the data in the database.
     :param username: username of the user playing the game
     :param mode: the mode of the game. "easy": easy AI game; "advanced": advanced AI game; "pvp": player vs player
     :return: A dict where only has one key "session_id"
     """
     session_id = self.generate_unique_session_id()
     game = ChessGame()
     self.sessions[session_id] = (game, username)
     self.modes[session_id] = mode
     time = datetime.datetime.now()
     fen = game.get_fen()
     status = game.check_game_status()
     conn = self.engine.connect()
     conn.execute(
         self.current_game.insert(), {
             "username": username,
             "session_id": session_id,
             "status": status,
             "fen": fen,
             "mode": mode,
             "start_time": time,
             "last_update": time
         })
     conn.close()
     return {"session_id": session_id, "valid": True}