Question

This is for Java Princess Perly has been kidnapped by the magical Hydra! The kingdom is...

This is for Java

Princess Perly has been kidnapped by the magical Hydra! The kingdom is in chaos. Now only PyPy — the bravest Knight of the country — can save the day!

Hydra is a powerful magical creature with H heads and T tails. Hydra can breathe fire from each of its heads, and can shoot poison from each of its tails. The only way to kill Hydra is to cut off all H heads and all T tails. Please note that a Hydra with 0 heads and T > 0 tails is still alive — in which case it is called a ‘Headless Hydra’.

Knight PyPy is indeed brave, but the Knight is still a novice when it comes to fighting magical creatures. Knight PyPy only know 4 moves:

  • With the first move, Knight PyPy can cut off exactly one of Hydra’s heads.
  • With the second move, Knight PyPy can cut off exactly one of Hydra’s tails.
  • With the third move, Knight PyPy can cut off exactly two of Hydra’s heads.
  • With the fourth move, Knight PyPy can cut off exactly two of Hydra’s tails.

Please remember that even though Hydra is a magical creature, the number of its heads and tails can never be negative. Thus PyPy cannot use a move when there are not enough heads or tails; e.g. PyPy cannot use the first move on a Headless Hydra.

However, Hydra is not so easy to kill. Immediately after PyPy’s move,

  • If PyPy cuts off exactly one head, a new head grows immediately.
  • If PyPy cuts off exactly one tail, two new tails grow immediately.
  • If PyPy cuts off exactly two tails, a new head grows immediately.
  • If PyPy cuts off exactly two heads, nothing happens.

Note that if after PyPy’s move Hydra has 0 heads and 0 tails, new heads and tails can still grow, in which case the Hydra is still alive.

Knight PyPy wants to kill Hydra as soon as possible, so that he can save Princess Perly. What is the minimum number of moves that PyPy needs to use to kill Hydra?

Input

Prompt for how many heads and how many tails to start the Hydra.

Output

For each hydra, allow the user to select which of the 4 moves PyPy can do. Keep track of the number of Hydra heads and tails and update the user after each attack. Keep allowing the user to attack until the Hydra is dead, or the user gives up. Allow for an option which will display S, where S is:

  • If it is impossible to kill Hydra, S=−1,
  • Otherwise, S is the minimum number of moves to kill Hydra.

Explanation of example

In this test case, Hydra has 3 heads and 3 tails initially. Following is a possible strategy for PyPy, with 9 moves:

  • Use the fourth move. Hydra now has 4 heads and 1 tail.
  • Use the third move. Hydra now has 2 heads and 1 tail.
  • Use the third move. Hydra now has 0 heads and 1 tail.
  • Use the second move. Hydra now has 0 heads and 2 tails.
  • Use the second move. Hydra now has 0 heads and 3 tails.
  • Use the second move. Hydra now has 0 heads and 4 tails.
  • Use the fourth move. Hydra now has 1 head and 2 tails.
  • Use the fourth move. Hydra now has 2 heads and 0 tails.
  • Use the third move. Hydra now has 0 heads and 0 tails. Because PyPy cuts of exactly 2 heads, no new head nor tail grow, and Hydra is dead.

Thus PyPy can kill Hydra with 9 moves. This is also the minimum number of moves for this test case.

Homework Answers

Answer #1
import java.util.*;

public class game
{
    static int S;
    static int head,tail;

    public static void main(String agrs[])
    {
        try
        {
        Scanner sc=new Scanner(System.in);
        int movechoice;

        System.out.println("*************Let us start the Game called Hydra Vs Knight PyPy*******************\n\n");
        System.out.println("The only way to kill Hydra is to cut off all H heads and all T tails.");

        System.out.println("Please Read Below Rules");


        System.out.println("Your Moves Are");
        System.out.println("***************************");
        System.out.println("First move ,you can cut off exactly one of Hydra’s heads.\n"+
        "Second move, you can cut off exactly one of Hydra’s tails.\n"+
        "Third move, you can cut off exactly two of Hydra’s heads.\n"+
        "Fourth move, you can cut off exactly two of Hydra’s tails.");
        System.out.println("***************************");
        System.out.println("Be Remember , you cannot use above moves when there are not enough heads or tails");


        
        System.out.println("\n\nIf you cuts off exactly one head, a new head grows immediately.");
        System.out.println("If you cuts off exactly one tail, two new tails grow immediately.");
        System.out.println("If you cuts off exactly two tails, a new head grows immediately.");
        System.out.println("If you cuts off exactly two heads, nothing happens.\n\n");
    

        //Prompt for how many heads and how many tails to start the Hydra.
        System.out.print("Choose how Many Heads to start the Hydra : ");
        head=sc.nextInt();
        System.out.print("Choose how Many Tails to start the Hydra : ");
        tail=sc.nextInt();


        while(head>0 || tail>0)
        {
            
            System.out.println("Press 1: First move ,you can cut off exactly one of Hydra’s heads.\n"+
            "Press 2: Second move, you can cut off exactly one of Hydra’s tails.\n"+
             "Press 3: Third move, you can cut off exactly two of Hydra’s heads.\n"+
             "Press 4: Fourth move, you can cut off exactly two of Hydra’s tails.\n"+
             "Press 5: For Quit !!");

            System.out.print("\nChoose Move :");

            movechoice=sc.nextInt();

            if(movechoice>0 && movechoice<6)
            {
                
                if(movechoice==5)
                {S=-1;
                 break; 
                }
                else if(movechoice==1)
                {
                    if(head>0)
                    {
                    S++;
                    }
                    else
                    {
                        System.out.println("Cannot perfomed this move , No Hydra head left!");
                    }

                }
                else if(movechoice==2)
                {
                    if(tail>0)
                    {
                    S++;
                    tail++;
                    }
                    else
                    {
                        System.out.println("Cannot perfomed this move , No Hydra tail left!");
                    }
                    
                }
                else if(movechoice==3)
                {
                    if(head>=2)
                    {
                    S++;
                    head=head-2;
                    }
                    else
                    {
                        System.out.println("Cannot perfomed this move !!");
                    }
                    
                }
            
                else 
                {
                    if(tail>1)
                    {
                    S++;
                    tail=tail-2;
                    head++;
                    }
                    else
                    {
                        System.out.println("Cannot perfomed this move , No Hydra head left!");
                    }
                    
                }

            }
            else
            {
                System.out.println("Invalid Moves");
            }
            System.out.println("\n****************\nHydra Head="+head+" Tail="+tail+" Remains\n****************\n");

        }

        if(S==-1)
        System.out.println("If it is impossible to kill Hydra!!");
        else
        {
         System.out.println("You killed hydra in "+S+" moves");
        }

    }
catch(Exception e)
{
    System.out.println("Error is "+e);
}
}
}

