In: Computer Science
Rewrite this code of a game of Moropinzee so that it works as intended without the "break;" in the last few lines of the code.
Code:
import java.util.*;
public class Moropinzee
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
while(true)
{
System.out.println("Player 1 enter a number 1-5 for Monkey, Robot, Pirate, Ninja, or Zombie:");
int p1 = sc.nextInt();
while(p1<1 || p1>5)
{
System.out.println("Invalid choice, Player 1. Enter a number 1-5:");
p1 = sc.nextInt();
}
System.out.println("Player 2 enter a number 1-5 for Monkey, Robot, Pirate, Ninja, or Zombie:");
int p2 = sc.nextInt();
while(p2<1 || p2>5)
{
System.out.println("Invalid choice, Player 2. Enter a number 1-5:");
p2 = sc.nextInt();
}
if((p1==1 && p2==4)||(p1==4 && p2==1))
{
System.out.print("Monkey fools Ninja. ");
if(p1==1)
{
System.out.println("Player 1 wins!");
}
else
{
System.out.println("Player 2 wins!");
}
}
else if((p1==1 && p2==2)||(p1==2 && p2==1))
{
System.out.print("Monkey unplugs Robot. ");
if(p1==1)
{
System.out.println("Player 1 wins!");
}
else
{
System.out.println("Player 2 wins!");
}
}
else if((p1==2 && p2==4)||(p1==4 && p2==2))
{
System.out.print("Robot chokes Ninja. ");
if(p1==2)
{
System.out.println("Player 1 wins!");
}
else
{
System.out.println("Player 2 wins!");
}
}
else if((p1==2 && p2==5)||(p1==5 && p2==2))
{
System.out.print("Robot crushes Zombie. ");
if(p1==2)
{
System.out.println("Player 1 wins!");
}
else
{
System.out.println("Player 2 wins!");
}
}
else if((p1==3 && p2==2)||(p1==2 && p2==3))
{
System.out.print("Pirate drowns Robot. ");
if(p1==3)
{
System.out.println("Player 1 wins!");
}
else
{
System.out.println("Player 2 wins!");
}
}
else if((p1==3 && p2==1)||(p1==1 && p2==3))
{
System.out.print("Pirate skewers Monkey. ");
if(p1==3)
{
System.out.println("Player 1 wins!");
}
else
{
System.out.println("Player 2 wins!");
}
}
else if((p1==4 && p2==3)||(p1==3 && p2==4))
{
System.out.print("Ninja karate chops Pirate. ");
if(p1==4)
{
System.out.println("Player 1 wins!");
}
else
{
System.out.println("Player 2 wins!");
}
}
else if((p1==4 && p2==5)||(p1==5 && p2==4))
{
System.out.print("Ninja decapitates Zombie. ");
if(p1==4)
{
System.out.println("Player 1 wins!");
}
else
{
System.out.println("Player 2 wins!");
}
}
else if((p1==5 && p2==3)||(p1==3 && p2==5))
{
System.out.print("Zombie eats Pirate. ");
if(p1==5)
{
System.out.println("Player 1 wins!");
}
else
{
System.out.println("Player 2 wins!");
}
}
else if((p1==5 && p2==1)||(p1==1 && p2==5))
{
System.out.print("Zombie savages Monkey. ");
if(p1==1)
{
System.out.println("Player 1 wins!");
}
else
{
System.out.println("Player 2 wins!");
}
}
else if(p1 == p2)
System.out.println("Nobody wins");
System.out.println("Would you like to play again?");
sc.nextLine();//goto next line
String s = sc.nextLine();//take input from user
if((s.equalsIgnoreCase("yes") || s.equalsIgnoreCase("y")) == false)
break;
}
System.out.println("Thanks for playing!");
}
}
If you don't want to use break when user entered other than y or yes then alternate solution is use do while loop.
first time code runs that is in do loop and continue runs until while condition satisfies.
Below is the modified code without the use of break satement.I have created java project named Moropinzee in NetBeans IDE.
package moropinzee;
import java.util.*;
public class Moropinzee {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//declare variable s that is used to store choice entered by user
String s;
do {
System.out.println("Player 1 enter a number 1-5 for Monkey, Robot, Pirate, Ninja, or Zombie:");
int p1 = sc.nextInt();
while (p1 < 1 || p1 > 5) {
System.out.println("Invalid choice, Player 1. Enter a number 1-5:");
p1 = sc.nextInt();
}
System.out.println("Player 2 enter a number 1-5 for Monkey, Robot, Pirate, Ninja, or Zombie:");
int p2 = sc.nextInt();
while (p2 < 1 || p2 > 5) {
System.out.println("Invalid choice, Player 2. Enter a number 1-5:");
p2 = sc.nextInt();
}
if ((p1 == 1 && p2 == 4) || (p1 == 4 && p2 == 1)) {
System.out.print("Monkey fools Ninja. ");
if (p1 == 1) {
System.out.println("Player 1 wins!");
} else {
System.out.println("Player 2 wins!");
}
} else if ((p1 == 1 && p2 == 2) || (p1 == 2 && p2 == 1)) {
System.out.print("Monkey unplugs Robot. ");
if (p1 == 1) {
System.out.println("Player 1 wins!");
} else {
System.out.println("Player 2 wins!");
}
} else if ((p1 == 2 && p2 == 4) || (p1 == 4 && p2 == 2)) {
System.out.print("Robot chokes Ninja. ");
if (p1 == 2) {
System.out.println("Player 1 wins!");
} else {
System.out.println("Player 2 wins!");
}
} else if ((p1 == 2 && p2 == 5) || (p1 == 5 && p2 == 2)) {
System.out.print("Robot crushes Zombie. ");
if (p1 == 2) {
System.out.println("Player 1 wins!");
} else {
System.out.println("Player 2 wins!");
}
} else if ((p1 == 3 && p2 == 2) || (p1 == 2 && p2 == 3)) {
System.out.print("Pirate drowns Robot. ");
if (p1 == 3) {
System.out.println("Player 1 wins!");
} else {
System.out.println("Player 2 wins!");
}
} else if ((p1 == 3 && p2 == 1) || (p1 == 1 && p2 == 3)) {
System.out.print("Pirate skewers Monkey. ");
if (p1 == 3) {
System.out.println("Player 1 wins!");
} else {
System.out.println("Player 2 wins!");
}
} else if ((p1 == 4 && p2 == 3) || (p1 == 3 && p2 == 4)) {
System.out.print("Ninja karate chops Pirate. ");
if (p1 == 4) {
System.out.println("Player 1 wins!");
} else {
System.out.println("Player 2 wins!");
}
} else if ((p1 == 4 && p2 == 5) || (p1 == 5 && p2 == 4)) {
System.out.print("Ninja decapitates Zombie. ");
if (p1 == 4) {
System.out.println("Player 1 wins!");
} else {
System.out.println("Player 2 wins!");
}
} else if ((p1 == 5 && p2 == 3) || (p1 == 3 && p2 == 5)) {
System.out.print("Zombie eats Pirate. ");
if (p1 == 5) {
System.out.println("Player 1 wins!");
} else {
System.out.println("Player 2 wins!");
}
} else if ((p1 == 5 && p2 == 1) || (p1 == 1 && p2 == 5)) {
System.out.print("Zombie savages Monkey. ");
if (p1 == 1) {
System.out.println("Player 1 wins!");
} else {
System.out.println("Player 2 wins!");
}
} else if (p1 == p2) {
System.out.println("Nobody wins");
}
System.out.println("Would you like to play again?");
sc.nextLine();//goto next line
s = sc.nextLine();//take input from user
if ((s.equalsIgnoreCase("yes") || s.equalsIgnoreCase("y")) == false) {
break;
}
}
//checks if user enterd y or yes then continue the game otherwise terminate the loop.
while (s.equalsIgnoreCase("yes") || s.equalsIgnoreCase("y"));
System.out.println("Thanks for playing!");
}
}
OUTPUT: