Question

Base Class enum HeroType {WARRIOR, ELF, WIZARD}; const double MAX_HEALTH = 100.0; class Character { protected:...

Base Class

enum HeroType {WARRIOR, ELF, WIZARD};

const double MAX_HEALTH = 100.0;

class Character {
 protected:
    HeroType type;
    string name;
    double health;
    double attackStrength;

 public:
     Character(HeroType type, const string &name, double health, double attackStrength);
     HeroType getType() const;
     const string & getName() const;
     /* Returns the whole number of the health value (static_cast to int). */
     int getHealth() const;
     void setHealth(double h);
     /* Returns true if getHealth() returns an integer greater than 0, otherwise false */
     bool isAlive() const;
     virtual void attack(Character &) = 0;
 };

Derived Classes

Warrior

Stores the warrior's allegiance as a string.

The warrior does not attack warriors that have the same allegiance.

The damage done by the warrior is the percentage of the warrior's health remaining (health / MAX_HEALTH) multiplied by the warrior's attack strength.

Elf

Stores the elf's family name as a string.

The elf does not attack elf's from its own family.

The damage done by the elf is the percentage of the elf's health remaining (health / MAX_HEALTH) multiplied by the elf's attack strength.

Wizard

Stores the wizard's rank as an int.

When a wizard attacks another wizard, the damage done is the wizard's attack strength multiplied by the ratio of the attacking wizard's rank over the defending wizard's rank.

The damage done to non-wizards is just the attack strength. The wizard's health is not taken into consideration.

Dynamic casting type of Character in attack function

In order to access the Warrior data field allegiance using the Character reference passed in to the attack function, you will need to dynamic cast the Character reference to a Warrior reference.

Here's an example of dynamic casting a Character reference named opponent to a Warrior reference named opp:

Warrior &opp = dynamic_cast<Warrior &>(opponent);

You will need to do the same for the Wizard and Elf attack functions, only dynamic casting to Wizard or Elf reference instead.

HeroType

Notice the enum declaration above the Character class declaration. This creates a special type called HeroType that has the values, WARRIOR, ELF, and WIZARD. Those are the values you store in a variable of type HeroType. For example, you can initialize a variable of type HeroType and set it to the value of WARRIOR like this:

HeroType type = WARRIOR;

You can compare a variable named t of type HeroType to one of the HeroType values like this:

if (t == WARRIOR) {
   // do something based on t being a warrior
}

Example main function

This shows you what your attack function should do in all situations. Look below this to find the main function you will need to pass the zyBook tests.

#include <iostream>

using namespace std;

#include "Warrior.h"
#include "Elf.h"
#include "Wizard.h"

int main() {

    Warrior w1("Arthur", 100, 5, "King George");
    Warrior w2("Jane", 100, 6, "King George");
    Warrior w3("Bob", 100, 4, "Queen Emily");
    Elf e1("Raegron", 100, 4, "Sylvarian");
    Elf e2("Cereasstar", 100, 3, "Sylvarian");
    Elf e3("Melimion", 100, 4, "Valinorian");
    Wizard wz1("Merlin", 100, 5, 10);
    Wizard wz2("Adali", 100, 5, 8);
    Wizard wz3("Vrydore", 100, 4, 6);
    e1.attack(w1);
    cout << endl;
    e1.attack(e2);
    cout << endl;
    w2.attack(w1);
    cout << endl;
    w3.attack(w1);
    cout << endl;
    wz1.attack(wz2);
    cout << endl;
    wz1.attack(wz3);
    cout << endl;

    return 0;
}

The output of this main function will look like this:

Elf Raegron shoots an arrow at Arthur --- TWANG!!
Arthur takes 4 damage.

Elf Raegron does not attack Elf Cereasstar.
They are both members of the Sylvarian family.

Warrior Jane does not attack Warrior Arthur.
They share an allegiance with King George.

Warrior Bob attacks Arthur --- SLASH!!
Arthur takes 4 damage.

Wizard Merlin attacks Adali --- POOF!!
Adali takes 6.25 damage.

Wizard Merlin attacks Vrydore --- POOF!!
Vrydore takes 8.33333 damage.

main function needed for zyBook tests

#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>

using namespace std;

#include "Character.h"
#include "Warrior.h"
#include "Elf.h"
#include "Wizard.h"

int main() {
    int seed;
    cout << "Enter seed value: ";
    cin >> seed;
    cout << endl;

    srand(seed);

    vector<Character *> adventurers;
    adventurers.push_back(new Warrior("Arthur", MAX_HEALTH, 5, "King George"));
    adventurers.push_back(new Warrior("Jane", MAX_HEALTH, 6, "King George"));
    adventurers.push_back(new Warrior("Bob", MAX_HEALTH, 4, "Queen Emily"));
    adventurers.push_back(new Elf("Raegron", MAX_HEALTH, 4, "Sylvarian"));
    adventurers.push_back(new Elf("Cereasstar", MAX_HEALTH, 3, "Sylvarian"));
    adventurers.push_back(new Elf("Melimion", MAX_HEALTH, 4, "Valinorian"));
    adventurers.push_back(new Wizard("Merlin", MAX_HEALTH, 5, 10));
    adventurers.push_back(new Wizard("Adali", MAX_HEALTH, 5, 8));
    adventurers.push_back(new Wizard("Vrydore", MAX_HEALTH, 4, 6));

    unsigned numAttacks = 10 + rand() % 11;
    unsigned attacker, defender;
    for (unsigned i = 0; i < numAttacks; ++i) {
        attacker = rand() % adventurers.size();
        do {
            defender = rand() % adventurers.size();
        } while (defender == attacker);

        adventurers.at(attacker)->attack(*adventurers.at(defender));
        cout << endl;
    }
    cout << "-----Health Remaining-----" << endl;
    for (unsigned i = 0; i < adventurers.size(); ++i) {
        cout << adventurers.at(i)->getName() << ": " 
            << adventurers.at(i)->getHealth() << endl;
    }
    return 0;
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Character.cpp

#include <iostream>
#include <string>

#include "Character.h"

using namespace std;


/*STUFFS

enum CharType {WARRIOR, ELF, WIZARD};

const double MAX_HEALTH = 100.0;

class Character {
protected:
    CharType type;
    string name;
    double health;
    double attackStrength;
*/

//public:

//no default constructor either
   
     Character::Character(CharType type, const string &name, double health, double attackStrength)
         :type(type), name(name), health(health), attackStrength(attackStrength)
     {}
   
     CharType Character::getType() const
     {
      return this->type;
     }
   
     const string & Character::getName() const
     {
        return this->name;
     }
   
     /* Returns the whole number of the health value (static_cast to int). */
     int Character::getHealth() const
     {
         return static_cast<int>(health);
     }
   
     void Character::setHealth(double h)
     {
         this->health = h;
         return;
     }
   
     /* Returns true if getHealth() returns an integer greater than 0, otherwise false */
     bool Character::isAlive() const
     {
         if(this->getHealth() > 0)
         {
            return true;
         }
         return false;
     }
   
Character.h

#ifndef __CHARACTER_H__
#define __CHARACTER_H__

#include <iostream>
#include <string>

using namespace std;


enum CharType {WARRIOR, ELF, WIZARD};

const double MAX_HEALTH = 100.0;

class Character {
protected:
    CharType type;
    string name;
    double health;
    double attackStrength;

public:
     Character(CharType type, const string &name, double health, double attackStrength);
     CharType getType() const;
     const string & getName() const;
     /* Returns the whole number of the health value (static_cast to int). */
     int getHealth() const;
     void setHealth(double h);
     /* Returns true if getHealth() returns an integer greater than 0, otherwise false */
     bool isAlive() const;
     virtual void attack(Character &) = 0;
};

#endif // __CHARACTER_H__

Elf.cpp

#include <iostream>
#include <string>

#include "Character.h"
#include "Elf.h"

using namespace std;

    // private:
    // string famName;
  
    // public:
    Elf::Elf(const string &name, double health, double attackStrength, const string &family)
        :Character(ELF, name, health, attackStrength), famName(family)
    {}
  
    void Elf::attack(Character &cname)
    {
        if(cname.getType() == ELF)
        {
           Elf &opp = dynamic_cast<Elf &>(cname);
            if(opp.famName == this->famName)
            {
              
                cout << "Elf " << this->name << " does not attack Elf " << cname.getName() << "." << endl;
                cout << "They are both members of the " << opp.famName << " family." << endl;
              
                return;
            }
        }
      

       double damage = 0;
       damage = (this->health / MAX_HEALTH) * (this->attackStrength) ;
     
       cname.setHealth(cname.getHealth() - damage);
      
        cout << "Elf " << this->name << " shoots an arrow at " << cname.getName() << " --- TWANG!!" << endl;
        cout << cname.getName() << " takes " << damage << " damage." << endl;
  
      
        return;
    }
  
  
Elf.h

#ifndef __ELF_H__
#define __ELF_H__

#include "Character.h"

#include <iostream>
#include <string>

using namespace std;

class Elf : public Character
{
    private:
    string famName;
  
    public:
    //Elf();
    Elf( const string &name, double health, double attackStrength, const string &family);
    void attack(Character &cname);
  

};


#endif // __ELF_H__

Warrior.cpp


#include <iostream>
#include <string>

#include "Character.h"
#include "Warrior.h"

using namespace std;

    // private:
    // string allegiance;
  
    //public:

    Warrior::Warrior( const string &name, double health, double attackStrength, const string &tribe)
       :Character(WARRIOR, name, health, attackStrength), allegiance(tribe)
    {}
  
    void Warrior::attack(Character &cname)
    {
        if(cname.getType() == WARRIOR)
        {
           Warrior &opp = dynamic_cast<Warrior &>(cname);
            if(opp.allegiance == this->allegiance)
            {
                cout << "Warrior " << this->name << " does not attack Warrior " << cname.getName() << "." << endl;
                cout << "They share an allegiance with " << opp.allegiance << "." << endl;
              
                return;
            }
        }
      
       double damage = 0;
       damage = (this->health / MAX_HEALTH) * (this->attackStrength) ;
     
       cname.setHealth(cname.getHealth() - damage);
      
        cout << "Warrior " << this->name << " attacks " << cname.getName() << " --- SLASH!!" << endl;
        cout << cname.getName() << " takes " << damage << " damage." << endl;
  
        return;


    }
  

  
Warrior.h

#ifndef __WARRIOR_H__
#define __WARRIOR_H__

#include "Character.h"

#include <iostream>
#include <string>

using namespace std;

class Warrior : public Character
{
  
    private:
    string allegiance;
  
    public:
    //Warrior();
    Warrior( const string &name, double health, double attackStrength, const string &tribe);
    void attack(Character &cname);
  

};


#endif // __WARRIOR_H__

Wizard.cpp

#include <iostream>
#include <string>

#include "Character.h"
#include "Wizard.h"

using namespace std;

    // private:
    // int rank;
  
    //public:
  
    Wizard::Wizard( const string &name, double health, double attackStrength, int level )
         :Character(WIZARD, name, health, attackStrength), rank(level)
    {}
  
    void Wizard::attack(Character &cname)
    {
        double damage = 0;
      
        if(cname.getType() == WIZARD)
        {
            Wizard& opp = dynamic_cast<Wizard&>(cname);
            damage = (this->attackStrength) * (static_cast<double>(this->rank)) / (static_cast<double>(opp.rank));
          
            cout << "Wizard " << this->name << " attacks " << cname.getName() << " --- POOF!!" << endl;
            cout << cname.getName() << " takes " << damage << " damage." << endl;
          
            cname.setHealth(cname.getHealth() - damage);
            return;
        }
      
        damage = attackStrength;
        cname.setHealth(cname.getHealth() - damage);
      
            cout << "Wizard " << this->name << " attacks " << cname.getName() << " --- POOF!!" << endl;
            cout << cname.getName() << " takes " << damage << " damage." << endl;
      
        return;
    }
  


Wizard.h

#ifndef __WIZARD_H__
#define __WIZARD_H__

#include "Character.h"

#include <iostream>
#include <string>

using namespace std;

class Wizard : public Character
{

    private:
    int rank;
  
    public:
    //Wizard();
    Wizard( const string &name, double health, double attackStrength, int level );
    void attack(Character &cname);


};


#endif // __WIZARD_H__

main1.cpp


//Look below this to find the main function you will need to pass the zyBook tests.

#include <iostream>

using namespace std;

#include "Warrior.h"
#include "Elf.h"
#include "Wizard.h"

int main() {

    Warrior w1("Arthur", 100, 5, "King George");
    Warrior w2("Jane", 100, 6, "King George");
    Warrior w3("Bob", 100, 4, "Queen Emily");
    Elf e1("Raegron", 100, 4, "Sylvarian");
    Elf e2("Cereasstar", 100, 3, "Sylvarian");
    Elf e3("Melimion", 100, 4, "Valinorian");
    Wizard wz1("Merlin", 100, 5, 10);
    Wizard wz2("Adali", 100, 5, 8);
    Wizard wz3("Vrydore", 100, 4, 6);
    e1.attack(w1);
    cout << endl;
    e1.attack(e2);
    cout << endl;
    w2.attack(w1);
    cout << endl;
    w3.attack(w1);
    cout << endl;
    wz1.attack(wz2);
    cout << endl;
    wz1.attack(wz3);
    cout << endl;

    return 0;
}


//main function needed for zyBook tests

#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>

using namespace std;

#include "Character.h"
#include "Warrior.h"
#include "Elf.h"
#include "Wizard.h"

int main() {
    int seed;
    cout << "Enter seed value: ";
    cin >> seed;
    cout << endl;

    srand(seed);

    vector<Character *> adventurers;
    adventurers.push_back(new Warrior("Arthur", MAX_HEALTH, 5, "King George"));
    adventurers.push_back(new Warrior("Jane", MAX_HEALTH, 6, "King George"));
    adventurers.push_back(new Warrior("Bob", MAX_HEALTH, 4, "Queen Emily"));
    adventurers.push_back(new Elf("Raegron", MAX_HEALTH, 4, "Sylvarian"));
    adventurers.push_back(new Elf("Cereasstar", MAX_HEALTH, 3, "Sylvarian"));
    adventurers.push_back(new Elf("Melimion", MAX_HEALTH, 4, "Valinorian"));
    adventurers.push_back(new Wizard("Merlin", MAX_HEALTH, 5, 10));
    adventurers.push_back(new Wizard("Adali", MAX_HEALTH, 5, 8));
    adventurers.push_back(new Wizard("Vrydore", MAX_HEALTH, 4, 6));

    unsigned numAttacks = 10 + rand() % 11;
    unsigned attacker, defender;
    for (unsigned i = 0; i < numAttacks; ++i) {
        attacker = rand() % adventurers.size();
        do {
            defender = rand() % adventurers.size();
        } while (defender == attacker);

        adventurers.at(attacker)->attack(*adventurers.at(defender));
        cout << endl;
    }
    cout << "-----Health Remaining-----" << endl;
    for (unsigned i = 0; i < adventurers.size(); ++i) {
        cout << adventurers.at(i)->getName() << ": "
            << adventurers.at(i)->getHealth() << endl;
    }
    return 0;
}

Default Term +Browser sh-4.2s main Enter seed value: 3 Elf Cereasstar shoots an arrow at Arthur TWANG!! Arthur takes 3 damage

Add a comment
Answer #2

I got error 


Character.cpp:9:21: error: expected constructor, destructor, or type conversion before ‘(’ token    9 | Character::Character(CharType type, const string &name, double health, double attackStrength)      |                     ^ Character.cpp:11:1: error: ‘CharType’ does not name a type   11 | CharType Character::getType() const      | ^~~~~~~~

Add a comment
Know the answer?
Add Answer to:
Base Class enum HeroType {WARRIOR, ELF, WIZARD}; const double MAX_HEALTH = 100.0; class Character { protected:...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ Inheritance Problem Step a: Suppose you are creating a fantasy role-playing game. In this game...

    C++ Inheritance Problem Step a: 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; //...

  • Godzilla Class Pt. 2 C++ I have to continue building off of this class and do...

    Godzilla Class Pt. 2 C++ I have to continue building off of this class and do the following I got help with the code below but could use some guidance as to how to finish this out. Any help would be appreciated This is the code for the main.cpp #include <iostream> #include "Godzilla.h" using namespace std; int main() { double health; double power; //prompt the user to input the health and power for Godzilla cout<< "Enter the health: " ;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT