Instructables

Java Rock, Paper, Scissors

Step 3: The Program

Picture of The Program
My program does the following... It opens a window that has three text boxes and 3 buttons. The three text fields are for your choice, computer's choice and the counter. The buttons are for your choice of rock, paper, or scissors.

And here is my program (drumroll...),


import java.awt.*;

import java.awt.event.*;

import java.util.Random;



import javax.swing.*;





public class GUI_RPS

{

// These are member variables.  They are available to an instance of a class.

JFrame RPS;

JButton Rock, Paper, Scissors;

JTextField HRPS;

JTextField CRPS;

JTextField Results;

JTextField Counter;

JPanel hInput;

char hChoice = ' ';

char cChoice = ' ';

char[] cc = {'R', 'P', 'S'};

int r3;

int tie = 0;

int computerwins = 0;

int humanwins = 0;



Random rGen = new Random();  
// This is a constructor.  It gets called when new GUI_RPS() is called from main.

public GUI_RPS()

{



  RPS      = new JFrame("Rock, Paper, Scissors");

  RPS.setSize(600, 600);

  Rock     = new JButton("Rock");

  Paper    = new JButton("Paper");

  Scissors = new JButton("Scissors");

  hInput   = new JPanel();
 
  HRPS     = new JTextField(20);

  CRPS     = new JTextField(20);
 
  Results  = new JTextField(20);
 
  Counter  = new JTextField(30);



       Rock.addActionListener(new ActionListener()

  {

   public void actionPerformed(ActionEvent e)

   {

    HRPS.setText("You Chose Rock");

    hChoice = 'R';

    play() ;

   }



  });



  Paper.addActionListener(new ActionListener()

  {

   public void actionPerformed(ActionEvent e)

   {

    HRPS.setText("You Chose Paper");

    hChoice = 'P';

    play() ;

   }

  });



  Scissors.addActionListener(new ActionListener()

  {

   public void actionPerformed(ActionEvent e)

   {

    HRPS.setText("You Chose Scissors");

    hChoice = 'S';

    play() ;

   }

  });


  hInput.setLayout(new FlowLayout());

  hInput.add(HRPS);

  hInput.add(CRPS);
 
  hInput.add(Counter);

  hInput.add(Rock);

  hInput.add(Paper);

  hInput.add(Scissors);



  RPS.getContentPane().add(hInput, BorderLayout.CENTER);



  RPS.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  RPS.pack();

  RPS.setVisible(true);
  }



// Added method

private void play()

{

  r3 = rGen.nextInt(3);
  cChoice = cc[r3];



  switch (cChoice)

  {

  case 'R':

  CRPS.setText("Computer Chose Rock");

  break;

  case 'P':

   CRPS.setText("Computer Chose Paper");

   break;

  case 'S':

   CRPS.setText("Computer Chose Scisssors");

   break;

  }


  if(hChoice == cChoice)
  {
   tie++;
  }
 
 
        if(hChoice == 'R' && cChoice == 'S')
        {
         humanwins ++;
        }
 
        if(hChoice == 'P' && cChoice == 'R')
        {
         humanwins++;
        }
 
  if(hChoice == 'S' && cChoice == 'P')
  {
   humanwins++;
  }
 
 
 
 
  if(cChoice == 'R' && hChoice == 'S')
        {
         computerwins ++;
        }
 
        if(cChoice == 'P' && hChoice == 'R')
        {
         computerwins++;
        }
 
  if(cChoice == 'S' && hChoice == 'P')
  {
   computerwins++;
  }
 
 
  Counter.setText("Ties =  " + tie +  ", Human Wins = " + humanwins + ", Computer Wins = " + computerwins);
 
}





public static void main(String[] args)
{

  // Set the look and feel to Java Swing Look

  try
  {

   UIManager.setLookAndFeel(

     UIManager.getCrossPlatformLookAndFeelClassName());

  } catch(Exception e) {}

  GUI_RPS application = new GUI_RPS();

}

}

 
Remove these adsRemove these ads by Signing Up