C++ ASSIGNMENT. PLEASE HELP Instructions - Assignment 15a [required in order to
ID: 3780061 • Letter: C
Question
C++ ASSIGNMENT.
PLEASE HELP
Instructions -
Assignment 15a [required in order to complete assignment 15b]
Note: you may omit documentation on all parts of this assignment.
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:
class creature {
private:
int type; // 0 human, 1 cyberdemon, 2 balrog, 3 elf
int strength; // how much damage this creature inflicts
int hitpoints; // how much damage this creature can sustain
string getSpecies() const; // returns the type of the species
public:
creature(); // initialize to human, 10 strength, 10 hitpoints
creature(int newType, int newStrength, int newHitpoints);
int getDamage() const; // returns the amount of damage this creature
// inflicts in one round of combat
// also include appropriate accessors and mutators
};
Here is an implementation of the getSpecies() function:
string creature::getSpecies() const {
switch (type) {
case 0: return "Human";
case 1: return "Cyberdemon";
case 2: return "Balrog";
case 3: return "Elf";
}
return "unknown";
}
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:
int creature::getDamage() {
int damage;
// All creatures inflict damage which is a random number up to their strength
damage = (rand() % strength) + 1;
cout << getSpecies() << " attacks for " << damage << " points!" << endl;
// Demons can inflict damage of 50 with a 25% chance
if (type == 2 || type == 1){
if (rand() % 4 == 0) {
damage = damage + 50;
cout << "Demonic attack inflicts 50 additional damage points!" << endl;
}
}
// Elves inflict double magical damage with a 50% chance
if (type == 3) {
if ((rand() % 2) == 0) {
cout << "Magical attack inflicts " << damage << " additional damage points!" << endl;
damage *= 2;
}
}
// Balrogs are so fast they get to attack twice
if (type == 2) {
int damage2 = (rand() % strength) + 1;
cout << "Balrog speed attack inflicts " << damage2 << " additional damage points!" << endl;
damage += damage2;
}
return damage;
}
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 object. The total damage is then calculated by combining the results of getDamage() at each level of the inheritance hierarchy. As an example, invoking getDamage() for a balrog object 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:
1. 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.
2. Each of the 6 classes will have exactly 2 constructors. Every class will have a getSpecies() function. It won't be possible to declare objects of type "creature" or "demon", but you should include getSpecies() functions for them anyway, and they should return "creature" and "demon", respectively.
3. Each of the 5 derived classes will have exactly 4 member functions (including constructors) and no data members
4. The creature class will have 8 member functions (including 2 constructors, 2 accessors, and 2 mutators) and 2 data members. You'll need to make the getSpecies() function public.
5. 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.
6. In the non-default constructors for the sub-classes, you will need to use initializer lists.
7. The creature class's getDamage() function will return an int representing the damage inflicted. It will contain no cout statements.
8. 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.
9. 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.
10. 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.
11. 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;".
12. 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.
13. All 6 getDamage() functions will return the damage inflicted.
14. 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.
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;
}
------------------------------------------ ------------------------------------------ ------------------------------------------ ------------------------------------------ ------------------------------------------ ------------------------------------------ ------------------------------------------ ------------------------------------------ ------------------------------------------ ------------------------------------------ ------------------------------------------
Here is the correct output. Your output should match this exactly except where random numbers are used.
default human strength/hitpoints: 10/10
default elf strength/hitpoints: 10/10
default cyberdemon strength/hitpoints: 10/10
default balrog strength/hitpoints: 10/10
non-default human strength/hitpoints: 20/30
non-default elf strength/hitpoints: 40/50
non-default cyberdemon strength/hitpoints: 60/70
non-default balrog strength/hitpoints: 80/90
Examples of human damage:
The human attacks for 8 points!
Total damage = 8
The human attacks for 13 points!
Total damage = 13
The human attacks for 1 points!
Total damage = 1
The human attacks for 14 points!
Total damage = 14
The human attacks for 10 points!
Total damage = 10
The human attacks for 1 points!
Total damage = 1
The human attacks for 18 points!
Total damage = 18
The human attacks for 12 points!
Total damage = 12
The human attacks for 20 points!
Total damage = 20
The human attacks for 8 points!
Total damage = 8
Examples of elf damage:
The elf attacks for 22 points!
Total damage = 22
The elf attacks for 32 points!
Total damage = 32
The elf attacks for 38 points!
Magical attack inflicts 38 additional damage points!
Total damage = 76
The elf attacks for 11 points!
Magical attack inflicts 11 additional damage points!
Total damage = 22
The elf attacks for 16 points!
Total damage = 16
The elf attacks for 27 points!
Total damage = 27
The elf attacks for 22 points!
Magical attack inflicts 22 additional damage points!
Total damage = 44
The elf attacks for 38 points!
Total damage = 38
The elf attacks for 1 points!
Magical attack inflicts 1 additional damage points!
Total damage = 2
The elf attacks for 5 points!
Magical attack inflicts 5 additional damage points!
Total damage = 10
Examples of cyberdemon damage:
The cyberdemon attacks for 30 points!
Total damage = 30
The cyberdemon attacks for 36 points!
Total damage = 36
The cyberdemon attacks for 37 points!
Demonic attack inflicts 50 additional damage points!
Total damage = 87
The cyberdemon attacks for 7 points!
Total damage = 7
The cyberdemon attacks for 10 points!
Total damage = 10
The cyberdemon attacks for 14 points!
Total damage = 14
The cyberdemon attacks for 6 points!
Total damage = 6
The cyberdemon attacks for 25 points!
Total damage = 25
The cyberdemon attacks for 16 points!
Total damage = 16
The cyberdemon attacks for 13 points!
Total damage = 13
Examples of balrog damage:
The balrog attacks for 14 points!
Demonic attack inflicts 50 additional damage points!
Balrog speed attack inflicts 77 additional damage points!
Total damage = 141
The balrog attacks for 57 points!
Balrog speed attack inflicts 67 additional damage points!
Total damage = 124
The balrog attacks for 27 points!
Balrog speed attack inflicts 19 additional damage points!
Total damage = 46
The balrog attacks for 23 points!
Balrog speed attack inflicts 64 additional damage points!
Total damage = 87
The balrog attacks for 64 points!
Balrog speed attack inflicts 12 additional damage points!
Total damage = 76
The balrog attacks for 70 points!
Balrog speed attack inflicts 33 additional damage points!
Total damage = 103
The balrog attacks for 17 points!
Balrog speed attack inflicts 69 additional damage points!
Total damage = 86
The balrog attacks for 79 points!
Balrog speed attack inflicts 57 additional damage points!
Total damage = 136
The balrog attacks for 54 points!
Balrog speed attack inflicts 6 additional damage points!
Total damage = 60
The balrog attacks for 66 points!
Demonic attack inflicts 50 additional damage points!
Balrog speed attack inflicts 74 additional damage points!
Total damage = 190
Assignment 15b
Note: you may omit documentation on all parts of this assignment.
It's messy that in the balrog class's getDamage() function and the cyberdemon class's getDamage() function we have to write the name of the species before calling the demon class's getDamage() function. It would be better if the demon class's getDamage() function could print the name of the species. Taking this a step further, it would be even better if we didn't have to repeat the cout statement "The attacks for ?? points!" in every class's getDamage() function. It would be better if that cout statement could occur just once, in the creature class's getDamage() function.
Make sure that when you test your classes you see examples of the elf doing a magical attack and the balrog doing a demonic attack and also a speed attack.
Don't forget you need to #include and #include
1. In the creature class's getDamage() function, insert the following statement:
2. Delete (or, if you prefer, comment out) the similar cout statements that appear in the getDamage() function of each of the 5 derived classes. (There will be one such cout statement to delete in each of the 5 getDamage() functions.)
3. Try executing the program. The results won't be quite what we were hoping for.
4. Now make the getSpecies() function in the creature class a virtual function, and execute the program again. The results will now be correct.
5. We can now simplify our derived classes even further. Two of the five derived classes have getDamage() functions that do nothing more than call their parent class's getDamage() function. Delete these two functions. (Don't forget to delete both the prototype in the class declaration and the definition.) We don't need them, because they can just inherit the getDamage() function from the creature class.
6. You may have noticed that the creature class's getSpecies() function never gets called. However, it is absolutely critical that any class that is derived from the creature class define a getSpecies() function since that function is called from the creature class's getDamage() function. The best way to implement this is to make the creature class's getSpecies() function a pure virtual function, so that every class that is derived from the creature class will be required to implement a getSpecies() function. Make the creature class's getSpecies() function a pure virtual function.
7. Comment out the getSpecies() function in the human class and try compiling the program to see what happens. then uncomment it (i.e., return it to it's previous state).
8. Make getDamage() a virtual function too. This will be important so that in your "battleArena" function (see below) you can say "creature1.getDamage()" and the damage will automatically be calculated for the correct creature. Note that the parameters for "battleArena" will be of type "creature" and they will need to be pass-by-reference. (You might try making them pass-by-value to see what happens.)
9. Make a function in your client program that is called from your main function, battleArena(creature &creature1, creature& creature2), that takes two creature objects as parameters. The function should calculate the damage done by creature1, subtract that amount from creature2's hitpoints, and vice versa. If both creatures end up with 0 or fewer hitpoints, then the battle results in a tie. Otherwise, at the end of a round, if one creature has positive hitpoints but the other does not, the battle is over. The function should loop until either a tie or over. Since the getDamage() function is virtual it should invoke the getDamage() function defined for the appropriate creature. Test your program with several battles involving different creatures. I've provided a sample main function below. Your only remaining task is to write the "battleArena" function and expand the main function so that the "battleArena" function is tested with a variety of different creatures.
10. All of the class should still be in the cs_creature namespace
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Take some time to reflect on the program you just completed from the perspective of the base class, creature. With respect to inheritance, you'll see 3 categories of member functions illustrated (not counting constructors, since they are not inherited).
1. The accessors and mutators are typical member functions that are inherited and never redefined. For example, the balrog class's getDamage() function calls getStrength(), which it can do because it has been inherited from the creature class (via the demon class).
2. getSpecies() is a pure virtual function. It is not defined in the creature class, but it must be defined in every class that is derived from the creature class, or the creature class won't work, because the creature class's getDamage() function depends on the fact that it exists in the derived class.
3. getDamage() is a virtual member function that is inherited and is sometimes redefined.
****************************************************************************************************************************************
Name each class file according to the class it contains (for example, elf.h, creature.cpp, etc.). There will be 13 files.
*********************************************************************************************************************************************
Explanation / Answer
Amswer for part a)
PROGRAM CODE:
/*
* creature.cpp
*
* Created on: 02-Dec-2016
* Author: kasturi
*/
#include <iostream>
using namespace std;
//creature class
class creature {
private:
// int type; // 0 human, 1 cyberdemon, 2 balrog, 3 elf
int strength; // how much damage this creature inflicts
int hitpoints; // how much damage this creature can sustain
public:
string getSpecies() ; // returns the type of the species
creature(); // initialize to human, 10 strength, 10 hitpoints
creature( int newStrength, int newHitpoints);
int getDamage() ; // returns the amount of damage this creature inflicts in one round of combat
int getStrength(); // return the damage inflicted by the creature
void setStrength(int newStrength);
int getHitpoints();
void setHitPoints(int newHitPoints);
// also include appropriate accessors and mutators
};
//demon class
class demon : public creature
{
public:
string getSpecies() ;
int getDamage() ;
demon();
demon(int newStrength, int newHitPoints);
};
//elf class
class elf : public creature
{
public:
string getSpecies() ;
int getDamage() ;
elf();
elf(int newStrength, int newHitPoints);
};
//human class
class human : public creature
{
public:
string getSpecies() ;
int getDamage() ;
human();
human(int newStrength, int newHitPoints);
};
//cyberdemon class
class cyberdemon : public demon
{
public:
string getSpecies() ;
int getDamage() ;
cyberdemon();
cyberdemon(int newStrength, int newHitPoints);
};
//balrog class
class balrog : public demon
{
public:
string getSpecies() ;
int getDamage() ;
balrog();
balrog(int newStrength, int newHitPoints);
};
//getSpecies for all the creatures
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";
}
//get damages for all the creatures
int creature::getDamage() {
int damage;
// All creatures inflict damage which is a random number up to their strength
damage = (rand() % strength) + 1;
//cout << getSpecies() << " attacks for " << damage << " points!" << endl;
return damage;
}
int human::getDamage() {
int damage = creature::getDamage();
// All creatures inflict damage which is a random number up to their strength
//damage = (rand() % strength) + 1;
cout << getSpecies() << " attacks for " << damage << " points!" << endl;
return damage;
}
int demon::getDamage() {
int damage = creature::getDamage();
// Demons can inflict damage of 50 with a 25% chance
cout << "attacks for " << damage << " points!" << endl;
if (rand() % 4 == 0) {
damage = damage + 50;
cout << "Demonic attack" << endl;
}
return damage;
}
int balrog::getDamage() {
cout<<"The balrog";
int damage = demon::getDamage();
// Balrogs are so fast they get to attack twice
int damage2 = (rand() % getStrength()) + 1;
cout << "Balrog speed attack inflicts " << damage2 << " additional damage points!" << endl;
damage += damage2;
return damage;
}
int cyberdemon::getDamage() {
cout<<"The cyberdemon ";
int damage = demon::getDamage();
return damage;
}
int elf::getDamage() {
int damage = creature::getDamage();
cout << getSpecies() << " attacks for " << damage << " points!" << endl;
// Elves inflict double magical damage with a 50% chance
if ((rand() % 2) == 0) {
cout << "Magical attack inflicts " << damage << " additional damage points!" << endl;
damage *= 2;
}
return damage;
}
//constructor for creature
creature::creature()
{
strength = 10;
hitpoints = 10;
}
creature::creature(int newStrength, int newHitPoints)
{
strength = newStrength;
hitpoints = newHitPoints;
}
//constructor for demon
demon::demon() : creature()
{
}
demon::demon(int newStrength, int newHitPoints) : creature(newStrength, newHitPoints)
{
}
//constructor for elf
elf::elf() : creature()
{
}
elf::elf(int newStrength, int newHitPoints) : creature(newStrength, newHitPoints)
{
}
//constructor for human
human::human() : creature()
{
}
human::human(int newStrength, int newHitPoints) : creature(newStrength, newHitPoints)
{
}
//constructor for cyberdemon
cyberdemon::cyberdemon() : demon()
{
}
cyberdemon::cyberdemon(int newStrength, int newHitPoints) : demon(newStrength, newHitPoints)
{
}
//constructor for balrog
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 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;
}
OUTPUT:
default human strength/hitpoints: 10/10
default elf strength/hitpoints: 10/10
default cyberdemon strength/hitpoints: 10/10
default balrog strength/hitpoints: 10/10
non-default human strength/hitpoints: 20/30
non-default elf strength/hitpoints: 40/50
non-default cyberdemon strength/hitpoints: 60/70
non-default balrog strength/hitpoints: 80/90
Examples of Human damage:
Human attacks for 13 points!
Total damage = 13
Human attacks for 14 points!
Total damage = 14
Human attacks for 20 points!
Total damage = 20
Human attacks for 18 points!
Total damage = 18
Human attacks for 5 points!
Total damage = 5
Human attacks for 13 points!
Total damage = 13
Human attacks for 10 points!
Total damage = 10
Human attacks for 14 points!
Total damage = 14
Human attacks for 5 points!
Total damage = 5
Human attacks for 1 points!
Total damage = 1
Examples of Elf damage:
Elf attacks for 17 points!
Magical attack inflicts 17 additional damage points!
Total damage = 34
Elf attacks for 1 points!
Magical attack inflicts 1 additional damage points!
Total damage = 2
Elf attacks for 4 points!
Magical attack inflicts 4 additional damage points!
Total damage = 8
Elf attacks for 3 points!
Total damage = 3
Elf attacks for 19 points!
Magical attack inflicts 19 additional damage points!
Total damage = 38
Elf attacks for 30 points!
Total damage = 30
Elf attacks for 1 points!
Magical attack inflicts 1 additional damage points!
Total damage = 2
Elf attacks for 22 points!
Total damage = 22
Elf attacks for 39 points!
Magical attack inflicts 39 additional damage points!
Total damage = 78
Elf attacks for 12 points!
Total damage = 12
Examples of Cyberdemon damage:
The cyberdemon attacks for 15 points!
Demonic attack
Total damage = 65
The cyberdemon attacks for 40 points!
Total damage = 40
The cyberdemon attacks for 28 points!
Total damage = 28
The cyberdemon attacks for 58 points!
Demonic attack
Total damage = 108
The cyberdemon attacks for 60 points!
Total damage = 60
The cyberdemon attacks for 11 points!
Demonic attack
Total damage = 61
The cyberdemon attacks for 17 points!
Demonic attack
Total damage = 67
The cyberdemon attacks for 60 points!
Total damage = 60
The cyberdemon attacks for 48 points!
Total damage = 48
The cyberdemon attacks for 55 points!
Demonic attack
Total damage = 105
Examples of Balrog damage:
The balrogattacks for 42 points!
Demonic attack
Balrog speed attack inflicts 40 additional damage points!
Total damage = 132
The balrogattacks for 80 points!
Balrog speed attack inflicts 20 additional damage points!
Total damage = 100
The balrogattacks for 28 points!
Balrog speed attack inflicts 67 additional damage points!
Total damage = 95
The balrogattacks for 14 points!
Balrog speed attack inflicts 12 additional damage points!
Total damage = 26
The balrogattacks for 19 points!
Balrog speed attack inflicts 56 additional damage points!
Total damage = 75
The balrogattacks for 55 points!
Demonic attack
Balrog speed attack inflicts 9 additional damage points!
Total damage = 114
The balrogattacks for 22 points!
Balrog speed attack inflicts 3 additional damage points!
Total damage = 25
The balrogattacks for 31 points!
Balrog speed attack inflicts 75 additional damage points!
Total damage = 106
The balrogattacks for 53 points!
Balrog speed attack inflicts 56 additional damage points!
Total damage = 109
The balrogattacks for 54 points!
Demonic attack
Balrog speed attack inflicts 69 additional damage points!
Total damage = 173
Answer for part b:
PROGRAM CODE:
/*
* creature.cpp
*
* Created on: 02-Dec-2016
* Author: kasturi
*/
#include <iostream>
using namespace std;
//creature class
class creature {
private:
// int type; // 0 human, 1 cyberdemon, 2 balrog, 3 elf
int strength; // how much damage this creature inflicts
int hitpoints; // how much damage this creature can sustain
public:
virtual string getSpecies() = 0; // returns the type of the species
creature(); // initialize to human, 10 strength, 10 hitpoints
creature( int newStrength, int newHitpoints);
virtual int getDamage() ; // returns the amount of damage this creature inflicts in one round of combat
int getStrength(); // return the damage inflicted by the creature
void setStrength(int newStrength);
int getHitpoints();
void setHitPoints(int newHitPoints);
// also include appropriate accessors and mutators
};
//demon class
class demon : public creature
{
public:
string getSpecies() ;
int getDamage() ;
demon();
demon(int newStrength, int newHitPoints);
};
//elf class
class elf : public creature
{
public:
string getSpecies() ;
int getDamage() ;
elf();
elf(int newStrength, int newHitPoints);
};
//human class
class human : public creature
{
public:
string getSpecies() ;
//int getDamage() ;
human();
human(int newStrength, int newHitPoints);
};
//cyberdemon class
class cyberdemon : public demon
{
public:
string getSpecies() ;
//int getDamage() ;
cyberdemon();
cyberdemon(int newStrength, int newHitPoints);
};
//balrog class
class balrog : public demon
{
public:
string getSpecies() ;
int getDamage() ;
balrog();
balrog(int newStrength, int newHitPoints);
};
//getSpecies for all the creatures
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";
}
//get damages for all the creatures
int creature::getDamage() {
int damage;
// All creatures inflict damage which is a random number up to their strength
damage = (rand() % strength) + 1;
//cout << getSpecies() << " attacks for " << damage << " points!" << endl;
cout << "The " << getSpecies() << " attacks for " << damage << " points!" << endl;
return damage;
}
/*int human::getDamage() {
int damage = creature::getDamage();
// All creatures inflict damage which is a random number up to their strength
//damage = (rand() % strength) + 1;
//cout << getSpecies() << " attacks for " << damage << " points!" << endl;
return damage;
}*/
int demon::getDamage() {
int damage = creature::getDamage();
// Demons can inflict damage of 50 with a 25% chance
//cout << "attacks for " << damage << " points!" << endl;
if (rand() % 4 == 0) {
damage = damage + 50;
cout << "Demonic attack" << endl;
}
return damage;
}
int balrog::getDamage() {
cout<<"The balrog";
int damage = demon::getDamage();
// Balrogs are so fast they get to attack twice
int damage2 = (rand() % getStrength()) + 1;
//cout << "Balrog speed attack inflicts " << damage2 << " additional damage points!" << endl;
damage += damage2;
return damage;
}
/*int cyberdemon::getDamage() {
//cout<<"The cyberdemon ";
int damage = demon::getDamage();
return damage;
}*/
int elf::getDamage() {
int damage = creature::getDamage();
cout << getSpecies() << " attacks for " << damage << " points!" << endl;
// Elves inflict double magical damage with a 50% chance
if ((rand() % 2) == 0) {
//cout << "Magical attack inflicts " << damage << " additional damage points!" << endl;
damage *= 2;
}
return damage;
}
//constructor for creature
creature::creature()
{
strength = 10;
hitpoints = 10;
}
creature::creature(int newStrength, int newHitPoints)
{
strength = newStrength;
hitpoints = newHitPoints;
}
//constructor for demon
demon::demon() : creature()
{
}
demon::demon(int newStrength, int newHitPoints) : creature(newStrength, newHitPoints)
{
}
//constructor for elf
elf::elf() : creature()
{
}
elf::elf(int newStrength, int newHitPoints) : creature(newStrength, newHitPoints)
{
}
//constructor for human
human::human() : creature()
{
}
human::human(int newStrength, int newHitPoints) : creature(newStrength, newHitPoints)
{
}
//constructor for cyberdemon
cyberdemon::cyberdemon() : demon()
{
}
cyberdemon::cyberdemon(int newStrength, int newHitPoints) : demon(newStrength, newHitPoints)
{
}
//constructor for balrog
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; //tie
}
}
}
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 !";
}
OUTPUT:
default human strength/hitpoints: 10/10
default elf strength/hitpoints: 10/10
default cyberdemon strength/hitpoints: 10/10
default balrog strength/hitpoints: 10/10
non-default human strength/hitpoints: 20/30
non-default elf strength/hitpoints: 40/50
non-default cyberdemon strength/hitpoints: 60/70
non-default balrog strength/hitpoints: 80/90
Examples of Human damage:
The Human attacks for 9 points!
Total damage = 9
The Human attacks for 17 points!
Total damage = 17
The Human attacks for 18 points!
Total damage = 18
The Human attacks for 8 points!
Total damage = 8
The Human attacks for 20 points!
Total damage = 20
The Human attacks for 3 points!
Total damage = 3
The Human attacks for 2 points!
Total damage = 2
The Human attacks for 11 points!
Total damage = 11
The Human attacks for 2 points!
Total damage = 2
The Human attacks for 18 points!
Total damage = 18
Examples of Elf damage:
The Elf attacks for 15 points!
Elf attacks for 15 points!
Total damage = 15
The Elf attacks for 6 points!
Elf attacks for 6 points!
Total damage = 12
The Elf attacks for 4 points!
Elf attacks for 4 points!
Total damage = 4
The Elf attacks for 2 points!
Elf attacks for 2 points!
Total damage = 2
The Elf attacks for 17 points!
Elf attacks for 17 points!
Total damage = 17
The Elf attacks for 11 points!
Elf attacks for 11 points!
Total damage = 22
The Elf attacks for 24 points!
Elf attacks for 24 points!
Total damage = 48
The Elf attacks for 31 points!
Elf attacks for 31 points!
Total damage = 31
The Elf attacks for 39 points!
Elf attacks for 39 points!
Total damage = 39
The Elf attacks for 11 points!
Elf attacks for 11 points!
Total damage = 11
Examples of Cyberdemon damage:
The Cyberdemon attacks for 33 points!
Total damage = 33
The Cyberdemon attacks for 12 points!
Total damage = 12
The Cyberdemon attacks for 10 points!
Demonic attack
Total damage = 60
The Cyberdemon attacks for 56 points!
Total damage = 56
The Cyberdemon attacks for 33 points!
Total damage = 33
The Cyberdemon attacks for 36 points!
Total damage = 36
The Cyberdemon attacks for 2 points!
Total damage = 2
The Cyberdemon attacks for 11 points!
Total damage = 11
The Cyberdemon attacks for 25 points!
Demonic attack
Total damage = 75
The Cyberdemon attacks for 45 points!
Total damage = 45
Examples of Balrog damage:
The balrogThe Balrog attacks for 73 points!
Total damage = 143
The balrogThe Balrog attacks for 76 points!
Total damage = 139
The balrogThe Balrog attacks for 21 points!
Demonic attack
Total damage = 147
The balrogThe Balrog attacks for 39 points!
Total damage = 59
The balrogThe Balrog attacks for 6 points!
Total damage = 53
The balrogThe Balrog attacks for 30 points!
Total damage = 58
The balrogThe Balrog attacks for 39 points!
Total damage = 61
The balrogThe Balrog attacks for 10 points!
Demonic attack
Total damage = 66
The balrogThe Balrog attacks for 9 points!
Total damage = 38
The balrogThe Balrog attacks for 54 points!
Total damage = 96
The balrogThe Balrog attacks for 63 points!
The Cyberdemon attacks for 7 points!
Its a tie !
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.