In C++ This question simulates a team of creatures fighting against a dragon. Yo
ID: 3838264 • Letter: I
Question
In C++
This question simulates a team of creatures fighting against a dragon. You will be using classes, inheritance, and polymorphism.
Create a new Creature class. Include the following private variables: string name, int health, int strength. Include appropriate constructors and accessor and mutator functions. Also include a virtual function getDamage(). The virtual function returns a random integer x, where 0 < x <= strength. Be careful how you generate the random number.
Create a new Human class that inherits Creature. A human’s damage is between (strength/2) and strength: (strength/2) < x <= strength.
Create a new Hobbit class that inherits Creature. A hobbit has a 50% chance of inflicting double damage (50% chance to return 2x instead of x).
Create a new Dragon class that inherits Creature. The getDamage() function is similar to Creature.
Implement the global function void fight(Creature, Creature). All that function does is calculate the damage for each creature and deduct that damage from the health of the other creature. Include cout statements to print the damages and health.
Inside your main function, create the following objects:
Assume that the human and hobbit class takes turn to “fight” with the dragon. Create an infinite loop that will continue until (both human and hobbit health are 0) or (dragon’s health is 0).
Inside the loop:
Alternate between human and hobbit (start with human object)
Let that Creature be C1 (it will be either hobbit or human). Call the function fight(C1, dragon).
If C1.getHealth() <= 0, do not consider C1 anymore.
Include cout statements to print each creature’s health.
Continue
Basically, the loop simulates each element of the human/hobbit team attacking the dragon.
Example output:
Explanation / Answer
Here is your code: -
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
class Creature {
private:
string name;
int health;
int strength;
public:
Creature() {
}
Creature(string name,int health,int strength) {
this->name = name;
this->health = health;
this->strength = strength;
}
void setName(string name) {
this->name = name;
}
string getName() {
return this->name;
}
void setHealth(int hel) {
this->health = hel;
}
int getHealth() {
return this->health;
}
void setStrength(int str) {
this->strength =str;
}
int getStrength() {
return this->strength;
}
//function to calculate damage
virtual int getDamage(){
srand(time(NULL));
return (rand() % this->strength);
}
};
class Human : public Creature {
public:
//calling of constructor of base class
Human(string name,int health,int strength) : Creature(name,health,strength){
}
int getDamage() {
srand(time(NULL));
return (rand() % this->getStrength()) +(this->getStrength()/2);
}
};
class Hobbit : public Creature {
public:
//calling of constructor of base class
Hobbit(string name,int health,int strength) : Creature(name,health,strength){
}
int getDamage() {
srand(time(NULL));
int dam = (rand() % this->getStrength());
srand(time(NULL));
int chance = (rand() % 2);
if(chance == 0) {
return dam;
} else {
return 2*dam;
}
}
};
class Dragon : public Creature {
public:
//calling of constructor of base class
Dragon(string name,int health,int strength) : Creature(name,health,strength){
}
int getDamage() {
srand(time(NULL));
return (rand() % this->getStrength());
}
};
void fight(Creature& a,Creature& b) {
int aDam = a.getDamage();
int bDam = b.getDamage();
//printing the result of the fight
cout<<a.getName()<<" Vs. "<<b.getName()<<endl;
cout<<"Before fight: Health of "<<a.getName()<<" is "<<a.getHealth()<<" and Health of "<<b.getName()<<" is "<<b.getHealth()<<endl;
cout<<a.getName()<<" damage: "<<aDam<<endl;
cout<<b.getName()<<" damage: "<<bDam<<endl;
int aHealth = a.getHealth() - bDam; //damage calculation
int bHealth = b.getHealth() - aDam;
if(aHealth < 0) {//updating the health
a.setHealth(0);
} else {
a.setHealth(aHealth);
}
if(bHealth < 0) {
b.setHealth(0);
} else {
b.setHealth(bHealth);
}
cout<<"After fight: Health of "<<a.getName()<<" is "<<a.getHealth()<<" and Health of "<<b.getName()<<" is "<<b.getHealth()<<endl;
}
int main() {
Human human("human",100,10);
Hobbit hobbit("hobbit",50,5);
Dragon dragon("Dragon",200,15);
//fight started
while((human.getHealth() > 0 || hobbit.getHealth() > 0) && dragon.getHealth() > 0) {
if(human.getHealth() > 0){
fight(human,dragon);
}
cout<<endl;
if(hobbit.getHealth() > 0){
fight(hobbit,dragon);
}
cout<<endl;
}
return 0;
}
Sample Run: -
human Vs. Dragon
Before fight: Health of human is 100 and Health of Dragon is 200
human damage: 11
Dragon damage: 11
After fight: Health of human is 89 and Health of Dragon is 189
hobbit Vs. Dragon
Before fight: Health of hobbit is 50 and Health of Dragon is 189
hobbit damage: 1
Dragon damage: 11
After fight: Health of hobbit is 39 and Health of Dragon is 188
human Vs. Dragon
Before fight: Health of human is 89 and Health of Dragon is 188
human damage: 11
Dragon damage: 11
After fight: Health of human is 78 and Health of Dragon is 177
hobbit Vs. Dragon
Before fight: Health of hobbit is 39 and Health of Dragon is 177
hobbit damage: 1
Dragon damage: 11
After fight: Health of hobbit is 28 and Health of Dragon is 176
human Vs. Dragon
Before fight: Health of human is 78 and Health of Dragon is 176
human damage: 11
Dragon damage: 11
After fight: Health of human is 67 and Health of Dragon is 165
hobbit Vs. Dragon
Before fight: Health of hobbit is 28 and Health of Dragon is 165
hobbit damage: 1
Dragon damage: 11
After fight: Health of hobbit is 17 and Health of Dragon is 164
human Vs. Dragon
Before fight: Health of human is 67 and Health of Dragon is 164
human damage: 11
Dragon damage: 11
After fight: Health of human is 56 and Health of Dragon is 153
hobbit Vs. Dragon
Before fight: Health of hobbit is 17 and Health of Dragon is 153
hobbit damage: 1
Dragon damage: 11
After fight: Health of hobbit is 6 and Health of Dragon is 152
human Vs. Dragon
Before fight: Health of human is 56 and Health of Dragon is 152
human damage: 11
Dragon damage: 11
After fight: Health of human is 45 and Health of Dragon is 141
hobbit Vs. Dragon
Before fight: Health of hobbit is 6 and Health of Dragon is 141
hobbit damage: 1
Dragon damage: 11
After fight: Health of hobbit is 0 and Health of Dragon is 140
human Vs. Dragon
Before fight: Health of human is 45 and Health of Dragon is 140
human damage: 11
Dragon damage: 11
After fight: Health of human is 34 and Health of Dragon is 129
human Vs. Dragon
Before fight: Health of human is 34 and Health of Dragon is 129
human damage: 11
Dragon damage: 11
After fight: Health of human is 23 and Health of Dragon is 118
human Vs. Dragon
Before fight: Health of human is 23 and Health of Dragon is 118
human damage: 11
Dragon damage: 11
After fight: Health of human is 12 and Health of Dragon is 107
human Vs. Dragon
Before fight: Health of human is 12 and Health of Dragon is 107
human damage: 11
Dragon damage: 11
After fight: Health of human is 1 and Health of Dragon is 96
human Vs. Dragon
Before fight: Health of human is 1 and Health of Dragon is 96
human damage: 11
Dragon damage: 11
After fight: Health of human is 0 and Health of Dragon is 85
--------------------------------
Process exited after 0.3079 seconds with return value 0
Press any key to continue . . .