2016-11-11 20 views
1

我一直在学习python的最近几个月,现在我正在尝试创建一个文本冒险游戏。程序/设置的要求如下: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_segment大多寻求帮助功能。评论描述了我正面临的问题,我想知道如何存储所有提示和选择数据。最好是创建一个带有三个选择的提示词典?如果我这样做,我将如何去关联level1_choice1与level2_prompt1?

让我知道如果有什么不清楚。

非常感谢!

+0

虽然它可能不是最好的代表,你可以*在这里使用字典。最外面的键将是第一级的选择,其余的可能性的字典,作为值,给出该选择。这将是一个结构递归的例子。 – BlackVegetable

回答

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 
+0

是的,这正是我所要找的,谢谢! –

1

它看起来最好的处理方式是两个有两个二维数组 - 一个用于提示,另一个用于选择。第一个索引将指定级别,下一个将指定它是哪个提示/选择。这将是这个样子:

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?", 
      "..."]] 

然后,接入说为第一级第二个提示,你只需访问prompt[0][1]。然后,您应该可以轻松地跟踪索引,以选择要向用户显示哪些提示/选择。

+0

例如,你会如何表示路径“选择1,2,0,1”?您提出的双深度阵列将不允许先前决定的序列确定未来选项。如果我理解正确,那么这个表示只允许'#Levels * 3 = 12'提示,而作者表示需要'3 + 9 + 27 + 81 = 120提示'。 – BlackVegetable

+0

我假设OP已经有了一些方法来跟踪游戏的路径,并且正在寻找一种方法来表示所有提示,以便他们不需要手动指定一堆变量名称。如果问题是如何跟踪路径,那么他们应该使用一个int列表(它将表示在每个级别采用的选择数组的索引)。 – user3030010

+0

啊,我想我跟着你。如果你将这个假设转化为你的答案,我会给它一个+1。 – BlackVegetable