0

I am creating a program that allows you to play a game similar to paper, scissors rock.

The game works, but I am trying to incorporate a loop into the code that will ask the user if they wish to continue playing.

If yes, it will ask them for another input. If no, the program will simply state "Thanks for playing"

Here's the code:

 import java.util.Scanner;
import java.util.Random;
public class OkekpeJMoropinzee
{
    public static void main(String[]args)
    {

    String yourMove;
    String compMove;


    int compInt;





    String[] characters = {"Monkey","Robot","Pirate","Ninja","Zombie"};

    Scanner input = new Scanner(System.in);
    Random rand = new Random(6);

    compInt = rand.nextInt(5)+1;

    if (compInt == 1) 
       compMove = "Monkey"; 
    else if (compInt == 2) 
       compMove = "Robot"; 
    else if (compInt == 3) 
       compMove = "Pirate"; 
    else if (compInt == 4)
        compMove = "Ninja";
    else if (compInt == 5)
        compMove = "Zombie";




    System.out.println("What do you choose?: "); 
    yourMove = input.next();
    //MONKEY
    if(yourMove == "Monkey" || compInt == 1)
        System.out.println("Tie");
    else if (yourMove== "Monkey" || compInt == 2)
        System.out.println("You Win! Monkey Unplugs Robot!");
    else if (yourMove=="Monkey" || compInt == 3)
        System.out.println("You Lose! Pirate Skewers Monkey!");
    else if (yourMove == "Monkey" || compInt==4)
        System.out.println("You Win! Monkey fools Ninja!");
    else if (yourMove== "Monkey" || compInt==5)
        System.out.println("You Lose! Zombie savages monkey!");

    //RoBOT
    else if(yourMove == "Robot" || compInt == 2)
        System.out.println("Tie");
    else if (yourMove== "Robot" || compInt == 1)
        System.out.println("You Lose! Monkey Unplugs Robot!");
    else if (yourMove=="Robot" || compInt == 3)
        System.out.println("You Lose! Pirate Drowns Robot!!");
    else if (yourMove == "Robot" || compInt==4)
        System.out.println("You Win! Robot Chokes Ninja");
    else if (yourMove== "Robot" || compInt==5)
        System.out.println("You win! Robot Crushes Zombie!");

    //PIRATE
    else if(yourMove == "Pirate" || compInt == 3)
        System.out.println("Tie");
    else if (yourMove== "Pirate" || compInt == 1)
        System.out.println("You Win! Pirate Skewers Monkey!");
    else if (yourMove=="Pirate" || compInt == 2)
        System.out.println("You Win! Pirate Drowns Robot!");
    else if (yourMove == "Pirate" || compInt==4)
        System.out.println("You Lose! Ninja Karate Chops Pirate!");
    else if (yourMove== "Pirate" || compInt==5)
        System.out.println("You Lose! Zombie Eats Pirate!");

    //NINJA
    else if(yourMove == "Ninja" || compInt == 4)
        System.out.println("Tie");
    else if (yourMove== "Ninja" || compInt == 1)
        System.out.println("You Lose! Monkey Fools Ninja!");
    else if (yourMove=="Ninja" || compInt == 2)
        System.out.println("You Lose! Robot Chokes Ninja!");
    else if (yourMove == "Ninja" || compInt==3)
        System.out.println("You Win! Ninja Karate Chops Pirate!");
    else if (yourMove== "Ninja" || compInt==5)
        System.out.println("You Win! Ninja Decapitates Zombie!");

    //ZOMBIE
    else if(yourMove == "Zombie" || compInt == 5)
        System.out.println("Tie");
    else if (yourMove== "Zombie" || compInt == 1)
        System.out.println("You Win! Zombie Savages Monkey!");
    else if (yourMove=="Zombie" || compInt == 2)
        System.out.println("You Lose! Robot Crushes Zombie!");
    else if (yourMove == "Zombie" || compInt==3)
        System.out.println("You Win! Zombie Eats Pirate!");
    else if (yourMove== "Zombie" || compInt==4)
        System.out.println("You Lose! Ninja Decapitates Zombie!");
    }
}

