Problem 3.2 - Guessing game
In this problem, you will use loops to write a simple guessing game.
Description of the game
A number between 1 and 100 is chosen at random.
The goal of the game is to correctly guess the number.
With each guess, the player is informed if the target number
is higher or lower than their guess.
The game ends when the correct number is guessed, and at that
point the player is asked if they want to play again.
Coding an implementation
No starting code is provided for this problem, but you might find
examples presented in class to be useful, especially for coding
the choice of a random number.
Example
Here's an example of what playing could look like:
C> java HighLow
Guess my number from 1 to 100...
Enter a guess: 50
Lower!
Enter a guess: 25
Higher!
Enter a guess: 37
Lower!
Enter a guess: 31
Congratulations, you guessed my number with 4 guesses!
Play another (0=no 1=yes)? 1
Guess my number from 1 to 100...
Enter a guess: 50
Higher!
Enter a guess: 75
Higher!
Enter a guess: 87
Higher!
Enter a guess: 93
Lower!
Enter a guess: 90
Congratulations, you guessed my number with 5 guesses!
Play another (0=no 1=yes)? 0
C>
Additional question
In the comments at the beginning of your program, give answers
to the following questions:
- What is the fewest number of guesses that could be given
before the target number is guessed?
- What is the largest number of distinct valid guesses that could
be given before the target number is guessed?
- What is the largest number of distinct guesses that could
be given before the target number is guessed if an optimal
strategy is used?
Note about input
Since we haven't covered much of the use of Strings yet, the player's
answer to the question of whether to play another game should be given
as the number zero for no, or the number one for yes.
Don't worry about bad input to the program. If numbers are not given,
exceptions will be thrown and the program will terminate. If numbers
beyond the range of 1 through 100 are given, or duplicate guesses are
made, the game will take longer, but your program does not need to
handle these cases specially.
What to turn in
Turn in HighLow.java, the source code for your implementation of this game.
Don't forget to include the answers to the questions above.