コード例 #1
0
    def _value_eval(self, game: ReversiGame, piece: str) -> Union[float, int]:
        """The evaluation function for minimax. For Positional Player, the evaluation function
        will evaluate the positional advantage of the pieces on the board.

        Preconditions:
            - piece in {BLACK, WHITE}

        :param game: the current game state for evaluation
        :return: value evaluated from the current game state
        """
        if game.get_winner() is not None:
            if game.get_winner() == piece:  # win
                return math.inf
            elif game.get_winner() == 'Draw':  # draw
                return 0
            else:  # lose
                return -math.inf
        else:
            num_black, num_white = game.get_num_pieces(
            )[BLACK], game.get_num_pieces()[WHITE]
            corner_black, corner_white = check_corners(game)
            board_filled = (num_black + num_white) / (game.get_size()**2)

            if piece == BLACK:
                if board_filled < 0.80:  # early to middle game
                    return 10 * (corner_black - corner_white) + len(
                        game.get_valid_moves())
                else:  # end game
                    return num_black / num_white
            else:
                if board_filled < 0.80:  # early to middle game
                    return 10 * (corner_white - corner_black) + len(
                        game.get_valid_moves())
                else:  # end game
                    return num_white / num_black
コード例 #2
0
def test_players(player1: Player, player2: Player, iterations: int) -> None:
    """
    test_players is a function that runs <iterations> number of games between player1
    and player2
    """
    white = 0
    black = 0
    ties = 0
    for _ in range(iterations):
        game = ReversiGame()
        prev_move = (-1, -1)
        while game.get_winner() is None:
            move = player1.make_move(game, prev_move)
            game.try_make_move(move)
            if game.get_winner() is None:
                prev_move = player2.make_move(game, move)
                game.try_make_move(prev_move)
        if game.get_winner() == 'white':
            print('White WINS')
            white += 1
        elif game.get_winner() == 'black':
            print('Black WINS')
            black += 1
        else:
            print('TIE')
            ties += 1
    print("Player 1 Wins: " + str(black))
    print("Player 2 Wins: " + str(white))
コード例 #3
0
def _run_ai_simulation(game_surface: pygame.Surface, size: int,
                       player1: Union[MobilityTreePlayer, PositionalTreePlayer, RandomPlayer,
                                      ReversiGame, MCTSTimeSavingPlayer],
                       player2: Union[MobilityTreePlayer, PositionalTreePlayer, RandomPlayer,
                                      ReversiGame, MCTSTimeSavingPlayer]) -> None:
    if size == 8:
        background = pygame.image.load('assets/gameboard8.png')
    elif size == 6:
        background = pygame.image.load('assets/gameboard6.png')
    else:
        raise ValueError("invalid size.")
    game_surface.blit(background, (0, 0))
    pygame.display.flip()
    game = ReversiGame(size)
    previous_move = '*'
    board = game.get_game_board()
    _draw_game_state(game_surface, background, size, board)
    pass_move = pygame.image.load('assets/pass.png')
    player1_side = BLACK
    while game.get_winner() is None:
        if previous_move == '*' or game.get_current_player() == player1_side:
            move = player1.make_move(game, previous_move)
        else:
            move = player2.make_move(game, previous_move)
        previous_move = move
        game.make_move(move)
        if move == 'pass':
            surface = game_surface
            game_surface.blit(pass_move, (300, 300))
            pygame.display.flip()
            pygame.time.wait(500)
            game_surface.blit(surface, (0, 0))
            pygame.display.flip()
        else:
            board = game.get_game_board()
            _draw_game_state(game_surface, background, size, board)
        pygame.time.wait(500)
    winner = game.get_winner()
    if winner == BLACK:
        victory = pygame.image.load('assets/player1_victory.png')
        game_surface.blit(victory, (300, 300))
        pygame.display.flip()
        pygame.time.wait(3000)
        return
    elif winner == WHITE:
        defeat = pygame.image.load('assets/player2_victory.png')
        game_surface.blit(defeat, (300, 300))
        pygame.display.flip()
        pygame.time.wait(3000)
        return
    else:
        draw = pygame.image.load('assets/draw.png')
        game_surface.blit(draw, (300, 300))
        pygame.display.flip()
        pygame.time.wait(3000)
        return
