完全公開,這是一項家庭作業的一部分(儘管是一小段代碼,該項目本身就是一個玩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