import SimpleOutput; import SimpleInput; import NumberGameOracle; // The oracle knows the answer. import NumberGameGuesser; // The guesser does not. /** * Let the computer play the number game. The user tells the computer * which number, and then the guesser tries to guess. * * @author Samuel A. Rebelsky * @version 1.0 of January 1999 */ public class NumberGame { /** * Play the game. */ public static void main(String[] args) { // A game with no output is fairly boring. SimpleOutput out = new SimpleOutput(); // We'll need to read a number to guess. SimpleInput in = new SimpleInput(); // Our intelligent guesser. NumberGameGuesser guesser = new NumberGameGuesser(); // And our even more intelligent oracle. We'll create the oracle once // we know the solution. NumberGameOracle oracle; // The number to guess. long solution; // The lower bound of the range. long lower; // The upper bound of the range. long upper; // The minimum lower and maximum upper bounds (required by the guesser). long min_lower = Long.MIN_VALUE/2; long max_upper = Long.MAX_VALUE/2; // Describe the game. out.println("Welcome to the amazing number game. You will give me"); out.println("a range of numbers and a number to guess. I will tell"); out.println("my friend to guess the number, and we'll see how it does."); out.println(); // Ask for the range. out.print("Enter the smallest possible answer " + "(at least " + min_lower + "): "); lower = in.readLong(); if (in.hadError()) { lower = min_lower; } out.print("Enter the largest possible answer " + "(at most " + max_upper + "): "); upper = in.readLong(); if (in.hadError()) { upper = max_upper; } // Validate the range. if (lower < min_lower) { out.println("Your smallest number is too small."); return; } if (upper > max_upper) { out.println("Your largest number is too large."); return; } if (upper < lower) { out.println("The smallest number is larger than the largest number."); return; } // Ask for input. out.print("Please enter the number to guess: " + "(between " + lower + " and " + upper + "): "); solution = in.readLong(); // Validate the solution. if (solution < lower) { out.println("Your solution is less than the smallest number."); return; } if (solution > upper) { out.println("Your solution is larger than the largest number."); return; } // Create the oracle. oracle = new NumberGameOracle(solution); // Okay, let's play the game. guesser.guessNumber(lower,upper,out,oracle); // We're done. } // main(String[]) } // class NumberGame