因此,我正在尝试为带有类的tictactoe创建决策树,而我无法获得每次都调用不同类变量的函数。
class Node(object):
def __init__(self, player, state):
self.possible = poss_moves(state)
self.player = player
self.local_state = state
self.score = 0
self.one = None
self.two = None
self.three = None
self.four = None
self.five = None
self.six = None
self.seven = None
self.eight = None
如果我尝试使用此功能调用新的子节点,它将无法正常工作。如果我用node.one,node.two等键入代码,它可以工作,但是我觉得必须有一种方法可以使此功能起作用。香港专业教育学院试图将其作为字符串传递,只是将node.one作为参数,将node.one分配给变量
def digger(self, node, child):
temp = copy.deepcopy(node.local_state)
temp[node.possible[0][0]][node.possible[0][1]] = node.player
if len(poss_moves(temp)) == 0 or wincon.win_check(temp) == node.player:
child = End_State(temp)
node.score += child.score
else:
if node.player == 2:
child = Node(1, temp)
self.deeper(child)
node.score += child.score
else:
child = Node(2, temp)
self.deeper(child)
node.score += child.score
node.possible.pop(0)
def deeper(self, node):
while len(node.possible) > 0:
self.digger(node, node.one)
if len(node.possible) == 0:
break
self.digger(node, node.two)
if len(node.possible) == 0:
break
self.digger(node, node.three)
if len(node.possible) == 0:
break
self.digger(node, node.four)
if len(node.possible) == 0:
break
self.digger(node, node.five)
if len(node.possible) == 0:
break
self.digger(node, node.six)
if len(node.possible) == 0:
break
self.digger(node, node.seven)
if len(node.possible) == 0:
break
self.digger(node, node.eight)