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.
Step-by-step explanation:
(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
Get Answers For Free
Most questions answered within 1 hours.