Question

In: Computer Science

Rewrite this code of a game of Moropinzee so that it works as intended without the...

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!");

}

}

Solutions

Expert Solution

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:


Related Solutions

Debug the code in the starter file so it functions as intended and does not cause...
Debug the code in the starter file so it functions as intended and does not cause any errors. This program is intended to take two integer inputs and determine whether the second is a multiple of the first or not. The number b is a multiple of the number a if b can be divided by a with no remainder. Remember though that no number can be divided by 0 - so no numbers should be considered a multiple of...
question is :rewrite the story completely in layman's terms or plain English so that someone without...
question is :rewrite the story completely in layman's terms or plain English so that someone without a medical or science background would be able to understand. Your translation must be clear and easy to understand. Part 1 – Jack and Jill Two individuals, one with a significant amount of testosterone and one with a large amount of estrogen went for a walk together up a hill. The male had genitalia that enabled the passing of semen and urine through the...
Can you please take this code and just rewrite it so it can be easily be...
Can you please take this code and just rewrite it so it can be easily be able to put into a complier. in java Implementation of above program in java: import java.util.HashMap; import java.util.Scanner; import java.util.Map.Entry; public class Shopping_cart {       static Scanner s= new Scanner (System.in);    //   declare hashmap in which key is dates as per mentioned and Values class as value which consists all the record of cases    static HashMap<String,Item> map= new HashMap<>();    public...
Rewrite the following Matlab code so it does the same thing but with complete new variable...
Rewrite the following Matlab code so it does the same thing but with complete new variable names and structure. function stump = stumpGenerator(dataX, dataY, Dt) intervals = 100; rangex1 = max(dataX(:,1)) - min(dataX(:,1)); rangex2 = max(dataX(:,2)) - min(dataX(:,2)); width = (rangex1/intervals); height = (rangex2/intervals); starterx1 = min(dataX(:,1)) - (width/2); starterx2 = min(dataX(:,2)) - (height/2); currepsilon = inf; stump = [0,0,0,1,0,0]; for i = 1:(intervals + 1)    horzRightError = sum(Dt(find(((dataX(:,1) - starterx1) .* dataY) < 0))); horzLeftError = sum(Dt(find(((dataX(:,1) -...
Use Java to rewrite the following pseudo-code segment using a loop structure without goto, break, or...
Use Java to rewrite the following pseudo-code segment using a loop structure without goto, break, or any other unconditional branching statement: k = (j+13)/27; //assume i,j,k are integers properly declared. loop: if k > 10 then goto out k=k+1.2; i=3*k-1; goto loop; out: …
NEED TO REWRITE THIS IN OOP MODE ######## HOMEWORK 19 ###### Rewrite the code in the...
NEED TO REWRITE THIS IN OOP MODE ######## HOMEWORK 19 ###### Rewrite the code in the OOP mode (class mode). ########################### lambda trick ##### first: changing label properties: after you've created a label, you ##### may want to change something about it. To do that, use configure method: ## label.configure(text = 'Yes') ## label.configure(bg = 'white', fg = 'black') ### that change the properties of a label called label. ################################################### ## from tkinter import * abc = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' def callback(x):...
Write python code so that in a game of hangman the word ‘apple’ is displayed as...
Write python code so that in a game of hangman the word ‘apple’ is displayed as “ _ _ _ _ _ “ and is usable code for any random words. Include in this code how to after the player has guessed a correct letter replace the _ with the correct guess shown as “ _ p _ _ _”
Can you please rewrite this Java code so it can be better understood. import java.util.HashMap; import...
Can you please rewrite this Java code so it can be better understood. import java.util.HashMap; import java.util.Scanner; import java.util.Map.Entry; public class Shopping_cart {       static Scanner s= new Scanner (System.in);    //   declare hashmap in which key is dates as per mentioned and Values class as value which consists all the record of cases    static HashMap<String,Item> map= new HashMap<>();    public static void main(String[] args) {        // TODO Auto-generated method stub        getupdates();    }...
USING C++ ONLY. Please study the code posted below. the goal is to rewrite the code...
USING C++ ONLY. Please study the code posted below. the goal is to rewrite the code implementing a template class using a linked list instead of an array. Note: The functionality should remain the same. /** * Queue implementation using linked list C style implementation ( no OOP). */ #include <cstdio> #include <cstdlib> #include <climits> #include <iostream> #define CAPACITY 100 // Queue max capacity using namespace std; /** Queue structure definition */ struct QueueType { int data; struct QueueType *...
So pretty much I need my code without the arrays, or lists. Please and thank you!...
So pretty much I need my code without the arrays, or lists. Please and thank you! Important: You may not use arrays, lists, or similar for your questions. This will be covered in the next module. The objective is to use conditionals in order to achieve the overall task. Checkpoint 3 is a continuation of the “Quiz” Programming Project. This module week, you will implement repetitive tasks in your program while using conditional and iteration statements in C#. Implement a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT