Issue with directions

  Kiến thức lập trình

I have everything working great for my school assignment correctly except that it is supposed to display the available move directions (I think the issue is in def show status(): ) depending on which room the player is in. The premise of the game is to collect all the items without running into the monster in the cellar. My code is below:

#Pizzaria Game
def show_instructions():
    #Game instructions
    print('Welcome to the pizzaria!')
    print('-------------------------')
    print('To move between rooms use the commands: go North, go South, go East, go West')
    print('Collect all six pizza toppings to win to feed the pizza monster to win!')
    print("To select an item type 'get [item]'")
    print('Make sure not to run into the pizza monster before his pizza is ready or you lose...')
    print('You can exit at any time by typing Exit')
    print('-------------------------')

#dictionary of rooms and the directions you can go within each room
rooms = {
        'Dining Room': {'South': 'Supply Closet', 'North': 'Game Room', 'East': 'Kitchen', 'West': 'Bathroom'},
        'Game Room': {'East': 'Event Room', 'South': 'Dining Room', 'Item': 'Sausage'},
        'Event Room': {'West': 'Game Room', 'Item': 'Cheese'},
        'Bathroom': {'East': 'Dining Room', 'Item': 'Mushroom'},
        'Kitchen': {'West': 'Dining Room', 'North': 'Office', 'Item': 'Anchovies'},
        'Office': {'South': 'Kitchen', 'Item': 'Pepperonis'},
        'Supply Closet': {'North': 'Dining Room', 'East': 'Basement', 'Item': 'Olives'},
        'Basement': {'West': 'Supply Closet'}

    }


current_room = 'Dining Room'
player_inventory = [] #player inventory
pizza_monster_room = 'Basement' #monster location



def get_new_state(direction_from_user, current_room):
    global player_inventory
    global rooms

    if direction_from_user in rooms[current_room]:
        new_room = rooms[current_room][direction_from_user]
        print("You moved to", new_room)

       # Check if the new room has an item
        if 'Item' in rooms[new_room]:
            item = rooms[new_room]['Item']
            print("You found an item:", item)

        return new_room
    else:
        print("You can't go that way.")
        return current_room
    
# Function to display player's status
def show_status():
        
        if 'Item' in rooms[current_room]:
            print("Item in Room:", rooms[current_room]['Item'])
        else:
            print("No item in this room.")
        print('--------------------')
        print("Inventory:", player_inventory)

        print("Available Directions:")
        available_directions = rooms[current_room].keys()
        available_directions = [direction for direction in available_directions if direction != 'Item']
        print(", ".join(available_directions))


def main():
    show_instructions()
    current_room = 'Dining Room'
    print('Current Room: Dining Room')
    while True:
        show_status()
        print('--------------------')
        command = input('Enter a command: ').lower()
        action, *params = command.split()

        #moving between rooms
        if action == 'go': 
            direction = params[0].capitalize()
            if direction in rooms[current_room]:
                current_room = get_new_state(direction, current_room)  
            else:
                print('Invalid direction!')
        
        #collecting items
        elif action == 'get':
            if len(params) == 0:
                print('Please specify the item name.')
            else:
                item_name = ' '.join(params).lower()  # Input item name to lowercase
                # Check if the item exists in the current room
                if 'Item' in rooms[current_room]:
                    room_item = rooms[current_room]['Item']
                    if item_name.lower() == room_item.lower():
                        player_inventory.append(room_item)
                        del rooms[current_room]['Item']
                        print(f'You obtained the {room_item} from this room.')
                    else:
                        print('Item not found in this room.')
                else:
                    print('No item in this room.')
        
        #if player wants to quit
        elif action == 'quit':
            break
        
        #invalid input
        else:
            print('Invalid input')

        #check for winning conditions
        if len(player_inventory) == 6 and current_room == 'Basement':
            print('Congratulations!n'
                  'You have collected all pizza toppings and fed the pizza monster!n'
                  'You win!')
            break

        #check for losing conditions
        if current_room == 'Basement' and len(player_inventory) < 6:
            print("You ran into the pizza monster without his pizza!n"
                  'You lose!')
            break

if __name__ == "__main__":
    main()

New contributor

etx_522 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT