-2

I am supposed to write a program for a simple text-based game. You have to move from one room to the next. The map starts in the Observation room and you can go south to the Rec room or exit the game. From the Rec room you can go north back to the Observation room, exit or you can go east to the Lab. From the Lab you can go west back to the Rec room or exit.

I need to display game instructions, display the room the player is in each time they move, and if they want to exit an exit message displays and game ends.

player_name = input('Enter your name Captain:')

def game_play_instructions():
    # print a main menu and the commands
    print('-----------------------------------------------------------------------------------')
    print("Hello, Captain", player_name, '!')
    print("You will need to navigate around the spaceship to get familiar with your surrounds.")
    print("Move commands: go South, go North, go East, go West")
    print('Type Exit to end the game at anytime.')
    print('-----------------------------------------------------------------------------------')

game_play_instructions()

current_room = 'Observation Room'

rooms = {
   'Observation Room': {'South': 'Rec Room'},
   'Rec Room': {'East': 'Lab', 'North': 'Observation Room'},
   "Lab": {"West": "Rec Room"}
}
directions = ['North', 'South', 'East', 'West', 'exit']
room = rooms['Observation Room']

while True:

    print()

    print('You are in the', room, '.')

    user_move = input('What would you like to do?')

    if user_move in directions:
        if user_move in room:
            room = rooms[room[user_move]]
    else:
        print('Invalid input, I do not understand!')
# function
def get_new_room(room, user_move):
    new_room = room  # declaring
    for i in rooms:  # loop
        if i == room:  # if
            if user_move in rooms[i]:  # if
                new_room = rooms[i][user_move]  # assigning new_state

    return new_room  # return



    print()

game_play_instructions()  # calling function

user_move = input('What would you like to do? ')  # asking user
direction = input(user_move)

while direction != 'exit':
    direction = input(user_move)

    if user_move == 'go East' or user_move == 'go West' or user_move == 'go North' or user_move == 'go South':  # if
        user_move = user_move[3:]
        new_room = get_new_room(room, user_move)  # calling function

    if new_room == room:  # if
        print('There is no passage that way, choose a different direction.')  # print
    else:
        room = new_room  # changing state value to new_state

else:
    print('Invalid direction!!')  # print

When I run my program this is what I get:

Enter your name Captain:CP
-----------------------------------------------------------------------------------
Hello, Captain CP !
You will need to navigate around the spaceship to get familiar with your surrounds.
Move commands: go South, go North, go East, go West
Type Exit to end the game at anytime.
-----------------------------------------------------------------------------------

You are in the {'South': 'Rec Room'} .

(PROGRAM SHOULD START IN OBSERVATION ROOM and room should update depending on the direction the player wants to go)

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 2
    you should print `current_room`, not `room`. And update that when they change to a new room. – Barmar Aug 04 '23 at 21:27
  • You need to split the command into words. Then you can check if the first word is `go` and then use the second word as the key in the `room` dictionary. – Barmar Aug 04 '23 at 21:27
  • you seem to have written the code twice, but slightly differently. – Barmar Aug 04 '23 at 21:29
  • You have a `while True:` loop at the top, and then `while direction != 'exit':` at the bottom. – Barmar Aug 04 '23 at 21:30
  • `user_move = input('What would you like to do? ') # asking user direction = input(user_move)` is going to ask for input twice. First it will ask what they want to do. Then it will use their answer as the prompt in the next request for input. You don't want this. – Barmar Aug 04 '23 at 21:31

0 Answers0