Search code examples
pythonrecursionminimax

Minimax Algorithm in Tic, Tac, Toe using python: Recursion won't come to end


I am very new to programming and wanted to program a Tic, tac, toe solver using the minimax algorithm. When I tested my program, it returns: maximum recursion depth exceeded in comparison. I have no idea why my recursion won't stop. Could someone possibly help me with this problem? Feel free to give me some tips on how to improve my code.

# in the scores list the scores of the moves are kept
scores = []
# in the empty_spots list all indices of the empty cells in the grid are kept
empty_spots = []

# function, which prints the grid
def show_grid(grid):
    a = 0
    for cell in grid:
        a = a + 1
        if cell == 1:
            if a < 3:
                print("X", end="")
            else:
                print("X")
                a = 0
        elif cell == -1:
            if a < 3:
                print("O", end="")
            else:
                print("O")
                a = 0
        else:
            if a < 3:
                print("_", end="")
            else:
                print("_")
                a = 0

# function which checks if there is a victory or draw
def check_victory(grid, player):
    Victory_Combos = [[0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7], [2, 5, 8], [0, 4, 8], [6, 4, 2]]
    for victorys in Victory_Combos:
        if grid[victorys[0]] == player*-1 and grid[victorys[1]] == player*-1 and grid[victorys[2]] == player*-1:
            return -10 * player
    if 0 not in grid:
        return 0

# function which finds all empty spots
def find_empty_spots(grid):
    for cell in range(9):
        if grid[cell] == 0:
            empty_spots.append(cell)
    return empty_spots



# minimax function
def minimax(grid, player, best_score, depth):
    if check_victory(grid, player) != None:
        return check_victory(grid, player)
    list = find_empty_spots(grid)
    for cell in list:
        grid[cell] = player
        scores.append(minimax(grid, player*-1, 1000*-player, depth + 1))
        if player == 1:
            best_score = -1000
            for score in scores:
                if best_score < score:
                    best_score = score
        else:
            best_score = 1000
            for score in scores:
                if best_score > score:
                    best_score = score
        grid[cell] = 0
    if depth == 0:
        grid[scores.index(best_score)] = player
        show_grid()
    scores.clear()
    return best_score



# example
print(minimax([-1,1,-1,1,1,0,1,-1,0],-1,1000,0))


Solution

  • The problem is with your empty_spots, you are never clearing it, thus you are always checking previous (empty) cells.

    def find_empty_spots(grid):
        empty_spots = []
        for cell in range(9):
            if grid[cell] == 0:
                empty_spots.append(cell)
        return empty_spots
    

    Added empty_spots = [] to clear the list before every call, else you are just appending the cells to and already existing list.

    And another thing - list = find_empty_spots(grid) is very very wrong, do not use keywords for variable names, the proper way to go would be lst = find_empty_spots(grid), or better, some meaningful name, empty_cells = find_empty_spots(grid).