Question

Code the Rock Paper Scissors program: a. Get a move choice from player A which must...

Code the Rock Paper Scissors program: a. Get a move choice from player A which must be R, P, S or r, p, s Exit with System.exit(0) and an error msg if the user fails to respond correctly b. Get a move choice from player B in the same manner Again, Exit with System.exit(0) and an error msg if the user fails to respond correctly c. Display the results using the accepted phrases from the game Rock breaks Scissors, Paper covers Rock, Scissors cuts Paper and indicate the winner (Player A wins) or Rock vs Rock it’s a Tie! Etc…

In Javascript please and must execute in nebeans.

Homework Answers

Answer #1

Step-by-step explanation:

  1. User can choose either r, p, s or R, P, S as an input to start the game.
  2. playerMove method is used to take input from the user till it not valid
  3. If user input a valid move then StartGame method is called with playerMove parameters.
  4. If user enter the same input then there is a tie other wise one player will win the game according to the input.
  5. Example: PlayerA choose paper and playerB choose rock then so according to rule paper convers the rock, so player A wins.
  6. User can play the game till he or she like to play.

(Tip: file named should be RockPaperScissorsProject.java)

SOURCE CODE:

import java.util.Scanner;

public class RockPaperScissorsProject 
{

  Scanner userInput=new Scanner(System.in);//USER INPUT FROM KEYBOARD  
  String playAgain;//CHOICE TO PLAY AGAIN OR NOT 
   
  public void StartGame(String playerAmove,String playerBmove)//GAME BEGAIN WITH USER INPUT PARAMETER 
  {
    if(playerAmove.equals("R")||playerAmove.equals("r"))
    {
      if(playerBmove.equals("S")||playerBmove.equals("s"))
      {
        System.out.println("Rock breaks Scissors");
        System.out.println("playerA wins");
      }
      else if(playerBmove.equals("R")||playerBmove.equals("r"))
      System.out.println("Rock vs Rock it's a Tie! ");
       
      else
      {
         System.out.println("Paper covers Rock");
         System.out.println("playerB wins");
      }
    }
    else if(playerAmove.equals("P")||playerAmove.equals("p"))
    {
      if(playerBmove.equals("R")||playerBmove.equals("r"))
      {
        System.out.println("Paper covers Rock");
        System.out.println("playerA wins");
      }
      else if(playerBmove.equals("P")||playerBmove.equals("p"))
      System.out.println("Paper vs Paper it's a Tie! ");
       
      else
      {
         System.out.println("Scissors cuts Paper\n");
        System.out.println("playerB wins");
      }
    }
    else 
    {
      if(playerBmove.equals("P")||playerBmove.equals("p"))
      {
        System.out.println("Scissors cuts Paper");
        System.out.println("playerA wins");
      }
      else if(playerBmove.equals("S")||playerBmove.equals("s"))
         System.out.println("Scissors vs Scissors it's a Tie! ");
      else
      {
        System.out.println("Rock breaks Scissors");
        System.out.println("playerB wins");
      }
    }
     
     
         
  }
  public void playerMove()
  {
    String playerAmove,playerBmove;//USER INPUT MOVE 

    for(;;)//INFINITE LOOP FOR GAME CONTINUE TILL USER WANT 
    {
   
    for(;;)//INFINITE LOOP TILL USER DO NOT ENTER CORRECT INPUT 
    {
    System.out.println("Enter Move for player A");
    playerAmove=userInput.next();
     
      if(playerAmove.equals("R")||playerAmove.equals("P")||playerAmove.equals("S")    //CHECKS FOR VALID INPUT IF VALID INPUT THEN COME OUT OF LOOP
          ||playerAmove.equals("r")||playerAmove.equals("p")||playerAmove.equals("s"))
      break;
      else
      {
        System.out.println("Enter a valid input(R or P or S or r or p or s");
      }
      }
      for(;;)
      {
      System.out.println("Enter Move for player B");
      playerBmove=userInput.next();
       
      if(playerBmove.equals("R")||playerBmove.equals("P")||playerBmove.equals("S")
          ||playerBmove.equals("r")||playerBmove.equals("p")||playerBmove.equals("s"))
      break;
    else
    {
      System.out.println("Enter a valid input(R or P or S or r or p or s");
    }
    }
    StartGame(playerAmove,playerBmove);//START THE GAME WHEN CORRECT INPUT BY PLAYER A AND PLAYER B
     
    System.out.println("Do you like to play again(Y/N)");
    playAgain=userInput.next();//USER ENTER CHOICE TO PLAY AGAIN OR NOT 
     
    if(!playAgain.equals("Y"))//IF PRESS Y THEN ONLY USER IS ALLOWEDE TO PLAY AGAIN ELSE BREAK THE LOOP
      break;
    }
  }
  public static void main(String[] args) //PROGRAM EXECUTION BEGAIN NOW 
  {
     
    
    new RockPaperScissorsProject().playerMove();//PLAYER OBJECT TO START THE GAME 
       
    
  }
   
}

Sample Output:

run:

Enter Move for player A

s

Enter Move for player B

R

Rock breaks Scissors

playerB wins

Do you like to play again(Y/N)

Y

Enter Move for player A

R

Enter Move for player B

s

Rock breaks Scissors

playerA wins

Do you like to play again(Y/N)

Y

Enter Move for player A

P

Enter Move for player B

p

Paper vs Paper it's a Tie!

Do you like to play again(Y/N)

Y

Enter Move for player A

R

Enter Move for player B

r

Rock vs Rock it's a Tie!

Do you like to play again(Y/N)

N

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions