
import java.util.Random;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;

/**
 * This program selects a number between 1 and 100, then prompts the user
 * to try and guess it.
 */
public class GuessNumber {
  public static void main(String[] args) throws IOException {
    /** Prepare an object to read user input with. */
    BufferedReader input = new BufferedReader(new InputStreamReader(System.in));

    /** Pick a random number between 0 and 99, then add 1 to */
    int number = new Random().nextInt(100) + 1;
    /** This is used to count the number of guesses */
    int guessCount = 0;

    System.out.println("I have picked a number between 1 and 100.");
    System.out.println("You have 7 guesses too figure out what it is.");

    /** Until 7 guesses have been made, keep asking the user. */
    while(guessCount < 7) {

      System.out.print("Enter your guess: ");
      System.out.flush();
      /** Read in a line of user-input and convert it to a number. */
      int guess = Integer.parseInt(input.readLine());

      /** Increment the number of guesses the user has made. */
      guessCount = guessCount + 1;

      /** Check the guess against our number */
      if ( guess == number ) {
        System.out.println("You got it in " + guessCount + " tries!");
        /** The user has guessed correctly, so we exit. */
        return;
      }
      /** Check to see if the guess is too small */
      if ( guess < number ) {
        System.out.println("Your guess is too small! Guess Again!!");
      }
      /** Check to see if the guess is too large */
      if ( guess > number ) {
        System.out.println("Your guess is too large! Guess Again!!");
      }
    }
    /** The user failed to guess the number, apologize and exit. */
    System.out.println("Sorry, the number was " + number);
    return;
  }
}
