CPE 101

Iteration and Selection Lab

Objectives

• To be able to develop looping logic to solve repetitive tasks.
• To be able to design functions that help solve a larger general problem without explicit specification.
• To appreciate the benefits of good problem decomposition.

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.

NORTH

0   1   2   3   4   5   6   7   8

-------------------------------------

0 |   |   |   |   |   |   |   |   |   |

-------------------------------------

1 |   |   |   |   |   |   |   |   |   |

-------------------------------------

2 |   |   |   |   |   |   |   |   |   |

-------------------------------------

3 |   |   |   |   |   |   |   |   |   |

-------------------------------------

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

-------------------------------------

5 |   |   |   |   |   |   |   |   |   |

-------------------------------------

6 |   |   |   |   |   |   |   |   |   |

-------------------------------------

7 |   |   |   |   |   |   |   |   |   |

-------------------------------------

8 |   |   |   |   |   |   |   |   |   |

-------------------------------------

SOUTH

Given

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:

srand(time(0));

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>

Suggestions

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.

Constraints

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.