University of Pittsburgh
Spring 2004
CS0007:  Introduction to Computer Programming
  Assignment 5: Rock - Paper - Scissors
PROJECT INFORMATION
DUE DATE
Wed Mar 17, midnight (St. Patrick's Day!)
VALUE
60 points
BACKGROUND READING
Chapter 4 JBD
INTRODUCTION

REMINDER:  You are to work on the projects independently. 
Never look at each other's code for any reason, this is cheating.


Remember the ultimate tie-breaking game of games? Some might think thumb-wrestling or even Monopoly... but none can compare to a good old fashioned game of rock-paper-scissors (RPS). 

The game is simple: each player pounds their fists into their other hand three times: on the third, each player comes up with either a rock (fist), paper (an open hand), or scissors (a v-shape with two fingers). The winner is determined like this: 

     paper covers rock 
     rock crushes scissors 
     scissors cut paper 

Many times people like to play best of 3 or 5, or some other number.
 

TERMINOLOGY & EXAMPLE

A game is simply when each player makes a choice and the winner is determined.  A match, then, refers to a bunch of games played in succession.  The goal of the player is to win the whole match, but it is entirely possible (and likely) that s/he will lose a game or more along the way.  It is also possible for games to result in a tie, which means neither player is credited with a win.

Suppose two people (Alice and Bob) want to play a 5 game match.  This means the first person to 3 wins wins the match.  Here is one possible way the game could proceed:
  1. Alice chooses rock, Bob chooses scissors.
    • Alice wins, she is up 1-0.
  2. Alice chooses paper, Bob chooses paper.
    • tie, Alice remains in the lead 1-0.
  3. Alice chooses scissors, Bob chooses paper.
    • Alice wins, so she goes up 2-0.
  4. Alice chooses scissors, Bob chooses rock.
    • Bob wins, so the record is now 2-1 in favor of Alice.
  5. Alice chooses paper, Bob chooses rock.
    • Alice wins taking her record to 3-1.
Alice wins this match.  Even though there is one game left to play, it doesn't matter since Bob can only reach 2 wins, so the game is skipped.  As you can see, ties are essentially ignored as they have no bearing on the record.

WHAT TO DO

You are to write a program that plays RPS against the user.  The program should ask the user how many times s/he would like to play, then run just enough games to determine a winner (sometimes referred to as "best of").  For example, if the user wants to play best of 5, then your program should stop when either the computer, or the user reach 3 wins.  Best of an even number is defined as 1 game over half, although that is kind of strange to do.

Your program will determine the computer's choice randomly (some hints appear below).

The match is over when either player reaches the required number of wins.  At this time, your program should print out the overall winner and the final record. 
 

PROGRAM REQUIREMENTS

Your program should meet the following requirements (make sure you understand these!):
  • Use good style, indentation, comments, and identifer names.
  • Use intuitive java constants (with final) to represent the three choices.
    • For example, just putting 1, 2, and 3 all over in your code is a poor choice.
    • To create a constant in java, just do something like this:  static final int ROCK = 1;
    • In your program, you can refer to ROCK instead of 1.  This is much easier to read and remember.
  • Write methods to handle the subtasks.
    • Analyze the problem and determine which subtasks are handled best by methods (and not in main()).
    • For example, you should have one that prints the rules of the game. 
    • Also, you should have one that accepts two game choices as input and returns the winner of that game to the call in main().  The function should do nothing beyond that.  It just tells you who wins a single game.  Handle other details in main() or in other methods you have written.
    • If you are nervous about this part, feel free to share your planning ideas with the TA or instructor before writing the code.  Part of the score for this assignment will be how well you organize your code using subprograms.
  • In each game, your program should print out both the user's choice and the computer choice before displaying the winner.
  • After the games are over, you should print out the final record (e.g., "You won 3-1" or "The computer wins 3-0").
 
TURNING THE ASSIGNMENT IN

Copy your source code (.java) and executable (.class) into the assignment5 handin directory.  Refer to the old handouts for more details.
 
FURTHER INFORMATION & COMMENTS

Random Numbers:

Java provides a random number generator, but you have to massage it a bit to make it useful in this program (this is common, actually).  We covered it in class, but here is a short overview just to be complete:
 
To get a random number in java, you need to use the Math class.  The call looks like this:

Math.random()

This will return a double between 0 and 0.9999...  In your program, you will need to randomly pick one of three values from this result.  There are several ways to go.  Here is one:

The first step is to multiply the number by 3.  This will give you something between 0.0 and 3.0 (not including 3.0).  That means the leading number will be 0, 1, or 2. 

Math.random() * 3

The fractional part is no longer needed, so we truncate it (a straight typecast does this).

(int) (Math.random() * 3)

This gives a 0, 1, or 2


Here is a small program you can run if you want to play around.  Store it in testran.java

class testran {
   public static void main (String[] args) {
      int a;
      a = (int) (Math.random() * 3);
      System.out.println(a);
   }
}

If you run this 5 or 6 times, you'll start to see the randomness.  If you need to get a 1, 2, or 3, just add 1 to the result.  In your pogram, it is probably best to tuck this away in a method whose job is to simply get the computer's choice.

You can view the Java math class here.

Good luck!
Last Updated: 2/26/03 by H. Chad Lane
© 2003-2004 University of Pittsburgh