/** This program simulates a guessing game. The computer will randomly * think of some number we are to guess. We'll get a certain number of * tries to get it right. Program features a loop and random number * generation. */ import java.io.*; public class Driver { public static void main(String [] args) throws IOException { BufferedReader kbd = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Welcome to my guessing game. Let's see if you can"); System.out.println("guess my secret number."); System.out.println("Let's start you off with $10."); // initialize the game Player p = new Player(10); // Keep playing the game until the player is broke or wants to quit. while (true) { System.out.print("\nHow much do you want to bet? "); int bet = Integer.parseInt(kbd.readLine()); // Check to make sure they aren't betting more than they have. if (!p.isValidBet(bet)) { System.out.println("You don't have that much money!"); continue; } // Loop until we win or lose the game. We don't know in advance // how many iterations the loop will take, so a while loop seems // appropriate. Guess game = new Guess(); while (true) { System.out.print("enter your guess: "); int newGuess = Integer.parseInt(kbd.readLine()); if (game.good(newGuess)) { System.out.println("Congratulations! You guessed my number!"); p.changeBalance(bet); break; } else if (game.isOver()) { System.out.println("Sorry, you have run out of guesses. Better luck" + " next time"); System.out.println("The answer was " + game.giveAnswer()); p.changeBalance(-bet); break; } } // Print the player's balance and see if they want to continue // playing or give up. System.out.println("You now have $ " + p.getBalance()); if (p.isBroke()) { System.out.println("Sorry -- you have run out of money!"); break; } System.out.print("Want to play some more? (y/n) "); char choice = kbd.readLine().charAt(0); if (! (choice == 'Y' || choice == 'y')) break; } } } // How could we modify the program so that if we win, it says how many // guesses it took us? Also, think about how we could modify the class so // that it can give advise in case our guess is really way off, for example // starting off with 90.