【问题标题】:How do you make a simple 3x3 maze on python using lists?如何使用列表在 python 上制作一个简单的 3x3 迷宫?
【发布时间】:2017-10-01 11:14:56
【问题描述】:

我有这个项目,我必须制作一个基本的基于文​​本的迷宫/冒险游戏。到目前为止,这是我的代码,但很奇怪。我希望它能够随时退出并在输入不在选项中时显示“请选择可用方向”。我知道我的代码是错误的,但我不知道该怎么做。有人可以帮我吗?谢谢!

顺便说一句,我将此链接用作指南http://programarcadegames.com/index.php?chapter=lab_adventure

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


current_tile = 0
print(maze_list[0])


done = False
while done != True:

    print('')
    print(maze_list[current_tile][0]) # available directions
    move = raw_input('Which direction will you take?' )
    if move == 'East' or 'east' or 'EAST':
        next_tile = maze_list[current_tile][2]
        current_tile = next_tile

        print('')
        print(maze_list[current_tile][0])
        move = raw_input('Which direction will you take? ')
        if move == 'South' or 'south' or 'SOUTH':
          next_tile = maze_list[current_tile][3]
          current_tile = next_tile

          print('')
          print(maze_list[current_tile][0])
          move = raw_input('Which direction will you take? ')
          if move == 'East' or 'east' or 'EAST':
            next_tile = maze_list[current_tile][2]
            current_tile = next_tile

            print('')
            print(maze_list[current_tile][0])
            move = raw_input('Which direction will you take? ')
            if move == 'South' or 'south' or 'SOUTH':
              next_tile = maze_list[current_tile][3]
              current_tile = next_tile
              print('Nice')


              if move == 'Quit' or 'quit' or 'QUIT':
                print('Better luck next time. \nGoodbye.')
              elif move == 'West' or 'west' or 'WEST':
                next_tile = maze_list[4]
                current_tile = next_tile
              else:
                print('Please choose an available direction')

          elif move == 'Quit' or 'quit' or 'QUIT':
            print('Better luck next time. \nGoodbye.')
          elif move == 'North' or 'north' or 'NORTH':
            next_tile = maze_list[1]
            current_tile = next_tile
          else:
            print('Please choose an available direction')

        elif move == 'Quit' or 'quit' or 'QUIT':
          print('Better luck next time. \nGoodbye.')
        elif move == 'East' or 'east' or 'EAST':
          next_tile = maze_list[current_tile][2]
          current_tile = next_tile
        else:
          print('Please choose an available direction')

    elif move == 'Quit' or 'quit' or 'QUIT':
      print('Better luck next time. \nGoodbye.')
        else:
        print('Please choose an available direction.')

【问题讨论】:

  • from random import random; print("\n".join(''.join('/' if random()<0.5 else '\\' for _ in range(3)) for _ in range(3))) ;)

标签: python maze


【解决方案1】:

我开始尝试这样做是一件有趣的事情。 (下面这可能不是答案,但无论如何......发布它)

但是让我告诉你一些事情:该指南的质量非常“低”(是的,我可能太苛刻了,但是嘿!只是一个意见)。即使认为这是初学者编程,我也没有理由不介绍字典和其他常用工具。

我在第 10 点或第 11 点停下来,继续我目前所做的。 基本上目标是根据你给我的网站上的数字移动到厨房。如果您有兴趣了解更多或有任何问题,我很乐意为您解答。

祝你好运:)

import sys
room_list = [] 

#2,3,4
room_list.append(["Bedroom 2, You can go north and east.",3,1,None,None])
room_list.append(["South Hall, ...",4,2,None,0])
room_list.append(["Dining Room, ...",5,None,None,1])
room_list.append(["Bedroom 1, ...",None,4,0,None])
room_list.append(["North Hall, ...",6,5,1,3])
room_list.append(["Kitchen, ...",None,None,2,4])
room_list.append(["Balcony, ...",None,None,4,None])

#5
current_room = 0

#6
#print(room_list)

#7
#print(room_list[0])

#8
#print(room_list[current_room])

#9
#print(room_list[current_room][0])

#10
done = False


#10++ Here I started writing my own code... :)

directions = {"North":1,"East":2,"South":3,"West":4}

#11
while not done:

    print("\n"+room_list[current_room][0])
    q = "0"
    valid_choice = False

    while not valid_choice:

        while not directions.get(q.title()):
            if q != "0":
                print("Try again...")
            q = raw_input("Where do you want to go? (North,East,South or West) Or Quit (Q)")
            if q.title() == "Q":
                print("You pressed Q. Exit")
                sys.exit()

        next_move = directions.get(q.title()) 
        next_room = room_list[current_room][next_move]

        if next_room:
            valid_choice = True
            current_room = next_room                
        else:
            print("You can't go that way")
            q = "0" # reset question

    if current_room == 5:
        print("\nYou are in: "+room_list[5][0])
        done = True

问:您好,谢谢!如果你不介意我问,那该怎么办 方向 = {"North":1,"East":2,"South":3,"West":4} 和 direction.get(q.title()) 做什么?

directions = {"North":1,"East":2,"South":3,"West":4} 在这种情况下是字典。它包含键 (North,East,South,West) 及其对应的值 (1,2,3,4)。因此,当您输入方向[“North”] 时,您实际上得到的值是 1。

然而,还有另一种获取值的方法,那就是使用 Directions.get() 函数。当您这样做并且密钥不存在时,返回 None。例如。 Directions.get("monkey") = None but Directions.get("East") = 2。当我们使用 while not 循环时,这非常有用。在用户输入有效的内容之前,q 将为 None 并且问题会重复。

最后,q 是从问题中返回的字符串。 str.title() 函数返回首字母大写的字符串。例如。 "hellOOO".title() = "Hellooo" 这就是为什么你可以输入 "eastT" 并仍然得到 "East" 的值,这是你需要让方向.get("East") 工作的值。

问:还有一个问题(对不起,我对此很陌生),如果我想补充的话 一个退出功能,我会在哪里插入它?我试着把它排成一行 在 else 语句之前,但我得到了错误。

我补充说:

import sys

if q.title() == "Q":
    print("You pressed Q. Exit")
    sys.exit()

【讨论】:

  • 嗨,谢谢!如果你不介意我问,directions = {"North":1,"East":2,"South":3,"West":4} 和 Directions.get(q.title() ) 做吗?
  • @user8638151 在我的“答案”中回答:)
  • 还有一个问题(对不起,我对此很陌生),如果我想添加一个退出功能,我应该在哪里插入它?我尝试将它放在 else 语句之前的行中,但出现错误。
  • @user8638151 不用担心...... SO 起作用的原因是因为有些人愿意帮助那些不熟悉事物的人(我也是)。我的代码已更新为另一种选择。
猜你喜欢
  • 2022-11-15
  • 2011-06-10
  • 2012-09-20
  • 2021-06-25
  • 2015-09-02
  • 1970-01-01
  • 2016-05-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多