// this is a rewrite of one of my first applets
// easier and better in swing, isn't everything
// John Sklar, Cardinal Stritch University, Spring, 2003
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Jrpsdemo extends JApplet implements ActionListener
{
JButton rock = new JButton(" Rock ");
JButton paper = new JButton (" Paper ");
JButton scissors = new JButton("Scissors");
JButton reset_it = new JButton("Reset");
JLabel MyName = new JLabel(" John Sklar Presents... ");
JLabel HeadLine = new JLabel(" - Rock-Paper-Scissors Game - ");
JLabel comp_play = new JLabel(" Click on one of ");
JLabel yourplay = new JLabel(" the buttons above!");
JLabel theResult = new JLabel(" ");
JLabel theScore = new JLabel("Wins: 0 Losses: 0 Ties: 0");
int wins=0,Mywins=0,Ties=0;
Font ArialFont = new Font("Arial", Font.PLAIN, 24);
Font ArialBold = new Font("Arial", Font.BOLD, 24);
Font buttonFont = new Font("Serif", Font.BOLD, 24);
JPanel verytop,top,middle,bottom,secondbottom,
thirdbottom,score,reset;
public void start()
{
MyName.setFont(ArialFont);
HeadLine.setFont(ArialFont);
comp_play.setFont(ArialBold);
yourplay.setFont(ArialBold);
theResult.setFont(ArialBold);
theScore.setFont(ArialFont);
rock.setFont(buttonFont);
paper.setFont(buttonFont);
scissors.setFont(buttonFont);
reset_it.setFont(buttonFont);
getContentPane().setLayout(new GridLayout(8,1));
verytop = new JPanel();
verytop.add(MyName);
verytop.setBackground(Color.black);
MyName.setForeground(Color.yellow);
top = new JPanel();
top.add(HeadLine);
top.setBackground(Color.yellow);
middle = new JPanel();
middle.add(rock);
middle.setBackground(Color.yellow);
rock.setBackground(Color.red);
rock.setForeground(Color.white);
middle.add(paper);
paper.setBackground(Color.white);
paper.setForeground(Color.blue);
middle.add(scissors);
scissors.setBackground(Color.blue);
scissors.setForeground(Color.white);
rock.addActionListener(this);
paper.addActionListener(this);
scissors.addActionListener(this);
bottom = new JPanel();
bottom.add(comp_play);
bottom.setBackground(Color.yellow);
secondbottom = new JPanel();
secondbottom.add(yourplay);
secondbottom.setBackground(Color.yellow);
thirdbottom = new JPanel();
thirdbottom.add(theResult);
thirdbottom.setBackground(Color.yellow);
score = new JPanel();
score.add(theScore);
score.setBackground(Color.yellow);
reset = new JPanel();
reset.setBackground(Color.yellow);
reset.add(reset_it);
reset_it.addActionListener(this);
reset_it.setEnabled(false);
getContentPane().add(verytop);
getContentPane().add(top);
getContentPane().add(middle);
getContentPane().add(bottom);
getContentPane().add(secondbottom);
getContentPane().add(thirdbottom);
getContentPane().add(score);
getContentPane().add(reset);
//setSize(400,400);
}
public void actionPerformed(ActionEvent e)
{
String userPressed="";
String computerGot="";
String[] Choice = { "rock", "paper", "scissors"};
Object source = e.getSource();
if(source == reset_it)
{
comp_play.setText(" Click on one of ");
yourplay.setText(" the buttons above!");
theResult.setText(" ");
theScore.setText("Wins: 0 Losses: 0 Ties: 0");
wins=0;Mywins=0;Ties=0;
reset_it.setEnabled(false);
}
else
{
reset_it.setEnabled(true);
if (source == rock)
{
userPressed = "rock";
yourplay.setText(" You clicked on: Rock");
}
else
if(source == paper)
{
userPressed = "paper";
yourplay.setText(" You clicked on: Paper");
}
else
if(source==scissors)
{
userPressed = "scissors";
yourplay.setText( " You clicked on: Scissors");
}
computerGot=Choice[(int) (Math.random()*3)];
comp_play.setText("The Computer Got: " + computerGot );
if(computerGot.equals(userPressed))
{// a tie
theResult.setText("Tie -- do it again");
Ties++;
}
else { // play the game.
if(userPressed.equals("rock"))// you got a rock
if(computerGot.equals( "paper"))
{
theResult.setText("Paper covers rock, You lose!");
Mywins++;
}
else {
theResult.setText("Rock breaks scissors, You win!");
wins++;
}
if(userPressed.equals("paper"))// you got a rock
if(computerGot.equals( "scissors"))
{
theResult.setText("Scissors cuts paper, You lose!") ;
Mywins++;
}
else{
theResult.setText( "Paper covers rock You win!") ;
wins++;
}
if(userPressed.equals("scissors"))// you got a rock
if(computerGot.equals("rock"))
{
theResult.setText( "Rock breaks scissors, You lose!");
Mywins++;
}
else{
theResult.setText( "Scissors cuts paper, You win!") ;
wins++;
}
}// end of the game
theScore.setText("Wins: " + wins +
" Losses: " + Mywins +
" Ties: " + Ties);
}// end of else
}// end of actionperformed
}// end of class