2016-01-24 115 views
0

在python中,我試圖創建一個簡單的遊戲,玩家在嘗試避免wumpus時選擇下一個洞穴。在建立它在某種程度上停止,永不初始洞穴結構開始遊戲,並出現以下錯誤:無法在python中啓動遊戲。遊戲永遠不會啓動,下面的錯誤顯示

Traceback (most recent call last): 
    File "/Users/JPagz95/Documents/Hunt_the_Wumpus_3.py", line 80, in <module> 
    link_caves() 
    File "/Users/JPagz95/Documents/Hunt_the_Wumpus_3.py", line 28, in link_caves 
    this_cave = choose_cave(visited_caves) 
    File "/Users/JPagz95/Documents/Hunt_the_Wumpus_3.py", line 50, in choose_cave 
    cave_number = choice(cave_list) 
    File "/Users/JPagz95/anaconda/lib/python3.5/random.py", line 253, in choice 
    i = self._randbelow(len(seq)) 
KeyboardInterrupt 

這裏我是爲遊戲當前的代碼。任何幫助表示讚賞:)

from random import choice 

cave_numbers = [x for x in range(20)] 
unvisited_caves = [x for x in range(20)] 
visited_caves = [] 

def setup_caves(cave_numbers): 
    """ create a starting list of caves """ 
    caves = [] 
    for number in cave_numbers: 
     caves.append(number) 
    return caves 

def visit_cave(cave_number): 
    """ mark a cave as visited """ 
    visited_caves.append(cave_number) 
    unvisited_caves.remove(cave_number) 

def print_caves(): 
    """ print out the current cave structure """ 
    for number in cave_numbers: 
     print (number, ":", caves[number]) 
    print ('----------') 

def link_caves(): 
    """ make sure all of the caves are connected with two way tunnels""" 
    while unvisited_caves != []: 
     this_cave = choose_cave(visited_caves) 
     next_cave = choose_cave(unvisited_caves) 
    create_tunnel(this_cave, next_cave) 
    visit_cave(next_cave) 

def create_tunnel(cave_from, cave_to): 
    """ create a tunnel between cave_from and cave_to """ 
    caves[cave_from].append(cave_to) 
    caves[cave_to].append(cave_from) 

def finish_caves(): 
    """ link the rest of the caves with one way tunnels """ 
    for caves in cave_numbers: 
     while len(caves) < 3: 
      passage_to = choose_cave(cave_numbers) 
      caves[cave].append(passage_to) 

def choose_cave(cave_list): 
    """ pick a cave from a list, provided that the 
    cave has less than 3 tunnels """ 
    cave_number = choice(cave_list) 
    while len(caves) >= 3: 
     cave_number = choice(cave_list) 
    return cave_number 

def print_location(player_location): 
    """ tell the player about where they are """ 
    print ("You are in a cave ", player_location) 
    print ("From here you can see caves: ") 
    print (caves[player_location]) 
    if wumpus_location in caves[player_location]: 
     print ("I smell a wumpus!") 

def get_next_location(): 
    """ Get the player's next location """ 
    print ("Which cave next?") 
    player_input = input(">") 
    if (not player_input.isdigit() or 
     int(player_input) not in 
      caves[player_location]): 
     print (player_input + "?") 
     print ("That's not a direction I can see!") 
     return none 
    else: 
     return int(player_input) 

caves = setup_caves(cave_numbers) 

visit_cave(0) 
print_caves() 
link_caves() 
print_caves() 
finish_caves() 

wumpus_location = choice(cave_numbers) 
player_location = choice(cave_numbers) 
while player_location == wumpus_location: 
    player_location = choice(cave_numbers) 

print ("Welcome to Hunt the Wumpus!") 
print ("You can see ", len(cave_numbers), " caves") 
print ("To play, just type the number") 
print ("of the cave you wish to enter next") 

while True: 
    print_location(player_location) 
    new_location = get_next_location() 
    if new_location != None: 
     player_location = new_location 
    if player_location == wumpus_location: 
     print ("Aargh! You got eaten by a wumpus!") 

回答

2

第一:

visited_caves = [] 

然後:

link_caves() 

然後:

this_cave = choose_cave(visited_caves) 

然後:

cave_number = choice(cave_list) 

您正在發送一個空的listrandom.choice(),失敗。

變化link_caves()只處理非空list S:

def link_caves(): 
    """ make sure all of the caves are connected with two way tunnels""" 
    if not (visited_caves and unvisited_caves): 
     return 
    this_cave = choose_cave(visited_caves) 
    next_cave = choose_cave(unvisited_caves) 
    create_tunnel(this_cave, next_cave) 
    visit_cave(next_cave) 

注意,如果該功能沒有通過無效參數random.choice(),就已經得到了根本卡住了,因爲它採用的是while循環中條件從未改變。我注意到您也在choose_cave()中執行此操作,while len(caves) >= 3: cave_number = choice(cave_list)。這個特定的片段將檢查caves的長度,如果它是>= 3,則從cave_list中選擇一個隨機洞穴。然後它會檢查caves的長度,發現它與以前一樣,永遠選擇一個隨機洞穴等。也許你期待random.choice()從傳遞的序列中刪除其選定的項目。它不這樣做。如果你願意,你可以通過多種方式刪除它,比如在cave_number之後選擇caves.remove(cave_number)

請記住,除了提示您的問題和我指出的其他問題之外,您的代碼中可能還有其他錯誤。

+0

感謝在這個生病的樣子,早上時查看調試!我喜歡你冗長的答案! –

0

TigerhawkT3很好的答案。

我最近查看了這段代碼的一個版本,你可以用一個小小的修改來解決一個問題。修正了link_caves錯別字()函數,通過縮進的最後兩行,讓他們在裏面的「而」循環:

def link_caves(): 
    """ make sure all of the caves are connected with two way tunnels""" 
    while unvisited_caves != []: 
     this_cave = choose_cave(visited_caves) 
     next_cave = choose_cave(unvisited_caves) 
     create_tunnel(this_cave, next_cave) 
     visit_cave(next_cave) 

這種變化應該創建所需的隧道,並調用visit_cave()每次通過循環。 visit_cave()的最後一行從unvisited_caves列表中刪除了正確的洞穴,這是TigerhawkT3提到的必要的。

你還有一些問題需要修復,以完成初始化並讓遊戲運行。

回溯語句會告訴你從哪裏開始尋找。

您可以添加打印語句來顯示洞穴列表,以幫助您在代碼掛起之前調試代碼。

輸出可能會更容易,如果你改變洞窟的數量小東西像4-5代替20