Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using C++: You will create a simple class hierarchy as the basis for a fantasy c

ID: 3760582 • Letter: U

Question

Using C++: You will create a simple class hierarchy as the basis for a fantasy combat game. Your ‘universe’ contains Goblins and Barbarians. Each will have characteristics for attack, defense, armor, and strength points.

Goblin (attack = 2d6, defense = 1d6, armor = 3, strength = 6)

barbarian (attack = 2d6, defense = 2d6, armor = 0, strength = 12)

example: 3d6 is rolling three 6-sided dice. 2d10 is rolling two 10-sided dice

To resolve an attack you will need to generate 2 dice rolls. The attacker rolls the appropriate number and type of dice under Attack. The defender rolls the appropriate number and type of dice under Defense. You subtract the Defense roll from the Attack roll. That is the damage.

To apply the damage you subtract the Armor value. The result is then subtracted from the Strength Points. That value becomes the new Strength Points for the next round. If Strength Points goes to 0 or less then the character is out of the combat. You need to create a Creature class. Then you will have a subclass for each of these characters. Note that the Creature class will be an abstract class. You will never instantiate one. For our purposes right now each subclass will vary only in the values in the table. Since each starts with the same data elements you will only need one constructor.

Explanation / Answer

#include <iostream>
#include <stdlib.h>
using namespace std;

// Base class
class Creature
{
public:
// pure virtual function providing interface framework.
virtual int getArmor() = 0;
virtual int getStrength() = 0;

//setter methods
void setArmor(int w)
{
armor = w;
}
void setStrength(int h)
{
strength = h;
}
void setAttack(string atk)
{
attack = atk;
}
void setDefense(string defen)
{
defense = defen;
}
protected:
string attack;
string defense;
int armor;
int strength;
};

// Derived classes
class Battle: public Creature
{
public:
int getArmor()
{
return armor;
}

int getStrength()
{
return strength;
}
};

int main(void)
{
//object instances declared
Battle Goblin;
Battle Barbarian;

//setting parameteres two participants
Goblin.setAttack("2d6");
Goblin.setDefense("1d6");
Goblin.setArmor(3);
Goblin.setStrength(6);

Barbarian.setAttack("2d6");
Barbarian.setDefense("1d6");
Barbarian.setArmor(0);
Barbarian.setStrength(12);
  
while(true){
//rolling dice
int num = (rand()%6) + (rand()%6);
num = num - Goblin.getArmor()-getStrength();
if(num > 0){ //reducing strength
Goblin.setStrength(num);
}
else{ //defeated message prints
cout<<" Goblin defeated.....Barbarian wins";
break;
}

num = (rand()%6) + (rand()%6);
num = num - Barbarian.getArmor()-getStrength();
if(num > 0){//reducing strength
Barbarian.setStrength(num);
}
else{ //defeated message prints
cout<<"Barbarian defeated.....Goblin wins";
break;
}


}
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote