Rock Paper Scissors Program Rockjava contains a skeleton for
Rock, Paper, Scissors Program Rock.java contains a skeleton for the game Rock, Paper, Scissors. Open it and save it to your directory. Add statements to the program as indicated by the comments so that the program asks the user to enter a play, generates a random play for the computer, compares them and announces the winner (and why). For example, one run of your program might look like this:
$ java Rock Enter your play: R, P, or S
r
Computer play is S
Rock crushes scissors, you win!
Note that the user should be able to enter either upper or lower case r, p, and s. The user’s play is stored as a string to make it easy to convert whatever is entered to upper case. Use a switch statement to convert the randomly generated integer for the computer’s play to a string
// ************************************************************
// Rock.java
//
// Play Rock, Paper, Scissors with the user
//
// ************************************************************
import java.util.Scanner;
import java.util.Random;
public class Rock
{
public static void main(String[] args)
{
String personPlay; //User\'s play -- \"R\", \"P\", or \"S\"
String computerPlay; //Computer\'s play -- \"R\", \"P\", or \"S\"
int computerInt; //Randomly generated number used to determine computer\'s play
Scanner scan = new Scanner(System.in);
Random generator = new Random();
//Get player\'s play -- note that this is stored as a string
//Make player\'s play uppercase for ease of comparison
//Generate computer\'s play (0,1,2)
//Translate computer\'s randomly generated play to string switch (computerInt)
{
}
//Print computer\'s play
//See who won. Use nested ifs instead of &&.
if (personPlay.equals(computerPlay))
System.out.println(\"It\'s a tie!\");
else if (personPlay.equals(\"R\"))
if (computerPlay.equals(\"S\"))
System.out.println(\"Rock crushes scissors. You win!!\");
else
//... Fill in rest of code
Solution
Rock.java
--------------
import java.util.Random;
//************************************************************
//Rock.java
//
//Play Rock, Paper, Scissors with the user
//
//************************************************************
import java.util.Scanner;
public class Rock {
public static void main(String[] args) {
String personPlay = \"\"; // User\'s play -- \"R\", \"P\", or \"S\"
String computerPlay = \"\"; // Computer\'s play -- \"R\", \"P\", or \"S\"
int computerInt; // Randomly generated number used to determine
// computer\'s play
Scanner scan = new Scanner(System.in);
Random generator = new Random();
// Get player\'s play -- note that this is stored as a string
System.out.print(\"Enter your Play: \");
personPlay = scan.next();
// Make player\'s play uppercase for ease of comparison
personPlay = personPlay.trim().toUpperCase();
System.out.println(\"Person Play:\" + personPlay);
scan.close();
// Generate computer\'s play (0,1,2)
int min = 0;
int max = 2;
computerInt = generator.nextInt((max - min) + 1) + min;
// Translate computer\'s randomly generated play to string
switch (computerInt) {
case 0:
computerPlay = \"R\";
break;
case 1:
computerPlay = \"P\";
break;
case 2:
computerPlay = \"S\";
break;
default:
System.out.println(\"Invalid computer play. Plase try again.\");
}
// Print computer\'s play
System.out.println(\"Computer Play:\" + computerPlay);
// here we are cosidering indexes of R,P and S as 0,1 and 2 respectively.
// And the least index one is a winner.
// See who won. Use nested ifs instead of &&.
if (personPlay.equals(computerPlay)) {
System.out.println(\"It\'s a tie!\");
} else {
if (personPlay.equals(\"R\")) {
if (computerPlay.equals(\"P\")) {
System.out.println(\"Rock crushes Paper, you win!\");
} else { // S
System.out.println(\"Rock crushes Scissors, you win!\");
}
} else if (personPlay.equals(\"P\")) {
if (computerPlay.equals(\"R\")) {
System.out.println(\"Rock crushes Paper, Computer win!\");
} else { // S
System.out.println(\"Paper crushes Scissors, User win!\");
}
} else if (personPlay.equals(\"S\")) {
if (computerPlay.equals(\"R\")) {
System.out.println(\"Rock crushes scissors, Computer win!\");
} else { // P
System.out.println(\"Paper crushes scissors, Computer win!\");
}
}
}
}
}
Output:
---------
Enter your Play: r
Person Play:R
Computer Play:R
It\'s a tie!
Enter your Play: r
Person Play:R
Computer Play:P
Rock crushes Paper, you win!
Enter your Play: s
Person Play:S
Computer Play:P
Paper crushes scissors, Computer win!


