
/**
 * This class maintains a range of integers and a random integer in that
 * range, and allows the user to guess at the number.
 *   @author Dave Reed
 *   @version 1/24/05
 */
public class GuessRange {
   private int lowLimit;      // low limit on the range of numbers
   private int highLimit;     // high limit on the range of numbers
   private int chosenNumber;  // random number chosen from within the range
   private int numGuesses;    // number of guesses made so far  
   
   /**
    * Constructs a range with the given limits and selects a random number
    * from within that range.
    *   @param low lower limit on the range (inclusive)
    *   @param high upper limit on the range (inclusive)
    */
   public GuessRange(int low, int high) 
   {
      lowLimit = low;
      highLimit = high;
      
      chosenNumber = (int)(Math.random()*(highLimit-lowLimit+1)) + lowLimit;
      
      numGuesses = 0;   
    }
 
   /**
    * Accessor method to determine the current lower limit on the range.
    *   @return the lower limit (inclusive)
    */
   public int getLowLimit()
   {
      return lowLimit;
   }
      
   /**
    * Accessor method to determine the current upper limit on the range.
    *   @return the upper limit (inclusive)
    */
   public int getHighLimit()
   {
      return highLimit;
   }
   
   /**
    * Accessor method to determine the number of guesses made so far.
    *   @return the number of guesses (i.e., times makeGuess has been called)
    */
   public int getNumberOfGuesses()
   {
      return numGuesses;
   }
   
   /**
    * Handles a guess at the number in the range, returning the result and 
    * updating the range appropriately.
    *    @return result of the guess (either "CORRECT", "TOO HIGH", or "TOO LOW")
    */
   public String makeGuess(int guess) 
   {
      numGuesses++;
      
      if (guess == chosenNumber) {
         return "CORRECT";
      }
      else if (chosenNumber < guess) {
         highLimit = guess-1; 
         return "TOO HIGH";     
      }
      else {
         lowLimit = guess+1;
         return "TOO LOW";
      }
   }
}

