Question: Java Assignment (Lab 11)-Dungeons and Dragons and Objects Instructions: You will need to create several classes: Player, Monster, Hoard, Goblin, Troll, and Dragon. Goblins, Trolls,

Java Assignment (Lab 11)-Dungeons and Dragons and Objects

Instructions:

You will need to create several classes: Player, Monster, Hoard, Goblin, Troll, and Dragon. Goblins, Trolls, and Dragons are all subclasses of class Monster.

At each iteration, the Player will face a randomly selected Monster, chosen by taking a random integer from 0 to 10. If the integer is 0, 1, 2, 3, or 4, the Player will encounter a Goblin. If a 5, 6, 7 or 8, a Troll. Finally, if a 9 or 10 is selected, the Player will encounter the Dragon.

The Player starts the game with 100 Hit Points and 3 Magic Points. These should be properties (attributes) of the Player.

During each encounter, the Player has several options: 1. (R)un away - The player leaves the encounter, but the Monster gets one free attack. 2. (A)ttack - The player attacks the monster while still maintaining a defense, causing a random number of hit points damage (between 1 and 15). 3. (B)erserk - The player attacks the monster while ignoring their own defense, causing triple damages (1 to 15, multiplied by 3) to the monster, but causing double damage to be done by the Monster to the Player. 4. (M)agic - The player casts a Magic Heal Spell. This reduces the number of Magic Points by one. The Magic Heal Spell resets the Player's Hit Points to 100. However, the monster has a free attack.

Monsters will only ever attack. A Goblin has 10 Hit Points and causes between 1 and 5 points of damage; a Troll has 30 Hit Points and causes between 1 and 10 points of damage; a Dragon has 100 Hit Points and causes between 1 and 20 points of damage. The same method name should be called for each of these monsters to attack.

