Project 2 - A Guessing Game
Part 1 - Due - April 5, 2003
Part 2 - Due - April 19/20, 2003
In this project, you will write a guessing game program similar to
MasterMind. In this game, a single player plays against the
computer. The computer selects a four character code (from the
characters 'R', 'Y', 'B', 'O', 'G' -- Red, Yellow, Blue, Orange,
Green) and the player has 10 tries to guess the secret code. Each
round, the computer will tell the player how many characters of
the guess were exact matches to the code, and how many
characters were non-exact matches to the code. An exact
match means that the player guessed the correct character in the
correct place. A non-exact match means that the player guessed
the correct character in the wrong place. Suppose the code were:
Y O B R. Following is an example of a guess and response:
Guess: B O G B
Exact: 1
Non-exact: 1
The exact match was for the character 'O' and the non-exact match
was for the character 'B'. Notice, only one 'B' from the guess
was counted since only 1 'B' appears in the code. Also notice
that the computer does not tell the player which characters were
exact/non-exact matches. It is up to the player to figure that out!
Your program should do the following:
- Read the secret code in from a file and store it in a
character array of size 4. You should have a text
file which has a single line -- the secret code. You can change
this file from game to game. Read in the contents of the file
and store it in a character array of size 4.
- Ask the player for a guess.
- Compute the number of exact and non-exact matches.
- Declare the player a winner if the number of exact matches
is 4.
- If the player has not guessed the code, ask for another
guess until the maximum of 10 guesses has been exceeded.
- If the player does not guess the code in 10 tries, you
should tell the player he/she has not won the game.
Before you even begin to write your code, you should think very
carefully about the algorithm you will use to solve this problem.
You will need to compare the guess against the code, and determine
the number of exact and non-exact matches. However, you must be
very careful to avoid counting any of the characters twice. I
would recommend that you make at least two passes to compare the
guess and the code. In the first pass, look for exact matches and
in the second pass, look for non-exact matches. But, you need to
consider how you will keep track of which characters have already
been counted as an exact or non-exact match so that you do not
count them again. I would recommend using a third array for this
purpose. This array should mark which of the code
characters have been matched (either with exact or non-exact
matches). You should also mark exact matches differently from
non-exact matches.
Once you have thought out your algorithm, design your
code.
You can actually play a version of this game HERE.
Part 1 - Due Monday, April 5, 2004
Think about your algorithm, code design, and test cases. Write down your algorithm and the design of your code. Your design should include documentation for all functions you will use in your program. The documentation should include the input, output, and side effects of each function.
- Turn in your algorithm, design document, and set of test cases.
Part 2 - Due Monday, April 19, 2004
- Complete your code.
- Make sure that each function is well documented. Your documentation should specify the type and function of the input paramaters and ouput.
- Make sure a lab assistant (Dianne or I) has seen your program run.
- Copy and paste the output of your program to the bottom of your program itself. Make sure the output is a comment. That means, put a /* before the output and a */ after the output.
Sami Rollins