“你好!Python”重构了Wumpus游戏 - 空列表,卡在循环中

时间:2014-04-03 16:42:49

标签: python

我跟随"你好!蟒"书和我坚持上市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

2 个答案:

答案 0 :(得分:2)

您会注意到create_tunnel中有拼写错误:caves[cave_from].append.cave(cave_to)应该是caves[cave_from].append(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?
>

答案 1 :(得分:0)

你的setup_caves函数会创建一堆空洞穴。

也许你想在打印之前链接并完成洞穴?


link_caves的缩进似乎已关闭。