/*
 * File: GuessNumberGame3.java
 * Author: xxx
 * --------------------------
 * This program is a game to guess number that Java generated
 */

import acm.io.*;
import java.awt.*;
import javax.swing.*;
import java.util.Random;

public class GuessNumberGame3
{
   public static void main(String[] argv)
   {
     // Create a frame and show
     JFrame frame = new JFrame("Guessing Game 3");
     IOConsole console = new IOConsole();
     frame.getContentPane().add(BorderLayout.CENTER, console);
     frame.setSize(500, 300);
     frame.setVisible(true);
     
     // Users will enter their information
     console.println( "This is a guessing game" );
   
     // variable declaration
     int number, guess, option;     
     int max = 10;
     int count = 0;
     boolean match = false;
     boolean quit = false;
     
     // pick the random number
     Random rand = new Random();
     number = rand.nextInt( max ) + 1;  // add 1 to make random number between 0 and max

     while( quit != true )
     {
          // program for the game 
          while( match != true )
          {
            guess = console.readInt( "Enter your guess (1~" + max + ") > " );
            count++;
            
            if( guess == number )
            {
              console.println();
              console.println( "Congratulation. You got it!!!");
              console.println( "You guessed " + count + " times");
              match = true;
            }
            else
            {
              if( guess > number )
              {
                console.println( "Your guess is larger" );
              }
              else
              {
                console.println( "Your guess is smaller" );
              } 
            }
          }     
          
          // ask users if they want to play again
          console.println();
          option = console.readInt( "You want to play again? (0:yes, 1:no) > " );
          if( option == 1 )
          {
              // let quit be true to exit bigger loop
              quit = true;
          }
          else
          {
              // initialize the game              
              match = false;    // let match be false to play again
              count = 0;        // reset count to 0
              number = rand.nextInt( max ) + 1;    // pick random number again
          }
     }
          
     console.println( "Thank you for playing" );
   }
}

