Question
Java: Please help me with my code. I cannot get it to Ask if the player wants to play again after congratulations. Please see my
Java: Please help me with my code. I cannot get it to Ask if the player wants to play again after congratulations. Please see my ToDO. Thanks.
Code:
Guessing Game:
import java.util.Random;
public class GuessingGame {
/** * Seven instance variables; */ private int answer;//integer represents answer private Random generator; //random generator object private boolean gameOver; //Boolean, false if game in progress, true if game is over private int differential;//an integer represents difference b/w a guess and an answer private int max; //number to guess would be between 0 and 100. private int maxGuessesAllowed; //max number of guesses the user gets private int numGuessesTaken; //stores the number of guessed taken so far in the game
/** * Default Constructor, initialize the object with max equal to zero */ public GuessingGame() { max = 0; //set max to zero generator = new Random(); //creates random generator object }
/** * Parameterized Constructor, parameter is integer max * Creates the random number generator object */ public GuessingGame(int max) { this.max = max; //integer parameter represents max value of number to guess generator = new Random(); //creates random generator object }
/** * newGame method; */ public void newGame(int maxGuessesAllowed) //takes an integer as parameter, sets maxGuessesAllowed { this.maxGuessesAllowed = maxGuessesAllowed; //take integer as a parameter represents max number of guesses allowed answer = generator.nextInt(max); //generates the answer using the random number generator gameOver = false; //set gameOver to false differential = max; //set differential to the max value numGuessesTaken = 0; //set numGuessTaken to zero
} /** * v. isGameOver method - returns the state of game. * 1. true if game is over * 2. false if still in progress. * @return public boolean isGameOver() { if(numGuessesTaken == maxGuessesAllowed)//if numGuessesTaken is equal to maxGuessesAllowed { gameOver = true; //set gameOver to true } continue? } */
/** * guess method */ public String guess(int newGuess) //takes integer as parameter representing new newGuess { String response = ""; //String response returns String if(newGuess < 0 || newGuess > max)//compare newGuess with the answer is below zero and above max { return "Out of range error";//return out of range error } if(newGuess > answer)//if newGuess greater than answer { response = String.format("Too High! "); //response is too high numGuessesTaken++; //take numGuessesTaken and increment } if(newGuess < answer)//if newGuess is less than the answer { response = String.format("Too Low! "); //response is too low numGuessesTaken++; //take numGuessesTaken and increment } if(numGuessesTaken == maxGuessesAllowed)//if numGuessesTaken is equal to maxGuessesAllowed { gameOver = true; //set gameOver to true } if(newGuess == answer)//if the newGuess is equal to the answer { return "Congratulations! "; /** * TODO: How do I have the game ask would you like ot play again from here or refer to tester? * System.out.println("Would you like to play again? Enter Y for yes, N for no."); guess = keyboard.next().charAt(0); guess = 'n'; */ } if(Math.abs(newGuess - answer) > differential)//absolute value of newGuess minus answer is greater than differential { response = response + String.format("Getting Colder!"); //response is response plus "Getting colder" } else { response = response + String.format("Getting Warmer!"); //response is response plus "Getting warmer" } differential = Math.abs(newGuess - answer); //differential is absolute value of newGuess minus answer return response; //return response }
/** * Accessor and mutator methods for all instance fields except the Random * number generator. Use the Accessor or mutator methods within the other * methods of the class rather than directly accessing the instance fields. * For example, use mutator methods in the parameterized constructor to modify * instance variables. */ //Accessor for getAnswer public int getAnswer() { return answer; }
//Accessor for differential public double getDifferential() { return differential; }
//Accessor for max public double getMax() { return max; }
//Accessor for maxGuessesAllowed public int getMaxGuessesAllowed() { return maxGuessesAllowed; }
//Accessor for numGuessesTaken public int getNumGuessesTaken() { return numGuessesTaken; }
//mutator GameOver public void setGameOver(boolean gameOver) { this.gameOver = gameOver; }
//mutator for answer public void setAnswer(int answer) { this.answer = answer;
}
//mutator for differential public void setDifferential(int differential) { this.differential = differential; }
//mutator for max public void setMax(int max) { this.max = max;
}
//mutator for maxGuessesAllowed public void setMaxGuessesAllowed(int maxGuessesAllowed) { this.maxGuessesAllowed = maxGuessesAllowed; }
//mutator for numGuessesTaken public void setNumGuessesTaken(int numGuessesTaken) { this.numGuessesTaken = numGuessesTaken; }
public boolean isGameOver() { // TODO Auto-generated method stub return false; } }
Tester:
import java.util.Scanner;
public class GuessingGameTester {
public static void main(String[] args) { System.out.println("Welcome to the Guessing Game");
Scanner keyboard = new Scanner(System.in);//creates scanner object to read input char guess = 'y'; while(guess != 'n' && guess != 'N')//while guess is not "no" { System.out.println("Enter maximum number: "); int max; max = keyboard.nextInt(); GuessingGame game = new GuessingGame(max); System.out.println("Enter number of guesses allowed: "); int allowedGuess = keyboard.nextInt(); game.newGame(allowedGuess);
while(game.isGameOver() == false) { System.out.println("Enter your guess, remember it must be between 0 and " + max); int guessNew = keyboard.nextInt(); System.out.println(game.guess(guessNew)); } System.out.println("Would you like to play again? Enter Y for yes, N for no."); guess = keyboard.next().charAt(0); guess = 'n'; keyboard.close(); } } }
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