![](/img/trans.png)
[英]Read data from a file and create a tree using anytree in python
[英]Create dynamic tree in python using anytree module
我嘗試為 minmax 算法創建一個實現,並且需要創建一個包含所有可能移動的樹。 在 python 3.7 中使用 anytree 模塊創建樹,但是當嘗試在第一個樹級別迭代並構建下一個級別時收到錯誤。
Traceback (most recent call last):
Hello from the pygame community. https://www.pygame.org/contribute.html
File "C:/Users/User/PycharmProjects/Tema5AI/Main.py", line 217, in <module>
min_max_algorithm(game)
File "C:/Users/User/PycharmProjects/Tema5AI/Main.py", line 209, in min_max_algorithm
new_node = Node((game, i, 0), parent=(pre, fill, node))
File "C:\Users\User\PycharmProjects\Tema5AI\venv\lib\site-packages\anytree\node\node.py", line 66, in __init__
self.parent = parent
File "C:\Users\User\PycharmProjects\Tema5AI\venv\lib\site-packages\anytree\node\nodemixin.py", line 126, in parent
msg = "Parent node %r is not of type 'NodeMixin'." % (value)
TypeError: not all arguments converted during string formatting
我的構建樹代碼是:
def min_max_algorithm(game):
first_black_move = util.get_all_available_black(game)
root = Node(game)
for i in first_black_move:
node = Node((game, i, 0), parent=root)
for pre, fill, node in RenderTree(root):
first_white_move = util.get_all_available_white(game)
for i in first_white_move:
new_node = Node((game, i, 0), parent=(pre, fill, node))
for pre, fill, node in RenderTree(root):
print("%s%s" % (pre, node.name))
更確切地說,問題是:如何通過當前樹將子節點添加到節點?
以下問題對我沒有幫助: How to specify children in anytree and print a tree
例外是誤導性的。 我創建了一張票https://github.com/c0fec0de/anytree/issues/121 。
您可以隨時添加節點。 請確保僅將節點對象設置為父對象或子對象。 也許最好使用迭代器而不是渲染 function,即PreOrderIter 。 請試試這個
def min_max_algorithm(game):
first_black_move = util.get_all_available_black(game)
root = Node(game)
for i in first_black_move:
node = Node((game, i, 0), parent=root)
for n in PreOrderIter(root):
first_white_move = util.get_all_available_white(game)
for i in first_white_move:
new_node = Node((game, i, 0), parent=n)
for pre, fill, node in RenderTree(root):
print("%s%s" % (pre, node.name))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.