使用元组和浮点数处理 Minimax 算法时出现 TypeError
TypeError when working on a Minimax algorithm with tuples and floats
我目前正在从事一个项目,我需要在其中实施 Minimax 算法来创建井字游戏。在某些时候,我正在比较元组和浮点数,虽然我知道这实际上无法完成,但我不知道该问题从何而来或如何解决。如果有人能帮助我理解那将是惊人的。谢谢!
这是我的代码
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
我收到了这条确切的错误消息
TypeError: 'tuple' 和 'float'
实例之间不支持“>”
您正在 return 在 MinValue
的 return utility(board), None
处创建一个元组。因此,当您在 temp = MinValue(result(board, move))
处调用函数时,如果 return 语句被调用
,则 temp
是一个元组
v
是一个浮点数。现在,当您尝试将它与 if temp < v:
处的 temp
进行比较时,您正在将浮点数与元组
进行比较
首先,MinValue 和 MaxValue 函数有问题,它们有时会返回两个值而不是一个值。
然后,出现了同样的 TypeError,这次是在将 NoneType 变量与浮点数进行比较时出现问题。事实证明这只是一个小错误,因为我的 MaxValue 函数没有返回任何东西。
我目前正在从事一个项目,我需要在其中实施 Minimax 算法来创建井字游戏。在某些时候,我正在比较元组和浮点数,虽然我知道这实际上无法完成,但我不知道该问题从何而来或如何解决。如果有人能帮助我理解那将是惊人的。谢谢!
这是我的代码
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
我收到了这条确切的错误消息
TypeError: 'tuple' 和 'float'
实例之间不支持“>”您正在 return 在 MinValue
的 return utility(board), None
处创建一个元组。因此,当您在 temp = MinValue(result(board, move))
处调用函数时,如果 return 语句被调用
temp
是一个元组
v
是一个浮点数。现在,当您尝试将它与 if temp < v:
处的 temp
进行比较时,您正在将浮点数与元组
首先,MinValue 和 MaxValue 函数有问题,它们有时会返回两个值而不是一个值。
然后,出现了同样的 TypeError,这次是在将 NoneType 变量与浮点数进行比较时出现问题。事实证明这只是一个小错误,因为我的 MaxValue 函数没有返回任何东西。