Write a console program in a class named Guess2D
that plays a two-dimensional number guessing game with the user.
The computer chooses a point between (1, 1) and (20, 20) inclusive.
The user is repeatedly asked to guess the point.
For each incorrect guess, the program will give the user one of the following three hints:
- hot (a distance = 1.5 from the correct answer)
- warm (a distance = 5.0 from the correct answer)
- cold (a distance > 5.0 away from the right answer)
The hints about guesses being hot, warm, or cold are based on distances between points. The formula to compute the distance between two points is to take the square root of the squares of the differences in x and y between the two points.
For example, if the correct point is (11, 4) and the user guesses (5, 7), their distance is the square root of ((11 - 5)2 + (4 - 7)2) or roughly 6.71, so the hint is "cold".
The game also hints about which direction the user should go from the current guess to find the right answer:
- north (the user should increase y)
- south (the user should decrease y)
- east (the user should increase x)
- west (the user should decrease x)
When the game ends, the program reports how many guesses were needed.
After each game, the program asks the user if he/she would like to play again.
Assume that the user will type a one-word string as the response to this question.
A new game should begin if the user's response starts with a lower- or upper-case Y, such as "y", "Y", "yes", "YES", "Yes", or "yeehaw".
Responses of "no", "No", "okay", "0", "certainly", and "hello" are all assumed to mean no.
Once the user chooses not to play again, the program prints overall statistics about all games.
The total number of games, total guesses made in all games, average number of guesses per game (as a real number rounded to the nearest tenth), and best game (fewest guesses needed to solve any one game) are displayed.
This program is a 2-D guessing game.
I will think of a point somewhere
between (1, 1) and (100, 100)
and give hints until you guess it.
Guess x and y: 5 7
You're cold. Go south east
Guess x and y: 18 5
You're cold. Go south west
Guess x and y: 15 2
You're warm. Go north west
Guess x and y: 12 3
You're hot! Go north west
Guess x and y: 11 4
You got it right in 5 guesses!
Play again? y
Guess x and y: 10 10
You're cold. Go south west
Guess x and y: 5 5
You're warm. Go east
Guess x and y: 8 5
You're hot! Go east
Guess x and y: 9 5
You got it right in 4 guesses!
Play again? YES
Guess x and y: 18 10
You're cold. Go south west
Guess x and y: 7 1
You're cold. Go north east
Guess x and y: 10 3
You're warm. Go north east
Guess x and y: 20 20
You're cold. Go south west
Guess x and y: 12 5
You're hot! Go north west
Guess x and y: 15 8
You're warm. Go south west
Guess x and y: 11 6
You got it right in 7 guesses!
Play again? n
Overall results:
Games played = 3
Total guesses = 16
Guesses/game = 5.3
You should handle the special case where the user guesses the correct number on the first try.
Print a message as follows:
I'm thinking of a number between 1 and 100...
Your guess? 71
You got it right in 1 guess!
In order to match the expected outputs in our logs, use a Random
object to produce random numbers.
On each game, first randomly choose the x coordinate from 0 to 19 (then shift it upward by 1), then do the same for y.
Assume valid user input.
When prompted for numbers, the user will type integers only, and they will be in proper ranges.
You should break down your program into several methods that use parameters and returns to exchange data.
Define a class constant for the maximum number used in the games.
The previous log shows games from 1 to 100, but you should be able to change the constant value to use other ranges such as from 1 to 50 or any maximum.