Rock Paper Scissors! 09/29/2017 at 11:55 PM Objective: Write a program that simu
ID: 3872569 • Letter: R
Question
Rock Paper Scissors!
09/29/2017 at 11:55 PM
Objective:
Write a program that simulates a game of rock, paper, scissors between a human and the computer in best 2 out of 3 rounds.
Requirements:
The player can enter either “rock”, “paper”, or “scissors”.
If the player enters anything other than that the computer automatically gets a point
The computer randomly selects one of the gestures
Use the Random type to make this easier
Also make sure you import java.util.Random
You can use randomly selected integers to represent the gestures
For each combination either the computer scores a point, the player scores a point, or they score neither on draws.
Rock vs Paper = Paper wins
Paper vs Scissors = Scissors wins
Scissors vs Rock = Rock wins
After 3 rounds the winner or a tie is declared
The player is then asked whether or not they want to play again, and if they do the whole process starts over.
Example Dialog:
Welcome to Rock Paper Scissors! Best 2 out of 3!
Enter "Rock", "Paper", or "Scissors"
Rock
Rock v Paper! Computer Wins!
Player has won 0 times and the computer has won 1 times
Enter "Rock", "Paper", or "Scissors"
Rock
Rock v Paper! Computer Wins!
Player has won 0 times and the computer has won 2 times
Enter "Rock", "Paper", or "Scissors"
Rock
Rock v Scissors! Player Wins!
Player has won 1 times and the computer has won 2 times
The Computer Wins!
Play again? "Yes" or "No"
Yes
Enter "Rock", "Paper", or "Scissors"
Paper
Paper v Paper! Tie!
Player has won 0 times and the computer has won 0 times
Enter "Rock", "Paper", or "Scissors"
Paper
Paper v Paper! Tie!
Player has won 0 times and the computer has won 0 times
Enter "Rock", "Paper", or "Scissors"
Paper
Paper v Rock! Player Wins!
Player has won 1 times and the computer has won 0 times
The Player wins!
Play again? "Yes" or "No"
Yes
Enter "Rock", "Paper", or "Scissors"
Purple
Not a valid input! Computer wins
Player has won 0 times and the computer has won 1 times
Enter "Rock", "Paper", or "Scissors"
Banana
Not a valid input! Computer wins
Player has won 0 times and the computer has won 2 times
Enter "Rock", "Paper", or "Scissors"
Paper
Paper v Paper! Tie!
Player has won 0 times and the computer has won 2 times
The Computer Wins!
Play again? "Yes" or "No"
No
Goodbye!
i need it by drjava program
Explanation / Answer
RockPaperSciccorsGame.java
import java.util.Random;
import java.util.Scanner;
public class RockPaperSciccorsGame {
public static void main(String[] args) {
//Declaring variables
String userChoice, computerChoice;
int playerWinCnt = 0, computerWinCnt = 0, tieCnt = 0;
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
while (true) {
while (true) {
//Getting the input entered by the user
System.out.println("Welcome to Rock Paper Scissors! Best 2 out of 3!");
System.out.println("Enter "Rock", "Paper", or "Scissors"");
userChoice = sc.next();
userChoice = userChoice.toLowerCase();
if (userChoice.equals("rock") || userChoice.equals("paper") || userChoice.equals("scissors")) {
//Calling the computer Selection method
computerChoice = randomSelection();
int res = playgame(userChoice, computerChoice);
if (res == 1) {
playerWinCnt++;
} else if (res == 2) {
computerWinCnt++;
} else {
tieCnt++;
}
System.out.println("Player has won " + playerWinCnt + " times and the computer has won " + computerWinCnt + " times");
//continue;
} else {
//continue;
userChoice = randomSelection();
//Calling the computer Selection method
computerChoice = randomSelection();
int res = playgame(userChoice, computerChoice);
if (res == 1) {
playerWinCnt++;
} else if (res == 2) {
computerWinCnt++;
} else {
tieCnt++;
}
System.out.println("Player has won " + playerWinCnt + " times and the computer has won " + computerWinCnt + " times");
}
if (playerWinCnt == 2) {
System.out.println("The Player Wins!");
break;
} else if (computerWinCnt == 2) {
System.out.println("The Computer Wins!");
break;
}
}
//Getting the character from the user 'Y' or 'y' or 'N' or 'n'
System.out.print(" Play again (Y/N) ::");
char ch = sc.next(".").charAt(0);
if (ch == 'Y' || ch == 'y')
continue;
else {
System.out.println(":: Program Exit ::");
break;
}
}
}
private static int playgame(String userChoice, String computerChoice) {
String winner;
int res;
//Finding who won in the game
if (userChoice.equals("rock") && computerChoice.equals("scissors")) {
winner = "Rock v scissors! Player win!";
res = 1;
} else if (userChoice.equals("scissors") && computerChoice.equals("rock")) {
winner = "Rock V scissors! Computer Wins!";
res = 2;
} else if (userChoice.equals("rock") && computerChoice.equals("paper")) {
winner = "Paper V rock! Computer Wins!";
res = 2;
} else if (userChoice.equals("paper") && computerChoice.equals("rock")) {
winner = "Paper v rock! Player win!";
res = 1;
} else if (userChoice.equals("paper") && computerChoice.equals("scissors")) {
winner = "Scissors v paper! Computer Wins!";
res = 2;
} else if (userChoice.equals("scissors") && computerChoice.equals("paper")) {
winner = "Scissors v paper! Player win!";
res = 1;
} else {
winner = "tie";
res = 0;
}
System.out.println(winner);
return res;
}
//This method will chooses the computer selection randomly
private static String randomSelection() {
String str = "";
Random rand = new Random();
int computerInt = 1 + rand.nextInt((3 - 1) + 1);
switch (computerInt) {
case 1:
str = "rock";
break;
case 2:
str = "paper";
break;
case 3:
str = "scissors";
break;
}
return str;
}
}
_____________________
Output:
Welcome to Rock Paper Scissors! Best 2 out of 3!
Enter "Rock", "Paper", or "Scissors"
Rock
Rock v scissors! Player win!
Player has won 1 times and the computer has won 0 times
Welcome to Rock Paper Scissors! Best 2 out of 3!
Enter "Rock", "Paper", or "Scissors"
Paper
tie
Player has won 1 times and the computer has won 0 times
Welcome to Rock Paper Scissors! Best 2 out of 3!
Enter "Rock", "Paper", or "Scissors"
Paper
tie
Player has won 1 times and the computer has won 0 times
Welcome to Rock Paper Scissors! Best 2 out of 3!
Enter "Rock", "Paper", or "Scissors"
Rock
Rock v scissors! Player win!
Player has won 2 times and the computer has won 0 times
The Player Wins!
Play again (Y/N) ::N
:: Program Exit ::
_____________Could you rate me well.Plz .Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.