Question: You will create a simple text - based guessing game with ( at minimum ) the following behaviour: - The player is prompted to pick
You will create a simple textbased guessing game with at minimum the following behaviour:
The player is prompted to pick a number between and some other number this other number could be hardcoded or could also be picked by the user
Once the player has picked their number, the game repeatedly...
o Makes guess at the number see Appendix A for a suggested approach at determining the guess and asks the player if the guess is high, low, or correct
o If the guess is correct the game prints a happy message
o If the guess is incorrect the game makes another guess according to whether the previous guess was high or low
After the game guesses the correct answer, it asks if the user would like to play again and acts accordingly
See Appendix B for examples of acceptable program output.
You may create this program in any way you like, but it must involve all the following:
All code is in the guesser package
Variables
Conditional statements CHALLENGE: can you find an appropriate place for a switch statement?
Loops noninfinite, of course Obtaining user input using Scanner and System.in Formatting of Strings using one or more of the formatformattedprintf functions discussed in class
Use of ArrayList eg perhaps store a record of the number of guesses required in each round, and when the user chooses to stop playing, display the record to the user in the exit message. See Appendix B for an example.
Appropriate division of code into functions ie class methods CHALLENGE: Can you break your program into separate ui and logic packages where all printing and user input is done in the ui package, and the rest of the game logic is done in the logic package?
Appropriate JavaDoc comments for all nontrivial functions
Code that gracefully handles invalid input from the user ie your program should not crash if the user enters invalid values; instead, it should display helpful messages. You may need to use trycatch blocks to accomplish this.
And feel free to enhance the program beyond the requirements listed above if you want to push yourself!
Submission
Commit any uncommitted changes you have made
Push your changes to GitHub
Submit on the LMS
a Your repository clone URL
b The ID of your most recent commit
Rubric
See the rubric attached to this lab in the LMS
Appendix A: Making a good guess
Naive
o A naive way to guess at the number would be to start with the lowest number and add one until you reach the correct number
Random
o Another naive approach would be to pick a random number between the lower and upper limits You would probably want to keep track of numbers that have already been guessed and remove those numbers from the set of possible numbers as the game progresses. See this StackOverflow question for more information about generating random numbers in Java.
Binary Search
o The best way to guess at the number is to keep track of the current lower and upper limits and always guess the number halfway in between. For example, when guessing a number between and start with a guess of If the player says that is too low, then becomes the new lower limit and the next guess would be halfway between and ; if is too high then it becomes the upper limit and the next guess is halfway between and Continuing in this way will always result in locating the correct number, and on average will locate that number in logn guesses, where n is the initial number of possible guesses. Eg in the example here, the average number of guesses will be log or around guesses. This strategy is called Binary Search
because at each guess you are dividing the remaining number of possible correct guesses by two.
Appendix B: Example program output
This is an example session using the sample solution. Your program does not have to have exactly this output, but it should give you a sense of the expected behaviour of the app.
Let's play a guessing game!
I'll guess an integer you're thinking of between and some other number.
First, tell me the other number...the higher the number the harder it will be for me
What's that other number? Asdf
That's not a valid response!
Ok pick a number between and
Press enter when you're ready...
Is it
Enter L if Im too low, H if Im too high, or if Im right! L
Is it
Enter L if Im too low, H if Im too high, or if Im right! H
Is it
Enter L if Im too low, H if Im too high, or if Im right! l
That's not a valid response!
L
Is it
Enter L if Im too low, H if Im too high, or if Im right! H
Is it
Enter L if Im too low, H if Im too high, or if Im right! L
Is it
Enter L if Im too low, H if Im too high, or if Im right! L
Is it
Enter L if Im too low, H if Im too high, or if Im right!
Yay, I guessed it in guesses!
Do you want to play again? YN Y
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
