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                  static constexpr double DEMONIC_ATTACK_PROBABILITY = 0.25;         static const int DEMONIC_BONUS_DAMAGE = 50;         static constexpr double MAGICAL_ATTACK_PROBABILITY = 0.5;         static constexpr double MAGICAL_ATTACK_MULTIPLIER = 2.0;         static const int DEFAULT_STRENGTH = 10

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

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

        

        static constexpr double DEMONIC_ATTACK_PROBABILITY = 0.25;

        static const int DEMONIC_BONUS_DAMAGE = 50;

        static constexpr double MAGICAL_ATTACK_PROBABILITY = 0.5;

        static constexpr double MAGICAL_ATTACK_MULTIPLIER = 2.0;

        static const int DEFAULT_STRENGTH = 10;

        static const int DEFAULT_HITPOINTS = 10;

    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    

};

 

Note: I realize we have not covered constexpr. For now, it is fine to just know that data members of type "double" cannot be const, so we use constexpr instead.

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 DEMONIC_ATTACK_PROBABILITY (initially set to 25%) chance of inflicting a demonic attack which is an additional DEMONIC_BONUS_DAMAGE (initially set to 50) damage points. Balrogs and Cyberdemons are Demons.
  • With a MAGICAL_ATTACK_PROBABILITY (initially set to 50%) chance, elves inflict a magical attack that multiplies the normal amount of damage by MAGICAL_ATTACK_MULTIPLIER (initially set to 2).
  • Balrogs are very fast, so they get to attack twice

An implementation of getDamage() is given below:

int Creature::getDamage() const {

    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 bonus damage of DEMONIC_BONUS_DAMAGE with a DEMONIC_ATTACK_PROBABILITY chance

    if (type == 2 || type == 1){

        if (rand() % 100 * 0.01 < DEMONIC_ATTACK_PROBABILITY) {

            damage = damage + DEMONIC_BONUS_DAMAGE;

            cout << "Demonic attack inflicts " << DEMONIC_BONUS_DAMAGE << " additional damage points!" << endl;

        }

    }

    

    // Elves inflict multiplied magical damage with a MAGICAL_ATTACK_PROBABILITY chance

    if (type == 3) {

        if (rand() % 100 * 0.01 < MAGICAL_ATTACK_PROBABILITY) {

            cout << "Magical attack inflicts " << damage << " additional damage points!" << endl;

            damage *= MAGICAL_ATTACK_MULTIPLIER;

        }

    }

    

    // 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.

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 6 steps

Blurred answer
Knowledge Booster
Class
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-engineering and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY