Python: Bulko

Programming Project 2:
An Adventure Game (Part 2)

This is a continuation of Project 1. In this project, you will add functionality to your Project 1 code to:

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 variables room1, room2, room3, ..., room7. Instead, the descriptions of the rooms will be stored in a file called ProjectData.txt.

Start by downloading this file here and storing it on your computer in the same directory/folder as your program. Examine the file.

To implement the data file:

The inventory

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 a command-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 <item>:  pick up an item currently in the room
drop <item>: 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 to look() 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 you exactly which 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!

Important: your code must still be data-independent.

If you don't remember what this means, go back and reread the section on this topic in Project 1. You should not hard-code anything that your program must read from the data file. That includes room names, connectivity information, or room contents. If you make references to "Living Room", "Kitchen", or "knife" anywhere in your code, you are doing it wrong and you will be penalized 20% on your grade for this assignment.

Reflection

When you complete this project, you will have a simple adventure game where you can navigate an arbitrary map, pick up and carry objects, and drop them in other places. You can easily create a new arena for your game by using a different ProjectData.txt file. Try creating one for your apartment or home, with various objects you could find there. (This is NOT required for this project.)

To make the game more interesting, add more commands to your CLI, along with functions that carry them out. You can create the command "use <item>" that does interesting things depending on the item and current location. For example, you could add "key" to the list of items that are found in the Kitchen. Then, you could add code that prevents you from going south from the Upper Hall into the Master Bedroom unless you use the command "use key" first to unlock the door: but to do that, you would previously have had to go the Kitchen, get the key, and carry it with you so that you've have it when you arrived at the Upper Hall. Note that these additional commands require exceptions to the "data-independence" rule, since you have to write code that only applies to the key, or the Master Bedroom. These cases are the only places that exceptions to the rule is allowed.

There are endless possibilities to have fun here! When I was a student at UT, I lived on campus for several years in Jester dormitory. For fun, I wrote a game called "Escape From Jester". When the game started, the player was a student standing at the main entrance to Jester East. There was a Daily Texan newsstand nearby. If the player did "get newspaper", the headline explained that an oven in the cafeteria exploded, and a good part of Jester had been destroyed, but there were a few dorm residents who were trapped inside. The player's job was to go rescue them. Many of the doors and stairwells were blocked: this was my way of keeping the map manageably small. Some doors required specific keys, and one could only be opened by a credit card (the player had to figure that one out!) There was a hole in the floor of one dorm room, and the player could get to the floor below by jumping through it - but only if they picked up and dropped a mattress into the hole first to cushion the landing. Of course, several of the people that the player interacted with in the dorm were my real-life friends, who said and did things that my friends would say and do.

Turning in the Assignment:

The program that you will write should appear in a file called Project2.py. Submit the file via Canvas before the deadline shown at the top of this assignment by attaching it to the Canvas "Assignment" called "Project 2".