0

当我运行这个程序时,我得到一个错误,即 MinimaxNode' 对象没有属性 'value'

ConnectFour 是另一个类,它初始化棋盘并标记移动,检查是否有人获胜等。实用程序只返回 2 分(仍在进行中)

问题出现在 MinimaxPlayer 中的 child.get_minimax_value 中,因为它提出了一个错误,即 MinimaxNode' 对象没有属性 'value'

4

3 回答 3

1

如果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.

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

但是没有可运行的代码真的很难说。

于 2013-04-02T21:57:03.410 回答
0

您看到的输出仅仅是因为您打印出 object MinmaxNode,该 object 必须没有__repr__设置方法。

您的“分配前引用局部变量 'col'”的错误是因为您在 for 循环中有条件地定义,但无论是否已分配,都col将其用作参数。您应该在运行循环之后和调用它之前play_turn检查col是否已定义。for

于 2013-04-02T21:50:10.243 回答
0

您收到该错误是因为它s possible the following line runs beforecol` 已分配

board.ConnectFour.play_turn(self.playernum, col)

col值仅在for循环主体内分配。python解释器看到并得出结论,如果循环体执行0次,或者if条件永远不会计算为真,则col不会被分配。

您需要在循环执行之前明确地为 col 分配一个值,并在调用时检查它play_turns

col = -1
for child in root.children
  ...
if col != -1: 
  board.ConnectFour.play_turn(self.playernum, col)
于 2013-04-02T21:50:25.357 回答