[英]Minimax For Connect four doesn't return a value
当我运行该程序时,出现错误,表明MinimaxNode对象没有属性“值”
ConnectFour是另一个类,用于初始化棋盘并标记动作,检查是否有人赢得比赛等。Utility仅返回2分(它仍在进行中)
问题出在child.get_minimax_value中的MinimaxPlayer中,因为它会产生一个错误,即MinimaxNode对象没有属性“值”
如果get_minimax_value
是一个方法,则child.get_minimax_value
应该是child.get_minimax_value()
。
没有括号, child.get_minimax_value
表示绑定的方法,而不是该方法返回的值。
因此, child.get_minimax_value
永远不等于v
,并且if-clause
条件为False,并且col
永远不会被设置。
然后,Python将在到达时引发错误
board.ConnectFour.play_turn(self.playernum, col)
我认为也许在MinimaxPlayer.minimax
中, return v
语句的缩进级别应该在for
-loops之外。 否则,节点的值将仅取决于node.children
的第一个子node.children
。
def minimax(self, node, cur_depth):
if cur_depth == self.ply_depth:
u = self.utility.compute_utility(node, self.playernum)
node.set_minimax_value(u)
return u
node.compute_children()
if cur_depth % 2 == 0:
v = float("-inf")
for child in node.children:
childval = self.minimax(child, cur_depth + 1)
v = max(v, childval)
node.set_minimax_value(v)
return v
if cur_depth % 2 != 0:
v = float("inf")
for child in node.children:
childval = self.minimax(child, cur_depth + 1)
v = min(v, childval)
node.set_minimax_value(v)
return v
但是,如果没有可运行的代码,真的很难说。
您看到的输出仅仅是因为您输出了对象MinmaxNode
,该对象必须没有设置__repr__
方法。
您出现“分配之前引用的本地变量'col'的错误”是因为您在for循环中有条件地定义了col
,但是无论是否分配了它,都将其用作play_turn
的参数。 在运行for
循环之后和调用它之前,应检查col
是否已定义。
您正在收到该错误,因为s possible the following line runs before
分配col` s possible the following line runs before
board.ConnectFour.play_turn(self.playernum, col)
col
值仅在for
循环的主体内分配。 python解释器看到了这一点,并得出结论,在循环体执行0次的情况下,或者if
条件从不评估为true的情况下,则不会分配col
。
您需要在循环执行之前为col明确分配一个值,并在调用play_turns
时检查该play_turns
col = -1
for child in root.children
...
if col != -1:
board.ConnectFour.play_turn(self.playernum, col)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.