3 Answers3

1

You could put the entire logic inside a do-while loop. The condition will be if the character entered is=='y' or 'Y'. The pseudo code should be:

char choice='n';
do
{ 
    < Insert Game logic here >

    System.out.println("Do you wanna continue? Enter y or Y for Yes")'
    choice = <obtain input using Scanner here>;
} 
while(choice=='y'||choice=='Y');

System.out.println("Thanks for Playing");
0

You could do something like this:

import java.util.Scanner;
import java.util.Random;
public class OkekpeJMoropinzee
{
    public static void main(String[]args)
    {
        String playAgain; 
    do{
        String yourMove;
        String compMove;


        int compInt;





        String[] characters = {"Monkey","Robot","Pirate","Ninja","Zombie"};

        Scanner input = new Scanner(System.in);
        Random rand = new Random(6);

        compInt = rand.nextInt(5)+1;

        if (compInt == 1) 
           compMove = "Monkey"; 
        else if (compInt == 2) 
           compMove = "Robot"; 
        else if (compInt == 3) 
           compMove = "Pirate"; 
        else if (compInt == 4)
            compMove = "Ninja";
        else if (compInt == 5)
            compMove = "Zombie";




        System.out.println("What do you choose?: "); 
        yourMove = input.next();
        //MONKEY
        if(yourMove == "Monkey" || compInt == 1)
            System.out.println("Tie");
        else if (yourMove== "Monkey" || compInt == 2)
            System.out.println("You Win! Monkey Unplugs Robot!");
        else if (yourMove=="Monkey" || compInt == 3)
            System.out.println("You Lose! Pirate Skewers Monkey!");
        else if (yourMove == "Monkey" || compInt==4)
            System.out.println("You Win! Monkey fools Ninja!");
        else if (yourMove== "Monkey" || compInt==5)
            System.out.println("You Lose! Zombie savages monkey!");

        //RoBOT
        else if(yourMove == "Robot" || compInt == 2)
            System.out.println("Tie");
        else if (yourMove== "Robot" || compInt == 1)
            System.out.println("You Lose! Monkey Unplugs Robot!");
        else if (yourMove=="Robot" || compInt == 3)
            System.out.println("You Lose! Pirate Drowns Robot!!");
        else if (yourMove == "Robot" || compInt==4)
            System.out.println("You Win! Robot Chokes Ninja");
        else if (yourMove== "Robot" || compInt==5)
            System.out.println("You win! Robot Crushes Zombie!");

        //PIRATE
        else if(yourMove == "Pirate" || compInt == 3)
            System.out.println("Tie");
        else if (yourMove== "Pirate" || compInt == 1)
            System.out.println("You Win! Pirate Skewers Monkey!");
        else if (yourMove=="Pirate" || compInt == 2)
            System.out.println("You Win! Pirate Drowns Robot!");
        else if (yourMove == "Pirate" || compInt==4)
            System.out.println("You Lose! Ninja Karate Chops Pirate!");
        else if (yourMove== "Pirate" || compInt==5)
            System.out.println("You Lose! Zombie Eats Pirate!");

        //NINJA
        else if(yourMove == "Ninja" || compInt == 4)
            System.out.println("Tie");
        else if (yourMove== "Ninja" || compInt == 1)
            System.out.println("You Lose! Monkey Fools Ninja!");
        else if (yourMove=="Ninja" || compInt == 2)
            System.out.println("You Lose! Robot Chokes Ninja!");
        else if (yourMove == "Ninja" || compInt==3)
            System.out.println("You Win! Ninja Karate Chops Pirate!");
        else if (yourMove== "Ninja" || compInt==5)
            System.out.println("You Win! Ninja Decapitates Zombie!");

        //ZOMBIE
        else if(yourMove == "Zombie" || compInt == 5)
            System.out.println("Tie");
        else if (yourMove== "Zombie" || compInt == 1)
            System.out.println("You Win! Zombie Savages Monkey!");
        else if (yourMove=="Zombie" || compInt == 2)
            System.out.println("You Lose! Robot Crushes Zombie!");
        else if (yourMove == "Zombie" || compInt==3)
            System.out.println("You Win! Zombie Eats Pirate!");
        else if (yourMove== "Zombie" || compInt==4)
            System.out.println("You Lose! Ninja Decapitates Zombie!");

        System.out.println("Would you like to play again? Type yes to play again."); 
        playAgain = input.next();
        } while(playAgain.equals("yes"));
    }

}
BlackHatSamurai
  • 23,275
  • 22
  • 95
  • 156
