/* Jemilah Mathis, CPTR 416
 Guessing Game Applet, February 25, 2004 */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Guess extends JApplet implements ActionListener {

  // GUI components
  JButton guessButton;
  JTextField guessField;
  JTextArea answerLabel;

  // variables used
  int randomNumber;
  boolean playingGame;
  int chances;
  String guesses;
  int playerGuess;

  public void init() {
    Container c = getContentPane();
    c.setLayout(new FlowLayout());
    c.setBackground(Color.white);

    guessButton = new JButton("Start Game");
    c.add(guessButton);
    guessButton.addActionListener(this);

    guessField = new JTextField(4);
    guessField.setEditable(false);
    c.add(guessField);

    answerLabel = new JTextArea(4,30);
    answerLabel.setEnabled(false);
    c.add(answerLabel);

    answerLabel.setLineWrap(true);
    answerLabel.setWrapStyleWord(true);
    playingGame = false;

    answerLabel.setText("Welcome to the Guessing Game!  Guess a number between 1 and 10." +
                        "You have 3 guesses.  Press Start Game to begin.");
  }

  public void actionPerformed (ActionEvent actionEvent)
  {
    try
    {
      if (playingGame) {
        playerGuess = Integer.parseInt(guessField.getText());
        guessField.setText("");

        // concatenating player's guesses in a string
        if (chances == 1)
          guesses += playerGuess;
        else
          guesses += ", " + playerGuess;

        if (playerGuess == randomNumber) {
          answerLabel.setText("Congratulations!!  " + playerGuess +
                              " was the correct answer!  " +
                              "It only took you " + chances + " try(s)!");
          playingGame = false; // finished game
          guessButton.setText("Start Game!"); // change button back to Start Game
          guessField.setEditable(false); // cannot enter # in textField
        } // end if (playerGuess == randomNumber)
        else if (chances == 3) {
          answerLabel.setText("SORRY!!!  The correct number was " +
                              randomNumber + ".  " +
                              "Let's play again!");
          playingGame = false; // finished game
          guessButton.setText("Start Game!"); // change button back to Start Game
          guessField.setEditable(false); // cannot enter # in textField
        } // end else if (chances == 3)
        else if (playerGuess > randomNumber) {
          chances++;
          if (chances == 2) { // chances = 2 and player's guess is > computer's #
            answerLabel.setText("Too high!!!  What is your " + chances +
                                "nd guess?\n"
                                + "Previous guesses:  " + guesses +
                                "\n             ");
          }
          else { // tries = 3 and player's guess is > computer's #
            answerLabel.setText("Too high!!  What is your " + chances +
                                "rd guess?\n"
                                + "Previous guesses:  " + guesses +
                                "\n              ");
          }
        } // end else if (playerGuess > randomNumber)
        else if (playerGuess < randomNumber) {
          chances++;
          if (chances == 2) { // tries = 2 and player's guess is < computer's #
            answerLabel.setText("Too low!!  What is your " + chances +
                                "nd guess?\n"
                                + "Previous guesses:  " + guesses +
                                "\n              ");
          }
          else { // tries = 3 and player's guess is > computer's #
            answerLabel.setText("Too low!!  What is your " + chances +
                                "rd guess?\n"
                                + "Previous guesses:  " + guesses +
                                "\n              ");
          }
        } // end else if (playerGuess < randomNumber)
      } // end if (playingGame)
      else { // playing game
        playingGame = true;
        chances = 1; // begin # of chances
        randomNumber = 1 + (int) (Math.random() * 10); // number computer generates

        answerLabel.setText("What's your " + chances + "st guess?");
        guessButton.setText("Guess!");
        guessField.setEditable(true);
        guesses = ""; // player's guesses aren't listed yet
      }
    } // end try block
    catch ( NumberFormatException formatException )
    {
      JOptionPane.showMessageDialog( this,
                                     "You must enter a number!!",
                                     "Invalid Number Format",
                                     JOptionPane.ERROR_MESSAGE );
    }
    paint(this.getGraphics());
  }

  public void paint(Graphics g)
  {
    super.paint(g);
  }
}

