Skip to main content
 首页 » 编程设计

python之"Hello! Python"重构的 Wumpus 游戏之空列表,卡在循环中

2024年11月01日10落叶

我正在阅读“Hello! Python”这本书,但我卡在了“ list 2.10 - 重构的 wumpus 游戏”上。

我已经创建了我的函数,并从我们按照本书创建的游戏的先前版本中删除了“多余”代码。

现在我在第一次调用 print_caves 函数后陷入循环并返回一个空白数组。这是我得到的:

Welcome to the Wumpus! 
... 
 === of the cave you wish to enter next 
0 : [] 
1 : [] 
2 : [] 
... 
19 : [] 
---------- 

这是我的代码。

from random import choice 
 
# functions , Convenience functions 
def create_tunnel(cave_from, cave_to): 
    """ Create a tunnel between cave_from and cave_to """ 
    caves[cave_from].append.cave(cave_to) 
    caves[cave_to].append(cave_from) 
def visit_cave(cave_number): 
    """ Mark a cave as visited """ 
    visited_caves.append(cave_number) 
    unvisited_caves.remove(cave_number) 
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[cave_number]) >= 3: 
        cave_number = choice(cave_list) 
    return cave_number 
def print_caves(): 
    """ Print out the current cave structure """ 
    for number in cave_numbers: 
        print number, ":", caves[number] 
    print '----------' 
# functions , Cave-creation 
def setup_caves(cave_numbers): 
    """ Create the starting list of caves """ 
    caves = [] 
    for cave in cave_numbers: 
        caves.append([]) 
    return caves 
def link_caves(): 
    """ Make sure that all of the caves are connected by 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  finish_caves(): 
    """ Link the rest of the caves with one-way tunnels """ 
    for cave in cave_numbers: 
        while len(caves[cave]) < 3: 
        passage_to = choose_cave(cave_numbers) 
        caves[cave].append(passage_to)   
# functions, player interaction 
def print_location(player_location): 
    """ Tell the player about where they are """ 
    print "--- You are in cave", player_location, "---" 
    print ">>> From here, you can see caves:", caves[player_location], " <<<" 
    if wumpus_location in caves[player_location]: 
        print "I smell the Wumpus lurking nearby!" 
def  get_next_location(): 
    """ Get the players next location """ 
    print "Which cave next?" 
    player_input = raw_input(">") 
    if (not player_input.isdigit() or 
        int(player_input) not in caves[player_location]): 
        print player_input + "?" 
        print "I cant go that way" 
        return None 
    else: 
        return int(player_input)     
#define variables 
cave_numbers = range(0,20) 
unvisited_caves = range (0,20) 
visited_caves = [] 
caves = setup_caves(cave_numbers) 
# welcome player  
print "0==[:::::::::::::> Welcome to the Wumpus! <::::::::::::]==0" 
print ">>> You can see", len(cave_numbers), "caves! <<<" 
print " === To play, just type a number" 
print " === of the cave you wish to enter next" 
# call functions 
visit_cave(0) 
print_caves() 
link_caves() 
print_caves() 
finish_caves() 
#define locations 
wumpus_location = choice(cave_numbers) 
player_location = choice(cave_numbers) 
while player_location == wumpus_location: 
    player_location = choice(cave_numbers) 
while True: 
    print_location(player_location) 
    new_location = get_next_location() 
    if new_location is not None: 
        player_location = new_location 
    if player_location == wumpus_location: 
        print "Arrgh! You've been devoured by a Wumpus!" 
        break 

请您参考如下方法:

您会注意到您在 create_tunnel 中有一个拼写错误:caves[cave_from].append.cave(cave_to) 应该是 caves[cave_from]。附加(cave_to)

这从未导致错误的事实表明您的 link_caves 有问题,因为它是调用 create_tunnel 的函数。我相信您想将最后两行移到 while 语句中:

def link_caves(): 
    """ Make sure that all of the caves are connected by 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) 

原来,没有洞穴被标记为已访问,这意味着未访问的洞穴列表永远不会缩小,因此 while 语句将永远运行。

如果您进行这两项更改,您的代码将运行:

0==[:::::::::::::> Welcome to the Wumpus! <::::::::::::]==0 
>>> You can see 20 caves! <<< 
 === To play, just type a number 
 === of the cave you wish to enter next 
0 : [] 
1 : [] 
2 : [] 
3 : [] 
4 : [] 
5 : [] 
6 : [] 
7 : [] 
8 : [] 
9 : [] 
10 : [] 
11 : [] 
12 : [] 
13 : [] 
14 : [] 
15 : [] 
16 : [] 
17 : [] 
18 : [] 
19 : [] 
---------- 
0 : [11, 12, 8] 
1 : [18] 
2 : [13] 
3 : [9, 14, 15] 
4 : [17] 
5 : [12, 18] 
6 : [15] 
7 : [15] 
8 : [0] 
9 : [11, 3] 
10 : [14] 
11 : [0, 9] 
12 : [0, 5, 16] 
13 : [19, 2] 
14 : [3, 19, 10] 
15 : [3, 7, 6] 
16 : [12] 
17 : [19, 4] 
18 : [5, 1] 
19 : [14, 13, 17] 
---------- 
--- You are in cave 14 --- 
>>> From here, you can see caves: [3, 19, 10]  <<< 
Which cave next? 
>