need help making a rock paper scissors game that plays for 5 rounds, preferably
ID: 3930414 • Letter: N
Question
need help making a rock paper scissors game that plays for 5 rounds, preferably using this format:
import java.util.Scanner;
import java.util.Random;
public class RockPaperScissors {
public static void main(String[] args) {
final int ROCK = 1;
final int PAPER = 2;
final int SCISSORS = 3;
final int MAX_ROUNDS = 5;
Scanner scnr = new Scanner(System.in);
Random rand = new Random(341L);
// TODO: Add variables for round, computerMaterial, playerMaterial, playerChoice, invalidChoice, etc.
while ( /* TODO: test number of rounds played */ ) {
System.out.println("Play! Enter: 1 (rock), 2 (paper), 3 (scissors)");
// TODO: get playerChoice and set playerMaterial accordingly. A value other than 1,2, or 3, causes the
// variable invalidChoice to be set to true.
// Start the round again if the user entered a value other than 1, 2, or 3.
if (invalidChoice) {
invalidChoice = false;
continue;
}
// TODO: get the computer choice from the random number generator and set computerMaterial accordingly.
System.out.println("Round " + round + ": computer chose " + computerMaterial
+ " and you chose " + playerMaterial + " ");
// Determine winner
// If both chose the same number, println "Tie!" and a blank line
// If you win, println "You win!" and a blank line
// TODO: Play the next round
}
System.out.println("Game over!");
}
}
Explanation / Answer
import java.util.Scanner;
import java.util.Random;
class RockPaperScissors {
public static void main(String[] args) {
final int ROCK = 1;
final int PAPER = 2;
final int SCISSORS = 3;
final int MAX_ROUNDS = 5;
Scanner scnr = new Scanner(System.in);
Random rand = new Random();
// TODO: Add variables for round, computerMaterial, playerMaterial, playerChoice, invalidChoice, etc.
int round =1;
boolean invalidChoice = false;
int playerChoice,computerChoice;
String playerMaterial,computerMaterial;
playerMaterial =" ";
computerMaterial = " ";
while ( round <= MAX_ROUNDS ) {
System.out.println("Play! Enter: 1 (rock), 2 (paper), 3 (scissors)");
// TODO: get playerChoice and set playerMaterial accordingly. A value other than 1,2, or 3, causes the
// variable invalidChoice to be set to true.
playerChoice = scnr.nextInt();
if(playerChoice < 1 || playerChoice > 3) invalidChoice =true;
else if(playerChoice == 1) playerMaterial ="rock";
else if(playerChoice == 2)playerMaterial ="paper";
else if(playerChoice == 3)playerMaterial ="scissors";
// Start the round again if the user entered a value other than 1, 2, or 3.
if (invalidChoice) {
invalidChoice = false;
continue;
}
// TODO: get the computer choice from the random number generator and set computerMaterial accordingly.
computerChoice = 1 + (int)(Math.random() * 3);
if(computerChoice == 1) computerMaterial ="rock";
else if(computerChoice == 2) computerMaterial ="paper";
else if(computerChoice == 3) computerMaterial ="scissors";
System.out.println("Round " + round + ": computer chose " + computerMaterial
+ " and you chose " + playerMaterial + " ");
// Determine winner
// If both chose the same number, println "Tie!" and a blank line
// If you win, println "You win!" and a blank line
if(playerChoice == computerChoice)
System.out.println("Tie!");
else if(playerChoice == 1 && computerChoice ==2)
System.out.println("Player Wins");
else if(playerChoice == 2 && computerChoice ==3)
System.out.println("Computer Wins Scissors cut Paper");
else if(playerChoice == 3 && computerChoice ==1)
System.out.println("Computer Wins rock Cut Scissors");
else if(playerChoice == 2 && computerChoice ==1)
System.out.println("Player Wins");
else if(playerChoice == 3 && computerChoice ==2)
System.out.println("Player Wins Scissors cut papers");
else if(playerChoice == 1 && computerChoice ==3)
System.out.println("Player Wins Rock cuts scissors");
round++;
}
System.out.println("Game over!");
}
}
output:
Success time: 0.06 memory: 711680 signal:0
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.