2016-11-11 23 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