CS 132: Intro to Computer Science II

Spring 1998

Assignment 7 & 8

The Wumpus Adventure Game: Using Arrays and Instance Variables

Due Wednesday March 24 (Part 1) and Monday March 29 (Part 2)

One of the first computer games in the 1970's was an adventure game called "Hunt the Wumpus". In this game, you are forced to wander around a maze of caves, hunting the dreaded wumpus (or possible more than one wumpi). You are armed only with hand grenades, which you can throw at a wumpus to kill it. The problem is that wumpi are both fierce and fast. If you ever wander into a cave that contains a wumpus, then that wumpus will attack and kill you before you even have a chance to throw a grenade. Your only hope is to throw the grenade into the cave from an adjacent cave. Fortunately, wumpi are rather odorous creatures, so you will be able to smell a wumpus when you are in an adjacent cave. Of course, you won't know which cave the wumpus is in (every cave is connected to three other caves), so you will have to guess when you throw your grenade.

The object of the game is to kill all of the wumpi before you run out of grenades (and without getting killed). As if this weren't hard enough, you don't know exactly how many wumpi there are, or how many grenades you have (you will have 4 grenades for every wumpus). Plus, there are other things to watch out for. Somewhere in the maze is a group of giant bats, which will pick you up and fly you to some random cave (but at least they will only drop you in an empty cave). There is also a bottomless pit which you must avoid. Luckily, there is some warning about these hazards: you will be able to hear the flapping wings of the bats and feel a draft coming from the pit when you are in an adjacent cave. Oh, and there are the Lost Caverns of the Wyrm, which are very difficult to get out of. Below is a sample execution of the game:

> java Wumpus
HUNT THE WUMPUS:  Your mission is to explore the maze of caves 
and destroy all of the wumpi (without getting yourself killed).
To move to an adjacent cave, enter 'M' and the tunnel number.
To toss a grenade into a cave, enter 'T' and the tunnel number.

You are currently in The Fountainhead
    (1) unknown
    (2) unknown
    (3) unknown

What do you want to do? m 2

You are currently in The Silver Mirror
    (1) The Fountainhead
    (2) unknown
    (3) unknown

What do you want to do? m 3

You are currently in Shelob's Lair
    (1) The Silver Mirror
    (2) unknown
    (3) unknown

What do you want to do? m 3

You are currently in The Lost Caverns of the Wyrm
    (1) unknown
    (2) unknown
    (3) unknown
You smell a WUMPUS!

What do you want to do? t 1
  Missed, dagnabit!
DANGER: Any nearby wumpi are on the move.
A wumpus is coming toward you with big, gnarly teeth... CHOMP CHOMP CHOMP
GAME OVER

PART 1

In order to write a program for playing Hunt the Wumpus, you will first need to complete the implementation of the Maze class, which is partially implemented in the file Maze.java. This class stores all of the information about a maze of caves, as well as state information about the game (location of the player and wumpi, number of grenades left, etc.). It also provides methods needed in order to write a game playing program.

The Maze class assumes the existence of a data file called "caves.dat", which stores the configuration of the cave maze. The first line of this file specifies the number of caves in the maze, and then each subsequent line contains the information for each cave: the cave number, the indices of of its adjacent caves, and the name of the cave.

The data structure used to store the maze information is an array of Cave objects, with each object containing the name of the cave, the numbers of adjacent caves, the contents of the cave (e.g., wumpus, bats, ...), and whether or not that cave has been previously visited. The constructor for the Maze class, which is provided for you, reads in the information from the "caves.dat" file and initializes the array. For example, the contents of the data file would be stored as shown below:
Instance Variables caveList[0]caveList[1] . . . caveList[19]
name The FountainheadThe Rumpus RoomThe Lost Caverns of the Wyrm
adj1 4 90 2 5 . . . 15 16 17
contentsEMPTYEMPTYEMPTY
visitedfalsefalsefalse

The constructor determines how many wumpi there are going to be (a random number between 1 and the number of caves divided by four), initializes the number of grenades (four grenades per wumpus), and randomly places the wumpi, bats and pit in the maze.

Complete the definition of the Maze class by implementing the following methods:

You will notice that the static main method in Maze.java already has some test code. However, you should add to this test code for a comprehensive test of your Maze methods. Make sure that your methods work before proceeding to part 2! Because part 1 is significantly more involved than part 2, part 1 is due on Wednesday, which allows for an additional lab period to work on it.

Submission for Part 1
Turn in a hardcopy of Maze.java and a script that shows the output of your test code.

PART 2

Using your Maze class, write a program which allows the user to play Hunt the Wumpus. You may find that your program will be quite short, since most of the work is done in the Maze member functions. However, if your main method takes more than a screenful of space, you should make use of an additional static method so that all methods are easy to read and understand. A WumpusCommand class has been provided, which retrieves valid commands from the user. Both this class and a start of the Wumpus class, which should run the game, is in the file Wumpus.java.

When grading this program, special attention will be paid to style and robustness. Robustness refers to your program's ability to behave correctly in all cases, even those that may be unexpected or rare. As you test your program, you should be very careful to think about any such cases that might arise, and test for them. For example, a wumpus that is startled by a nearby explosion is supposed to run to an adjacent empty cave. What if there are no empty caves adjacent to a wumpus? Although this is unlikely, especially with a small number of wumpi, it is possible. In such a case, the wumpus should stay where it is. Special cases such as these may affect your design of the wumpus program, and may force some modifications to code in the Maze methods.

Submission for Part 2
Turn in a hardcopy of Wumpus.java and Maze.java and a script that shows you running games with a full range of results.