Python文本冒险存储数据时遇到的问题
过去几个月我一直在学习python,现在我正在尝试创建一个文本冒险游戏。程序/设置的要求如下: 创建一个文本冒险游戏,根据用户的选择总共有4个级别 根据您以前的选择,每个级别必须有三个不同的选择 例如,级别1有3个选项,每个选项都会将您带到级别2。根据您在级别1中所做的选择,您现在将有3个选择,导致级别1有3个选择,级别2有9个选择,级别3有27个选择,级别4有81个选择 在我的代码中,我有一个创建提示和选项的基本设置,但我很难找到一种方法将每个特定提示与三个选项关联起来 以下是我目前的代码:Python文本冒险存储数据时遇到的问题,python,python-3.x,Python,Python 3.x,过去几个月我一直在学习python,现在我正在尝试创建一个文本冒险游戏。程序/设置的要求如下: 创建一个文本冒险游戏,根据用户的选择总共有4个级别 根据您以前的选择,每个级别必须有三个不同的选择 例如,级别1有3个选项,每个选项都会将您带到级别2。根据您在级别1中所做的选择,您现在将有3个选择,导致级别1有3个选择,级别2有9个选择,级别3有27个选择,级别4有81个选择 在我的代码中,我有一个创建提示和选项的基本设置,但我很难找到一种方法将每个特定提示与三个选项关联起来 以下是我目前的代码:
# every prompt has an associated 3 choices with it, and every
choice made has a prompt with three more choices to be made
associated with it.
def print_segment(prompt, choice1, choice2, choice3):
print(prompt)
print("[A] " + choice1)
print("[B] " + choice2)
print("[C] " + choice3)
answer = input().lower()
if answer == "A": # if answer is A, print the next prompt associated with A
if answer == "B": # if answer is B, print the next prompt associated with B
if answer == "C": # if answer is C, print the next prompt associated with C
# level 1 choices
level1_choice1 = "Go to the scene of the most recent murder."
level1_choice2 = "Go to the scene of the first murder."
level1_choice3 = "Wait a few hours and see how the situation develops."
# level 1 prompts
level1_prompt1 = '''You are a murder investigator and you are tasked on the case of a mysterious string of killings
that have happened in the past few weeks. How do you start?'''
# level 2 prompts
level2_prompt1 = "You arrive at the scene of the most recent murder. What would you like to do first?"
level2_prompt2 = "You arrive at the scene of the first murder. What would you like to do first?"
level3_prompt3 = "You receive a letter from an unknown source saying that you should meet them at a specific location. What do you do?"
print_segment(level1_prompt1, level1_choice1, level1_choice2, level1_choice3)
我正在尽可能彻底地解释这一点,这样事情就不会变得混乱,但我主要是在我的print_段函数中寻求帮助。这些评论描述了我面临的问题,我想知道如何存储所有提示和选择数据。最好创建一个包含三个选项的提示字典吗?如果我这样做,我将如何将level1_choice1与level2_prompt1关联起来
如果有什么不清楚,请告诉我
非常感谢 处理这个问题的最佳方法似乎是两个二维数组,一个用于提示,另一个用于选择。第一个索引将指定级别,下一个索引将指定它是哪个提示符/选项。它看起来像这样:
prompts = [["Go to the scene of the most recent murder.",
"Go to the scene of the first murder.",
"Wait a few hours and see how the situation develops."],
["You arrive at the scene of the most recent murder. What would you like to do first?",
"..."]]
然后,要访问第一级的第二个提示符,只需访问
提示符[0][1]
。然后,您应该能够轻松地跟踪索引,以选择要向用户显示的提示/选项。这就是您所追求的结构吗
choices = {
1 : { 'prompt' : {
'prompt' : 'Level 1 prompt',
'A' : 'Choice A',
'B' : 'Choice B',
'C' : 'Choice C' },
},
2 : { 'promptA' : {
'prompt' : 'Level 2 prompt A',
'A' : 'A Choice A',
'B' : 'A Choice B',
'C' : 'A Choice C' },
'promptB' : {
'prompt' : 'Level 2 prompt B',
'A' : 'B Choice A',
'B' : 'B Choice B',
'C' : 'B Choice C' },
'promptC' : {
'prompt' : 'Level 2 prompt C',
'A' : 'C Choice A',
'B' : 'C Choice B',
'C' : 'C Choice C' },
},
3 : { 'promptA' : {
'prompt' : 'Level 3 prompt A',
'A' : 'A Choice A',
'B' : 'A Choice B',
'C' : 'A Choice C' },
'promptB' : {
'prompt' : 'Level 3 prompt B',
'A' : 'B Choice A',
'B' : 'B Choice B',
'C' : 'B Choice C' },
'promptC' : {
'prompt' : 'Level 3 prompt C',
'A' : 'C Choice A',
'B' : 'C Choice B',
'C' : 'C Choice C' },
}
}
def print_segment(level, prev_choice = ''):
d = choices.get(level).get('prompt' + prev_choice)
print(d.get('prompt'))
for c in 'ABC':
print("[{}] {}".format(c, d.get(c)))
# Output
>>> print_segment(1)
Level 1 prompt
[A] Choice A
[B] Choice B
[C] Choice C
>>> print_segment(2, 'A')
Level 2 prompt A
[A] A Choice A
[B] A Choice B
[C] A Choice C
>>> print_segment(3, 'B')
Level 3 prompt B
[A] B Choice A
[B] B Choice B
[C] B Choice C
虽然它可能不是最好的表示,但您可以在这里使用字典。最外层的键将是第一级的选择,其中包含剩余可能性的字典(给定该选择)作为值。这将是一个结构递归的例子。例如,您将如何表示路径
选项1、2、0、1
?您建议的两个深度阵列不考虑以前决定未来选项的顺序。如果我理解正确,此表示法只允许#Levels*3=12
提示,而作者表示需要3+9+27+81=120提示
。我假设OP已经有某种方法跟踪游戏的路径,并且正在寻找一种表示所有提示的方法,这样他们就不需要手动指定一组变量名。如果问题是如何跟踪路径,那么他们应该使用一个整数列表(表示在每个级别上获取的选择数组的索引)。如果你把这个假设放到你的答案中,我会给它一个+1。是的,这正是我想要的,谢谢!