Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The Rock-Paper-Scissors gesture game is described in the textbook. Here is the p

ID: 3815263 • Letter: T

Question

The Rock-Paper-Scissors gesture game is described in the textbook. Here is the partial ly implemented RPsGame class. public class RPSGame t private int roundsTotal; number of rounds to play private int roundsPlayed; number of rounds that have completed private String round inner; the winner of the current round private int onewins; number of rounds won by first player private int two ins; number of rounds won by second player private String oneMove first player's move private String twoMove second player's move private String winner; which player won? public RPSGame (int numRounds) f rounds Total 3 numRounds public void play Game to be implemented in the next question private void initialize Game() rounds played onellins 30; twowins 30; Wlnnel private boolean gameover return rounds Played rounds Total) private void advancePlay() f rounds Played++; oneMove chooseRPS() twoMove chooseRPS() one Move. equals ("Rock && twoMove.equals Scissors l I if Move equals Scissors && twoMove equals Paper II one one Move. equals ("Paper && two Move. equals ("Rock"))) one wins++;

Explanation / Answer

Here, the implementation of chooseRPS() method is as follows:

private String chooseRPS()
{
   float num;

num = (float) Math.random();

   if(num <= 0.33)
   {
       return "Rock";
   }

   // control reaches here which means num > 0.33 so no need to write
   // it inside the condition again.

   else if(num <= 0.66)
   {
       return "Paper";
   }

   //if both above condition does not satisfy means else, but you can
   // directly return as there will be no further code after else.

   return "Scissors";

}

Here, in the question, it is mentioned to use Math.random(). It gives the value between 0 and 1, 0 inclusive and 1 is excluded from the result. It is a double value, so I casted it in the float first. Here we are having 3 choices, Rock, Paper, and Scissors, and we are having till 0.99 value, we can divide values in 3 segments so that probability of a generated number is equal for all the segments which is 33 %. i.e. 1/3.

So, as per that programming, I used if else condition to return specific values. Like till 0.33, Rock is chosen, from 0.34 to 0.66, Paper is chosen and if both of these conditions are false, then Scissors is chosen. So, 1 from RPS is chosen with equal probability.

Please comment if there is any query. Thank you. :)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote