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

  1. Start it off like the last lab:
  2. public void actionPerformed(ActionEvent event)

    {

    Object cause = event.getSource();

  3. Add a line to randomly choose a number between 1 and 3 and assign it to the variable computerInt. It'll look like this:
  4. computerInt = RandomIntGenerator.getInt(1,3);
  5. Now, keeping in mind that if computerInt contains 1, the Computer Player chose Paper, if computerInt contains 2, the Computer Player chose Rock, and if computerInt contains 3, the Computer Player chose Scissors, fill in all the empty sets of parentheses in the following code (just retype the parts you're given):

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

  1. Include the last two close curly braces you need to match the open curly braces written earlier in the program.
  2. Make sure to save your code in a file named PRS.java (for Paper, Rock, Scissors...if you use another filename, points will be taken off), compile it, and correct all errors until it compiles correctly.
  3. Make the HTML document to display your applet on the web. You'll need to specify the width and height of the applet, to make sure it's all visible (It might be visible on some browsers without this step, but not on others. Points will be taken off if you don't do this step), so the applet line of the file will look like this:

    <applet code = "PRS.class" width = 600 height = 300>

  4. Test the applet in a web browser and correct any errors in function.
  5. Submission instructions will be posted shortly...