CPS 1 Paper, Rock, Scissors Lab (Lab 3, Fall 1999)
Overview
In this lab, you’ll write an applet that lets a human player enjoy a game of Paper, Rock, Scissors against a computer opponent. In case you don't remember or don't know, Paper, Rock, Scissors is a classic game in which each of two players simultaneously chooses either Paper, Rock, or Scissors, and then uses the following decision rule to determine whose choice wins:
This lab, of course, draws heavily on your experience from last week. You should have your work from Lab 2 on hand to get you through the Java basics.
Beginning your applet
1) As always, the first lines of the applet must specify what you need to import. For this program, and all the others that you’ll write this semester, those lines will be:
import java.awt.*;
import java.awt.event.*;
import awb.*;
2) Write the init method. Start it off like this:
public void init()
{
and add lines to do the following:
and then close up the init method with a close curly brace: }
Writing the actionPerformed method
public void actionPerformed(ActionEvent event)
{
Object cause = event.getSource();
computerInt = RandomIntGenerator.getInt(1,3);
if ( )
{
humanChoice.setText("Human player chose PAPER");
if ( )
{
computerChoice.setText("Computer player chose PAPER");
winner.setText("It's a draw");
}
if ( )
{
computerChoice.setText("Computer player chose ROCK");
winner.setText("Human player wins!");
}
if ( )
{
computerChoice.setText("Computer player chose SCISSORS");
winner.setText("Computer player wins!");
}
}
if ( )
{
humanChoice.setText("Human player chose ROCK");
if ( )
{
computerChoice.setText("Computer player chose PAPER");
winner.setText("Computer player wins!");
}
if ( )
{
computerChoice.setText("Computer player chose ROCK");
winner.setText("It's a draw.");
}
if ( )
{
computerChoice.setText("Computer player chose SCISSORS");
winner.setText("Human player wins!");
}
}
if ( )
{
humanChoice.setText("Human player chose SCISSORS");
if ( )
{
computerChoice.setText("Computer player chose PAPER");
winner.setText("Human player wins!");
}
if ( )
{
computerChoice.setText("Computer player chose ROCK");
winner.setText("Computer player wins!");
}
if ( )
{
computerChoice.setText("Computer player chose SCISSORS");
winner.setText("It's a draw.");
}
}
Finishing Up
<applet code = "PRS.class" width = 600 height = 300>
4) Test the applet in a web browser and correct any errors in function.