Spring 2004 CS0007: Introduction to Computer Programming Assignment 5: Rock - Paper - Scissors |
||||||
|
||||||
|
||||||
|
||||||
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 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:
|
||||||
|
||||||
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. |
||||||
|
||||||
Your program should meet the following requirements (make sure you understand these!):
|
||||||
|
||||||
Copy your source code (.java) and executable (.class) into the assignment5 handin directory. Refer to the old handouts for more details. |
||||||
|
||||||
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. |