I'm using Python to create a unbeatable TicTacToe game with the MiniMax Algorithm. My code does not seem to outputting the correct evaluation for each position. From what I understand, the alogorithm should "test" each position by assuming that the human player plays optimally. However, the evaluation always returns a NoneType.
I tried using other numbers in the evaluation function with an else. However, doing that does not seem to return the correct evaluation, or at least, I think it's not correct.
board= ["O", 1, "X",
"X", 4, "X",
6, "O", "O"]
human = "O"
robot= "X"
#winning board combinations
def winning(board, player):
#horizontal tests
if((board[0] == player and board[1]==player and board[2]==player) or
(board[3]==player and board[4]==player and board[5]==player) or
(board[6]==player and board[7]==player and board[8]==player) or
#diagonal tests
(board[0]==player and board[4]==player and board[8]==player) or
(board[2]==player and board[4]==player and board[6]==player) or
#vertical tests
(board[0]==player and board[3]==player and board[6]==player) or
(board[1]==player and board[4]==player and board[7]==player) or
(board[2]==player and board[5]==player and board[8]==player)
):
return True
else:
return False
#evaluates the score of the game
def evaluation(board):
if (winning(board, human)):
return +10
elif (winning(board, robot)):
return -10
elif(len(empty_spots)==0):
return 0
#trying to get another value
# else:
# return 2
#this is to find the empty spots on the board
def empty_spots_func(board):
for i in board:
if(i!="X" and i!="O"):
empty_spots.append(i)
def minimax(spot, empty_spots, depth, maximizing_player):
if depth<=0:
eval= evaluation(board)
print(f'The evaluation function returns: {evaluation(board)}')
return eval
if maximizing_player:
maxEval= -1000000
#for each move in all possible moves
for spot in empty_spots:
#make the move
board[spot]=robot
#evaluate the outcome of the move
eval = minimax(spot, empty_spots, depth-1, False)
#then remove the move and replace it with the number
board[spot]=spot
maxEval= max(maxEval, eval)
# print(f'The maximum evaluation {maxEval}')
return maxEval
else:
minEval= +1000000
#for each move in all possible moves
for spot in empty_spots:
#make the move
board[spot]=human
#evaluate the outcome of the move
eval= minimax(spot, empty_spots, depth-1, True)
#then remove the spot
board[spot]=spot
#figure out the minimal evaluation
minEval=min(minEval, eval)
# print(f'The minimal evaluation is {minEval}')
return minEval
And now testing for one position in the main function:
test_spot_eval=minimax(0, empty_spots, len(empty_spots), True)
empty_spots_func(board)
print(f'The spot eval is {test_spot_eval}')
Without the last "else" in evaluation, minimax just returns a NoneType.
I made a separate program to check the evaluation and the winning combinations. It turns out, the errors I was making was:
I was not placing the human input on the board, after taking in the user input
I had not noticed that I incorrectly made the evaluation function return a positive value for the human win and a negative value for the robot win when it should have been the other way around.
There might be more mistakes I have not noticed yet.
Error 1 was causing my evaluation function to return a NoneType because, without the human input, the algorithm could not evaluate a winning position.