Minimax For Connect 4不返回值

时间:2013-04-02 21:46:02

标签: python

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

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

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

3 个答案:

答案 0 :(得分:1)

如果get_minimax_value是方法,则child.get_minimax_value应为child.get_minimax_value()

如果没有括号,child.get_minimax_value表示绑定方法,而不是方法返回的值。

因此,child.get_minimax_value永远不会等于vif-clause条件为False,col永远不会被设置。

Python会在到达

时引发错误
board.ConnectFour.play_turn(self.playernum, col)

我想也许在MinimaxPlayer.minimaxreturn v语句的缩进级别应该在for - 循环之外。否则,节点的值仅取决于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

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

答案 1 :(得分:0)

您看到的输出仅仅是因为您打印出对象MinmaxNode,该对象不能设置__repr__方法。

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

答案 2 :(得分:0)

您收到该错误,因为它已分配s possible the following line runs before col`

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

col值仅在for循环的正文中指定。 python解释器看到并得出结论,在循环体执行0次或者if条件永远不会计算为true的情况下,将不会分配col

您需要在循环执行前明确指定col值,并在调用play_turns时检查该值

col = -1
for child in root.children
  ...
if col != -1: 
  board.ConnectFour.play_turn(self.playernum, col)