Text base game

I've been working on a very simple text base game. Ive set up the Classes of Priest,Warrior, Mage in the class i have either Intellect, Endurance,Armor or Health Depending on which uses which. How would i code though that for every 5 armor ignores 1 DMG and if i used an attack it would deplete Lets say 5 Intellect.
So you're saying that battling make them dumber? hehe. :-P The games I've played would have something like mana or stamina that would deplete, and Intelligence would only be an indication of how powerful their non-melee attacks would be, for instance.

But to each its own.

To easily answer your question, your specialized classes all have these basic properties, so you should have them inherit from the same base class. This base class would hold these numbers. Then you can create a single function that takes two instances of these specialized classes polymorphically (asuming the base class is called Character, and that each attack is also an object based on a class called Attack, int PerformAttack(Character &attacker, Character &defender, Attack &attack)) and would then perform the actual calculation on the damage.
Umm So how would i write the first part of your answer? How do i have something inherit another? im new at programming so i understand some of the terms but, not all
Here's an example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
 * The parent class; all fighters are of this type.
 * This type should not be instantiating, but extended ("subclassed.")
 */
class Fighter {
 private:
    int intellect;
    int endurance;
    int armor;
    int health;

 protected:
    Fighter(int i, int e, int a, int h) : intellect(i), endurance(e), armor(a), health(h) {}

 public:
    //methods that all Fighters can use (i.e.  things that all fighters can do)
    virtual void Fight(Fighter &opponent);
    virtual void hurt(int damage) { health -= damage; }
    virtual bool isDead() const { return health <= 0; }
    //...
};

/*
 * These classes are the various types of fighters.
 */
class Priest : public Fighter {
 public:
    Priest() : Fighter(10, 5, 2, 5) {}
    //...
};

class Warrior : public Fighter {
 public:
    Warrior() : Fighter(2, 8, 5, 7) {}
    //...
};

class Mage : public Fighter {
 public:
    Mage() : Fighter(10, 2, 4, 4) {}
    //...
};

//... 
Last edited on
To elaborate on Mathead's example, you should make the hurt() method virtual so each specialized class can perform their own calculations. For example, a Mage could have the protection of a spell, making it take 10% less damage.
Ah, that's a good point webJose. All those methods should be virtual, and also isDead() should be const too. I'll edit my above post.
Last edited on
Topic archived. No new replies allowed.