In my years as an instructor at the University of Utah, my students have
enjoyed programming adventure games more than any other project I have
assigned. Adventure games give the programmer a way to interact with the user in
a personal, imaginative way. In addition, they can be programmed with
relatively simple code, but they can also make use of elegant object-oriented
programming. Your task is to use your programming skills, along with your
creativity and writing skills to produce a text-based adventure game.
If you have applets enabled in your browser, you should see my implementation of a
simple text-based adventure game with images below. (Note that images are
optional in your solution.)
To win this adventure, your task is to get out of the building. Here are
the commands to win:
Writing an adventure game can be a daunting project, even for experienced
programmers. To help get you started, you may download the source code
for the above application / applet (written in Java). See the 'Example
source code' section below.
Your program and documentation will be anonymized and then judged on two subjective criteria:
Creativity / Storytelling - Your adventure game will be played by
three judges and evaluated for creativity in storytelling. Games that
have interesting plot lines, well-written text, and clear instructions will
score well. Your goal is to use written language and clever programming
to engage the user in interesting ways.
Good luck, and have lots of fun!
Sample code for a text adventure with images:
Source code for the above example is now available! Please download the
following zip file:
source.zip
(If you do not have a program to unzip this file, you can download each file
manually here. Just make sure to build the same
source directory tree in a directory named 'source'. In this directory,
place the directories called 'adventure' and 'image'.)
Compiling the code from the command line
To compile your code, unzip the source code. (You may put the 'source'
directory anywhere you want, in my examples I'll assume you unzipped it to
C:\source.)
If you have a Java JDK installed and correctly added to your path, you can
compile this program from the command line (in Windows):
- Click 'Start'
- Select 'Run'
- Type '
cmd' and press return to open a command line shell.
- Type '
cd \source' to change directories to C:\source. If you
unzipped the files to another location, change to that directory.
- Type '
dir' and press return. You should see a directory listing with
exactly two subdirectories in it: 'adventure' and 'images'.
- Check to make sure the correct version of Java is installed and in your
path. Type '
javac -version'. You should get a list of options for
running the Java compiler. At the top of this list you should see the
version for the compiler, make sure it is 1.5.0 or newer.
- Compile the code. Type '
javac adventure/Adventure.java'. If
the command quietly completes (and it should), you have successfully compiled
the adventure game application.
Once you get the game compiled, it is easy to run:
- Make sure you are in the source directory. Follow steps 1 through 5
above.
- Type '
java adventure.Adventure' to run the adventure game.
If you have any problems, make sure of the following:
- You must have a recent version of the Java SDK installed. You
should have Java 5 (1.5.0) or later. (The Java JRE is
insufficient.) To get the latest version of Java, click here and
download JDK 6.
- If Java will not run from the command line, make sure to set your path up
correctly. View the installation instructions for JDK 6. A link to
the instructions can be found on the download webpage.
Compiling the code from within a Java IDE
There are many Java IDEs that make it simpler to edit, compile, and run Java
programs. Instructions for importing the Java code into your IDE are
specific to your IDE. Typically, you'll need to do the following steps:
- Unzip the source to a temporary location.
- Create a new Java application project in your favorite IDE.
- Use 'File->Import' or some similar feature to import the 'adventure'
package directory and all its contents to your project. If your IDE does
not have such a command, you may be able to simply copy the entire 'adventure'
directory into the source code directory for your project, and then 'refresh'
your project.
- Compile the project. You should have one class file for every .java file
in the project. Locate the root directory of the compiled class files - this is the
directory that contains the adventure directory that contains all the compiled
class files.
- Copy the 'images' directory to the root directory. You should now
have two directories in the root output directory of your project: 'images'
and 'adventure', and 'adventure' contains all the compiled class files.
- Try running your project. If you have the option, make sure to set
the current working directory to be the root output directory for your project.
The above instructions are only a suggestion, and will vary according to the
IDE that you use. If anyone has success importing this into their IDE,
please send me a step-by-step set of instructions and I'll post them here!
The most important thing to remember about running the application is that the
images directory needs to be located in the 'current working directory' that is
used when the application is run.
Sample code tutorial
The sample code for the graphical adventure program is quite large, but
fortunately you can use it simply if you concentrate on just a few
classes. (Click on the class names to view the API for that class.):
- GameState - This class manages the game. It creates Room and
Item objects, it handles commands typed by the user, and it has utility methods
that make it easier to manage the game state.
- Room - This interface specifies methods that must exist in Room
classes. Every room in the game is represented by its own class, and
these 'Room' classes all extend this Room interface.
- Item - This interface specifies methods that must exist in Item
classes. Every item in the game is represented by its own class, and
these
'Item' classes all extend this Item interface.
Also, you need to know that all images for the game are stored in the 'images'
directory. If you would like to see the complete API for this
application, you may see it here:
Complete API for this application
To adapt the game, you will:
-
Create new classes that represent new rooms. Each of your room classes
must implement the Room interface.
Also, you should create
one room class for every room you add. (This is not as much work as you
might think.)
- Create new classes that represent new items. Each of your item
classes
must implement the Item interface.
Also, you should create one
item class for every item you add.
- In your room and item classes, change the image that it loads,
change the name, change the description, and change the behavior (actions) that
occur.
- Add the correct type of
room and item objects to the game state. This is accomplished by
modifying the GameState constructor.t (Follow the pattern shown by
existing
statements in the GameState constructor.)
A simple example - Adding a room to the game.
Let's start with the image:
- Find a 640 x 480 pixel jpeg image to represent a kitchen, copy it to the
images directory, and rename it 'kitchen.jpg'. You can take the picture
with a digital camera or cell phone and resize it, or you can just use Google
images to find one by searching on "kitchen 640 480".
Next, create the 'Kitchen' class:
- In the 'rooms' package, create a new class called 'Kitchen.java'.
Copy everything from 'LabRoom.java' into 'Kitchen.java'.
- Edit this new
code for 'Kitchen.java' and change the class name and constructor name to
'Kitchen' so that it compiles.
- In the Kitchen class, find the constructor and change the name of the image
file it loads to be 'kitchen.jpg'.
- In the Kitchen class, find the getName method and change the name that it
returns to "Kitchen".
- In the Kitchen class, find the outputDescription method and change the
text. Make sure your text indicates that there is an exit to the
north.
- In the Kitchen class, find the takeAction method. Currently it has
code that allows the user to go in several directions. Remove the code
that deals with south and west, and change the code that moves north so that
when the player moves north, they enter the "Closet".
- Make sure the entire project, including the Kitchen class, compiles.
Next, change the 'ClosetRoom' class so that the player can move south to the
kitchen:
- Edit 'ClosetRoom.java' and find the takeAction method.
Currently this method only allows the player to move to the east.
Add statements that allow the player to go "south" to the "Kitchen".
(Just duplicate and modify the statements that allow the player to go east.)
- In the ClosetRoom class, change the description to indicate that the player
can move south.
Finally, you need to change the game state object so that it creates a kitchen
object when the game starts and adds it to the game:
- Edit 'GameState.java" and find the GameState constructor
- Find the statements that create room objects and adds them to the game.
- Add one more statement to this group of statements that creates a kitchen
object and adds it to the game:
this.addNewRoom(new Kitchen());
Run the game. Move west to the closet, then south to your brand new
kitchen!
You can follow these instructions to add dozens and dozens of rooms to your
game. (Remember to draw a map for yourself!)
A simple example - Adding items to the game.
These instructions assume that you successfully added the kitchen to the
game. Start by creating a 'Slippers' item:
Let's start with the image:
- Find a 100 x 100 pixel jpeg image to represent a pair of red slippers, copy it to the
images directory, and rename it 'slippers.jpg'. (Note: I used gif images
with a transparent color, but this requires more work.) You can take the picture
with a digital camera or cell phone and resize it, or you can just use Google
images to find one by searching on "red slippers".
Next, create the 'Slippers' class:
- In the 'items' package, create a new class called 'Slippers.java'.
Copy everything from 'Flashlight.java' into 'Slippers.java'.
- Edit this new
code for 'Slippers.java' and change the class name and constructor name to
'Slippers' so that it compiles.
- In the Slippers class, find the constructor and change the name of the image
file it loads to be 'slippers.jpg'.
- In the Slippers class, find the getName method and change the name that it
returns to "Slippers".
- In the Slippers class, find the outputDescription method and change the
text. Make sure your text briefly indicates that these are red slippers.
- In the slippers class, find the takeAction method. Remove all the
action code, and add the following code:
// Allow the user to examine this item closely.
if (verb.equals("inspect") && noun.equals("slippers"))
{
output.addText("These are ruby red slippers. Hmmm, perhaps wearing");
output.addText(" them is a good idea.");
return true;
}
// If the user wears the slippers, send them outside.
if (verb.equals("wear") && noun.equals("slippers"))
{
output.addText("Something magical happens, you find yourself spinning...");
game.setCurrentRoom("Outside");
return true;
}
// Default - action not handled here.
return false;
- Make sure the entire project, including the Slippers class, compiles.
Finally, you need to change the game state object so that it creates a Slippers
object and puts it in the kitchen when the game starts:
- Edit 'GameState.java" and find the GameState constructor
- Find the statements that create item objects and adds them to the game.
- Add one more statement to this group of statements that creates a slippers
object and adds it to the game (in the kitchen):
this.addNewItem(new Slippers(), "Kitchen");
Compile and run the game. You should now find slippers in the kitchen,
and wearing them ("wear slippers") should send you outside!
You now have enough information to use this sample code to create a very
elaborate game. Please review the requirements and scoring for this
problem.
FAQ
There have been surprisingly few questions during the take home period.
The one question that was asked over and over was:
Can I do "xyz" in my game or is it too violent?
Avoid direct descriptions of violence or implied violence that serve no other
purpose than to shock or disturb the reader.
Submission instructions
Your solution to this take-home project may consist of many, many files.
To simply judging, please copy your entire submission (all needed files) into a subdirectory named
"Adventure". Next, archive the "Adventure" directory into a single
.zip file called Adventure.zip. Please include:
- A
readme.txt file that describes your game. Include
instructions for executing your game. The specific requirements are:
Teams must write instructions to
help the judges execute and play their solution. The application
instructions should be detailed - explain how to unpack, compile, and execute
the application. (Coaches may help with this, it won't be judged.)
Teams (not coaches) should also give a set of instructions that will tell the
judges the exact commands required to win the game, along with any other
interesting facets to the game.
Please also let us know the version numbers of tools and languages that your
are using. Also, please make sure that if you access any data files
(images, etc.), that you use relative pathnames.
- All of your source code. Your instructions for compiling the source
code should be in the
readme.txt file.
- A compiled
.exe file or self-extracting .jar file
(if possible). This is not required, but it will help greatly during
judging.
- All artwork and data files needed to run your adventure game.
All of these files should be within the "Adventure" directory (or its
subdirectories) before you create the Adventure.zip archive from
the directory. (If you need a program to help you create the
.zip file, just Google "zip utility" and download one of the many
free ones.)
Once you get the archive made, please submit it to us electronically.
Follow these instructions:
- Visit this webpage to get started:
Web-based submit
https://cgi.eng.utah.edu/webhandin/
(Ignore any certificate security warnings and proceed to the webpage.)
- Log in using the team name and password that was sent to
your coach. Note: You must have cookies
enabled to access this web site.
Logins and passwords have been sent by email to
coaches. If you have not received this information, please email me your
team name and
.zip file before the deadline. pajensen@cs.utah.edu (Do not do both
submission methods -
if you use the web tools to submit a file then do not email me a file,
I'll ignore it.)
- The next screen will ask you to "Enter class". Enter the following
for the class:
hspc
- The next screen will ask you to "Select Assignment for hspc". Make
sure "take_home" is selected, then press OK.
- The next screen will ask you to upload and submit a file. Browse your
computer and find "Adventure.zip". (You must submit only this single
.zip file.) Upload and submit this file.
- You should get a confirmation dialog if the file is correctly
submitted. At this point select "Upload and submit more files..."
- Select "View submitted files". You'll get a message indicating that
you did not submit any more files. Beneath this message you should see a
list of all the files you have submitted. Make sure "Adventure.zip" is in
this list and that it is the correct file size. (An incorrect file size
means the file was not submitted.)
- You're done. Logout.
You may submit and resubmit your game as many times as you want. We'll
only judge the last file that you submit.
Please make sure you submit your game on or before Thursday, March 15.
I will disable web submissions at 12:01 AM on Friday, March 16. The
judges and I look forward to seeing your games!