Reputation: 33
#A dictionary for the simplified text game that links a room to other rooms.
rooms = {
'Great Hall': {'South': 'Bedroom'},
'Bedroom': {'North': 'Great Hall', 'East': 'Cellar'},
'Cellar': {'West': 'Bedroom'}
}
instructions = 'Welcome to the Wizard game, reach the bedroom to win. To move type: go North, go East, go West, go South'
directions = ['go North', 'go South', 'go East', 'go West']
print(instructions)
while True:
current_room = 'Great Hall'
if current_room == 'Bedroom':
print('Congratulations! You have reached the Bedroom and defeated the Wizard!')
break
# displays the players current location
print('You are in the {}.'.format(current_room))
# gets the users input
command = input('\nWhat do you wish to do?')
# this controls the movement
if command in directions:
if command in current_room:
current_room = command.split("")
else:
# if the player inputs a bad movement
print('You cant go that way!')
# checks to see if the player quits the game
elif command == 'exit':
print('Thanks for playing!')
break
# invalid command
else:
print('Invalid input')
I'm still new to working with python, and for my class I have to build a simple text based game. So far I have the code above, which is designed to simply move between the rooms. So far it reads the input and tells me if I cant go a certain direction and if the input is invalid, but it doesn't recognize when its supposed to go to the next room and I am not quite sure how to do so. Any help is appreciated here.
Upvotes: 0
Views: 3225
Reputation: 1865
You can add the below function at the end of the while loop:
def move_room(current_room,command ): #command = the direction the player inputs
directions = rooms.get(current_room)
if "South" in command:
room = rooms.get(current_room).get("South")
if "North" in command:
room = rooms.get(current_room).get("North")
if "West" in command:
room = rooms.get(current_room).get("West")
if "East" in command:
room = rooms.get(current_room).get("East")
if room is None:
room = current_room # in case there is no room attached in that direction
return room
(Soon there will be switch statements in Python to make this function prettie) Use like this:
while...
current_room = move_room(current_room, command)
Also move where you defined current_room to be above the while loop or it will be reset
Upvotes: 0
Reputation: 180
First of all, current_room = 'Great Hall'
this line shouldn't be in the while loop. If this line is inside the while loop no matter what the user does, they'll always end up back in the Great Hall.
The directions say to enter the commands in this format: goLeft, goRight...etc
. So when the input is read the variable is storing the direction prepended by go
. You want to remove go
from the variable before comparing it with the directions you have available in each room. Also, to check which directions you have available, you need to do rooms[roomName]
. So if the current_room
= Great Hall, rooms[current_room]
will give you {'South': 'Bedroom'}
.
Your code should look like this:
#A dictionary for the simplified text game that links a room to other rooms.
rooms = {
'Great Hall': {'South': 'Bedroom'},
'Bedroom': {'North': 'Great Hall', 'East': 'Cellar'},
'Cellar': {'West': 'Bedroom'}
}
instructions = 'Welcome to the Wizard game, reach the bedroom to win. To move type: goNorth, goEast, goWest, goSouth'
directions = ['goNorth', 'goSouth', 'goEast', 'goWest']
print(instructions)
current_room = 'Great Hall'
while True:
if current_room == 'Bedroom':
print('Congratulations! You have reached the Bedroom and defeated the Wizard!')
break
# displays the players current location
print('You are in the {}.'.format(current_room))
# gets the users input
command = input('\nWhat do you wish to do? ') # this controls the movement
if command in directions:
command = command.replace("go", "")
if command in rooms[current_room].keys():
current_room = rooms[current_room][command]
print(current_room)
else:
# if the player inputs a bad movement
print('You cant go that way!')
# checks to see if the player quits the game
elif command == 'exit':
print('Thanks for playing!')
break
# invalid command
else:
print('Invalid input')
Upvotes: 3
Reputation: 1171
A few things you can look at and try to address yourself:
your instructions prompt the user to enter one of [goNorth, goEast, goWest, goSouth]
, but your directions
list has a space between the words (e.g. go North
), so the user will always be entering an invalid command. Make sure instructions
and directions
are consistent with one another.
if command in current_room
isn't doing what you intend it to. Note that current_room
is a string (e.g. "Great Hall"), and not a dictionary. rooms[current_room]
will return a dictionary that maps directions to the next room, so you could consider checking command in rooms[current_room]
instead.
Related to (2), command
is of form go<direction>
, whereas rooms[current_room]
is a dictionary with the <direction>
as keys. Currently, command in rooms[current_room]
would be false because the command
is not consistent with the way the directions are named in the room. You can consider using command[2:]
to extract the direction from the user command.
Your while loop always resets the current room to 'Great Hall'. If that is the initial room, then that assignment can go outside (before) the while loop.
Upvotes: 0