Posts: 3
Threads: 1
Joined: Apr 2020
Apr-05-2020, 10:43 PM
(This post was last modified: Apr-06-2020, 07:02 PM by buran.)
Hi there.
Learning Python with my daughter, and we're looking for some help with her project.
Code below:
class room():
def __init__(self, desc, exit, moveto):
self.desc = desc
self.exit = exit
self.moveto = moveto
door = room('You see a door', 'entrance', 1)
entrance = room("You are in the entrance", 'door', door)
hall = room('You are in the Hall', 'entrance', entrance)
bedroom = room('You are in a bedroom', 'hall', hall)
currentRoom = bedroom
def playerAction():
print(currentRoom.desc)
print('Exits:', currentRoom.exit)
while True:
playerAction()
action = input('What do you do? >')
if action == currentRoom.exit :
print(action + 'Entered')
currentRoom = currentRoom.moveto
else :
print('No') Question: When trying to move back a room, say from hall to bedroom, it gives an error that currentRoom is not defined. I'm guessing the variables in the Room class are defined in order : how would we be able to move from "entrance" to "hall".
Thanks
Posts: 1,358
Threads: 2
Joined: May 2019
Appreciate the effort, but your tags did not work to show this as code. You need to use the button (beside Bold, Italic, etc.)
Still I think I see what is going on. In your program logic it looks like you can only go to one place from each location. From hall you can go to entrance but no where else, from what I see.
Your rooms are a linked list. From one instance you can go to another. It needs to be a double linked list in order to allow movement in both directions. Alternatively, you could put your movements on a stack (a special kind of list) and when the user types "back" you pop the last room off the stack and it becomes the current room, essentially retracing your steps.
Not sure that answers your question, so if you can clarify a little it would help.
J
Posts: 3
Threads: 1
Joined: Apr 2020
Apr-05-2020, 11:37 PM
(This post was last modified: Apr-05-2020, 11:37 PM by Slack86.)
A for effort. Let me try that again.
what you're saying is basically it : i can only move rooms in cadence. Now if I added a second moveto instance, say
class room():
...
self.moveto2 = moveto2
entrance = room("You are in the entrance", 'door', door, entrance)
If i try to move from entrance to door, the error still says that I have not defined door. I'm assuming my entire logic and the way I'm using the class is wrong. I understand that technically door comes before entrance, so it sees currentRoom as being door and stops. Just not sure if I can make it keep going down the variable list as is to see the variable after the one that's used.
Now if I were to want to go from door to bedroom, let's say, I wouldn't be able to use a "back to previous room" function, so with my current layout that would be impossible to do?
class room():
def __init__(self, desc, exit, moveto):
self.desc = desc
self.exit = exit
self.moveto = moveto
door = room('You see a door', 'entrance', 1)
entrance = room("You are in the entrance", 'door', door)
hall = room('You are in the Hall', 'entrance', entrance)
bedroom = room('You are in a bedroom', 'hall', hall)
currentRoom = bedroom
#End room definitions
#Defintes function for and prints what you see on the screen
def playerAction():
print(currentRoom.desc)
print('Exits:', currentRoom.exit)
print('************************')
while True:
playerAction()
action = input('What do you do? >')
#Defines what to do with the inputs
if action == currentRoom.exit :
currentRoom = currentRoom.moveto
else :
print('No')
Posts: 3
Threads: 1
Joined: Apr 2020
Apr-06-2020, 10:50 AM
(This post was last modified: Apr-06-2020, 10:50 AM by Slack86.)
Update on this issue:
I had to create a dictionary and add the rooms to it, then call the room function at my if statement.
class room():
def __init__(self, desc, exit, details):
self.desc = desc
self.exit = exit
self.details = details
world = {}
world['door'] = room('You see a door.', 'entrance', 'Test')
world['entrance'] = room("You are in the entrance.", 'door', 'A white door blocks your way.')
world['hall'] = room('You are in the Hall.', ('entrance', 'bedroom'), 'The hardwood floor is scuffed and there is a mirror on the wall.')
world['bedroom'] = room('You are in a bedroom.', 'hall', 'There is only an old metal framed bed and a worn dresser.')
currentRoom = world['bedroom']
def playerAction():
print(currentRoom.desc)
print('Exits:', currentRoom.exit)
print('************************')
time.sleep(0.5)
while True:
playerAction()
action = input('What do you do? >')
#Defines what to do with the inputs
if action in currentRoom.exit :
currentRoom = world[action]
Posts: 1,358
Threads: 2
Joined: May 2019
There are different ways to approach this. One way that would add flexibility is to have exit be a list. That way a room like 'hall' could have options of 'bathroom', 'bedroom1', 'bedroom2', and 'stairs'. Once in bedroom1 the exits could be 'hall' and 'attic'. With this, you could have some twists and turns, make it so entering bedroom2 does not go back to the hall - the door locked behind you, so you have to go through some other route.
You would accomplish movement by seeing if the destination is in the exit list. If it is, that becomes the current room.
Just an idea.
Posts: 1,358
Threads: 2
Joined: May 2019
Idea on how to do and you can expand upon - modified your code to allow a list of exits. Changed init to just have the name and description, defined the rooms, then added the exits to each room. Trying to put the exits into init was problematic as rooms would reference other rooms that had not been created yet. Had to make the responses check against the name field. If you run it, can run around using the different connections.
class room():
def __init__(self, desc, name):
self.desc = desc
self.name = name
# Define the rooms
door = room('You see a door','door')
entrance = room("You are in the entrance",'entrance')
hall = room('You are in the Hall','hall')
bedroom = room('You are in a bedroom','bedroom')
# Make the map
door.exit = [entrance]
entrance.exit = [door,hall]
hall.exit = [entrance,bedroom]
bedroom.exit = [hall]
currentRoom = bedroom
def playerAction():
print(currentRoom.desc)
print('Exits:')
for ruhm in currentRoom.exit :
print(ruhm.name)
while True:
playerAction()
action = input('What do you do? >')
moved = False
for ruhm in currentRoom.exit :
if (action) == ruhm.name :
print(action + 'Entered')
currentRoom = ruhm
moved = True
if not moved :
print('No')
|