Perform a complete top-down design for
the following problem. Code the final algorithm in C++. Compile,
debug (if necessary!) and execute your program. See below for
what to hand in.
TITLE
HiLo--- Guessing-the-number Game
DESCRIPTION
This game consists of selecting a number from 1 to 1000 and asking
the opponent to guess it. For each guess the opponent gets a response
of ``High'' (if the opponent's guess is higher than the selected
number), ``Low'' (if the guess is smaller), or ``You got it!''
(if the guess is correct).
The game is played between a computer and a user where both the
computer and the user take turns selecting the number and responding
to each other's guesses (of course, we assume the computer doesn't
use the fact that it really ``knows'' what the user has selected).
The objective of the game is to try to guess the correct number
in a minimum number of guesses (a maximum of 20 guesses are allowed).
The user can choose to play any number of rounds. At the end of
the game (i.e. when the user decides to quit) the average number
of guesses per number by each player (the user and the computer)
is displayed and a winner is determined (the lowest average number
of guesses wins).
INPUT
The computer always goes first, i.e. it selects a number between
1 and 1000. The user's guesses are the prompted for input until
the user guesses the correct number.
When the computer guesses, the input response from the user can
be 1, 0, or -1, depending on the guess being high, correct, or
low, respectively.
OUTPUT
The output messages from the computer are as follows (XX.YY and
N denote some numbers):
I have selected a number from
1 to 1000. Enter your guess :
Your guess is high.
Your guess is low.
You got it!
The number was XXXX; you got it in N tries.
My turn now. Select a number and hit any key followed by a RETURN
for my next guess.
My next guess is XXXX.
Did I get it (1, 0, or -1)?
I got it in N tries.
Do you want to play again (Enter Y or N)?
Sorry, you ran out of tries.
Your guess should be between 1 and 1000. Please guess again. I
will
not count that as a guess.
RESULTS
Number of rounds played : N
Average number of guesses/round for me : XX.YY
Average number of guesses/round for you : XX.YY
I win!
You win!
It is a draw!
ERRORS
Any input which does not meet the specifications above is detected
and the user is reprompted for input after appropriate messages
(shown above) are displayed.
EXAMPLE
I have selected a number from
1 to 1000.
Enter your guess : 345
Your guess is high.
Enter your guess : 200
Your guess is low.
Enter your guess : 1100
Your guess should be between 1 and 1000. Please guess again. I
will not
count that as a guess.
Enter your guess : 250
Your guess is low.
Enter your guess : 283
You got it!
The number was 283, you guessed it in 4 tries.
My turn now. Select a number and hit any key followed by a RETURN
for my guess.
My guess is 500
Did I get it? -1
My guess is 750
Did I get it? 1
My guess is 625
Did I get it? 1
My guess is 563
Did I get it? -1
My guess is 594
Did I get it? 0
I got it in 5 tries.
Do you want to play again (Enter Y or N)? N
RESULTS
Number of rounds played : 1
Average number of guesses/round for me : 5.0
Average number of guesses/round for you : 4.0
You win!
NOTES
You must decide how the computer will select its number to be
guessed by you as well as how the computer will determine its
guesses when you have selected a number. The latter we leave to
your ingenuity. For the former, however, you will wish to be able
to select a random number in the range from 1 to 1000.
EXTRA CREDIT
Suppose the upper limit of the guesses is G rather than 1000.
As a function of G can you give a formula for the maximum number
of guesses it should take the computer (or you if you know how
to play the game optimally) to guess the number selected independently
of what that number is? You should show how this formula is
obtained. In addition to your formula you should present the algorithm
which tells how to compute the first guess and then, based on
the response (high or low), how to compute the next guess until
the correct number is obtained.
WHAT TO HAND IN
Hand in the following in the order given:
2. A series of runs of your program demonstrating its correctness.
3. Your solution, if any, to the extra credit part of the project.
Additionally, write a short paragraph/essay on your experiences in working on this assignment.
Each C++ program should have the following:
// Program Name: <The name of your C++ program file>
// Programmer: <Your name here>
//
Address: <your e-mail and Campus Address here>
//
Assignment Number: <put project number here, e.g. Project#1,
Part A>
// Purpose: <A short problem description>