完全公开,这是一项家庭作业的一部分(尽管是一小段代码,该项目本身就是一个玩AI的游戏)。Python中的无尽递归
我有这个功能内置到树节点类:
def recursive_score_calc(self):
current_score = self.board
for c in self.children:
child_score = c.recursive_score_calc()
if(turn_color == 1):
if(child_score > current_score):
current_score = child_score
else:
if(child_score < current_score):
current_score = child_score
self.recursive_score = current_score
return current_score
深度1(根有的孩子)的树,它击中了Python递归限制了。该函数旨在使用动态编程从下到上构建最小最大树。说实话,我不知道为什么它不能按预期工作,但我对Python也相当陌生。
堆栈溢出的好人:为什么这段代码给我堆栈溢出?
有关整个类:
from Numeric import *
class TreeNode:
children = []
numChildren = 0
board = zeros([8,8], Int)
turn_color = 0 # signifies NEXT to act
board_score = 0 # tally together board items
recursive_score = 0 # set when the recursive score function is called
def __init__(self, board, turn_color):
self.board = copy.deepcopy(board)
self.turn_color = turn_color
for x in range (0,7):
for y in range (0,7):
self.board_score = self.board_score + self.board[x][y]
def add_child(self, child):
self.children.append(child)
self.numChildren = self.numChildren + 1
def recursive_score_calc(self):
current_score = self.board # if no valid moves, we are the board. no move will make our score worse
for c in self.children:
child_score = c.recursive_score_calc()
if(turn_color == 1):
if(child_score > current_score):
current_score = child_score
else:
if(child_score < current_score):
current_score = child_score
self.recursive_score = current_score
return current_score
与此(请注意,这是接壤的什么是适当的在这里发表边缘相互作用的功能,我将我接受后删除此部分答案):[它反正不是关键部分]
你能告诉我们创建树的代码吗?您可能将自己添加为其中一个孩子。 – 2010-02-18 03:14:49
现在所有。合法移动是当前游戏状态中的一组可能移动,并且应该包含树节点的所有子节点。 – alexwood 2010-02-18 03:18:46