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

I am currently making a rock paper scissors game in Java and need help with one

ID: 649326 • Letter: I

Question

I am currently making a rock paper scissors game in Java and need help with one part. The FULL move history of the user is is to be placed in an array of Strings and the FULL move history of the computer is to be placed in a LinkedList. Upon pressing "q" in the terminal when prompted for your move (which can be either "r" (rock), "p" (paper), "s"(scissors) or "q" (quit)), the game should end and the move histories of both the user and computer (up to the last 10 moves, or how ever many games available if less than 10 games played) as well as the outcome of each game, should be printed out. The array (which begins at size 5) containing the user history should double after each time the number of inputs exceeds array size (e.g. 10 after 6th move, 20 after 11th move etc.) For this, simply create new array each time, and copy over contents of old one. The move histories and outcomes should be printed in reverse order. Please help!

Explanation / Answer

import java.util.Scanner;
import java.lang.String;
import java.util.Random;

public class RockPaperScissors_InClass
{
   public static void main(String[] args)
   {

       // Create our objects
       Scanner scan = new Scanner(System.in);      
       Random ran= new Random();                   // The random number generator allows computer to throw a random hand
              
       // Initialize constants
       final String ROCK = "rock";
       final String PAPER = "paper";
       final String SCISSORS = "scissors";
       final String QUIT = "q";
      
       final String LOSS_MSG = "You lost!";
       final String WIN_MSG = "You won!";
       final String TIE_MSG = "Tie!";
      
       String computerHand = "";
       String playerHand = "";
       String roundResult = "You ";
       String scoreMsg = "Score: %d, Computer Score: %d ";
       String winnerMsg = "";
       int playerScore = 0;
       int computerScore = 0;
      
       // This is the main game loop!
       while (true)
       {
           System.out.println(" "scissors" ("q" to quit): ");
          
          
           boolean inputValid = true;
           playerHand = scan.next();
          
          
           if ( !(   playerHand.equals(ROCK)
                   || playerHand.equals(PAPER)
                   || playerHand.equals(SCISSORS)
                   || playerHand.equals(QUIT)     ))
           {
               inputValid = false;
           }
          
           // If input is valid, get new user input
           while (!inputValid) {
              

               playerHand = scan.next();
              
               // Check new input is valid
               if (       playerHand.equals(ROCK)
                       || playerHand.equals(PAPER)
                       || playerHand.equals(SCISSORS)
                       || playerHand.equals(QUIT)       )
               {
                   inputValid = true;
               }
           }
              
          
           // If user typed "q" then quit the game
           if (playerHand.equals(QUIT))
           {
               System.out.println("Thanks for playing!");
               return;
           }

           // Randomly generate computer's hand
           int tempHand = rand.nextInt(3);
           switch (tempHand)
           {
           case 0:
               computerHand = ROCK;
               break;
           case 1:
               computerHand = PAPER;
               break;
           case 2:
               computerHand = SCISSORS;
               break;
           }
          
           // Check for tie
           if (playerHand.equals(computerHand))
           {
               winnerMsg = TIE_MSG;
           }
           else
           {
               // If not a tie, compute the winner
               switch (computerHand)
               {
               case ROCK:
                   if (playerHand.equals(PAPER))
                   {
                       winnerMsg = WIN_MSG;
                       playerScore++;
                   }
                   else if (playerHand.equals(SCISSORS))
                   {
                       winnerMsg = LOSS_MSG;
                       computerScore++;
                   }
                   break;
                  
               case PAPER:
                   if (playerHand.equals(ROCK))
                   {
                       winnerMsg = LOSS_MSG;
                       computerScore++;
                   }
                   else if (playerHand.equals(SCISSORS))
                   {
                       winnerMsg = WIN_MSG;
                       playerScore++;
                   }
                   break;
                  
               case SCISSORS:
                   if (playerHand.equals(ROCK))
                   {
                       winnerMsg = WIN_MSG;
                       playerScore++;
                   }
                   else if (playerHand.equals(PAPER))
                   {
                       winnerMsg = LOSS_MSG;
                       computerScore++;
                   }
                   break;
               }
           }
          
           // Output the results of the round
           System.out.format(roundResult, playerHand, computerHand);
           System.out.println(winnerMsg);
           System.out.format(scoreMsg, playerScore, computerScore);
          
       }
      
   }
}