def compMove(node, depth, maximizingPlayer):
for place in range(1, 10):
if isSpaceFree(place):
node[place] = ai
score = minimax(node, depth, maximizingPlayer)[1]
node[place] = ' '
best_score = -math.inf
if score != 473:
if score > best_score:
best_score = score
return place
def minimax(node, depth, maximizingPlayer):
value = 0
if depth == 0:
if isBoardFull(node):
if not isWinner(node, 'O') and not isWinner(node, 'X'):
return [0, 473]
if isWinner(node, 'O'):
return [1,473]
if isWinner(node, 'X'):
return [-1,473]
if is_terminal(node):
if isBoardFull(node):
if not isWinner(node, 'O') and not isWinner(node, 'X'):
return [0, 473]
if isWinner(node, 'O'):
return [1, 473]
if isWinner(node, 'X'):
return [-1, 473]
if maximizingPlayer:
value = -math.inf
for place in range(1, 10):
if isSpaceFree(place):
insertLetter('O', place, node)
childValue = minimax(node, depth - 1, False)
node[place] = ' '
if childValue > value:
value = childValue
return [473,value]
else: # basically now the minimizing player's turn
value = math.inf
for place in range(1, 10):
if isSpaceFree(place):
insertLetter(human, place, node)
childValue = minimax(node, depth - 1, True)
node[place] = blank
if childValue < value:
value = childValue
return [473,value]