Answered step by step
Verified Expert Solution
Question
1 Approved Answer
For this assignment you will write up code for the same number guessing game, in which the user picks a number, and your program tries
For this assignment you will write up code for the same number guessing game, in which the user picks a number, and your program tries to guess the number. Each time that the program guesses a number the user responds by telling the program either the correct answer is higher or lower, or that the programs guess is correct.
A sample run of the program is printed below. In the example the user picks the value for the first game, and then for the second game. The user's input is in bold orange. Here is how the output might look:
Sample run :
Think of a number between and :
Is the number hlcr: h
Is the number hlcr: l
Is the number hlcr: h
Is the number hlcr: h
Is the number hlcr: c
You picked Great pick.
Sample run :
Think of a number between and :
Is the number hlcr: h
Is the number hlcr: l
Is the number hlcr: r
Is the number hlcr: l
Is the number hlcr: c
You picked Great pick.
Notice that the program is guessing numbers, and the user is responding by entering hlc or r for higher, lower, correct or reset.
The essential part of this assignment is writing a NumberGuesser class. This class will contain all of the logic for remembering the current state of the programs guesses.
When a new instance of a NumberGuesser class is created the upper and lower bounds of the possible values should be passed into its constructor. From that point on a NumberGuesser object will always return the midpoint of the possible values when the getCurrentGuess member function is called. If the higher or lower member functions are called, the NumberGuesser object should adjust its private variables to represent the new possible range of values.
For example, if a NumberGuesser is created with the following line of code then the range will be the numbers from to :
guesser NumberGuesser
If the getCurrentGuess method is called it should return which is the midpoint between and If the higher method is then called, the object should adjust its state accordingly so that it knows that the correct value is now between and inclusive, and getCurrentGuess would now return the midpoint between and If the lower method is then called, it should adjust its state to represent that the possible values are now between and and getCurrentGuess should return the midpoint between and By following this strategy the number guesser should be able to eventually guess the proper value that the user guessed.
The reset method should return a NumberGuesser object to the state that it was in when it was constructed, ie with its original lowerBound and upperBound reset back to their original values. This allows reusing this same NumberGuesser object to play a new game. Hint: you can create extra member variables in your class to store these original values.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started