jeudi 2 décembre 2021

Number Guessing Game Project

Write a program to generate a random number given a range that is input by the user and have them guess the number.

Do the following:

Prompt the user to enter the min and max values for the number range that includes the number they want to guess. Use this range to generate a random number. Use the below statement to generate the random number (int) Math.floor(Math.random()*(max-min+1)+min); Let the user play the game as many times as they want. Have them enter 'q' or 'Q' to quit the game. Do the below checks on the number range - min to max: Don't allow negative numbers. Make sure the minimum range value is not > the maximum range value. Make sure the max value is <= 10000 Feel free to add a few more checks if you want. When the user guesses the number, give them a hint if they guess either too high or too low to help them eventually guess the number. Record the number of tries and output it when the user guesses the correct number. Also allow them the option to quit the guesses by entering a 'q' or 'Q'. See the sample runs below for guidance.

When the program is completed, output the following:

total number of games the user played the average number of guesses Make sure your program has the following:

Prompts for the user input. Remember you have to handle the input of a 'q' or 'Q' or min and max range. Some type of loop to allow the user to play the game multiple times and to allow them to guess at the number multiple times. So what's the best way to guess a number? Have you ever heard of binary search? If you perform a binary search, you can eliminate half of the numbers with each guess so the time it takes to find the number would be log2(n) where n is the size of the number range (here log2 is log base 2). So if you have a range of 1000 numbers, it should only take ~10 guesses using this approach (that's because 2 ** 10 = 1024 which is ~1000);

"Binary Search is an example of the divide and conquer strategy where given a large problem we don’t attack it directly. Instead we divide it into smaller problems, solve those, and then reassemble the whole problem to get the result. Often this is a much more efficient approach!". This is from Algorithms: Binary Search - William Vincent (wsvincent.com) (Links to an external site.) Sample runs:

  1. Quit the Game right away

run: Welcome to Guess a Number Game The system will generate a random # that you will try to guess Enter 'q' to quit if you're done The system will then output the average guesses it took for you Please enter the min and max values for the number range in integer, q or Q to quit: q I'm quitting per your command Game Over 2. Play the game 2 times and see what your average guess score is

Welcome to Guess a Number Game The system will generate a random # that you will try to guess Enter 'q' to quit if you're done The system will then output the average guesses it took for you Please enter the min and max values for the number range in integer, q or Q to quit: 1 10 Please guess the number from 1 to 10 enter q or Q to quit: 5 Higher - try again Please guess the number from 1 to 10 enter q or Q to quit: 7 Higher - try again Please guess the number from 1 to 10 enter q or Q to quit: 9 Higher - try again Please guess the number from 1 to 10 enter q or Q to quit: 10 You got it in 4 guesses - Nice!

Please enter the min and max values for the number range in integer, q or Q to quit: 1 100 Please guess the number from 1 to 100 enter q or Q to quit: 50 Lower - try again Please guess the number from 1 to 100 enter q or Q to quit: 25 Lower - try again Please guess the number from 1 to 100 enter q or Q to quit: 12 Lower - try again Please guess the number from 1 to 100 enter q or Q to quit: 7 Lower - try again Please guess the number from 1 to 100 enter q or Q to quit: 4 Higher - try again Please guess the number from 1 to 100 enter q or Q to quit: 6 You got it in 6 guesses - Nice! Please enter the min and max values for the number range in integer, q or Q to quit: q I'm quitting per your command

Game Summary You played the game 2 times Your average guesses were 5.00 Game Over 3. Quit during the 1st guess game

run: Welcome to Guess a Number Game The system will generate a random # that you will try to guess Enter 'q' to quit if you're done The system will then output the average guesses it took for you Please enter the min and max values for the number range in integer, q or Q to quit: 1 10 Please guess the number from 1 to 10 enter q or Q to quit: 5 Higher - try again Please guess the number from 1 to 10 enter q or Q to quit: q I'm quitting this guessing round - try again Please enter the min and max values for the number range in integer, q or Q to quit: q I'm quitting per your command Game Over 4. Put in negative min value and check to make sure the program detects it and prompts you again

run: Welcome to Guess a Number Game The system will generate a random # that you will try to guess Enter 'q' to quit if you're done The system will then output the average guesses it took for you Please enter the min and max values for the number range in integer, q or Q to quit: 1 -10 You used a negative number for the range so try again Please enter the min and max values for the number range in integer, q or Q to quit: 1 10 Please guess the number from 1 to 10 enter q or Q to quit: q I'm quitting this guessing round - try again Please enter the min and max values for the number range in integer, q or Q to quit: q I'm quitting per your command Game Over 5. Put in a min value that is > max and make sure the program detects it and prompts you again

run: Welcome to Guess a Number Game The system will generate a random # that you will try to guess Enter 'q' to quit if you're done The system will then output the average guesses it took for you Please enter the min and max values for the number range in integer, q or Q to quit: 10 0 C'mon man - min is > max so try again Please enter the min and max values for the number range in integer, q or Q to quit: 1 5 Please guess the number from 1 to 5 enter q or Q to quit: 3 Higher - try again Please guess the number from 1 to 5 enter q or Q to quit: 4 You got it in 2 guesses - Nice! Please enter the min and max values for the number range in integer, q or Q to quit: 1 1000 Please guess the number from 1 to 1000 enter q or Q to quit: 500 Lower - try again Please guess the number from 1 to 1000 enter q or Q to quit: 250 Lower - try again Please guess the number from 1 to 1000 enter q or Q to quit: 125 Higher - try again Please guess the number from 1 to 1000 enter q or Q to quit: 175 Higher - try again Please guess the number from 1 to 1000 enter q or Q to quit: 200 Higher - try again Please guess the number from 1 to 1000 enter q or Q to quit: 225 Lower - try again Please guess the number from 1 to 1000 enter q or Q to quit: 210 Higher - try again Please guess the number from 1 to 1000 enter q or Q to quit: 220 Lower - try again Please guess the number from 1 to 1000 enter q or Q to quit: 215 Higher - try again Please guess the number from 1 to 1000 enter q or Q to quit: 217 You got it in 10 guesses - Nice! Please enter the min and max values for the number range in integer, q or Q to quit: q I'm quitting per your command

Game Summary You played the game 2 times Your average guesses were 6.00 Game Over 6. Put in a max range value > 10000

run: Welcome to Guess a Number Game The system will generate a random # that you will try to guess Enter 'q' to quit if you're done The system will then output the average guesses it took for you Please enter the min and max values for the number range in integer, q or Q to quit: 1 100000 You exceed the max value for max (10000) so try again Please enter the min and max values for the number range in integer, q or Q to quit: q I'm quitting per your command Game Over

I don't have much right now besides

import java.util.Random; import java.util.Scanner;

int answer, guess;

int min, max;

char c;

(int) Math.floor(Math.random()*(max-min+1)+min)




Aucun commentaire:

Enregistrer un commentaire