I will provide the code from Project 1.
This is a continuation of Project 1. In this project, you will add functionality to your Project 1 code to:
- read the map data from a text file instead of using hardcoded assignment statements to set up your floor plan.
- enable the definition of items that can be found in a room, picked up by the player, carried around in the player'sinventory, and dropped in another room.
- replace the test code hardcoded into the main program with an interactive user interface, where the player can type in simple one-word or two-word commands to navigate the map and interact with items.
The data file
In this phase of the project, you no longer want to hard-code the room descriptions, as done in Part 1 of the project via the variablesroom1, room2, room3, ..., room7. Instead, the descriptions of the rooms will be stored in a file calledProjectData.txt.
Start by downloading this filehereand storing it on your computer in the same directory/folder as your program. Examine the file.
- First, notice that each line in the file contains a series of items separated by commas. Each item is either a string or the word "None". The first seven items correspond to the seven items you saw previously in Project 1: the name of the room, followed by the room you would reach by traveling each of the directions from the current room, in the same order as they appeared in Project 1.
- Then notice that some lines have more than seven items. Any additional items in the line represent random objects that can be initially found in that room. For example, the third line in the file looks like:
"Kitchen",None,"Dining Room",None,None,None,None,"cup","fork","knife"
This indicates the name of the room (Kitchen), the door to the east that leads to the dining room, and no other doors. The three additional items at the end indicate that you can find a cup, a fork, and a knife in the Kitchen.
To implement the data file:
- Modify the__init()__method to add a parameter and a property called "contents", which will store the list of objects currently located in that room as a property of theRoomobject.
- Modify thecreateRoom()method to incorporate this new parameter.
- Remove all of the code from the body ofloadMap().
- Write new code forloadMap(), using the appropriate file operations to read the data fromProjectData.txtand create the sevenRoomobjects and the listfloorPlan.
The inventory
- ModifydisplayRoom()so that it prints one additional line. After printing all of the neighboring rooms, it should also print "Room contents:", followed by the list of items currently in the room.
The output should look like this:
Room name: Dining Room Room to the south: Living Room Room to the west: Kitchen Room contents: ['plate']
If there are no items in the room, it should look like this:
Room name: Living Room Room to the north: Dining Room Room above: Upper Hall Room contents: []
- Modifylook()so that, after printing "You are currently in the CURRENTROOM", it also prints the contents of the room.
If there are items in the room, it should look like this:
You are currently in the Kitchen. Contents of the room: cup fork knife
If there are no items in the room, it should look like this:
You are currently in the Living Room. Contents of the room: None
- Add a global variableinventory, a list that represents the items the player is carrying. In the main program, initialize it to the empty list.
- Add a new functionpickup()that attempts to remove an item from the current room and add it toinventory. It takes one parameter, a string representing the item to be picked up. If the item is present in the room, print the message "You now have the ITEM." If the item is not there, print "That item is not in this room."
- Add a new functiondrop()that attempts to remove an item frominventoryand add it to the room's contents. It takes one parameter, a string representing the item to be dropped. If the item is in the inventory, print the message, "You have dropped the ITEM." If the item was not in the inventory, print the message, "You don't have that item."
- Add a new functionlistInventory()that shows what the player is currently carrying.
If there are objects in the inventory, the output should look like this:
You are currently carrying: plate fork
If there are no items in the inventory, it should look like this:
You are currently carrying: nothing.
The interactive user interface and expected output
Delete all of the lines in the main program of Project 1 starting with the comment "TEST CODE" and replace it with acommand-line interpreter(CLI). A CLI is a block of code that asks the user to enter a command (possibly with arguments), executes code that carries out the purpose of the command, and then asks for another command. It repeats this until the user types in a command that indicates there are no more commands.
The best way to explain what the CLI does is to show you sample interactive output from the program. Below is what appears in a sample run of a working program in the Shell window. Items in red are typed in by the player.
You are currently in the Living Room. Contents of the room: None Enter a command: help look: display the name of the current room and its contents north: move north east: move east south: move south west: move west up: move up down: move down inventory: list what items you're currently carrying get : pick up an item currently in the room drop : drop an item you're currently carrying help: print this list exit: quit the game Enter a command: north You are now in the Dining Room. Enter a command: inventory You are currently carrying: nothing. Enter a command: look You are currently in the Dining Room. Contents of the room: plate Enter a command: get plate You now have the plate. Enter a command: look You are currently in the Dining Room. Contents of the room: None Enter a command: inventory You are currently carrying: plate Enter a command: exit Quitting game. >>>
I included a call tolook()before I asked the player to enter the first command. It makes sense to let the player know where he/she is when the game first starts.
When the player enters the command "help", your program should simply print out the text shown above. However, not only does this tell the player what commands exist, this also tells youexactlywhich commands your CLI must support.
So how do you write this code? Basically, a CLI is code that conforms to the user confirmation pattern we learned earlier this semester!
- You ask the user to enter a command.
- Start a loop.
- The main part of the loop is a big if/elif/else structure that figures out what to do for each possible command. If the command is "look", you make a call tolook(). If the command is "get knife", you make a call topickup("knife"). And so on. Note that some commands (get and drop) take items as arguments, so you need to handle the fact that the command that was input by the player may consist of more than one word.
- After executing the command, ask the player to enter another command, and return to the top of the loop.
- When the command entered by the player is "Exit", you print the message "Quitting game." and leave the loop. This is the end of your program.