/*
 * appletGui.java
 *
 * Created on November 7, 2008, 3:57 PM
 * Author: Stephen Rahn
 */

import java.util.Random;

public class appletGui extends java.applet.Applet
{
   Random rand = new Random(1);
   int won, lost = 0;
   // private Game game = new Game();
   /** Initializes the applet appletGui */
   @Override
   public void init()
   {
      try
      {
         setSize(400, 300);

         java.awt.EventQueue.invokeAndWait( new Runnable()
         {

            public void run()
            {
               initComponents();
               txtAIOut.setFocusable(false);
            }
         } );
      }
      catch ( Exception ex )
      {
         ex.printStackTrace();
      }
   }

   /** This method is called from within the init() method to
    * initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is
    * always regenerated by the Form Editor.
    */
   // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
   private void initComponents() {

      txtAIOut = new java.awt.TextField();
      btnThrow = new java.awt.Button();
      lblTitle = new java.awt.Label();
      txtPlayerIn = new java.awt.TextField();
      lblResult = new java.awt.Label();
      lblRecord = new java.awt.Label();

      setLayout(null);

      txtAIOut.setEditable(false);
      add(txtAIOut);
      txtAIOut.setBounds(215, 80, 30, 20);

      btnThrow.setLabel("Throw");
      btnThrow.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnThrowActionPerformed(evt);
         }
      });
      add(btnThrow);
      btnThrow.setBounds(165, 150, 70, 25);

      lblTitle.setAlignment(java.awt.Label.CENTER);
      lblTitle.setFont(new java.awt.Font("Dialog", 0, 14)); // NOI18N
      lblTitle.setText("Rock-Paper-Scissors");
      add(lblTitle);
      lblTitle.setBounds(125, 0, 150, 30);

      txtPlayerIn.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
            txtPlayerInActionPerformed(evt);
         }
      });
      add(txtPlayerIn);
      txtPlayerIn.setBounds(155, 80, 30, 20);

      lblResult.setAlignment(java.awt.Label.CENTER);
      add(lblResult);
      lblResult.setBounds(125, 225, 150, 20);

      lblRecord.setAlignment(java.awt.Label.CENTER);
      add(lblRecord);
      lblRecord.setBounds(125, 280, 150, 20);
   }// </editor-fold>//GEN-END:initComponents

private void btnThrowActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThrowActionPerformed
   // int winner = game.Throw(txtPlayerIn.getText().trim(), txtAIOut.getText().trim());
   String pIn = txtPlayerIn.getText().trim();
   int winner = 0;

   int aIn = rand.nextInt(3);
   if(aIn == 0)
      txtAIOut.setText("R");
   else if(aIn == 1)
      txtAIOut.setText("P");
   else
      txtAIOut.setText("S");
   
   if((pIn.equals("R") || pIn.equals("R") || pIn.equals("R")) && (aIn >= 0 && aIn < 3))
   {
      if(pIn.equals("R") && aIn == 1)
         winner = 1;
      else if(pIn.equals("P") && aIn == 2)
         winner = 1;
      else if(pIn.equals("S") && aIn == 0)
         winner = 1;
      else if(aIn == 0 && pIn.equals("P"))
         winner = -1;
      else if(aIn == 1 && pIn.equals("S"))
         winner = -1;
      else if(aIn == 2 && pIn.equals("R"))
         winner = -1;
   }
   else
   {
      txtAIOut.setText("");
      if(aIn < 0 || aIn >= 3)
         lblResult.setText("AI output error.");
      else
         lblResult.setText("Please enter R, P, or S.");
      return;
   }
   
   if(winner == 1)
      won++;
   else if(winner == -1)
      lost++;
   
   if(winner == 1)
      lblResult.setText("You win :-)");
   else if(winner == -1)
      lblResult.setText("You lose :-(");
   else
      lblResult.setText("Cat's game :-|");
   
   lblRecord.setText("You are " + won + " and " + lost + ".");
}//GEN-LAST:event_btnThrowActionPerformed

private void txtPlayerInActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtPlayerInActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_txtPlayerInActionPerformed

   // Variables declaration - do not modify//GEN-BEGIN:variables
   private java.awt.Button btnThrow;
   private java.awt.Label lblRecord;
   private java.awt.Label lblResult;
   private java.awt.Label lblTitle;
   private java.awt.TextField txtAIOut;
   private java.awt.TextField txtPlayerIn;
   // End of variables declaration//GEN-END:variables
}
