Search code examples
pythonmaze

How do you make a simple 3x3 maze on python using lists?


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!

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.')

Solution

  • 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.

    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.

    Good luck :)

    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: 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} 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.

    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.

    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: 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.

    I added:

    import sys
    

    And

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