Assignment 3 / Lab 3
Paper, Rock, Scissors
[Due Thu May 31, 11:59pm]
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:
In English, here's how your applet will work: the user will be
presented with 3 buttons, labeled "Paper,"
"Rock," and "Scissors." When one of those
buttons is clicked, your ActionPerformed method is activated, and
your code will use the RandomGenerator to pick, at random, a
number between 1 and 3 as the Computer Player's choice. 1 means
the Computer Player chose Paper, 2 means Rock, and 3 means
Scissors. After getting that random number, your code will use a
series of if statements to display information
correctly and determine the winner of the contest.
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.*;
As always the first line is:
public class PRS extends java.applet.Applet implements
ActionListener
Now do your declarations: 3 Buttons named paper, rock and scissors; 3 TextFields named humanChoice, computerChoice and winner; and an int named computerInt.
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
{
Object cause = event.getSource();
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>