0

I am trying to make an ultimate tic-tac-toe game in python which is a little different than the actual one in a way that this game ends when there is a win in any one sub-board. I am using minimax algorithm with alpha-beta pruning to find out the best move for the bot to play. The problem is that when i run the code and it is the time for bot to play its move, it runs endlessly without coming to a conclusion and returning a best_move.

The communication with the board is already handled. All i need is the best value and once i get that, i can retrieve the index from that state.

Initailly, once the game is started, the user is prompted to make a move from 1-9 which is then fed to the function: Boards is a list of list which contains the state of each sub-board.

# choose a move to play
def play1(user_move):
    # print_board(boards)
    boards_list = main_boards.tolist()
    player = 1
    depth = 20
    end_move = make_bot_move(boards_list, user_move, player, depth)
    place(curr, end_move, 1)
    return end_move

The make_bot_move function takes the position of the human and figures out in which sub-board it should play its best_move:

def make_bot_move(state, user_move, player, depth):
    #sub_board = state[user_move]
    # if suboptimal(state, user_move, player) != 0:
    #     return suboptimal(state, user_move, player)
    pseudo_states = successors(state, player, user_move)
    best_move = (-inf, None)
    alpha = -inf
    beta = inf
    for x in pseudo_states:
        state = x[0]
        index = x[1]
        val = minimax(state, index, opponent(player), depth-1, alpha, beta)
        if val > best_move[0]:
            best_move = (val, index)
#        print("val = ", val)
#        print_board(s[0])
    return best_move[1]

The successors function returns the possible states where it can play its move:

def successors(boards, player, user_move):
    sub_board = boards[user_move]
    value_index = []
    possible_states = []
    for idx, value in enumerate(sub_board):
        if value == 0 and idx != 0:
            value_index.append(idx)
            copied_board = deepcopy(boards)
            possible_states.append(get_possible_state(copied_board, user_move, idx, player))
    #print(possible_states)
    return zip(possible_states, value_index)

Finally, every possible move is fed to minimax function which returns a val of the best move:

def minimax(state, last_move, player, depth, alpha, beta):

    if depth <= 0 or get_game_status(state, player) != 0:
        return evaluate(state, opponent(player))

    if player == 1:
        max_eval = -inf
        pseudo_states = successors(state, player, last_move)
        for x in pseudo_states:
            state = x[0]
            index = x[1]
            print(depth)
            #print_board(np.array(state))
            eval = minimax(state, index, opponent(player), depth-1, alpha, beta)
            max_eval = max(max_eval, eval)
            alpha = max(alpha, eval)
            if beta<= alpha:
                break
        #print_board(np.array(state))
        return max_eval

    if player == 2:
        min_eval = inf
        pseudo_states = successors(state, player, last_move)
        for x in pseudo_states:
            state = x[0]
            index = x[1]
            print(depth)
            #print_board(np.array(state))
            eval = minimax(state, index, opponent(player), depth - 1, alpha, beta)
            min_eval = min(min_eval, eval)
            beta = min(beta, eval)
            if beta<= alpha:
                break
        #print_board(np.array(state))
        return min_eval

To know whether someone has WON || LOSS || DRAW, get_game_status function is called inside minimax function:

def get_game_status(state, player):
    other_player = opponent(player)
    for each_box in state[1:10]:
        win_state = [
            [each_box[1], each_box[2], each_box[3]],
            [each_box[4], each_box[5], each_box[6]],
            [each_box[7], each_box[8], each_box[9]],
            [each_box[1], each_box[4], each_box[7]],
            [each_box[2], each_box[5], each_box[8]],
            [each_box[3], each_box[6], each_box[9]],
            [each_box[1], each_box[5], each_box[9]],
            [each_box[3], each_box[5], each_box[7]],
        ]
        if [player, player, player] in win_state:
            return player
        elif [other_player, other_player, other_player] in win_state:
            return other_player
        else:
            return 0

And the scoring is handled using evaluate function:

def evaluate(state, player):
    if(get_game_status(state, player) and player ==1) :
        score = 10
    elif(get_game_status(state, player) and player == 2):
        score = -10
    else:
        score = 0
    return score

The expected result is to get the best move but instead, it runs endlessly.

Kindly suggest what changes I should make, or where I am going wrong.

Akhil Jain
  • 71
  • 1
  • 11

0 Answers0