Need help figuring out the recursion error in my minimax algorithm

0

I'm creating a TicTacToe game for a player to play against an AI. I decided to approach this using the "Minimax Algorithm". After implementation, I ran the program but it repeated itself for a long time, repeating the moves the AI would make while switching with the player.

I've tried to adjust dept values as well as create temporary tictactoe boards for the AI to operate on instead of drawing the board everytime the AI checked a box, even though I have made sure that if the AI puts its symbol in a spot to check, it calls the minimax function but it also later removes the symbol, replacing it with a blank.

def minimax(self, board,  player, depth=0):

    if player == 'X':
        best = 10000
    else:
        best = -10000

    if ' ' not in board.values():
        if self.getWinner() == 'X':
            return -10 + depth, None
        elif self.getWinner() == 'O':
            return 10 - depth, None
        elif self.getWinner() == "tie":
            return 0

    for moves in self.getavailableMoves(): 

getavailablemoves returns a 1D array called availalbemoves

        depth = len(self.availableMoves)
        self.makeMove(moves, player) #make the move
        val = self.minimax(board, self.getEnemy(player), depth - 1)
        self.makeMove(moves, ' ') 
        if player == 'O':
            if val > best:
                best = val
                bestMove = moves
        else:
            if val < best:
                val = best
                bestMove = moves

    return bestMove

No error is being show, it just shifts the output screen and scrolls it very far down showing the move the AI would do, but it switches that spot with the player's symbol

Eg: ** I put the 'X' symbol in the index [1] and AI puts its symbol in index [0]

O |X | (2)

(3) | (4) | (5)

(6) | (7) |(8)

X |X | (2) <---- Replaces its move with players symbol instead of asking ---------- for input to the player

(3) | (4) | (5)

(6) | (7) | (8)

This repeats for countless times until it finally ends with an exit code of -1073741571 (0xC00000FD)

python-3.7
asked on Stack Overflow Aug 4, 2019 by vrund_

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0