Question: TheRoomclass: Your first task will be to create a PythonclasscalledRoom. This class must contain at least the two following methods: __init__().In addition toself, it takes
TheRoomclass:
Your first task will be to create a PythonclasscalledRoom. This class must contain at least the two following methods:
- __init__().In addition toself, it takes seven arguments:
- name, a string representing the name of the room.
- north, a string representing the name of the room you would reach if you went north from this room.
- east, a string representing the name of the room you would reach if you went east from this room.
- south, a string representing the name of the room you would reach if you went south from this room.
- west, a string representing the name of the room you would reach if you went west from this room.
- up, a string representing the name of the room you would reach if you went up a flight of stairs from this room.
- down, a string representing the name of the room you would reach if you went down a flight of stairs from this room.
- A value ofNonefor one of these arguments means you can't travel in that direction from this room.
- displayRoom()
- displayRoomtakes no arguments other thanself. It tells the Room object to display its name, along with the names of all of the rooms it is adjacent to. For example, ifmyRoomwere a pointer to the Upper Hall, thenmyRoom.displayRoom()would print out:
Room name: Upper Hall Room to the north: Bathroom Room to the east: Small Bedroom Room to the south: Master Bedroom Room below: Living Room - followed by a blank line. Note that if the value of a parameter is None, nothing should be printed for that direction.
The data structure:
This is the data structure you must build to represent the house:
Writing the program:
A skeleton of your project has already been started for you. Download this file:Project1.py
Let's walk through the file from the bottom up.
- main()
- This is already written for you.
- create a variable calledcurrentthat represents the room the player is currently in. Theglobalkeyboard makescurrenta global variable that can be accessedand changedfrom anywhere. Note thatcurrentis an instance of the classRoom, aRoomobject,notjust a string representing the name of the room.
- callloadMap()to create the data structure.
- calldisplayAllRooms()to print out the data structure, so we can verify that it was created correctly.
- setcurrentto the Living Room, our starting point for testing our code.
- execute a series of calls tomove()andlook().
- end the program.
- loadMap()
- This is already written for you.
- create a variable calledfloorPlan, a list containing all of theRoomobjects. Theglobalkeyboard makesfloorPlana global variable that can be accessed and changed from anywhere.
- define seven local variablesroom1, room2, room3, ..., room7that contain data for each of the seven rooms in our house. Each list contains the following informationin this order:
- the name of this room
- the name of the room you would reach by traveling north from this room
- the name of the room you would reach by traveling east from this room
- the name of the room you would reach by traveling south from this room
- the name of the room you would reach by traveling west from this room
- the name of the room you would reach by traveling up from this room
- the name of the room you would reach by traveling down from this room
- calculate the value offloorPlanby callingcreateRoom()seven times and collecting the objects created into a list.
- displayAllRooms()
- You must write this.
- For each room infloorPlan, use the methoddisplayRoom()to print out the properties of that room.
- move()
- You must write this.
- move()takes one argument: the direction the player wants to move from the current room.
- move()should return aRoomobject, which represents the room the player ends up in after the move. This can be used by the calling program to set the new value ofcurrent.
- If there is no room in the indicated direction,move()should print "You can't move in that direction." Otherwise, you should print "You are now in the NEWROOMNAME."
- getRoom()
- You must write this.
- getRoom()is a utility function that takes one argument, the name of a room (a string), and returns the correspondingRoomobject. You will find it useful to call this in writingmove().
- look()
- You must write this.
- look()takes no arguments and returns no values. It simply prints out the message, "You are currently in the CURRENTROOMNAME."
- createRoom()
- You must write this.
- createRoom()is a utility function that takes one argument, a list containing connectivity data for one room. It uses the data in that list to create a newRoomobject with all of its properties defined according to the contents of the list. This will be used byloadMap()to create all of theRoomobjects infloorPlan.If you don't understand what the argument list contains, reread the description of the variablesroom1, room2, room3, ..., room7in the description ofloadMap()above.
Expected output:
Your output should look like the following:
Room name: Living Room Room to the north: Dining Room Room above: Upper Hall Room name: Dining Room Room to the south: Living Room Room to the west: Kitchen Room name: Kitchen Room to the east: Dining Room Room name: Upper Hall Room to the north: Bathroom Room to the east: Small Bedroom Room to the south: Master Bedroom Room below: Living Room Room name: Bathroom Room to the south: Upper Hall Room name: Small Bedroom Room to the west: Upper Hall Room name: Master Bedroom Room to the north: Upper Hall You are currently in the Living Room. You can't move in that direction. You can't move in that direction. You are now in the Dining Room. You are now in the Living Room. You are now in the Upper Hall. You are currently in the Upper Hall. You are now in the Small Bedroom. You can't move in that direction. You are now in the Upper Hall. You are now in the Master Bedroom. You are now in the Upper Hall. You are now in the Bathroom. You are now in the Upper Hall. You are currently in the Upper Hall. You can't move in that direction. You are currently in the Upper Hall. You are now in the Living Room. You are now in the Dining Room. You are now in the Kitchen. You can't move in that direction. >>>
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
