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

Assignment 7.1 Suppose you are creating a fantasy role-playing game. In this gam

ID: 3728134 • Letter: A

Question

Assignment 7.1

Suppose you are creating a fantasy role-playing game. In this game we have four different types of Creatures: Humans, Cyberdemons, Balrogs, and elves. To represent one of these Creatures we might define a Creature class as follows:

Here is an implementation of the getSpecies() function:

The getDamage() function outputs and returns the damage this Creature can inflict in one round of combat. The rules for determining the damage are as follows:

Every Creature inflicts damage that is a random number r, where 0 < r <= strength.

Demons have a 25% chance of inflicting a demonic attack which is an additional 50 damage points. Balrogs and Cyberdemons are demons.

With a 50% chance elves inflict a magical attack that doubles the normal amount of damage.

Balrogs are very fast, so they get to attack twice

An implementation of getDamage() is given below:

One problem with this implementation is that it is unwieldy to add new Creatures. Rewrite the class to use inheritance, which will eliminate the need for the variable "type". The Creature class should be the base class. The classes demon, Elf, and Human should be derived from Creature. The classes Cyberdemon and Balrog should be derived from demon. You will need to rewrite the getSpecies() and getDamage() functions so they are appropriate for each class.

For example, the getDamage() function in each class should only compute the damage appropriate for that specific class. The total damage is then calculated by combining that damage with the results when getDamage() is called on the class's parent class. As an example, Balrog inherits from demon, and demon inherits from Creature. So invoking getDamage() for a Balrog object invokes getDamage() for a demon object, which should invoke getDamage() for the Creature object. This will compute the basic damage that all Creatures inflict, followed by the random 25% damage that demons inflict, followed by the double damage that Balrogs inflict.

Also include mutator and accessor functions for the private variables.

Adhere to the following additional requirements:

Do not use any concepts from lesson 18.3 to write this program. In other words, don't use the word "virtual". One of the main points of this assignment is to illustrate how using virtual may improve our code, so things may seem a little messy here.

Each of the 6 classes will have exactly 2 constructors. Every class will have a getSpecies() function. We won't be declaring objects of type "Creature" or "demon", but you should include getSpecies() functions for them anyway, and they should return "Creature" and "demon", respectively. Make getSpecies() a public member instead of private.

Each of the 5 derived classes will have exactly 4 member functions (including constructors) and no data members

The Creature class will have 8 member functions (including 2 constructors, 2 accessors, and 2 mutators) and 2 data members.

Do not use the "protected" keyword. Many computer programmers consider it to be poor practice because only the base class itself should have uncontrolled access to that data. The derived classes can access the data members through accessors and mutators.

In the non-default constructors for the sub-classes, you will need to use initializer lists.

The Creature class's getDamage() function will return an int representing the damage inflicted. It will contain no cout statements.

The Human class's getDamage() function will (1) call the Creature class's getDamage() function to determine the damage inflicted and (2) print the message reporting the damage inflicted. To review, the syntax for calling the Creature class's getDamage() will be Creature::getDamage().

The Elf class's getDamage() function will be just the same as for the Human class, except there will be some additional couts and calculations after the initial damage inflicted is reported.

The Cyberdemon class's getDamage() function will (1) print the words "The Cyberdemon" and (2) call the demon class's getDamage() function to determine the damage. The words "The Cyberdemon" have to be printed here before calling the demon class's getDamage() function because once we are inside the demon class's getDamage() function there is no way for us to determine which type of demon (Cyberdemon or Balrog) we are working with.

The Balrog class's getDamage() function will (1) print the words "The Balrog", (2) call the demon class's getDamage() function to determine the damage, (3) calculate the damage inflicted by the Balrog's second attack (which is a basic "Creature" attack), and (4) print those results. Don't call the Creature class's getDamage() function to calculate the damage inflicted by the second attack. Instead use something like "damage2 = (rand() % strength) + 1;".

The demon class's getDamage() function will (1) call the Creature class's getDamage() function to determine the damage inflicted, (2) print the words "attacks for ?? points!", (3) determine whether a demonic attack occurs, and if so, (4) print the "Demonic attack" message.

All 6 getDamage() functions will return the damage inflicted.

You must place all of your classes, both the interface and the implementation, in a namespace named "cs_creature".

Here is the client program that you must use to test your classes.

Here is the correct output. Your output should match this exactly except where random numbers are used.

Explanation / Answer

code:

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

class creature {
private:
int strength;

int hitpoints;

public:
virtual string getSpecies() = 0;
creature();
creature( int newStrength, int newHitpoints);
virtual int getDamage() ;
int getStrength();
void setStrength(int newStrength);
int getHitpoints();
void setHitPoints(int newHitPoints);

};

class demon : public creature
{
public:
string getSpecies() ;
int getDamage() ;
demon();
demon(int newStrength, int newHitPoints);
};

class elf : public creature
{
public:
string getSpecies() ;
int getDamage() ;
elf();
elf(int newStrength, int newHitPoints);
};

class human : public creature
{
public:
string getSpecies() ;
human();
human(int newStrength, int newHitPoints);
};

class cyberdemon : public demon
{
public:
string getSpecies() ;
cyberdemon();
cyberdemon(int newStrength, int newHitPoints);
};

