gpt4 book ai didi

Adventure Story Graph Tuple Bug(冒险故事图表二元组Bug)

转载 作者:bug小助手 更新时间:2023-10-25 13:42:22 35 4
gpt4 key购买 nike



I'm working on a practice project dealing with graphs. It's an interactive adventure game where the user inputs choices and tries to escape the wilderness, however, right now, when the user chooses an answer for the first prompt, it ends the game without prompting the other choices to make the game go on longer and make more sense to the user. I used tuples to represent a branching narrative, but I think I'm using it incorrectly. If anyone has a second to look through this or has any tips, I would greatly appreciate it!!

我正在做一个处理图表的练习项目。这是一款互动冒险游戏,用户输入选择并试图逃离荒野,然而,现在,当用户选择第一个提示的答案时,它会在不提示其他选择的情况下结束游戏,以使游戏持续更长时间,对用户来说更有意义。我使用元组来表示分支叙述,但我认为我没有正确地使用它。如果谁有时间浏览这篇文章或有什么建议,我将不胜感激!


Here's the main.py file that creates the root node of the story tree and initiates the game:

以下是创建故事树根节点并启动游戏的main.py文件:


# Import TreeNode class and story_data from respective files
from story_data import story_data
from tree_node import TreeNode

def main():
# Create the root node with the initial story piece
story_root = TreeNode(story_data.story_piece, story_data.choices)

# Print the initial story piece
print("Once upon a time...")
print(story_root.story_piece)

# Begin the story traversal
story_root.traverse()

# Check if this file is being run directly
if __name__ == "__main__":
main()

Here's the tree_node.py file that represents nodes in the story tree:

下面是代表故事树中节点的tree_node.py文件:


class TreeNode:
def __init__(self, story_piece, choices=None):
self.story_piece = story_piece
self.choices = choices or []

def add_child(self, node):
self.choices.append(node)

def traverse(self):
story_node = self
while story_node.choices:
choice = input("Enter 1 or 2 to continue the story: ")
if choice not in ["1", "2"]:
print("Invalid choice. Try again.")
else:
chosen_index = int(choice) - 1
chosen_child = story_node.choices[chosen_index]
print(chosen_child.story_piece)
story_node = chosen_child

And lastly, here's the story_data.py file that defines the structure of the story, creating instances of the tree node to represent different parts of the story:

最后,下面是Story_data.py文件,它定义了故事的结构,创建了树节点的实例来表示故事的不同部分:


from tree_node import TreeNode

story_data = TreeNode("""
You are in a forest clearing. There is a path to the left.
A bear emerges from the trees and roars!
Do you:
1 ) Roar back!
2 ) Run to the left...
""")

choice_a_1 = TreeNode("""
The bear returns and tells you it's been a rough week. After
making peace with
a talking bear, he shows you the way out of the forest.

YOU HAVE ESCAPED THE WILDERNESS.
""")

choice_a_2 = TreeNode("""
The bear returns and tells you that bullying is not okay before
leaving you alone
in the wilderness.

YOU REMAIN LOST.
""")

choice_b_1 = TreeNode("""
The bear is unamused. After smelling the flowers, it turns
around and leaves you alone.

YOU REMAIN LOST.
"""
)
choice_b_2 = TreeNode("""
The bear understands and apologizes for startling you. Your new
friend shows you a
path leading out of the forest.

YOU HAVE ESCAPED THE WILDERNESS.
"""
)

story_data.add_child(choice_a_1)
story_data.add_child(choice_a_2)
story_data.add_child(choice_b_1)
story_data.add_child(choice_b_2)

更多回答

Thanks @mkrieger1, I've added the code to my issue.

谢谢@mkrieger1,我已经将代码添加到我的问题中。

You have only added child nodes to story_data. None of the other nodes contain child nodes, so the while loop quits.

您只向STORY_DATA添加了子节点。其他节点都不包含子节点,因此While循环退出。

What, specifically should the program do instead of stopping? What graph of TreeNode items should the program traverse?

具体来说,程序应该做什么而不是停止?程序应该遍历哪些TreeNode项的图表?

Thanks @quamrana! You were right, I had to add more child nodes to choices and instead of using it as a graph with tuples, I just used instances of the TreeNode for the choices.

谢谢@quamrana!你是对的,我不得不向choices添加更多的子节点,而不是将它用作一个带有元组的图,我只是使用TreeNode的实例来进行选择。

优秀答案推荐

as stated in the comments,

正如评论中所说,



You have only added child nodes to story_data. None of the other nodes contain child nodes, so the while loop quits.



To give a bit more detail, after executing the code in story_data.py

