Question

Buh-RING IT! For this assignment, you’re going to simulate a text-based Role-Playing Game (RPG). Design (pseudocode)...

Buh-RING IT! For this assignment, you’re going to simulate a text-based Role-Playing Game (RPG). Design (pseudocode) and implement (source) for a program that reads in 1) the hero’s Hit Points (HP – or health), 2) the maximum damage the hero does per attack, 3) the monster’s HP and 4) the maximum monster’s damage per attack.   When the player attacks, it will pick a random number between 0 and up to the maximum damage the player does, and then subtract that from the monster. The same thing happens when the monster attacks the hero, but damage is to the hero. The program should display rounds and the HP of the hero and monster each round. If the hero or monster dies, it should print that this happened and should NOT continue (i.e. no extra text). To learn how to create random numbers, see the appendix.

Homework Answers

Answer #1

The pseudocode for the given scenario is provided below:

The complete code is provided below:

Screenshot of the code:

Sample Output:

Code To Copy:

//Import the packages.

import java.util.Scanner;

import java.util.Random;

//Define the class.

public class Main

{

   //Create an random number.

   Random random_number = new Random();

  

   //Define the function.

   public int getting_Attack(int maximum_damage)

   {

       int attack = random_number.nextInt(maximum_damage + 1);

       return attack;

   }

  

   //Define the simulation function.

   public void simulation(int initial_hero, int Hero_damage, int Init_Mons, int Mons_Damage)

   {

       int Hero_Attack,Monster_attack;

       int number_of_rounds = 0;

      

       while(initial_hero > 0 && Init_Mons > 0)

       {

         

           ++number_of_rounds;

           System.out.println("\n======- Round "+number_of_rounds

+ " ======-");

           //Getting the hero attack.

           Hero_Attack = getting_Attack(Hero_damage);

           System.out.println("Hero attacks for: "+Hero_Attack);

          

           Init_Mons -= Hero_Attack;

         

           System.out.println("Monster has "+Init_Mons+" HP left");

           //check for monster death

           if(Init_Mons <= 0)

           {

               System.out.println("The monster dies and you earn 5 XP");

               System.out.println("Battle ends");

               break;

           }

          

           Monster_attack = getting_Attack(Mons_Damage);

           System.out.println("Monster attacks you for:

"+Monster_attack);

          

           initial_hero -= Monster_attack;

           System.out.println("You have "+ initial_hero+" HP left");

          

           if(initial_hero <= 0)

           {

               System.out.println("You are killed by the monster and

lose 10 gold.");

               System.out.println("Battle ends");

               break;

           }

       }

   }

   //Define the main function.

   public static void main(String args[])

   {

     //Create an object of Scanner class.

       Scanner object = new Scanner(System.in);

       //Declare the variables.

       int initial_hero;

       int Hero_Dam;

       int init_mons;

       int Mons_dam;

      

       //Prompt the user to enter the input.

       System.out.print("Enter the hero's initial hit points: ");

       initial_hero = object.nextInt();

       System.out.print("Enter the damage the hero’s weapon does per

strike: ");

       Hero_Dam = object.nextInt();

       System.out.print("Enter the monster's initial hit points: ");

       init_mons = object.nextInt();

       System.out.print("Enter the monster's damage per strike: ");

       Mons_dam = object.nextInt();

      

       //Craete an object of Main class.

       Main obj = new Main();

       //Execute the simulation.

       obj.simulation(initial_hero, Hero_Dam,init_mons, Mons_dam);

   }

}

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
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT