Skip to content
Advertisement

Looping a 2 player rock paper scissors game (Java)

im trying to create a 2 player rock paper scissors game with a prompt to continue or end the game. and also re-ask for your move if entered incorrectly. i’ve been trying to use do-while loops but i get an error every time. it doesn’t recognize the do-while i put in, because it’s not reading the while(playAgain.equals("Y"); let me know what i can fix and where i should start my do and start my while. thanks!

import java.util.Scanner;

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

        //player one input
        Scanner in = new Scanner(System.in);

        //loop start?
do {
        System.out.println("Player One, please enter your move [R/P/S]: ");
        String playerOne = in.nextLine();

        //verify move is valid
        if (!playerOne.equals("R") && !playerOne.equals("P") && !playerOne.equals("S")) {
            System.out.println("Invalid input, please enter valid move.");

        } else {

        //player two input

        System.out.println("Player Two, please enter your move [R/P/S]: ");
        String playerTwo = in.nextLine();

        //verify move is valid
        if (!playerTwo.equals("R") && !playerTwo.equals("P") && !playerTwo.equals("S")) {
            System.out.println("Invalid input, please enter valid move.");
        }
        //game outcome
        if (playerOne.equals(playerTwo)) {
            System.out.println("You tied!");
        } else if (
            (playerOne.equals("R") && playerTwo.equals("S")) ||
            (playerOne.equals("S") && playerTwo.equals("P")) ||
            (playerOne.equals("P") && playerTwo.equals("R"))) {

            System.out.println("Player One has won!");
        } else if (
            (playerTwo.equals("R") && playerOne.equals("S")) ||
            (playerTwo.equals("S") && playerOne.equals("P")) ||
            (playerTwo.equals("P") && playerOne.equals("R"))) {

            System.out.println("Player Two has won!");



        }}}//loop end? 
while (playAgain.equals("Y"));

        //prompt user to play again
        System.out.println("Would you like to play again? [Y/N]");

        String playAgain = in.nextLine();
        if (playAgain.equals("N")) {
            System.out.println("Game stopped. Thanks for playing!");
        }

        if (!playAgain.equals("Y") && !playAgain.equals("N")) {
            System.out.println("Invalid input, please enter valid response.");

        }}}

Advertisement

Answer

I will look further, though to improve readability|simplify logic

if (!playerOne.equals("R") && !playerOne.equals("P") && !playerOne.equals("S"))

is the same as

if (!((playerOne.equals("R") || playerOne.equals("P") || playerOne.equals("S")))

EDIT: In your logic, I don’t see a case for asking the player again. This can/will lead to a logic hole.

bool inPlay = true;
while(inPlay)
{
   ...
   if(valid plays)
   {
       if(tie) print tie;
       else if(p1 wins) print player1;
       else if(p2 wins) print player2;

       inPlay = ask: want to play again?
   }else
   {
       tell them it is invalid, loop again;
   }
   ...
}

^This will allow you to ask again

EDIT 2: for a do-while, it is essentially the same deal:

bool inPlay = true;
do
{
    above logic;
}while(inPlay);

EDIT 3: With your most recent version I see a vital flaw here:

do
{
    ...
}
while (playAgain.equals("Y"));//<-- semicolon
//prompt user to play again
System.out.println("Would you like to play again? [Y/N]");
...

You can’t go back to the start of the do-while loop with that prompt after the semicolon ; has been reached. You need to ask that question within the curly brackets {} after the game has been finished.

EDIT 4: to expand on OP’s “I’m getting an error that my playAgain variable is not initialized even when I add String playAgain;

String playAgain = "";
do
{
   ...
   playAgain = their answer;
   ...
}while(playAgain.equals("Y"));

However, I don’t think you need to keep the String outside the scope of the loop, a boolean is all you need, and a boolean is easier to read. So perhaps:

//See EDIT 2 above
...
// will result in true for Y and false for !Y
playAgain = (answer == 'Y')
...
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement