Hello everyone I am currently taking CS50AI course. 1st assignment is creating a tictactoe AI with minimax function. My problem is this: As far as I understand, there has to be a static evaluation for positions of the game. I was trying to write something like this in pseudocode :
If next move is a winning move:
return 10 point
elif opponent is going to win stop him:
return 8 point
kind of thing. But when I checked others minvalue - max value function there was no such a thing.
def minimax(board):
"""
Returns the optimal action for the current player on the board.
"""
currentactions = actions(board)
if player(board) == X:
vT = -math.inf
move = set()
for action in currentactions:
v, count = maxvalue(result(board,action), 0)
if v > vT:
vT = v
move = action
else:
vT = math.inf
move = set()
for action in currentactions:
v, count = minvalue(result(board,action), 0)
if v < vT:
vT = v
move = action
print(count)
return move
def maxvalue(board, count):
"""
Calculates the max value of a given board recursively together with minvalue
"""
if terminal(board): return utility(board), count+1
v = -math.inf
posactions = actions(board)
for action in posactions:
vret, count = minvalue(result(board, action), count)
v = max(v, vret)
return v, count+1
def minvalue(board, count):
"""
Calculates the min value of a given board recursively together with maxvalue
"""
if terminal(board): return utility(board), count+1
v = math.inf
posactions = actions(board)
for action in posactions:
vret, count = maxvalue(result(board, action), count)
v = min(v, vret)
return v, count+1
This is sikburn's tictactoe implementation's max - min functions. I could not understand what outcome will come from the max or min value functions. Can anyone clarify my logic please ? By the way, terminal() function checks if the game ended (has a winner or tie) and result() function takes a board and action as an input and returns the resultant board. Thanks for all the help.
In the
utilityfunction (not included in your code), you probably assigned 1 to X victory, -1 to O victory, and 0 otherwise. Theminimaxfunction callsminvalueandmaxvaluerecursively, for all possible moves, until it comes to the end of the game, be it a tie or a victory. Then it callutilityto get the value. Bothminvalueandmaxvalueassure X and O will always select the best possible moves.Don't forget to check if the board is terminal and return
Noneif it is, before proceding, in theminimaxfunction.Swap the the calling of
minvalueandmaxvaluefunctions inminimax: for X, callminvalue(because X wants to know O next move) and for O, callmaxvalue(for the same reason).If you want to see the evalution at each iteration, you can print something like
f"Minvalue: {v}, Iteration: {count+1}"andf"Maxvalue: {v}, Iteration: {count+1}"at the end ofminvalueandmaxvaluefunctions, just before returning those values. I think it would be easier to understand.I hppe I clarified your doubts.