Output Screenshot

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Create in JAVA Suppose you are designing a game called King of the Stacks. The rules...
Create in JAVA Suppose you are designing a game called King of the Stacks. The rules of the game are as follows:  The game is played with two (2) players.  There are three (3) different Stacks in the game.  Each turn, a player pushes a disk on top of exactly one of the three Stacks. Players alternate turns throughout the game. Each disk will include some marker to denote to whom it belongs.  At the end...
IN JAVA Speed Control Problem: The files SpeedControl.java and SpeedControlPanel.java contain a program (and its associated...
IN JAVA Speed Control Problem: The files SpeedControl.java and SpeedControlPanel.java contain a program (and its associated panel) with a circle that moves on the panel and rebounds from the edges. (NOTE: the program is derived from Listing 8.15 and 8.16 in the text. That program uses an image rather than a circle. You may have used it in an earlier lab on animation.) The Circle class is in the file Circle.java. Save the program to your directory and run it...
CASE Wyatt Earp - The Buffalo Hunter F. Robert Jacobs, Indiana University The legend of Wyatt...
CASE Wyatt Earp - The Buffalo Hunter F. Robert Jacobs, Indiana University The legend of Wyatt Earp lives on largely based on his exploits as a gunfighter and Marshall of the frontier West in the 1880s. The classic tales of the shootout at the O.K. Corral in Tombstone or his sawed-off shotgun duel with Curly Bill are possibly the most celebrated gunfights of frontier history and cannot fail to stir the reader's imagination. Wyatt lived to be over 80 years...
Please review the following below and provide , one-page reaction to this budget proposal. 1. Budget...
Please review the following below and provide , one-page reaction to this budget proposal. 1. Budget The President’s Budget and Health Care While the president’s budget is not likely to be acted upon by Congress, it does signal what the administration’s priorities are—as well as what policy initiatives they might push. Repeal the Affordable Care Act: The administration’s budget includes a plan that is based upon the plan put forward by Sens. Lindsey Graham (R-SC) and Bill Cassidy (R-LA) last...
Asia’s e-commerce landscape has been booming in recent years. The swift adoption of smartphones and greater...
Asia’s e-commerce landscape has been booming in recent years. The swift adoption of smartphones and greater access to the internet has allowed consumers in the region to be a major force in the global digital economy. The expansion looks set to continue at a rapid pace. According to a November 2018 report by Fitch Solutions, e-commerce sales in the region are forecast to increase by 14.2% this year, with an estimated average annual increase of 14% over the medium term...
Assessment Identify the Variables! In rotational kinematics - the variables are: t = time, which is...
Assessment Identify the Variables! In rotational kinematics - the variables are: t = time, which is measured in s (for seconds) θ = angle = what angle did the object turn thru, usually measured radians ωO = initial angular velocity = the rotational speed of the object at the beginning of the problem, which is measured in rad/s ω = final angular velocity = the rotational speed of the object at the end of the problem, which is measured in...
Business Problem-Solving Case Walmart and Amazon Duke It Out for E-Commerce Supremacy Walmart is the world’s...
Business Problem-Solving Case Walmart and Amazon Duke It Out for E-Commerce Supremacy Walmart is the world’s largest and most successful retailer, with $487.5 billion in 2014 sales and nearly 11,000 stores worldwide, including more than 4,000 in the United States. Walmart has 2.2 million employees and ranks first on the Fortune 500 list of companies. Walmart had such a large and powerful selling machine that it really didn’t have any serious competitors—until now. Today, Walmart’s greatest threat is Amazon.com, often...
What role could the governance of ethics have played if it had been in existence in...
What role could the governance of ethics have played if it had been in existence in the organization? Assess the leadership of Enron from an ethical perspective. THE FALL OF ENRON: A STAKEHOLDER FAILURE Once upon a time, there was a gleaming headquarters office tower in Houston, with a giant tilted "£"' in front, slowly revolving in the Texas sun. The Enron Corporation, which once ranked among the top Fortune 500 companies, collapsed in 2001 under a mountain of debt...
It had been a busy day for Marsha Chamberland. She had spent most of it cleaning...
It had been a busy day for Marsha Chamberland. She had spent most of it cleaning and running errands in prepara- tion for her brother-in-law Ed’s return, and now she was preparing a quick dinner for her family. Ed, an industry official whose job it was to decide whether or not new products needed premarket approval by the U.S. Food and Drug Administration, had spent the last two weeks in Tennessee expressing his views on genetic engineering in food. He...
Discuss how the respective organizations’ relations with stakeholders could have potentially been affected by the events...
Discuss how the respective organizations’ relations with stakeholders could have potentially been affected by the events that took place at Enron and how the situation could have been dealt with differently to prevent further damage? THE FALL OF ENRON: A STAKEHOLDER FAILURE Once upon a time, there was a gleaming headquarters office tower in Houston, with a giant tilted "£"' in front, slowly revolving in the Texas sun. The Enron Corporation, which once ranked among the top Fortune 500 companies,...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT