Instructions say to complete the RockPaperScissors program in Java to these spec
ID: 3553680 • Letter: I
Question
Instructions say to complete the RockPaperScissors program in Java to these specifications: <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /?>
Write a game that plays many rounds of Rock Paper Scissors. The user and computer will each choose between three items; rock (defeats scissors, but loses to paper), paper (defeats rock, but loses to scissors), and scissors (defeats paper, but loses to rock). If the player and computer choose the same item, the game is a tie.
You could extend this program to include different algorithmic strategies for choosing the best item. Should the computer pick randomly? Should it always pick a particular item or a repeating pattern of items? Should it count the number of times the opponent chooses various items and base its strategy on this history?
-Your program should use the prompt and messages shown in the following example run:
- Name your class RockPaperScissors
Enter you choice:
R or r for Rock
P or p for paper
S or s for scissors
Q or q to quit
R
Computer picked: rock
You tie with the computer
Enter you choice:
R or r for Rock
P or p for paper
S or s for scissors
Q or q to quit
P
Computer picked: rock
You win against the computer
Enter you choice:
R or r for Rock
P or p for paper
S or s for scissors
Q or q to quit
s
Computer picked: paper
You win against the computer
Enter you choice:
R or r for Rock
P or p for paper
S or s for scissors
Q or q to quit
p
Computer picked: scissors
You lose to the computer
Enter you choice:
R or r for Rock
P or p for paper
S or s for scissors
Q or q to quit
q
Enter you choice:
R or r for Rock
P or p for paper
S or s for scissors
Q or q to quit
R
Computer picked: rock
You tie with the computer
Enter you choice:
R or r for Rock
P or p for paper
S or s for scissors
Q or q to quit
P
Computer picked: rock
You win against the computer
Enter you choice:
R or r for Rock
P or p for paper
S or s for scissors
Q or q to quit
s
Computer picked: paper
You win against the computer
Enter you choice:
R or r for Rock
P or p for paper
S or s for scissors
Q or q to quit
p
Computer picked: scissors
You lose to the computer
Enter you choice:
R or r for Rock
P or p for paper
S or s for scissors
Q or q to quit
q
Explanation / Answer
// ****************************************************************
// Rock.java
//
// Play Rock, Paper, Scissors with the user
//
// ****************************************************************
import java.util.Scanner;
import java.util.Random;
public class RockPaperScissors
{
public static void main(String[] args)
{
Scanner scan= new Scanner(System.in);
String personPlay; //User's play -- "R", "P", or "S"
String computerPlay="F"; //Computer's play -- "R", "P", or "S"
int computerInt; //Randomly generated number used to determine
//computer's play
while(true){
System.out.println("Enter your choice:");
System.out.println("R or r for Rock");
System.out.println("P or p for paper");
System.out.println("S or s for scissors");
System.out.println("Q or q to Quit");
personPlay = scan.nextLine();
personPlay = personPlay.toUpperCase();
if(personPlay.equals("Q"))
break;
Random generator = new Random();
computerInt = generator.nextInt (3) +1;
if(computerInt==1)
{
computerPlay="R";
System.out.println("Computer picked: rock");
}
if(computerInt==2)
{
computerPlay="P";
System.out.println("Computer picked: paper");
}
if(computerInt==3)
{
computerPlay="S";
System.out.println("Computer picked: scissors");
}
if (personPlay.equals(computerPlay))
System.out.println ("You tie with the computer!");
else
{
if (personPlay.equals("R"))
{ if (computerPlay.equals("S"))
System.out.println ("You win against the computer!");
if (computerPlay.equals("P"))
System.out.println ("You lose to the computer!");
}
if (personPlay.equals("P"))
{ if (computerPlay.equals("R"))
System.out.println ("You win against the computer!");
if (computerPlay.equals("S"))
System.out.println ("You lose to the computer!");
}
if (personPlay.equals("S"))
{
if (computerPlay.equals("R"))
System.out.println ("You lose to the computer!");
if (computerPlay.equals("P"))
System.out.println ("You win against the computer!");
}
}
}
}
}