I'm currently working on a project where I need to implement a Minimax algorithm to create a tic-tac-toe game. At some point I'm comparing tuples and floats, and while I know this can't actually be done, I don't know where that problem comes from or how do I fix it. If anyone could help me understand that would be amazing. Thanks!
Here's my code
import copy
X = "X"
O = "O"
EMPTY = None
def initial_state():
"""
Returns starting state of the board.
"""
return [[EMPTY, EMPTY, EMPTY],
[EMPTY, EMPTY, EMPTY],
[EMPTY, EMPTY, EMPTY]]
def player(board):
Xboard_count = board[0].count(X) + board[1].count(X) + board[2].count(X)
if Xboard_count % 2 == 1:
return 0
else:
return X
def actions(board):
"""
Returns set of all possible actions (i, j) available on the board.
"""
move = []
for i in range(3):
for j in range(3):
if board[i][j] == EMPTY:
move.append((i, j))
return move
def result(board, move):
"""
Returns the board that results from making move (i, j) on the board.
"""
if move not in actions(board):
raise Exception("this action is not a valid action")
dcopb = copy.deepcopy(board)
dcopb[move[0]][move[1]] = player(board)
return dcopb
def winner(board):
"""
Returns the winner of the game, if there is one.
"""
for x in range(3):
if board[x][0] == board[x][1] == board[x][2] != EMPTY:
return board[x][0]
for y in range(3):
if board[0][y] == board[1][y] == board[2][y] != EMPTY:
return board[0][y]
if board[0][0] == board[1][1] == board[2][2] != EMPTY:
return board[0][0]
if board[2][0] == board[1][1] == board[0][2] != EMPTY:
return board[2][0]
else:
return None
def terminal(board):
"""
Returns True if game is over, False otherwise.
"""
if winner(board) == X:
return True
elif winner(board) == O:
return True
for x in range(3):
for y in range(3):
if board[x][y] is None:
return False
return True
('si winner est égal à X :\n'
' return True\n'
' si winner est égal à 0\n'
' return true \n'
' \n'
' si le nombre de cases vides == 0\n'
' return true\n'
' else:\n'
' return false')
def utility(board):
"""
Returns 1 if X has won the game, -1 if O has won, 0 otherwise.
"""
if winner(board) == X:
return 1
if winner(board) == 0:
return -1
else:
return 0
def minimax(board):
"""
Returns the optimal action for the current player on the board.
"""
if terminal(board):
return None
if player(board) == X:
v = v = float('-inf')
for move in actions(board):
temp = MinValue(result(board, move))
if temp > v:
v = temp
best = move
else:
v = float('inf')
for move in actions(board):
temp = MaxValue(result(board, move))
if temp < v:
v = temp
best = move
return best
def MaxValue(board):
if terminal(board):
return utility(board), None
v = float('-inf')
for move in actions(board):
v = max(v, MinValue(result(board, move)))
return
def MinValue(board):
if terminal(board):
return utility(board), None
v = float('inf')
for move in actions(board):
v = min(v, MaxValue(result(board, move)))
return v
I get this exact error message
TypeError: '>' not supported between instances of 'tuple' and 'float'
First of all, there was a problem with the MinValue and MaxValue function, they were sometimes returning two values instead of one.
Then, there was the same TypeError, this time with a problem when comparing NoneType variables with floats. Turns out there was just a small mistake because my MaxValue function wasn't returning anything.