コード例 #4
0
def run_game(black: Player,
             white: Player,
             size: int,
             verbose: bool = False) -> tuple[str, list[str]]:
    """Run a Reversi game between the two given players.
    Return the winner and list of moves made in the game.
    """
    game = ReversiGame(size)

    move_sequence = []
    previous_move = None
    timer = {BLACK: 0, WHITE: 0}
    current_player = black

    if verbose:
        game.print_game()

    while game.get_winner() is None:
        t0 = time.time()  # record time before player make move
        previous_move = current_player.make_move(game, previous_move)
        t = time.time()  # record time after player make move

        game.make_move(previous_move)
        move_sequence.append(previous_move)
        if verbose:
            if current_player is black:
                print(f'{BLACK} moved {previous_move}. Used {t - t0:.2f}s')
            else:
                print(f'{WHITE} moved {previous_move}. Used {t - t0:.2f}s')
            game.print_game()

        if current_player is black:
            timer[BLACK] += t - t0
            current_player = white
        else:
            timer[WHITE] += t - t0
            current_player = black

    # print winner
    if verbose:
        print(f'Winner: {game.get_winner()}')
        print(
            f'{BLACK}: {game.get_num_pieces()[BLACK]}, {WHITE}: {game.get_num_pieces()[WHITE]}'
        )
        print(
            f'{BLACK} used {timer[BLACK]:.2f}s, {WHITE} used {timer[WHITE]:.2f}s'
        )

    return game.get_winner(), move_sequence
コード例 #5
0
    def _minimax(self, root_move: tuple[int, int], depth: int,
                 game: ReversiGame, alpha: float, beta: float) -> GameTree:
        """
        _minimax is a minimax function with alpha-beta pruning implemented that returns
        a full GameTree where each node stores the given evaluation

        Preconditions
            - depth >= 0
        """
        white_move = (game.get_current_player() == -1)
        ret = GameTree(move=root_move, is_white_move=white_move)
        # early return at max depth
        if depth == self.depth:
            ret.evaluation = heuristic(game, self.heuristic_list)
            return ret
        possible_moves = list(game.get_valid_moves())
        if not possible_moves:
            if game.get_winner() == 'white':
                ret.evaluation = 10000
            elif game.get_winner() == 'black':
                ret.evaluation = -10000
            else:
                ret.evaluation = 0
            return ret
        random.shuffle(possible_moves)
        best_value = float('-inf')
        if not white_move:
            best_value = float('inf')
        for move in possible_moves:
            new_game = game.copy_and_make_move(move)
            new_tree = self._minimax(move, depth + 1, new_game, alpha, beta)
            ret.add_subtree(new_tree)
            # we update the alpha value when the maximizer is playing (white)
            if white_move and best_value < new_tree.evaluation:
                best_value = new_tree.evaluation
                alpha = max(alpha, best_value)
                if beta <= alpha:
                    break
            # we update the beta value when the minimizer is playing (black)
            elif not white_move and best_value > new_tree.evaluation:
                best_value = new_tree.evaluation
                beta = min(beta, best_value)
                if beta <= alpha:
                    break
        ret.evaluation = best_value
        return ret
コード例 #6
0
 def _minimax(self, root_move: tuple[int, int], game: ReversiGame,
              depth: int) -> GameTree:
     """
     _minimax is a function that returns a tree where each node has a value determined by
     the minimax search algorithm
     """
     white_move = (game.get_current_player() == -1)
     ret = GameTree(move=root_move, is_white_move=white_move)
     # early return if we have reached max depth
     if depth == self.depth:
         ret.evaluation = heuristic(game, self.heuristic_list)
         return ret
     possible_moves = list(game.get_valid_moves())
     # game is over if there are no possible moves in a position
     if not possible_moves:
         # if there are no moves, then the game is over so we check for the winner
         if game.get_winner() == 'white':
             ret.evaluation = 10000
         elif game.get_winner() == 'black':
             ret.evaluation = -10000
         else:
             ret.evaluation = 0
         return ret
     # shuffle for randomness
     random.shuffle(possible_moves)
     # best_value tracks the best possible move that the player can make
     # this value is maximized by white and minimized by black
     best_value = float('-inf')
     if not white_move:
         best_value = float('inf')
     for move in possible_moves:
         new_game = game.copy_and_make_move(move)
         new_subtree = self._minimax(move, new_game, depth + 1)
         if white_move:
             best_value = max(best_value, new_subtree.evaluation)
         else:
             best_value = min(best_value, new_subtree.evaluation)
         ret.add_subtree(new_subtree)
     # update the evaluation value of the tree once all subtrees are added
     ret.evaluation = best_value
     return ret
