In: Computer Science
Game Description: The popular rock-paper-scissors game is usually played between two people in which each player simultaneously chooses either a rock or a paper or scissors (usually with an outstretched hand). The rule of the game is simple: rock crushes scissors, scissors cut paper, and paper wraps rock. If both the players choose the same object, then it ends in a tie.
Problem Description: You have to play the rock-paper-scissors game against the computer for 100 times. You receive the following rewards each time you play the game:
You get $5 if you win
You get $2 if there is a tie
You get $-1 if you lose
The computer randomly chooses rock, paper, or scissors in each game. Rather than deciding what to play (rock, paper or scissors) for each individual game, you decide to use the following strategy: Play (i) scissors – (ii) rock – (iii) paper sequentially and repeatedly, over and over
Example:
Round 1: Play scissors
Round 2: Play rock
Round 3: Play paper
Round 4: Play scissors …
Write a program to play the rock-paper-scissors game against the computer for 100 times using the above strategy. You are required to calculate the total reward that you accumulate after playing the game 100 times.
Hint: Use loops to simulate the game 100 times. In each iteration, generate a random integer 1, 2, or 3 representing rock, paper, and scissors, respectively and use that number to know what computer has played (i.e. 1 is for rock, 2 is for paper, 3 is for scissors). Using your strategy described above, figure out whether you win, lose or tie the game in each iteration. Maintain a variable to keep track of the amount that you have won and keep updating that amount in each iteration depending on the result of the game. You can generate a random integer 1, 2, or 3 as follows: int randomNum = 1 + (int)(3*Math.random( ));
Deliverables:
The total reward that you receive (after playing the game 100 times) as a comment on top of your Java code.
1. Properly define the loop (2 points)
2. Correctly define sequential (i) scissors-(ii) rock-(iii) paper strategy for yourself (1.5 points)
3. Correctly define how computer randomly chooses rock, paper, or scissors (1.5 points)
4. Calculate how many points you gain for each outcome (4 points) 5. Proper display of output and comments where necessary (1 point)
5. Proper display of output and comments where necessary (1 point)
Note: Could you plz go through this code and let me
know if u need any changes in this.Thank You
_________________
// RockPaperScissors.java
import java.util.Random;
public class RockPaperScissors {
// Creating a random Class object
static Random r = new Random();
public static void main(String[] args) {
//Declaring variables
int tot = 0, comp, user = 0;
//Playing the game for 100
times
for (int i = 1; i <= 100; i++)
{
//getting the
computer selection
comp =
getComputerSelection();
if (i % 3 == 1)
{
user = 3;
} else if (i % 3
== 2) {
user = 1;
} else if (i % 3
== 0) {
user = 2;
}
if (comp == 1
&& user == 1) {
tot += 2;
} else if (comp
== 1 && user == 2) {
tot += 5;
} else if (comp
== 2 && user == 1) {
tot -= 1;
} else if (comp
== 2 && user == 2) {
tot += 2;
} else if (comp
== 2 && user == 3) {
tot += 5;
} else if (comp
== 3 && user == 2) {
tot -= 1;
} else if (comp
== 1 && user == 3) {
tot -= 1;
} else if (comp
== 3 && user == 1) {
tot += 5;
} else if (comp
== 3 && user == 3) {
tot += 2;
}
}
System.out.println("Total amount
won : $" + tot);
}
//Getting the computer choice
private static int getComputerSelection() {
return r.nextInt(3) + 1;
}
}
_________________________
Output:
Total amount won : $194
_______________Could you plz rate me well.Thank
You