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.
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:
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 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.
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 7.2 [45 points]
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
int main() { srand((time(0))); Elf e(50,50); Balrog b(50,50);; battleArena(e, b); }
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
In the Creature class's getDamage() function, insert the following statement:
cout << "The " << getSpecies() << " attacks for " << damage << " points!" << endl;
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.)
Try executing the program. The results won't be quite what we were hoping for.
Now make the getSpecies() function in the Creature class a virtual function, and execute the program again. The results will now be correct.
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.
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.
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).
Make getDamage() a virtual function. 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.)
Create a new client program (discard the client program from part 1 of the assignment). 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. (When I say "subtract that amount from Creature2's hitpoints, I mean that the actual hitpoints data member of the Creature2 object will be modified. Also note that this means that both attacks are happening simultaneously; that is, if Creature2 dies because of Creature1's attack, Creature2 still gets a chance to attack back.) 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.
All of the classes should still be in the cs_creature namespace
Assignment 7 Reflection [0 points]
You won't submit anything or be graded for this but I think it is an important part of the assignment so I made it a separate part.
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).
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).
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 getSpecies() exists in the derived class.
getDamage() is a virtual member function that is inherited and is sometimes redefined.
Submit Your Work
Name your client program a7.cpp. Name each class file according to the class it contains (for example, elf.h, creature.cpp). There will be 13 files. Execute your client program and copy/paste the output into the bottom of your client file, making it into a comment. Zip the files and use the Assignment Submission link to submit the zip file. When you submit your assignment there will be a text field in which you can add a note to me (called a "comment", but don't confuse it with a C++ comment). In this "comments" section of the submission page let me know whether the program works as required.
****************************************************************************************************************************************************************************************************************
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started