我最初是使用递归来最小化树叶中的最大值,只有我可以计算分数,但因为我需要知道最小或最大值的深度,所以我切换到从depth=0开始。然而,错误,因为有时currentRoot.ia = None,因为没有分数已经计算了那么深。我想要的是跟踪深度,并找到currentRoot.ia计算过的最深的叶子,以及每个叶子的最小最大深度。
我检查是否有孙子节点,因为当我评估一个位置以获得分数时,我还添加了一个给出该分数的移动节点,因此在叶节点上不应该有任何分数。分数是从引擎的角度来看的,所以我不得不在奇怪的深度进行否定,尽管如果我总是最大限度地提高分数,也许我可以逃脱惩罚。
def minimax(currentRoot, depth):
if len(currentRoot.children) > 0 and len(currentRoot.children[0].children) > 0: #There are grandchildren
for child in currentRoot.children:
minimax(child, depth+1)
else:
if depth%2 == 0:
currentRoot.score = currentRoot.ia
else:
currentRoot.score = -currentRoot.ia
return currentRoot.score
measure = min if depth % 2 else max
currentRoot.score = measure(c.score for c in currentRoot.children)
return currentRoot.score发布于 2016-06-25 07:41:49
我认为这可能会解决我的错误,但我不认为它是优雅的。我递归,直到我找到一个值,所以ia不是None,我继续深入,直到我发现我在一棵树没有被进一步评估的叶子上。
def minimax(currentRoot, depth):
notAtLeaf = True
if currentRoot.ia is None:
notAtLeaf = False
for child in currentRoot.children:
minimax(child, depth+1)
else: #currentRoot.ia is not None
for child in currentRoot.children:
if child.ia is not None:
notAtLeaf = False
minimax(child, depth+1)
if notAtLeaf:
if depth%2 == 0:
currentRoot.score = currentRoot.ia
else:
currentRoot.score = -currentRoot.ia
return currentRoot.score
measure = min if depth % 2 else max
currentRoot.score = measure(c.score for c in currentRoot.children)
return currentRoot.scorehttps://stackoverflow.com/questions/38019582
复制相似问题