Gil Robledo
Gil Robledo

Reputation: 1

Python text based game room to room movement for House Game

Need help in adjusting my code, i am unable to move to other room, i can only move North and south no other room is being recognized. the goal is to be able to go back and forth between the rooms. Rooms in use are as followed : Attic, Living Room, Foyer, Front Porch, Bedroom, Basement, Kitchen, Dinning Room.

The Design pattern is as followed: ( Starting out in the Attic)

Attic (Start) - Go South to Living Room

Living Room - Go West to Dining Room, North back to Attic, East to Foyer

Foyer - Go West to Living Room, North to Bedroom, South to Front Porch

Bedroom - Go south to Foyer

Front Porch - Go North to Foyer

dining Room - Go East to Living Room, North to Kitchen, South to Basement

Kitchen - Go South to Dining Room

Basement - Go North to Dining Room

My Code is as Followed :

 rooms = {
'Attic': {'South': ' Living Room'},

'Living Room': {'East': 'Foyer', 'West': 'Dining Room','North': 'Attic'},
      
'Foyer': {'North': 'Bedroom', 'South': 'Front Porch', 'West': 'Living Room'},
     
'Bedroom': {'South': 'Foyer'},
    
'Dining Room': {'East': 'Living Room', 'North': 'Kitchen', 'South': 'Basement'},
     
'Kitchen': {'South': 'Dining Room'},
     
'Basement': {'North': 'Dining Room'},
    
'Front Porch': {'North': 'Foyer'},
    
}

def possible_moves():
  moves = rooms[location].keys()
  print("Option:", *moves)

location = 'Attic'

Direction = ''

while True:
  print('You are in the', location)

  possible_moves()

  direction = input("Which way would you like to go? ")

  if direction == 'Exit' or Direction == 'exit':

    print('Thank you for playing!')
    break
  elif direction == 'South' or direction == 'south':
      location = 'Living Room'
      print('You are in the', location)

      possible_moves()

      direction = input("Which way would you like to go? ")

      if direction == 'East' or direction == 'east':

        location = 'Foyer'
        print('You are in the', location)
        possible_moves()
        direction = input("Which way would you like to go? ")

  elif direction == 'North' or direction == 'north':

        location = 'Attic'
        print('You are in the', location)
        possible_moves()
        direction = input("Which way would you like to go? ")

  else:
   print('Invalid')

Upvotes: 0

Views: 354

Answers (1)

Emlore
Emlore

Reputation: 3

Your code should look like this. I noticed that the if statements were unnecessary.

rooms = {
    'Attic': {'South': 'Living Room'},

    'Living Room': {'East': 'Foyer', 'West': 'Dining Room','North': 'Attic'},
  
    'Foyer': {'North': 'Bedroom', 'South': 'Front Porch', 'West': 'Living Room'},
 
    'Bedroom': {'South': 'Foyer'},
 
    'Dining Room': {'East': 'Living Room', 'North': 'Kitchen', 'South': 'Basement'},
 
    'Kitchen': {'South': 'Dining Room'},
  
    'Basement': {'North': 'Dining Room'},

    'Front Porch': {'North': 'Foyer'},
}

def possible_moves():
    moves = ", ".join(list(rooms[location].keys()))
    print("Options: ", *moves)

location = 'Attic'

Direction = ''

while True:
    print('You are in the ', location)

    possible_moves()

    direction = input("Which way would you like to go?\n>> ")
    while direction.title() not in list(rooms[location].keys()):
        direction = input('invalid direction\n>> ')
    location = rooms[location][direction.title()]

A compact and simple solution.

If you want the if statements though, just replace the location == rooms[location][direction.title()] with the following code:

if direction.title() == "South":
    location = rooms[location]["South"]
elif direction.title() == "North":
    location = rooms[location]["North"]
elif direction.title() == "East":
    location = rooms[location]["East"]
elif direction.title() == "West":
    location = rooms[location]["West"]

But I recommend doing the previous example.

Upvotes: 0

Related Questions