You are going to write a program that allows the user to play a simple guessing
ID: 640205 • Letter: Y
Question
You are going to write a program that allows the user to play a simple guessing game in which your program thinks up an integer and allows the user to make guesses until the user gets it right. For each incorrect guess you will tell the user whether the right answer is higher or lower. Your program is required to exactlyreproduce the format and behavior of the log of execution at the end of this write-up.
At a minimum, your program should have the following static methods in addition to method main:
Explanation / Answer
import java.util.Random;
import java.util.Scanner;
/*************
*
* This program allows the user to play a simple guessing game In this game
* program thinks up an integer and allows the user to make guesses until the
* user gets it right.
*
*/
public class Guess {
// to read input from users
private static Scanner sc = new Scanner(System.in);
private static final int GUESS_LOWER_LIMIT = 1; // lower limit to guess
private static final int GUESS_UPPER_LIMIT = 100; // upper limit to guess
private static int totalGamesPlayed = 0; // total games played in the game
private static int totalGuesses = 0;
private static float averageGuessesPerGame = 0;
private static int bestGame;
private static final int maxCount = 9999;
// give the game introduction
public static void gameIntroduction() {
System.out.println("This program allows you to play a guessing game.");
System.out.println("I will think of a number between "
+ GUESS_LOWER_LIMIT + " and" + " " + GUESS_UPPER_LIMIT
+ " and will allow you to guess until"
+ " you get it. For each guess, I will tell you"
+ " whether the right answer is higher or lower"
+ " than your guess.");
}
// generate a random number upto the Upper limit
public static int randomGererator() {
Random generator = new Random();
int randomNo = generator.nextInt(GUESS_UPPER_LIMIT);
if (randomNo == 0) {
randomNo++;
}
return randomNo;
}
// game starts here
public static void playGame() {
totalGamesPlayed++; // increment by one
int guessCounter = 0;// guess count for current game
int noGuessedByUser = 0;
System.out.println("I'm thinking of a number between "
+ GUESS_LOWER_LIMIT + " and " + GUESS_UPPER_LIMIT + "...");
int noToGuess = randomGererator();// calling the function to get random
// number
boolean flag = true;
do {
System.out.println("Your guess?");
noGuessedByUser = sc.nextInt();// user guessed number
guessCounter++;// increment by one
totalGuesses++;// increment by one
if (noGuessedByUser == noToGuess && guessCounter < maxCount+1) {
if (guessCounter == 1) {// if guess count is 1
System.out.println("You got it right in " + guessCounter
+ " guess");
flag=false;
} else {// if guess count is greater than 1
System.out.println("You got it right in " + guessCounter
+ " guesses");
flag=false;
}
} else {
if(guessCounter < maxCount){
evaluateGuess(noToGuess, noGuessedByUser);// call the games to evalute whether the value entered is lower or higher
}else{
System.out.println("You have crossed the maximum guess limit");//if the user crosses the max limit of guessing
flag=false;
}
}
} while (flag);
if(guessCounter <= maxCount){
bestGame = (guessCounter <= bestGame) ? guessCounter : bestGame;// updating the best game
}
}
// evalute whether the value entered is lower or higher
private static void evaluateGuess(int noToGuess, int noGuessedByUser) {
if (noGuessedByUser < noToGuess) {
System.out.println("It's higher.");
} else if (noGuessedByUser > noToGuess) {
System.out.println("It's lower.");
}
}
// print the overall game report
public static void overallGameReportGenerator() {
System.out.println("total games = " + totalGamesPlayed);
System.out.println("total guesses = " + totalGuesses);
averageGuessesPerGame = totalGuesses / totalGamesPlayed;
System.out.printf("Average guesses per game(guesses/game) = "
+ averageGuessesPerGame);
System.out.println(" Best game = " + bestGame);
}
// Main method
public static void main(String args[]) {
gameIntroduction();
do {
playGame();
System.out.println("Continue? (y/n): ");
} while (yes());
overallGameReportGenerator();
}
// to check whether user wants to continue or not
private static boolean yes() {
String str = null;
boolean flag = false;
do {
str = sc.next();
if (str.equalsIgnoreCase("Y")
|| str.substring(0, 1).equalsIgnoreCase("Y")) {
return true;
} else if (str.equalsIgnoreCase("N")
|| str.substring(0, 1).equalsIgnoreCase("N")) {
flag = true;
} else {
System.out.print("Invalid input. Try again.");
}
} while (!flag);
return false;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.