コード例 #7
0
def heuristic(game: ReversiGame, heuristic_list: list[list[int]]) -> float:
    """
    heuristic takes a given heuristic_list and returns the game-state value
    given by the list
    """
    if game.get_winner() is None:
        pieces = game.get_board().pieces
        black = 0
        white = 0
        length = len(pieces)
        for i in range(length):
            for m in range(length):
                if pieces[i][m] == 1:
                    black += heuristic_list[i][m]
                elif pieces[i][m] == -1:
                    white += heuristic_list[i][m]
        return white - black
    elif game.get_winner() == 'white':
        return 100000
    elif game.get_winner() == 'black':
        return -100000
    else:
        return 0
コード例 #8
0
    def _value_eval(self, game: ReversiGame, piece: str) -> Union[float, int]:
        """The evaluation function for minimax. For Positional Player, the evaluation function
        will evaluate the positional advantage of the pieces on the board.

        Preconditions:
            - piece in {BLACK, WHITE}

        :param game: the current game state for evaluation
        :return: value evaluated from the current game state
        """
        if game.get_winner() is not None:
            if game.get_winner() == piece:  # win
                return math.inf
            elif game.get_winner() == 'Draw':  # draw
                return 0
            else:  # lose
                return -math.inf
        else:
            num_black, num_white = game.get_num_pieces(
            )[BLACK], game.get_num_pieces()[WHITE]
            board_filled = (num_black + num_white) / (game.get_size()**2)
            if game.get_size() == 8:
                selected_board_weight = BOARD_WEIGHT_8
            else:
                selected_board_weight = BOARD_WEIGHT_6

        if board_filled < 0.80:
            return positional_early(game, selected_board_weight, piece)
        else:
            if piece == BLACK:
                return num_black / num_white

            if piece == WHITE:
                return num_white / num_black

        return 0
コード例 #9
0
    def _value_eval(self, game: ReversiGame, piece: str) -> Union[float, int]:
        """The evaluation function for minimax. The evaluation function will
        return the number of piece of its side

        Preconditions:
            - piece in {BLACK, WHITE}

        :param game: the current game state for evaluation
        :return: the evaluated value of the current state
        """
        if game.get_winner() is not None:
            if game.get_winner() == piece:  # win
                return math.inf
            elif game.get_winner() == 'Draw':  # draw
                return 0
            else:  # lose
                return -math.inf
        else:
            if piece == BLACK:
                return game.get_num_pieces()[BLACK] / game.get_num_pieces(
                )[WHITE]
            else:
                return game.get_num_pieces()[WHITE] / game.get_num_pieces(
                )[BLACK]
コード例 #10
0
def check_same(player1: Player, player2: Player) -> None:
    """
    check_same is a function that determines if two players return the same move throughout a game.
    this is particularly useful for comparison between MinimaxPlayer and MinimaxABPlayer.
    It also gives the time that each player takes to find a move. You must comment out the
    random.shuffle() line of code in both players before testing
    """
    game = ReversiGame()
    prev_move = (-1, -1)
    while game.get_winner() is None:
        start_time = time.time()
        print("Player 1 CHOOSING")
        move1 = player1.make_move(game, prev_move)
        print("--- %s seconds ---" % (time.time() - start_time))
        start_time = time.time()
        print("Player 2 CHOOSING")
        move2 = player2.make_move(game, prev_move)
        print("--- %s seconds ---" % (time.time() - start_time))
        print("Player 1 chose: ", move1, "  Player 2 chose: ", move2)
        assert move1 == move2
        game.try_make_move(move1)
        prev_move = move1
コード例 #11
0
    def build_minimax_tree(self,
                           game: ReversiGame,
                           piece: str,
                           depth: int,
                           find_max: bool,
                           previous_move: str,
                           alpha: float = -math.inf,
                           beta: float = math.inf) -> MinimaxTree:
        """Construct a tree with a height of depth, prune branches based on the Tree's
        evaluate function"""
        game_tree = MinimaxTree(move=previous_move,
                                maximize=find_max,
                                alpha=alpha,
                                beta=beta)

        if game.get_winner() is not None or depth == 0:
            game_tree.eval = self._value_eval(game, piece)
        else:
            valid_moves = game.get_valid_moves()
            random.shuffle(valid_moves)
            for move in valid_moves:

                game_after_move = game.simulate_move(move)
                subtree = self.build_minimax_tree(game_after_move,
                                                  piece,
                                                  depth - 1,
                                                  not find_max,
                                                  move,
                                                  alpha=game_tree.alpha,
                                                  beta=game_tree.beta)

                game_tree.add_subtree(subtree)

                if game_tree.beta <= game_tree.alpha:
                    break

        return game_tree
