def test_get_next_move_returns_correct_value(self):
     ai = AdvancedAI(ChessGame())
     move = ai.get_next_move()
     self.assertEqual(move, (0, 1, 2, 2))
     ai = AdvancedAI(ChessGame())
     ai.game.update((0, 1), (2, 2), "Queen", is_ai=True)
     move = ai.get_next_move()
     self.assertEqual(move, (6, 0, 5, 0))
Example #2
0
    def test_get_next_move_returns_correct_value(self):
        simple_ai = SimpleAI(ChessGame())
        move = simple_ai.get_next_move()
        self.assertEqual(move, (0, 1, 2, 2))

        simple_ai = SimpleAI(ChessGame())
        simple_ai.game.update((0, 1), (2, 2), "Queen")
        move = simple_ai.get_next_move()
        self.assertEqual(move, (6, 2, 5, 2))
Example #3
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}
 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)
Example #5
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)]
Example #6
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)
Example #7
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)
Example #8
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)
Example #9
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] = Bishop(self.chess_game, Color.WHITE, 0,
                                          0)
     self.chess_game.board[4][4] = Bishop(self.chess_game, Color.BLACK, 4,
                                          4)
     self.chess_game.board[3][3] = Bishop(self.chess_game, Color.BLACK, 3,
                                          3)
Example #10
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] = Knight(self.chess_game, Color.WHITE, 0,
                                          0)
     self.chess_game.board[0][2] = Knight(self.chess_game, Color.WHITE, 0,
                                          2)
     self.chess_game.board[1][0] = Knight(self.chess_game, Color.BLACK, 1,
                                          0)
     self.chess_game.board[1][2] = Knight(self.chess_game, Color.BLACK, 1,
                                          2)
Example #11
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)
Example #12
0
    def __init__(self, db_url="init"):
        """
        Set up database using db_url. Connect to the database, fetch data and create dict sessions where key is
        session_id and value is chess (game instance, username).
        :param db_url: if db_url is "init", the db_url is
            'postgresql+psycopg2://postgres:[email protected]:5432'
        Otherwise, db_url can be the value of the parameter passed in
        """
        if db_url == "init":
            db_url = 'postgresql+psycopg2://postgres:[email protected]:5432'
        self.engine = create_engine(db_url)
        meta = MetaData()

        self.current_game = Table(
            'game', meta, Column('username', String),
            Column('session_id', Integer, primary_key=True),
            Column('fen', String), Column('status', String),
            Column('mode', String), Column('start_time', DateTime),
            Column('last_update', DateTime))

        self.history = Table('history', meta,
                             Column('session_id', Integer, primary_key=True),
                             Column('step', Integer, primary_key=True),
                             Column('src', String), Column('tar', String),
                             Column('fen', String))

        self.users = Table('users', meta,
                           Column('username', String, primary_key=True),
                           Column('password', String),
                           Column('total_hours', Float),
                           Column('score', Integer))

        meta.create_all(self.engine)

        self.sessions = {}
        self.modes = {}
        conn = self.engine.connect()
        sessions_info = conn.execute(self.current_game.select())
        for session in sessions_info:
            session_id = session["session_id"]
            fen = session["fen"]
            game = ChessGame(fen)
            self.sessions[session_id] = (game, session["username"])
            self.modes[session_id] = session["mode"]

        for session_id in self.sessions:
            session_history = conn.execute(self.history.select().where(
                self.history.c.session_id == session_id).order_by(
                    self.history.c.step)).all()
            game = self.sessions[session_id][0]
            game.init_history(session_history)
        conn.close()
    def test_load_fen_restores_correct_moves_and_clock(self):
        game = ChessGame()
        game.load_fen("8/8/8/8/8/8/8/8 w KQkq - 0 1")
        self.assertEqual(game.half_move_clock, 0)
        self.assertEqual(game.full_move_clock, 1)
        self.assertEqual(game.count, 0)

        game.load_fen("8/8/8/8/8/8/8/8 b KQkq - 10 15")
        self.assertEqual(game.half_move_clock, 10)
        self.assertEqual(game.full_move_clock, 15)
        self.assertEqual(game.count, 29)

        game.load_fen("8/8/8/8/8/8/8/8 w - - 20 14")
        self.assertEqual(game.half_move_clock, 20)
        self.assertEqual(game.full_move_clock, 14)
        self.assertEqual(game.count, 26)
    def test_load_fen_gives_correct_board(self):
        game = ChessGame()
        game.load_fen("8/8/8/8/8/8/8/8 w KQkq - 0 1")
        for row in range(8):
            for col in range(8):
                self.assertEqual(type(game.board[row][col]), Empty)

        game.load_fen("8/8/1r1R4/8/8/8/8/8 w KQkq - 0 1")
        self.assertEqual(type(game.board[5][1]), Rook)
        self.assertEqual(game.board[5][1].color, Color.BLACK)

        self.assertEqual(type(game.board[5][3]), Rook)
        self.assertEqual(game.board[5][3].color, Color.WHITE)

        game.load_fen("8/8/8/8/8/8/8/k7 w KQkq - 0 1")
        self.assertEqual(type(game.board[0][0]), King)
        self.assertEqual(game.board[0][0].color, Color.BLACK)
Example #15
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[3][3] = Empty(self.chess_game, Color.EMPTY, 3, 3)
 def test_game_evaluation_returns_correct_value(self):
     ai = AdvancedAI(ChessGame())
     ai.game.fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
     self.assertEqual(ai.game_evaluation(Color.WHITE),
                      ai.game_evaluation(Color.BLACK))
     self.assertEqual(ai.game_evaluation(Color.WHITE), 0)
 def setUp(self) -> None:
     self.game = ChessGame(fen="start")
Example #18
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] = Queen(self.chess_game, Color.WHITE, 0, 0)
     self.chess_game.board[3][3] = Queen(self.chess_game, Color.BLACK, 3, 3)
     self.chess_game.board[1][3] = King(self.chess_game, Color.BLACK, 1, 3)