Answered step by step
Verified Expert Solution
Link Copied!

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 72 for the first game, and then 25 for the second game. The user's input is in bold orange. Here is how the output might look:
Sample run 1:
> Think of a number between 1 and 100:
> Is the number 50?(h/l/c/r): h
> Is the number 75?(h/l/c/r): l
> Is the number 62?(h/l/c/r): h
> Is the number 69?(h/l/c/r): h
> Is the number 72?(h/l/c/r): c
> You picked 72? Great pick.
Sample run 2:
> Think of a number between 1 and 100:
> Is the number 50?(h/l/c/r): h
> Is the number 75?(h/l/c/r): l
> Is the number 62?(h/l/c/r): r
> Is the number 50?(h/l/c/r): l
> Is the number 25?(h/l/c/r): c
> You picked 25? Great pick.
Notice that the program is guessing numbers, and the user is responding by entering 'h','l','c', 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 1 to 100:
guesser = NumberGuesser(1,100)
If the getCurrentGuess() method is called it should return 50, which is the midpoint between 1 and 100. 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 51 and 100, inclusive, and getCurrentGuess() would now return 75, the midpoint between 51 and 100. If the lower() method is then called, it should adjust its state to represent that the possible values are now between 51 and 74, and getCurrentGuess() should return 62, the midpoint between 51 and 74. 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, i.e. 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

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions