我试图使用列表,循环和函数在python上创建一个迷宫,但由于某种原因它不能工作

时间:2017-10-03 04:57:34

标签: python arrays list while-loop maze

我的迷宫应该由三个不同的关卡组成。我的简单级别工作得很好,但平均和困难的人不断出错。我只是按照第一个使用的相同模式和逻辑,所以我真的不明白。有人可以解释什么是错的以及为什么它是错的?谢谢! :(

这是一个很好的简单级别:

def easy():

    maze1_list = []
    # [Description, North, East, South, West]
    maze1_list.append(['Available Directions: East', None, 1, None, None]) #0
    maze1_list.append(['Available Directions: East, South, West', None, 2, 3, 0]) #1
    maze1_list.append(['Available Directions: West', None, None, None, 1]) #2
    maze1_list.append(['Available Directions: North, East', 1, 4, None, None]) #3
    maze1_list.append(['Available Directions: South, West', None, None, 5, 3]) #4

    current_tile = 0
    done = False

    directions = {'North' or 'north' or 'NORTH':1, 'East' or 'east' or 'EAST':2, 'South' or 'south' or 'SOUTH':3, 'West' or 'west' or 'WEST':4}
    #Source: obtained from <https://stackoverflow.com/questions/46511833/how-do-you-make-a-simple-3x3-maze-on-python-using-lists/46517061#46517061>
    #Date: <October 2, 2017>
    #Name of Author/Programmer: <Anton vBR>

    while not done:
        print('')
        print(maze1_list[current_tile][0])
        x = '0'
        available = False

        while not available:

            while not directions.get(x.title()): #Source: obtained from <http://programarcadegames.com/index.php?chapter=lab_adventure>
                                                 #Date: <October 2, 2017>
                                                 #Name of Author/Programmer: <Paul Vincent Craven>
                if x!='0':
                    print('Please choose an available direction. ')
                x = input('Which direction will you take? \n')

            next_move = directions.get(x.title()) #Source: obtained from <http://programarcadegames.com/index.php?chapter=lab_adventure>
                                                  #Date: <October 2, 2017>
                                                  #Name of Author/Programmer: <Paul Vincent Craven>   
            next_tile = maze1_list[current_tile][next_move]

            if next_tile:
                available = True
                current_tile = next_tile

            elif x == 'quit':
                quit()

            else:
                print('Please choose an availble direction.')
                x = '0'

        if current_tile == 5:
            print('Congratulations! You found the exit. \nGoodbye.')
            done = True

以下是通过第一个“可用路线:东方

后不再进步的平均水平”
def average():

    maze2_list = []
    # [Description, North, East, South, West]
    maze2_list.append(['Available Directions: East', None, 1, None, None]) #0
    maze2_list.append(['Available Directions: East, South, West', None, 2, 5, 0]) #1
    maze2_list.append(['Available Directions: East, West', None, 3, None, 1]) #2
    maze2_list.append(['Available Directions: West', None, None, None, 2]) #3
    maze2_list.append(['Available Directions: East, South', None, 5, 8, None]) #4
    maze2_list.append(['Available Directions: North, East, West', 1, 6, None, 4]) #5
    maze2_list.append(['Available Directions: East, West', None, 7, None, 5]) #6
    maze2_list.append(['Available Directions: South, West', None, None, 11, 6]) #7
    maze2_list.append(['Available Directions: North, South', 4, None, 12, None]) #8
    maze2_list.append(['Available Directions: East', None, None, 5, 3]) #9
    maze2_list.append(['Available Directions: South, West', None, None, 14, 9]) #10
    maze2_list.append(['Available Directions: North, South', 7, None, 15, None]) #11
    maze2_list.append(['Available Directions: North, East', 8, 13, None, None]) #12
    maze2_list.append(['Available Directions: East, West', None, 12, None, 12]) #13
    maze2_list.append(['Available Directions: North, East', 10, 15, None, 14]) #14

    now_tile = 0
    finished = False

    compass = {'North' or 'north' or 'NORTH':1, 'East' or 'east' or 'EAST':2, 'South' or 'south' or 'SOUTH':3, 'West' or 'west' or 'WEST':4}
    #Source: obtained from <https://stackoverflow.com/questions/46511833/how-do-you-make-a-simple-3x3-maze-on-python-using-lists/46517061#46517061>
    #Date: <October 2, 2017>
    #Name of Author/Programmer: <Anton vBR>

    while not finished:
        print('')
        print(maze2_list[now_tile][0])
        y = '0'
        present = False

        while not present:

            while not compass.get(y.title()): #Source: obtained from <http://programarcadegames.com/index.php?chapter=lab_adventure>
                                              #Date: <October 2, 2017>
                                              #Name of Author/Programmer: <Paul Vincent Craven>
                if y!='0':
                    print('Please choose an available direction. ')
                y = input('Which direction will you take? \n')

            next_move2 = compass.get(y.title()) #Source: obtained from <http://programarcadegames.com/index.php?chapter=lab_adventure>
                                                #Date: <October 2, 2017>
                                                #Name of Author/Programmer: <Paul Vincent Craven>
            next_tile2 = maze2_list[now_tile][next_move2]

            if next_tile2:
                available = True
                now_tile = next_tile2

            else:
                print('Please choose an available direction.')
                y= '0'
        if now_tile == 15:
            print('Congratulations! You found the exit. \nGoodbye.')
            finished = True

我不再包含困难的那个,所以我可以看到并分析平均水平出了什么问题,然后将其应用到困难的水平。

0 个答案:

没有答案