您如何使用清单在python上制作一个简单的3x3迷宫? [英] How do you make a simple 3x3 maze on python using lists?

查看:83
本文介绍了您如何使用清单在python上制作一个简单的3x3迷宫?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have this project wherein I have to make a basic text-based maze/adventure game. This is my code so far but it's weird. I want it to be able to quit anytime and for it to display 'Please choose an available direction' when the input is not among the choices. I know my code is wrong but I don't know what to do about it. Can someone help me out? Thanks!

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

btw, I used this link as a guide 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.')

推荐答案

我开始尝试这样做很有趣. (下面的答案可能不是答案,但无论如何……将其发布)

I started doing this as a fun thing to try. (And this below might not be an answer but anyway... posting it)

但是,让我告诉您一些信息:该指南的质量非常低"(是的,我可能太苛刻,但是,嘿!只是一种意见).甚至以为这是初学者编程,我看不出不引入字典和其他常用工具的原因.

But let me tell you something: The guide is very "low" quality (Yes I might be too harsh but hey! just an opinion). Even thought this is beginner programming I don't see a reason not to introduce dictionaries and other common tools.

我在第10点或第11点停下来,继续我到目前为止的一切. 基本上,目标是按照您给我的网站上的图移到厨房.如果您有兴趣了解更多信息或有任何疑问,我很乐意为您解答.

I stopped at point 10 or 11 and continued with what I had so far. Basically the goal is to move to the kitchen according to the figure in the site you gave me. If you are interested to understand more or have any questions I can gladly answer them.

祝你好运:)

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


Q:谢谢你!如果您不介意我问,该怎么办 Directions = {"North":1,"East":2,"South":3,"West":4}和 directions.get(q.title())可以吗?

Q: Hi, thanks for this! If you don't mind me asking, what do the directions = {"North":1,"East":2,"South":3,"West":4} and the directions.get(q.title()) do?

在这种情况下,

directions = {"North":1,"East":2,"South":3,"West":4}.它包含键(北,东,南,西)及其对应的值(1、2、3、4).因此,当您输入Directions ["North"]时,实际上得到的是值1.

directions = {"North":1,"East":2,"South":3,"West":4} in this case is a dictionary. It contains keys (North,East,South,West) and their corresponding values (1,2,3,4). So when you type directions["North"] you actually get the value 1.

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

There is however another way to get the values and that is by using the directions.get() function. When you do and the key does not exist None is returned. E.g. directions.get("monkey") = None but directions.get("East") = 2. This is quite useful when we use a while not loop. Until the user inputs something valid the q will be None and the question repeated.

最后,q是从问题返回的字符串. str.title()函数返回首字母大写的字符串.例如. "hellOOO" .title()="Hellooo",这就是为什么您可以输入"easT"并仍然获得"East"的原因,这是使Directions.get("East")工作所需的值.

Finally q is the string that was returned from the question. And str.title() function returns the string with the first letter capital. E.g. "hellOOO".title() = "Hellooo" and that is why you can type in "easT" and still get "East" which is the value you need to make the directions.get("East") work.

Q:还有一个问题(对不起,我对此很陌生),如果我想补充一下 退出功能,我应该在哪里插入?我试图将它放在行中 在其他语句之前,但我得到了错误.

Q: one more question (sorry i'm quite new at this), if i wanted to add a quit function, where would i insert it? i tried placing it in lines before else statements but i get errors.

我添加了:

import sys

还有

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

这篇关于您如何使用清单在python上制作一个简单的3x3迷宫?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