更详细地说,在执行STORY_data.py中的代码之后


the TreeNodes look like this:

TreeNode如下所示:


story_data.story_piece = "You are in a forest [..]"
story_data.choices = [choice_a_1, choice_a_2, choice_b_1, choice_b_2]

choice_a_1.story_piece = "The bear [..] tells you it's been a rough week [..]"
choice_a_1.choices = []

choice_a_2.story_piece = "The bear [..] tells you bully is not okay [..]"
choice_a_2.choices = []

choice_b_1.story_piece = "The bear is unamused [..]"
choice_b_1.choices = []

choice_b_2.story_piece = "The bear understands [..]"
choice_b_2.choices = []

The first iteration of the while loop works because the conditional, story_node.choices, finds a non-empty list which evaluates to True.

While循环的第一次迭代之所以起作用,是因为条件Story_node.chotions找到了一个计算结果为True的非空列表。


When the user chooses a value from ["1","2"], they are effectively choosing between choice_a_1 and choice_a_2. For both of these, story_node.choices will be an empty list, which evaluates as False, ending the loop without a second iteration.

当用户从[“1”,“2”]中选择一个值时,他们实际上是在CHOICE_A_1和CHOICE_A_2之间进行选择。对于这两个选项,STORY_NODE.CHOICES将是一个空列表,其计算结果为FALSE,从而结束循环而不进行第二次迭代。


If you're ever unsure about a python program's behavior, you can always debug it with an IDE like VS Code/PyCharm/etc., or use print statements right before and/or after wherever your suspect the problem is.

如果您不确定某个Python程序的行为,您可以随时使用VS Code/PyCharm/等之类的IDE对其进行调试,或者在您怀疑问题所在的任何位置之前和/或之后使用print语句。


So in this case, try running the program with the following change:

因此,在本例中,尝试使用以下更改运行该程序:


class TreeNode:
# .. (unchanged)

def traverse(self):
story_node = self
while story_node.choices:
print("story_node.choices before choice " + story_node.choices) # <-- added
choice = input("Enter 1 or 2 to continue the story: ")
if choice not in ["1", "2"]:
print("Invalid choice. Try again.")
else:
chosen_index = int(choice) - 1
chosen_child = story_node.choices[chosen_index]
print(chosen_child.story_piece)
story_node = chosen_child
print("story_node.choices after choice " + story_node.choices) # <-- added

def __repr__(self): # <-- added
return self.story_piece # <-- added

With this you would get something like the following output:

这样,您将获得类似于以下输出的内容:


Once upon a time...
1) [..]
2) [..]

story_node.choices before choice [
The bear returns and tells you it's been a rough week. [..]
,
The bear returns and tells you that bullying is not okay [..]
,
The bear is unamused. [..]
,
The bear understands and apologizes [..]
]
Enter 1 or 2 to continue the story: 1

The bear returns and tells you it's been a rough week. [..]

story_node.choices after choice []

notice the last line of output is an empty list, which is likely leading to the behavior you mention.

请注意,输出的最后一行是一个空列表,这很可能导致您提到的行为。




As an aside, ["1", "2"] is semantically a list, a tuple would look like ("1", "2"), but the behavior is identical in this instance as there are no assignments or hashing, you can read a bit about differences)

顺便说一句,[“1”,“2”]在语义上是一个列表,一个元组看起来像(“1”,“2”),但在这个实例中的行为是相同的,因为没有赋值或散列,您可以了解一些区别)


You may also want to check out the visitor design pattern as it would let you move your processing of each TreeNode outside the object itself, since after the first iteration, there's no reason for the traverse method to be inside TreeNode. Example here

您可能还想查看访问者设计模式,因为它可以让您将每个TreeNode的处理移动到对象本身之外,因为在第一次迭代之后,没有理由让遍历方法在TreeNode内部。的例子


更多回答

Wow, thank you @mxt for taking the time to explain all of that to me. I see now how the error was occurring. Instead of using the graph, i just used instances of the TreeNode to make the branching for responses flow better with the added child nodes for choices..if you want to check out what I changed, you can find it here : github.com/Sk-223/Adventure_Game.git Thanks again for being so helpful, I really do appreciate your time!

哇,谢谢你@MXT花时间给我解释了这一切。我现在明白了这个错误是如何发生的。我没有使用图形,而是使用TreeNode的实例来使响应的分支更好地流动,并增加了子节点供选择。如果您想查看我所做的更改,可以在此处找到:githorb.com/sk-223/Adenture_Game.git再次感谢您的帮助,真的非常感谢您抽出时间!

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com