コード例 #12
0
    # List of moves made
    moves_made = [(-1, -1)]

    # List of game win results
    results = []

    # Add UI to the window
    ui_handler.add_ui(window, game, results, colour_to_player)

    # Window loop
    while window.is_running():
        """ UPDATE STUFF """

        # Get game winner
        winner = game.get_winner()
        if winner is not None:
            results.append(winner)
            ui_handler.update_games_stored_text(len(results), window)
            increment_player_score(winner, window)
            game.start_game(human_player=game.get_human_player())

        # If the game is not paused, look for mouse clicks and process moves.
        if not ui_handler.get_game_paused():
            if game.get_human_player() == game.get_current_player():
                # Look at the mouse clicks and see if they are in the board.

                for event in window.get_events():
                    if event[0] == pygame.MOUSEBUTTONUP:
                        square = board_manager.check_mouse_press(
                            event[1], game.get_board())
コード例 #13
0
def _run_ai_game(game_surface: pygame.Surface, size: int,
                 ai_player: Union[MobilityTreePlayer, PositionalTreePlayer, RandomPlayer,
                                  ReversiGame, MCTSTimeSavingPlayer],
                 user_side: str = BLACK) -> None:
    if size == 8:
        background = pygame.image.load('assets/gameboard8.png')
    elif size == 6:
        background = pygame.image.load('assets/gameboard6.png')
    else:
        raise ValueError("invalid size.")
    game_surface.blit(background, (0, 0))
    pygame.display.flip()
    game = ReversiGame(size)
    previous_move = '*'
    if user_side == BLACK:
        ai_side: str = WHITE
    else:
        ai_side: str = BLACK
    board = game.get_game_board()
    _draw_game_state(game_surface, background, size, board)

    pass_move = pygame.image.load('assets/pass.png')

    while game.get_winner() is None:
        if (previous_move == '*' and user_side == WHITE) or game.get_current_player() == user_side:
            if game.get_valid_moves() == ['pass']:
                game.make_move('pass')
                previous_move = 'pass'

                surface = game_surface
                game_surface.blit(pass_move, (300, 300))
                pygame.display.flip()
                pygame.time.wait(1000)
                game_surface.blit(surface, (0, 0))
                pygame.display.flip()

                continue
            while True:
                event = pygame.event.wait()
                if event.type == pygame.MOUSEBUTTONDOWN:
                    mouse_pos = pygame.mouse.get_pos()
                    if 585 <= mouse_pos[0] <= 795 and 10 <= mouse_pos[1] <= 41:
                        return
                    else:
                        move = _search_for_move(mouse_pos, size)
                        print(move)
                        if move == '' or move not in game.get_valid_moves():
                            continue
                        else:
                            previous_move = move
                            game.make_move(move)
                            board = game.get_game_board()
                            _draw_game_state(game_surface, background, size, board)
                            pygame.time.wait(1000)
                            break
                if event.type == pygame.QUIT:
                    return
        else:
            move = ai_player.make_move(game, previous_move)
            previous_move = move
            game.make_move(move)
            if move == 'pass':
                surface = game_surface
                game_surface.blit(pass_move, (300, 300))
                pygame.display.flip()
                pygame.time.wait(1000)
                game_surface.blit(surface, (0, 0))
                pygame.display.flip()
            else:
                board = game.get_game_board()
                _draw_game_state(game_surface, background, size, board)
    winner = game.get_winner()
    if winner == user_side:
        victory = pygame.image.load('assets/victory.png')
        game_surface.blit(victory, (300, 300))
        pygame.display.flip()
        pygame.time.wait(3000)
        return
    elif winner == ai_side:
        defeat = pygame.image.load('assets/defeat.png')
        game_surface.blit(defeat, (300, 300))
        pygame.display.flip()
        pygame.time.wait(3000)
        return
    else:
        draw = pygame.image.load('assets/draw.png')
        game_surface.blit(draw, (300, 300))
        pygame.display.flip()
        pygame.time.wait(3000)
        return