0

Well...let's be honest here. Your code actually doesn't work. I mean, yes it runs but it will definitely not carry out the game play properly.

Your random generator for computer game character selection will not work properly. You will need to do it this way:

Random rand = new Random();
compInt = rand.nextInt(5)+1;

Leave the 6 out of the initialization of rand.

The logic within the conditions for all your IF and IF/ELSE statements is set up so that at no time will any User input check past the MONKEY game character and this is because you use the OR (||) operator instead of the AND (&&) operator. The way you have it set up, if the computer chooses ROBOT and a User chooses ZOMBIE then the condition contained within:

else if (yourMove == "Monkey" || compInt == 2) {
    System.out.println("You Win! Monkey Unplugs Robot!");
}

will always fall true and display the message:

You Win! Monkey Unplugs Robot!

even though the User entered Zombie. Remember...the condition is basically set to be either or which is exactly what you don't want. You want both conditions to be true so you need to use the AND (&&) operator.

Besides that:

yourMove == "Monkey"

will always give unexpected results and here's why. Use the String.equals() method instead, something like:

yourMove.equals("Monkey")

You declare the characters variable as a string array yet nowhere do you utilize that Array. Why even bother. Just make the characters variable a space delimited String and utilize it to see if the User actually has supplied a character within the game play, for example:

String characters = "Monkey Robot Pirate Ninja Zombie";

yourMove = "";
while (yourMove.equals("")) {
    System.out.println("What do you choose? --> "); 
    yourMove = input.nextLine().toLowerCase();
    if (yourMove.equals("quit")) { 
        System.out.println("Thanks for playing. Bye Bye"); 
        System.exit(0); 
    }
    if (!characters.toLowerCase().contains(yourMove) || yourMove.equals("")) {
        System.out.println("You entered an invalid Game Character! Try again...\n"); 
        continue;
    }
}

In any case, here is a complete working game:

package okekpejmoropinzee;

import java.util.Random;
import java.util.Scanner;

