def test_blank_board():
    """
    Testing for result consistency between the 3 approaches to minimax algorithm
    Testing algorithm result from evaluating a blank board
    """
    board = create_board()

    minimax_result = minimax(board, get_depth(board), True)
    minimax_soft_alpha_beta_result = minimax_soft_alpha_beta(
        board, get_depth(board), True, -inf, +inf)
    minimax_alpha_beta_result = minimax_alpha_beta(board, get_depth(board),
                                                   True, -inf, +inf)

    assert minimax_result == minimax_soft_alpha_beta_result

    assert minimax_alpha_beta_result[0] == minimax_result[0]
    assert minimax_alpha_beta_result[1][0] in minimax_result[1]
def test_board_bot_1st_turn():
    """
    Testing for result consistency between the 3 approaches to minimax algorithm
    Testing algorithm result from evaluating all possible states of the board where the bot starts first
    """
    for turn_num in range(1, 9):
        boards = get_all_possible_board_states(turn_num, BOT_STATE,
                                               HUMAN_STATE)

        is_maximizing_player = True if turn_num % 2 == 0 else False

        for board in boards:
            minimax_result = minimax(board, get_depth(board),
                                     is_maximizing_player)
            minimax_soft_alpha_beta_result = minimax_soft_alpha_beta(
                board, get_depth(board), is_maximizing_player, -inf, +inf)
            minimax_alpha_beta_result = minimax_alpha_beta(
                board, get_depth(board), is_maximizing_player, -inf, +inf)

            assert minimax_result == minimax_soft_alpha_beta_result

            assert minimax_alpha_beta_result[0] == minimax_result[0]
            assert minimax_alpha_beta_result[1][0] in minimax_result[1]
Exemple #3
0
    def make_move(self, board):
        """
        Calls the minimax algorithm if the player is a bot, else request user input for human player.

        :param board: type: numpy.ndarray
        The current state of the Tic Tac Toe board game
        Input for the minimax algorithm to find the optimal move

        :return: type: tuple
        Selected move index in numpy array format (<row_index>, <column_index>)
        """
        if self._bot:
            # Minimax algorithm
            _, moves = minimax_soft_alpha_beta(board, get_depth(board), True,
                                               -inf, +inf)
            move = random.choice(moves)
        else:
            # Prompt the user to select a move
            index = input("Enter move: ")
            move = Player.convert_index_to_move(int(index))

        return move
def bot_move_input_handler(board, bot):
    """
    Calls the minimax algorithm and calculates the best possible move for the board state and updates the best possible
    move on the board

    :param board: type: numpy.array
    The current state of the Tic Tac Toe board game

    :param bot: type: class 'game.player.Player'
    Player class instance of the bot player
    """
    # Minimax algorithm
    if all(box == BLANK_STATE for row in board for box in row):
        # To reduce load, random the first turn for bot
        row, box = (random.randint(-1, 2), random.randint(-1, 2))
    else:
        # Subsequent turn will not take long to calculate
        _, moves = minimax_soft_alpha_beta(board, get_depth(board), True, -inf,
                                           +inf)
        row, box = random.choice(moves)

    update_board(board, (row, box), bot)