**CS110 Introduction to Computer Science
Spring 2009
Assignment#5
Due on: Monday, March 30, 2009. **

**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. 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.

Alternately, when the computer guesses, the input response from the user is obtained by pressing one of the buttons "High" "Low" or "Correct" using the askQuestion dialog from the myro library. Similarly, user's guesses can be input using the ask dialog from the myro library.

**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 (Optional, for extra credit) **

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.

**ADDITIONAL EXTRA CREDIT**

Suppose the upper limit of the range of numbers to guess from 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*? Using this, what would be the maximum number of guesses required for numbers between 1..1000. 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.