CPE 101

Iteration and Selection Lab


Problem Description

You will be developing a simple guessing game called Hurkle.  The Hurkle is an imaginary creature that hides somewhere in a two-dimensional 9x9 integer game-space.  Each time the game is run the program places the Hurkle at a random position in the game-space (a row and column pair) and the player has a maximum of five (5) guesses to guess the Hurkle s location.  The Hurkle does not move during the game.  If the player finds the Hurkle, the program reports the success and ends.  If the player does not find the Hurkle the program gives a hint about what direction to the player move to find the Hurkle.  The hint must be correct and the optimal direction   the program doesn t lie or mislead the player!  There are eight possible hints: north, northeast, east, southeast, south, southwest, west, and northwest.  The program must detect guesses that are not in the game-space and re-prompt until a valid value is given (invalid guesses don t count as one of the five).

Except for a few explicit requirements (specified below), the program design - both the code and the look-and-feel - are up to you.  To receive full credit the program should display a map of the game-space that looks very similar to the example below and contain all of the same information, i.e., map row and column values, and compass directions.  This map only has to be displayed once at the beginning of the game.  You should design a user interface that is easy to use and plays well   have fun and be creative!


Be sure to read the Given, Suggestions, Constraints, and Grading sections below before you begin to write any code.


            0   1   2   3   4   5   6   7   8


        0 |   |   |   |   |   |   |   |   |   |


        1 |   |   |   |   |   |   |   |   |   |


        2 |   |   |   |   |   |   |   |   |   |


        3 |   |   |   |   |   |   |   |   |   |


WEST    4 |   |   |   |   |   |   |   |   |   |    EAST


        5 |   |   |   |   |   |   |   |   |   |


        6 |   |   |   |   |   |   |   |   |   |


        7 |   |   |   |   |   |   |   |   |   |


        8 |   |   |   |   |   |   |   |   |   |




To place the Hurkle in a random place in the game-space you will need to obtain random numbers for its row and column.  To do so you should use the provided code below.  Your instructor will likely discuss pseudo-random numbers and their generation in lecture or lab.  Using them is relatively easy, first you must initialize the C-Standard Library s random number generator by calling the function srand once (with a nested function call to the function time, then you call the function rand any time you need another random number.  So, place the following function call in your code so that it executes only once and before you obtain the Hurkle s position:


Any time after the srand call above you may call the function rand to get a different pseudo-random number between 0 and a constant RAND_MAX (a large to very large integer value that depends on the compiler implementation)

int row;

row = rand();

Notice that you probably want values between 0 and 8 (9 unique values) for row and column values in the game-space.  Think about how you might use the modulus operator (%) to modify the value returned by rand to do that.

To be able to use the srand, rand, and time functions be sure to include the following header files:

     #include <time.h>

     #include <stdlib.h>


  1. Design the prompts and game-flow on paper before you begin writing code.  Your design must match the game description and minimal requirements given but you have quite a bit of freedom to be creative here   show us what you can do!
  1. You should determine what functions you will want to solve the problem before you write any code.  Think about how you could break this game up into separate steps   these steps might be good candidates for actual functions in your solution.  Come up with descriptive names for these functions and then determine their inputs (parameters) and their outputs (return types).  You may modify, add, and/or delete functions to your initial design as you go but the more thought you give to the problem before you begin coding the faster and easier the actual development will go.


  1. Do not use any arrays in your solution   just store the Hurkle s position as two variables representing its row and column position.
  2. Do not use any global variables (variables declared outside of any function).
  3. Use nested loops to display the game-space map.  It could be done without loops but one of the important outcomes of this lab is for you to learn how to write loops!


  1. Your implementation must meet the Constraints above.
  2. The game must play correctly (per Problem Description above).
  3. You must display a game-space map that is very similar to the one provided above.
  4. The usability of your prompts and output has a small affect your grade.
  5. The functional decomposition of your code is important - be prepared to discuss your choices when you demo!
  6. The Hurkle's position must be generated using srand and rand.

Handing in Your Source Electronically 

  1. Move the necessary file(s) to your Unix1account using your favorite FTP client program.
  1. Log on to Unix1 using your favorite Shell client program.
  1. Change directory (cd-command) to the directory containing the file(s) to hand in.
  1. Be sure to compile and test your code on Unix1 using the required compiler flags one last time just before turning the files in.
  1. Use the following handin command.

handin graderjd Lab06 hurkle.c