public class OkekpeJMoropinzee {
     static boolean playAgain = true;

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);


        while (playAgain == true) {
            playGame(input);
        }

        System.out.println("Thanks for playing");
        input.close();
    }

    private static void playGame(Scanner input) {
        String yourMove = "", compMove = "";
        int compInt = 0;
        String gameCharacters = "Monkey Robot Pirate Ninja Zombie";

        Random rand = new Random();
        compInt = rand.nextInt(5)+1;

        if (compInt == 1) { compMove = "Monkey"; }
        else if (compInt == 2) { compMove = "Robot"; }
        else if (compInt == 3) { compMove = "Pirate"; }
        else if (compInt == 4) { compMove = "Ninja"; }
        else if (compInt == 5) { compMove = "Zombie"; }
        System.out.println(compMove + " --- " + compInt);

        yourMove = "";
        while (yourMove.equals("")) {
            System.out.println("What do you choose? --> "); 
            yourMove = input.nextLine().toLowerCase();
            if (yourMove.equals("quit")) { 
                System.out.println("Thanks for playing. Bye Bye"); 
                System.exit(0); 
            }
            if (!gameCharacters.toLowerCase().contains(yourMove) || yourMove.equals("")) {
                System.out.println("You entered an invalid Game Character! Try again...\n"); 
                continue;
            }
        }

        //MONKEY
        if (yourMove.equals("monkey") && compInt == 1) { 
            System.out.println("Tie"); 
        }
        else if (yourMove.equals("monkey") && compInt == 2) { 
            System.out.println("You Win! Monkey Unplugs Robot!"); 
        }
        else if (yourMove.equals("monkey") && compInt == 3) { 
            System.out.println("You Lose! Pirate Skewers Monkey!"); 
        }
        else if (yourMove.equals("monkey") && compInt == 4) { 
            System.out.println("You Win! Monkey fools Ninja!"); 
        }
        else if (yourMove.equals("monkey") && compInt == 5) { 
            System.out.println("You Lose! Zombie savages monkey!"); 
        }

        //ROBOT
        else if (yourMove.equals("robot") && compInt == 2) { 
            System.out.println("Tie"); 
        }
        else if (yourMove.equals("robot") && compInt == 1) { 
            System.out.println("You Lose! Monkey Unplugs Robot!"); 
        }
        else if (yourMove.equals("robot") && compInt == 3) { 
            System.out.println("You Lose! Pirate Drowns Robot!!"); 
        }
        else if (yourMove.equals("robot") && compInt == 4) { 
            System.out.println("You Win! Robot Chokes Ninja"); 
        }
        else if (yourMove.equals("robot") && compInt == 5) { 
            System.out.println("You win! Robot Crushes Zombie!"); 
        }

        //PIRATE
        else if (yourMove.equals("pirate") && compInt == 3) { 
            System.out.println("Tie"); 
        }
        else if (yourMove.equals("pirate") && compInt == 1) { 
            System.out.println("You Win! Pirate Skewers Monkey!"); 
        }
        else if (yourMove.equals("pirate") && compInt == 2) { 
            System.out.println("You Win! Pirate Drowns Robot!"); 
        }
        else if (yourMove.equals("pirate") && compInt == 4) { 
            System.out.println("You Lose! Ninja Karate Chops Pirate!"); 
        }
        else if (yourMove.equals("pirate") && compInt == 5) { 
            System.out.println("You Lose! Zombie Eats Pirate!"); 
        }

        //NINJA
        else if(yourMove.equals("ninja") && compInt == 4) { 
            System.out.println("Tie"); 
        }
        else if (yourMove.equals("ninja") && compInt == 1) { 
            System.out.println("You Lose! Monkey Fools Ninja!"); 
        }
        else if (yourMove.equals("ninja") && compInt == 2) { 
            System.out.println("You Lose! Robot Chokes Ninja!"); 
        }
        else if (yourMove.equals("ninja") && compInt == 3) { 
            System.out.println("You Win! Ninja Karate Chops Pirate!"); 
        }
        else if (yourMove.equals("ninja") && compInt == 5) { 
            System.out.println("You Win! Ninja Decapitates Zombie!"); 
        }

        //ZOMBIE
        else if(yourMove.equals("zombie") && compInt == 5) { 
            System.out.println("Tie"); 
        }
        else if (yourMove.equals("zombie") && compInt == 1) { 
            System.out.println("You Win! Zombie Savages Monkey!"); 
        }
        else if (yourMove.equals("zombie") && compInt == 2) { 
            System.out.println("You Lose! Robot Crushes Zombie!"); 
        }
        else if (yourMove.equals("zombie") && compInt==3) { 
            System.out.println("You Win! Zombie Eats Pirate!"); 
        }
        else if (yourMove.equals("zombie") && compInt==4) { 
            System.out.println("You Lose! Ninja Decapitates Zombie!"); 
        }

        //Ask if User wants to play the game again...
        String playMore = "";
        while (!playMore.equals("y") && !playMore.equals("n")) {
            System.out.println("\nDo you want to play another game? (y/n) ");
            playMore = input.nextLine().toLowerCase();
        }
        if (playMore.equals("n")) { playAgain = false; }
    }
}
Community
  • 1
  • 1
DevilsHnd - 退職した
  • 8,739
  • 2
  • 19
  • 22
  • Wow, this was extremely helpful. Thankyou! I did think my code was working until I actually tried to play it and it didnt work, lol. Thankyou very much for your help –  Nov 14 '16 at 05:52