2007 Utah High School
Programming and Design Competition

University of Utah
March 19, 2007

2007 Take-Home Problem

Adventure Game
Project Details

Due Thursday, March 15 by
electronic submission.


The project for this year's take home problem is to design and code up a text-based adventure game (with optional images).  Your program will get commands from the user, such as "North" or "Get Key", and then display the results of that action.  With a proper sequence of commands, the user should be able to win or complete the game.  In addition to writing the program, you must also come up with a setting and a plot for your adventure game.

Revision History


Introduction: An Adventure Game

Quote from Wikipedia:

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:

Here is the layout of the sample adventure game world:

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.

Students are allowed and encouraged to use the sample code as the base for their own solutions.  Sample code will be provided in Java (with graphics) and C++ (without graphics).  Using images in the program is not required and will not help during scoring.  On the other hand, images are a lot of fun.  ;)


Basic requirements:

Write a text-based adventure game program that allows the user to enter simple commands to navigate through and interact with a fictional world.  The size and nature of the fictional world is entirely up to you, be creative.  There are a few simple rules: Division I and Division II teams will be judged separately.  It is anticipated that teams will make progress in varying amounts.  It is perfectly acceptable for a team to simply take the sample program, change the text to add a story, change the pictures, change the layout, and turn it in.  More experienced students, however, should try to expand and elaborate on the sample programs in a significant way, or simply write a brand new program that takes a different approach.

Your program and documentation will be anonymized and then judged on two subjective criteria:

  • Technical Merit - The source code for your application will be reviewed by three expert programmers and evaluated for use of object-oriented programming techniques, code clarity, and technical proficiency.  Code that is well organized, clearly written, and documented thoroughly will score well.

  • 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):

    1. Click 'Start'
    2. Select 'Run'
    3. Type 'cmd' and press return to open a command line shell.
    4. Type 'cd \source' to change directories to C:\source.  If you unzipped the files to another location, change to that directory.
    5. Type 'dir' and press return.  You should see a directory listing with exactly two subdirectories in it: 'adventure' and 'images'.
    6. 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.
    7. 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:

    1. Make sure you are in the source directory.  Follow steps 1 through 5 above.
    2. Type 'java adventure.Adventure' to run the adventure game.

    If you have any problems, make sure of the following:

    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:

    1. Unzip the source to a temporary location.
    2. Create a new Java application project in your favorite IDE.
    3. 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.
    4. 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.
    5. 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.
    6. 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.):

    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:

    A simple example - Adding a room to the game.
    Let's start with the image:

    1. 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:

    1. In the 'rooms' package, create a new class called 'Kitchen.java'.  Copy everything from 'LabRoom.java' into 'Kitchen.java'. 
    2. Edit this new code for 'Kitchen.java' and change the class name and constructor name to 'Kitchen' so that it compiles.
    3. In the Kitchen class, find the constructor and change the name of the image file it loads to be 'kitchen.jpg'.
    4. In the Kitchen class, find the getName method and change the name that it returns to "Kitchen".
    5. 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.
    6. 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".
    7. 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:

    1. 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.)
    2. 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:

    1. Edit 'GameState.java" and find the GameState constructor
    2. Find the statements that create room objects and adds them to the game.
    3. 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:

    1. 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:

    1. In the 'items' package, create a new class called 'Slippers.java'.  Copy everything from 'Flashlight.java' into 'Slippers.java'. 
    2. Edit this new code for 'Slippers.java' and change the class name and constructor name to 'Slippers' so that it compiles.
    3. In the Slippers class, find the constructor and change the name of the image file it loads to be 'slippers.jpg'.
    4. In the Slippers class, find the getName method and change the name that it returns to "Slippers".
    5. In the Slippers class, find the outputDescription method and change the text.  Make sure your text briefly indicates that these are red slippers.
    6. In the slippers class, find the takeAction method.  Remove all the action code, and add the following code:
    7.         // 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;
      
      
    8. 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:

    1. Edit 'GameState.java" and find the GameState constructor
    2. Find the statements that create item objects and adds them to the game.
    3. 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?


    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: 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:

    1. 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.)

    2. 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.)

    3. The next screen will ask you to "Enter class".  Enter the following for the class:
          hspc
      

    4. The next screen will ask you to "Select Assignment for hspc".  Make sure "take_home" is selected, then press OK.

    5. 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.

    6. You should get a confirmation dialog if the file is correctly submitted.  At this point select "Upload and submit more files..."

    7. 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.)

    8. 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!