CS 302 Section 9
Program Five: Hunt the Wumpus!

Due Friday, 11/7 at midnight

Solution

Program Five will deal with two-dimensional arrays and more practice with file streams. WARNING: This program will be somewhat longer than previous assignments. It would be more aptly named a project. Note that the percent of your final grade is roughly twice that of the previous programs. Please use the extra time I have given you to make a plan of attack before writing code -- design function prototypes and algorithms, and *think* about the overall structure of the program, before you start coding. Good luck!

Background

This program is a variation on a very old computer game called Hunt the Wumpus. In this game, you will move a human through a maze in search of a creature known only as The Wumpus. The terrified Wumpus can smell you coming, and will attempt to stay away from you. A functioning solution executable is available; go to the DOS command line and type (or -- strongly suggested -- cut and paste!):
W:\u\m\a\manville\public\html\302\programs\program5\wumpus
(This is in the directory of one of the other TAs but should work fine for you)

The Maze

The maze will be represented on the screen as a rectangle of characters. A . (period) represents open space, a X represents a wall, a * (asterisk) represents the human player, and a # (double hash) represents The Wumpus. Both the player and the wumpus are free to move one square at a time in any direction (including diagonally) as long as there is no wall in the way. Neither the player or The Wumpus may move off of the top, bottom, left, or right of the maze. The maze we will use -- including player and Wumpus starting positions -- looks like this:
 
........................ 
..X.............XXXXXX..  
..X..XXXXXXXXXXXX..#.... 
..X.............X.......  
XXXXXXXX....X...XXXXXXX. 
.......X....X...X.....X.  
.*.....X....XXX.X.....X. 
.......XXXXXX...X.X.X.X.  
..................X.X.X. 
XXXXXXXXXXXXXXX...X.X.X.  
..X..........X....X.X.X. 
..X..XXXXXXXXXX...X.XXX. 
..................X..... 
The maze your program must use is contained in P:\course\cs302\public\html\C++\wumpus\maze.1. The maze should be loaded from this file at the beginning of your program. Notice that this file also contains the initial positions of the player and The Wumpus. The maze will be 24 characters wide and 13 characters tall. However, do not use these numbers throughout your program. Declare some appropriate global constants and use those instead.

The Game

In every round of the game, your program will display the maze, including the positions of the player and The Wumpus. The program must then ask the user what direction he/she wishes to go. The user may specify a direction by entering a number. The direction is given by that number's position on the numeric keypad. For example, 8 means go north, 9 means go northeast, and 6 means go east (pressing 5 should make the player stay in place for the turn). Thus, the player may move one square in any straight or diagonal direction. (You may need to press the NumLock key to make the keypad word).

There are, however, some limitations. The player may not enter a square containing a wall, nor may the player move off the edge of the map. If the player selects a valid direction, the player will move one square in that direction. If the player enters the same location as The Wumpus, the game is over and the player wins. If not, the program decides how the wumpus must move, and then begins a new round. The player may end the program prematurely by entering 'q'.

When the player wins the game, the program should ask if the player wishes to play again.

The Wumpus

The Wumpus is a poor little creature who does not want to be caught by the player. You may give the Wumpus whatever decision-making ability you feel is necessary to make it evade the player. However, it must follow these three rules:
  • The Wumpus cannot pass through walls, move into the square occupied by the player, or go off the edge of the board.
  • The Wumpus may not sit still. It must exactly one square every round, unless it is trapped on all sides.
  • The Wumpus' behaviour should NOT be coded specifically for the maze we give you (ie. it should work with any legal starting position in any legal maze)
  • You could have the Wumpus move randomly (but legally), or you could make the Wumpus smart enough to move away from the player. It's up to you. No matter what method you use, comment your Wumpus code enough to make its purpose obvious.

    Program Structure Suggestions

    As always, your program must have the elements of good design: modularity, readability, etc. I strongly suggest you split up your program into a number of functions. For your reference, here are the prototypes from my solution:
    bool LoadMaze(...);
         // Read the maze from a file & initialize player & wumpus position,
         // if found.  Return true if loaded successfully.
    void PrintMaze(...);
         // Print the maze (and player/wumpus position within it) 
    bool GetPlayerMove(...);
         // Get the player's action and perform it.  return false if action
         // is to quit (true otherwise)
    void GetWumpusMove(...);
         // Decide The Wumpus' move & perform it 
    bool IsLegalMove(...);
         // Can player or wumpus move to this position?
    
    These are provided only for suggestion; you are not obligated to use these prototypes, and additional functions might be helpful. Whatever program design you use, provide enough comments so that the reader can easily understand your code.

    The (...) indicates the parameter list; I have decided not to show you the ones I used. FYI, I used a lot of parameters, a number of them are reference parameters, some of the functions above are NOT called from main(), but from one or more of the other functions, and (non-const) global variables are not necessary, so please don't use them

    What to turn in

    Electronically submit one C++ source file, called "wumpus.cpp" (you do not need to hand in a paper copy). I would appreciate it very much if you also called your project "wumpus", so that the executable will have that name also. The program should be set up to read text files from the directory & file listed above (feel free to create your own maze files for testing by changing the file name or directory, but when you hand in the program make sure it's set up to look here). Again, there's a handin batch file provided that may or may not make it easier to turn in your program.

    To use it:

    1. open a command prompt window (Go to Start, then Programs; Command Prompt is near the top of the Programs submenu).
    2. At the command prompt, type:
      handin
      for directions. If you named your Visual C++ project wumpus, as I asked, to hand in you will have to type:
      handin 5 wumpus dbs
      Make sure to check the directory listing displayed by handin when it is done processing to verify that wumpus.cpp is listed. To be doubly sure, you are encouraged to also go look at the file via the "My Computer" icon. I will no longer accept excuses for files not being turned in properly!

    Going Further

    If you write a program that meets the above specifications correctly, you should receive full credit. If you get finished early, though, and feel like coding some more, there are ample opportunities to code up extras. I can't offer extra credit for it, but any modifications will at least make it more interesting for me to grade your program. Anyway, here are a few things off the top of my head: I'm sure there's more ideas like this that you can think of. I encourage you to add features if you have the time, but, like I said, you'll get full credit as long as you do the required portion (and if your required portion is wrong, you won't get full credit!) Note that the third and fourth suggestions will require you to keep track of the location of the doors and pits separately, as you don't want them getting replaced by .s when the player walks onto them.