import java.util.Random; public class NumberGuessGame { //////////////// fields ///////////////// private int minNumber = 1; private int maxNumber = 100; private int pickedNumber; private int numGuesses = 0; private Random randomNumGen = new Random(); //////////////////// constructors ///////// public NumberGuessGame() { this.pickNumber(); } public NumberGuessGame(int min, int max) { this.minNumber = min; this.maxNumber = max; this.pickNumber(); } public void pickNumber() { int numValues = this.maxNumber - this.minNumber + 1; this.pickedNumber = this.randomNumGen.nextInt(numValues); this.pickedNumber = this.pickedNumber + this.minNumber; } /////////// methods //////////////////// public void playGame() { boolean done = false; // loop till guess is correct while (!done) { // need to get a guess from the user int guess = SimpleInput.getIntNumber("Pick a number "+ "between " + this.minNumber + " and " + this.maxNumber); // increment the number of guesses this.numGuesses++; // we need to check the guess (compare to pickedNum) if (guess == this.pickedNumber) { // tell the user she/he was right SimpleOutput.showInformation("That was right! " + "It took you " + this.numGuesses + " tries"); done = true; } else if (guess < this.pickedNumber) { // we need to tell the user too low SimpleOutput.showInformation("Too low"); } else { // tell the user the guess is too high SimpleOutput.showInformation("Too high"); } } } public static void main(String[] args) { NumberGuessGame game = new NumberGuessGame(); game.playGame(); } }