CSC 533: Organization of Programming Languages
Fall 2000

HW4: C++ Classes and Design

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

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

Note: This game in no way intends to promote or condone violence. If you object to the violent nature of this game, alter the rules accordingly. Perhaps a wumpus is a poor soul who has been placed under a spell, and instead of grenades you are tossing bottles of potion that release the person from that spell. Use your imagination.


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 (partially defined in the Wumpus code directory). 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 member functions needed in order to write a game playing program.

Your program should assume the existence of a data file called "caves.dat", which stores the configuration of the cave maze. The first line of this file should specify the number of caves in the maze, and then each subsequent line will contain the following information:

<cave number> <adjacent1> <adjacent2> <adjacent3> <cave name> where the cave number is a unique number between 0 and the number of caves minus 1, the next three numbers are the numbers of the caves adjacent to this one (all caves are adjacent to three other caves), and the rest of the line is the name of the cave. For example, the standard data file for this game is given below:

20 0 1 4 9The Fountainhead 1 0 2 5The Rumpus Room 2 1 3 6Buford's Folly 3 2 4 7The Hall of Kings 4 0 3 14The Silver Mirror 5 1 9 11The Gallimaufry 6 2 7 12The Den of Iniquity 7 3 6 8The Findledelve 8 7 3 13The Page of the Deniers 9 0 5 10The Final Tally 10 9 11 14Ess four 11 5 10 12The Trillion 12 6 11 13The Scrofula 13 8 12 18Ephemeron 14 4 10 15Shelob's Lair 15 15 16 19The Lost Caverns of the Wyrm 16 15 17 19The Lost Caverns of the Wyrm 17 16 17 18The Lost Caverns of the Wyrm 18 13 17 19The Lost Caverns of the Wyrm 19 15 16 17The Lost Caverns of the Wyrm

However, it should be possible to customize the game simply by replacing this file with a different cave configuration. The data structure used to store the maze information is a vector of structs, with each struct 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 vector. In addition, 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 member functions:

You should write a driver program to test your code. For easier testing, consider modifying the constructor temporarily so that you know exactly where things are. As always, interleave the coding and testing phases -- test each function independently as you complete it.


PART 2: Using your Maze class, write a program that 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 .

When grading this program, special attention will be paid to style and robustness. As always, your program should be modular and should follow good commenting, indentation, and naming conventions. 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 member functions.