All Monsters have a Hoard of gold (if there's one thing I learned from role-playing games, it is that monsters are alwayshoarding gold). This Hoard is a separate class, which consists only of a random number of gold pieces (between 1 and 100).

Encounters will continue until either the Player has 0 or fewer Hit Points, or when the Player defeats the Dragon. After defeating the Dragon, the game will display statistics: specifically, how many gold pieces were collected (this should be stored in the Player class), and how many Goblins, Trolls, and Dragons were defeated (this information should be stored in their respective classes). If the Player did not defeat the Dragon, there is no need to display the statistics.

This should all be done in proper object-oriented style; you should have a Player class, a Monster class, a Hoard class, and the three subclasses of Monster (Goblin, Troll, and Dragon).

When subclassing, methods in the subclass override methods in the superclass. However, you can call them on a superclass reference that actually contains a subclass. For example, if you have the following:

Monster m = new Dragon(); m.attack(); 

It will call the attack method in the Dragon class, not the one in the Monster class. This is an example of polymorphism, as discussed in class. These attacks should cause different amounts of damage (as explained above).

Sample code for Monster.java and Goblin.java- These are just templates for the classes Monster.java and Goblin.java. They do not have to be exactly coded based on this template, they are just there to help.

Monster.java:

import java.util.*;

// This is an example Monster class. You do NOT need to use // it if you don't want to, but it's certainly a good start.

// Also note that you may need additional methods or attributes!

public class Monster {

// Number of Hit Points the monster has public int _hp = 0;

// Our random number generator public Random _rng = new Random();

// The hoard of gold that monsters inevitably have public Hoard _hoard = new Hoard();

// Return the name of the monster. Subclasses will // override this method to return the name of the monster // of that subclass. public String getName() { return "Monster"; }

// If you hit a monster, then the number of points of damage // caused by the player should be subtracted from the // monster's hit points.

// The return value from the monster is the amount of damage // dealt to the player. // If a monster's hit points go below 0, the monster is // vanquished. public int attack(int hit) { _hp -= hit; return 0; }

// With a berserk attack, the damage to the monster is // tripled, but the damage to the player is doubled. public int berserk(int hit) { _hp -= (hit * 3); return 0 * 2; } }

Goblin.java:

import java.util.*;

// This is an example Goblin class. You do NOT need to use // it if you don't want to, but it's certainly a good start.

// Also note that you may need additional methods or attributes!

public class Goblin extends Monster {

// Total number of all Goblins defeated // Note that this is a STATIC variable as it applies // to the whole class of Goblins, not one particular one. public static int _defeats = 0;

// Constructor. Goblins have ten hit points.

// Note that we are re-using the _hp variable from the // superclass Monster. public Goblin() { _hp = 10; }

// Here, we are overriding the getName() method in Monster // by having it return "Goblin". public String getName() { return "Goblin"; }

// Calculate damage from a regular attack public int attack(int hit) { _hp -= hit;

if (_hp < 0) { _defeats++; }

int damage = _rng.nextInt(5) + 1; return damage; }

// Calculate damage from a berserk attack

public int attack(int hit) { _hp -= (hit * 3);

if (_hp < 0) { _defeats++; }

int damage = _rng.nextInt(5) + 1; return damage * 2; }

}

Sample Output of Final Program:

A losing game..

You encounter a Goblin! HP: 75 MP: 3 Monster HP: 10 (A)ttack (B)erserk (M)agic (R)un away Your choice > B You dished out 9 points, and you received 10 points. HP: 65 MP: 3 Monster HP: 1 (A)ttack (B)erserk (M)agic (R)un away Your choice > A You dished out 7 points, and you received 3 points. You have defeated the Goblin! You have gained 25 gold pieces! You encounter a Dragon! HP: 62 MP: 3 Monster HP: 100 (A)ttack (B)erserk (M)agic (R)un away Your choice > A You dished out 1 points, and you received 15 points. HP: 47 MP: 3 Monster HP: 99 (A)ttack (B)erserk (M)agic (R)un away Your choice > A You dished out 4 points, and you received 6 points. HP: 41 MP: 3 Monster HP: 95 (A)ttack (B)erserk (M)agic (R)un away Your choice > A You dished out 3 points, and you received 15 points. HP: 26 MP: 3 Monster HP: 92 (A)ttack (B)erserk (M)agic (R)un away Your choice > M You feel revitalized! You are healed! But the monster attacks for 28 points. HP: 72 MP: 2 Monster HP: 92 (A)ttack (B)erserk (M)agic (R)un away Your choice > B You dished out 21 points, and you received 26 points. HP: 46 MP: 2 Monster HP: 71 (A)ttack (B)erserk (M)agic (R)un away Your choice > B You dished out 18 points, and you received 28 points. HP: 18 MP: 2 Monster HP: 53 (A)ttack (B)erserk (M)agic (R)un away Your choice > M You feel revitalized! You are healed! But the monster attacks for 24 points. HP: 76 MP: 1 Monster HP: 53 (A)ttack (B)erserk (M)agic (R)un away Your choice > B You dished out 6 points, and you received 54 points. HP: 22 MP: 1 Monster HP: 47 (A)ttack (B)erserk (M)agic (R)un away Your choice > B You dished out 24 points, and you received 30 points. You have been vanquished by the Dragon. 

A winning game...

You encounter a Troll! HP: 100 MP: 3 Monster HP: 30 (A)ttack (B)erserk (M)agic (R)un away Your choice > R You run away! But the monster attacks for 6 points. You have run from the Troll. You encounter a Troll! HP: 94 MP: 3 Monster HP: 30 (A)ttack (B)erserk (M)agic (R)un away Your choice > B You dished out 9 points, and you received 12 points. HP: 82 MP: 3 Monster HP: 21 (A)ttack (B)erserk (M)agic (R)un away Your choice > B You dished out 24 points, and you received 14 points. You have defeated the Troll! You have gained 80 gold pieces! You encounter a Dragon! HP: 68 MP: 3 Monster HP: 100 (A)ttack (B)erserk (M)agic (R)un away Your choice > B You dished out 18 points, and you received 38 points. HP: 30 MP: 3 Monster HP: 82 (A)ttack (B)erserk (M)agic (R)un away Your choice > M You feel revitalized! You are healed! But the monster attacks for 2 points. HP: 98 MP: 2 Monster HP: 82 (A)ttack (B)erserk (M)agic (R)un away Your choice > B You dished out 36 points, and you received 2 points. HP: 96 MP: 2 Monster HP: 46 (A)ttack (B)erserk (M)agic (R)un away Your choice > B You dished out 3 points, and you received 2 points. HP: 94 MP: 2 Monster HP: 43 (A)ttack (B)erserk (M)agic (R)un away Your choice > B You dished out 9 points, and you received 8 points. HP: 86 MP: 2 Monster HP: 34 (A)ttack (B)erserk (M)agic (R)un away Your choice > B You dished out 12 points, and you received 8 points. HP: 78 MP: 2 Monster HP: 22 (A)ttack (B)erserk (M)agic (R)un away Your choice > B You dished out 3 points, and you received 18 points. HP: 60 MP: 2 Monster HP: 19 (A)ttack (B)erserk (M)agic (R)un away Your choice > B You dished out 36 points, and you received 24 points. You have defeated the Dragon! You have gained 39 gold pieces! Number goblins defeated: 0 Number trolls defeated: 1 Number dragons defeated: 1 Gold pieces acquired: 119 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!