Introduction: Java Rock, Paper, Scissors

About: I am interested in computer programming and robotics engineering. I am a true nerd.

   In this instructable I will show you how to create a program in Java that will allow you to play rock, paper, scissors with your computer.

Step 1: Software

The software you need is all free, the list goes...

Java JDK, http://www.oracle.com/technetwork/java/javase/downloads/index.html

Elclipse IDE, http://www.eclipse.org/downloads/


Step 2: Computer Setup

After you download Eclipse, go to command prompt, then type mkdir workspace. Open Eclipse, and it will prompt you to open a workspace, click ok. Once you are in Eclipse go to file>new>Java project. Once you are there, name it whatever you want, then where it says project layout, click use project folder for sources and class files. Then make a new class and uncheck the Inherited abstract methods, name it  GUI_RPS. Delete all that is in the program.

Step 3: 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();

}

}

Step 4: Run the Program

Enjoy the program! :)

The Teacher Contest

Participated in the
The Teacher Contest

ShopBot Challenge

Participated in the
ShopBot Challenge

Toy Challenge 2

Participated in the
Toy Challenge 2