class balrog : public demon
{
public:
string getSpecies() ;
int getDamage() ;
balrog();
balrog(int newStrength, int newHitPoints);
};

string demon::getSpecies()
{
return "Demon";
}
string human::getSpecies()
{
return "Human";
}
string elf::getSpecies()
{
return "Elf";
}
string cyberdemon::getSpecies()
{
return "Cyberdemon";
}
string balrog::getSpecies()
{
return "Balrog";
}
string creature::getSpecies()
{
return "Creature";
}

int creature::getDamage() {
int damage;
damage = (rand() % strength) + 1;
cout << "The " << getSpecies() << " attacks for "<< damage << " points!" << endl;
return damage;
}

int demon::getDamage() {
int damage = creature::getDamage();
if (rand() % 4 == 0) {
damage = damage + 50;
cout << "Demonic attack" << endl;
}

return damage;
}
int balrog::getDamage()
{
cout<<"The balrog";
int damage = demon::getDamage();
int damage2 = (rand() % getStrength()) + 1;
damage += damage2;
return damage;
}

int elf::getDamage() {
int damage = creature::getDamage();
cout << getSpecies() << " attacks for " << damage<< " points!" << endl;
if ((rand() % 2) == 0) {
damage *= 2;
}

return damage;
}

creature::creature()
{
strength = 10;
hitpoints = 10;
}
creature::creature(int newStrength, int newHitPoints)
{
strength = newStrength;
hitpoints = newHitPoints;
}


demon::demon() : creature()
{

}
demon::demon(int newStrength, int newHitPoints) : creature(newStrength, newHitPoints)
{

}

elf::elf() : creature()
{

}
elf::elf(int newStrength, int newHitPoints) : creature(newStrength, newHitPoints)
{

}

human::human() : creature()
{

}
human::human(int newStrength, int newHitPoints) : creature(newStrength, newHitPoints)
{

}

cyberdemon::cyberdemon() : demon()
{

}
cyberdemon::cyberdemon(int newStrength, int newHitPoints) : demon(newStrength, newHitPoints)
{

}

balrog::balrog() : demon()
{

}
balrog::balrog(int newStrength, int newHitPoints) : demon(newStrength, newHitPoints)
{

}
void creature::setHitPoints(int newHitPoints)
{
hitpoints = newHitPoints;
}
void creature::setStrength(int newStrength)
{
strength = newStrength;
}
int creature::getHitpoints()
{
return hitpoints;
}
int creature::getStrength()
{
return strength;
}
int battleArena(creature &creature1, creature& creature2)
{
bool isContinue = true;
int damage1 ,damage2;
while(isContinue)
{
damage1 = creature2.getHitpoints() - creature1.getDamage();
damage2 = creature1.getHitpoints() - creature2.getDamage();
if(damage1<0 || damage2<0)
{
isContinue = false;
return 0; //over
}
else if((damage1 >= 0 && damage1 <3) || (damage2> = 0 && damage2 < 3))
{
isContinue = false;
return 1;

}
}


}

int main()
{
srand(time(0));

human h1;
elf e1;
cyberdemon c1;
balrog b1;

human h(20, 30);
elf e(40, 50);
cyberdemon c(60, 70);
balrog b(80, 90);
cout << "default human strength/hitpoints: " << h1.getStrength() << "/" << h1.getHitpoints() << endl;
cout << "default elf strength/hitpoints: " << e1.getStrength() << "/" << e1.getHitpoints() << endl;
cout << "default cyberdemon strength/hitpoints: " << c1.getStrength() << "/" << c1.getHitpoints() << endl;
cout << "default balrog strength/hitpoints: " << b1.getStrength() << "/" << b1.getHitpoints() << endl;
cout << "non-default human strength/hitpoints: " << h.getStrength() << "/" << h.getHitpoints() << endl;
cout << "non-default elf strength/hitpoints: " << e.getStrength() << "/" << e.getHitpoints() << endl;
cout << "non-default cyberdemon strength/hitpoints: "<< c.getStrength() << "/" << c.getHitpoints()<< endl;
cout << "non-default balrog strength/hitpoints: " << b.getStrength() << "/" << b.getHitpoints() << endl;
cout << endl << endl;
cout << "Examples of " << h.getSpecies() << " damage: " << endl;
for (int i = 0; i < 10; i++){
int damage = h.getDamage();
cout << " Total damage = " << damage << endl;
cout << endl;
}
cout << endl;

cout << "Examples of " << e.getSpecies() << " damage: " << endl;
for (int i = 0; i < 10; i++)
{
int damage = e.getDamage();
cout << " Total damage = " << damage << endl;
cout << endl;
}
cout << endl;

cout << "Examples of " << c.getSpecies() << " damage: " << endl;
for (int i = 0; i < 10; i++)
{
int damage = c.getDamage();
cout << " Total damage = " << damage << endl;
cout << endl;
}
cout << endl;

cout << "Examples of " << b.getSpecies() << " damage: " << endl;
for (int i = 0; i < 10; i++)
{
int damage = b.getDamage();
cout << " Total damage = " << damage << endl;
cout << endl;
}
cout << endl;
int result = battleArena(b,c);
if(result == 0)
cout<<"Its a tie !";
else
cout<<"Game over !";
system("pause");
}

If any queries please get back to me

Thank You