Adding each unit they're current value and letting the user pick as many units as they want

Pages: 12
Before I go into detail, please do not give me code. I'm only asking for advice as I generally want to do this by myself. Now that I have prefaced my intro with that sentence, I trying to add different values to different units and I have prompt the user to enter however many units that they want. Regardless the different values each unit will have, each value will remain constant(I haven't decided if want my game to have status ailments that decreases your stats). That much I know. Since I'm using vector I'm pretty sure there is some little neat thing I can do but I can't think of anything at the moment. So a little step in the right direction is all I'm asking.

main.cpp
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "SwordArmy.h"
#include "Attributes.h"
#include <iostream>
#include <vector>
#include <random>
#include <ctime>
#include <limits>

using namespace std;

int main()
{
    // Weloming Message
    cout << "\t\tWelcome to my 2014 Combat Simulator. " << endl;

    // Introduction.
    cout << "\n\nFor a thousand years, the forces of evil have a laid seige";
    cout << " on the kingdom of \nThebel. Their campaign have been relentless";
    cout << " and ongoing throughout the ages.The victor of this";
    cout << " battle will be remembered in history and be declared as";
    cout << "King. The only question that remains is which side will";
    cout << "you be fighting for? " << endl << endl;

    string  fightingSide; // Prompting the user to pick a side

    cout << "\t\tPick a side!" << endl << endl;

    cout << "1.) Sword Keepers " << "     OR      " <<   "2.) The Nocturnes   ";
    getline(cin, fightingSide, '\n');

    // Checking for input validation.
    while(fightingSide != "Sword Keepers" && fightingSide != "The Nocturnes" )
    {
        cerr << "\nYou've entered a faction that doesn't exist in the game. ";
        getline(cin, fightingSide, '\n');
    }

    // A greeting from their faction.
    if(fightingSide == "Sword Keepers")
    {
        cout << "\n(Human Voice)--Now let us fight in jolly cooperation!!! ";
    }
    else if(fightingSide == "The Nocturnes")
    {
        cout << "\n(Grating Voice)-May you show the humans no mercy commander. ";
    }

    cout << "\n\nPress any button to continue. ";
    cin.get();
    cin.ignore(numeric_limits<streamsize>::max(), '\n');

    cout << endl;

     mt19937 randomGenerator(time(NULL));
     uniform_real_distribution<float> attackChance(0.0f, 1.0f);

    // Advice before the battle begins.
    cout << "\n\nBefore you can begin, you're going to need to assembly";
    cout << " your army. Each unit has different strengths and weaknesses." << endl;
    cout << "\nYou're only allowed to have 10000 units. Which means, you";
    cout << " better pick wisely. ";

    cout << endl;

    SwordArmy army;

    army.setArmy(); // Gathers the name of each unit.

    cout << endl;

    army.displayArmy(fightingSide); // Displays the army.

    return 0; // Return zero if the program ran successful.
}



SwordArmy.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef SWORDARMY_H
#define SWORDARMY_H
#include <vector>
#include <string>

class SwordArmy
{
public:
    SwordArmy();
    void setArmy();
    void displayArmy(std::string &);
protected:
     std::vector<std::string> unitName;

};

#endif 



SwordArmy.cpp
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
45
46
47
48
49
#include "SwordArmy.h"
#include <iostream>
#include <vector>

using namespace std;

SwordArmy::SwordArmy()
{

}

void SwordArmy::setArmy()
{
    vector<string> name;

   // The name of each unit
   name.push_back("Arcane Mage");
   name.push_back("Templar");
   name.push_back("Rogue");
   name.push_back("Berseker Knight");
   name.push_back("Pikeman");
   name.push_back("Archer");
   name.push_back("Wyvern Rider");
   name.push_back("Girffions");
   name.push_back("Infantry");
   name.push_back("Defenders");

   vector<string>::iterator iter; // an iterator that moves through the name vector.

   for(iter=name.begin(); iter != name.end(); iter++)
   {
       unitName = name; // insert each string in the private member vector.
   }
}

void SwordArmy::displayArmy(string &allegiance)
{
    // If the user chose this faction.
    if(allegiance == "Sword Keepers")
    {
        // Display their units.
        cout << "These are the units available for battle. " << endl << endl;
        for(unsigned int i=0; i<unitName.size(); i++)
        {
            cout << i+1 << ".) " << unitName[i] << endl;
        }

    }
}


Attributes.h
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#ifndef ATTRIBUTES_H
#define ATTRIBUTES_H
#include <vector>
#include <string>

class Attributes
{
public:
    Attributes();
protected:
    int damage;
    int speed;
    int defense;
    int willPower;
    float hitChance;
};

class ArcaneMage: public Attributes
{
public:
    void setAttributes();
private:
    int mana;
    int spellDamage;
    float spellChance;
};

class Templar: public Attributes
{
public:
    void setAttributes();
};

class Rogue: public Attributes
{
public:
    void setAttributes();
};

class BerserkerKnight: public Attributes
{
public:
    void setAttributes();
};

class PikeMan: public Attributes
{
public:
    void setAttributes();
};

class Archer: public Attributes
{
public:
    void setAttributes();
};

class WyvernRider: public Attributes
{
public:
    void setAttributes();
};

class Griffions: public Attributes
{
public:
    void setAttributes();
};

class Infantry: public Attributes
{
public:
    void setAttributes();
};

class Defenders: public Attributes
{
public:
    void setAttributes();
};

#endif // ATTRIBUTES_H 
Last edited on
I see you are asking for no code suggestions, but I also am kinda lacking an idea of what your goal is. In 1 or 2 sentences, can you state your goal, then follow it with some direct, simple questions. Ie, What would be a simple way of having an army of different units (ie, 2 pikemen, 10 infantry)? Thank, Spike :D
Sorry for replying so late but, my final goal is to allow the user to chose whichever of the two sides that they want and, depending if they are on one side or the other, they'll either be defending their capital city or will be invading the capital city. Whoever completes either of those tasks will be the victor. Now my current goal is to assign different values to each unit that you see in the SwordArmy.cpp file. So a berserker knight would have slow speed but high damage compared to a rogue who has high speed but low damage. Then before the battle the user has to carefully chose what units he think will be useful in the coming battle. So if in the user wants 100 rogues, 10 templars, and 5 mages, then that is what he's going to use.
Might want to use a map maybe?
std::map<std::string, int>

the string being the unit type and the int being the number of that unit requested.
http://www.cplusplus.com/reference/map/map/map/
ok, but how would you assign different values for each unit without overwriting other unit's values. The really long way that I'm thinking is creating separate functions for each unit and assigning their values in their function but that would be 10 different functions and I have a feeling that there is a better way to it.
Last edited on
sounds like you need a Unit class then? with a 'name' (e.g. "Mage1"), a 'type' (e.g. "Arcane Mage"), and a 'value'. Then you would have objects with unique names. You could even sub-class Unit into ArcaneMage, Templar etc etc

That would, i think, fit in better with the OO of what you're trying to do. At the moment your Army class has collections to hold unit attributes, but wouldn't it make more sense to have these attributes some kind of unit class? e.g.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

class Unit
{
protected:
 std::string unitName;
    int damage;
    int speed;
    int defense;
    int willPower;
    float hitChance;
};

class ArcaneMage : public Unit
{

...

private:
    int mana;
 
};


edit: can i also comment on this:
vector<string> SwordArmy::setArmy(vector<string> &name)

You're passing in a reference so your return type here should be void. and you don't need to do this:
1
2
3
 

   return unitName; // Return the member data type 
Last edited on
Oh I see, so you're suggesting I create class devoted to the attributes and a class for the units themselves and each of them will have access to the class with the attributes. From there I can then assign their appropriate values. From your perspective, it makes sense seeing that each unit will have the same properties(with the exception of the mages) but different values. Though wouldn't this be like the suggestion that I proposed; creating different functions for each unit.

In your example, should I still use vectors or is there still something I can do in regards of what you're proposing I should do.

One last thing, what's the different between protected and private. Never really learned the different between the two.

edit: yes you're right. I've changed the setarmy to a void and I've deleted that return unitname.
Last edited on
Protected means only the base class and derived classes can access those members. Private means only the class they're implemented in may access them. The friend keyword is an exception to this.
Last edited on
oh and I've decided that certain unit will have special moves. for example, infantry will have the option to either attack or defend. On the other hand, mages can either attack with their staves or use their magic spells(Magic spells been their special attack). For this, should I have a class that encompass all of these moves?
Last edited on
I assuming base class means the class that the members originally started in or that you created. If so, what would derived class be?
Are you familiar with polymorphism? If so I'd make an abstract base class in which your different units can inherit from. The beauty of this is that you can redefine the implementation of say for example Attack() and Defend() for each derived class(unit).
I guess I have to look up polymorphism because I haven't touched it yet.
You don't have to. It just simplifies what you want to do. I'll write you and example.

1
2
3
4
5
6
class BaseUnit
{
public:
    virtual void Attack() = 0:
    virtual void Defend() = 0:
}


1
2
3
4
5
6
7
8
9
10
11
12
13
class DerivedUnit1 : public BaseUnit
{
public:
    void Attack()
    {
        //do something
    }

    void Defend()
    {
        //do something
    }
}


1
2
3
4
5
6
7
8
9
10
11
12
class DerivedUnit2 : public BaseUnit
{
public:
    void Attack()
    {
        //do something else
     }

    void Defend()
    {
        //do something else
    }
Alright thanks for the example. I'll keep this topic open until I'm finish with this project. I'll keep you updated.
I was thinking of creating a separate class file that has all the attributes(attributes class) and each unit will inherit that class and I will assign their values in that class. After that, I will create another class(called UnitMove class) that inherits the unit class and create different moves for each unit. How's that? I've watched a couple of videos on inheritance and polymorphism and that is what I was able to come up with.
Attributes.cpp
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include "SwordArmy.h"
#include "Attributes.h"
#include <iostream>
#include <vector>

using namespace std;

Attributes::Attributes()
{

}

void ArcaneMage::setAttributes()
{
    int tempDamage = 15;
    int tempSpell = 100;
    int tempSpeed = 50;
    int tempDefense = 25;
    int tempWill = 75;
    int tempMana = 100;
    float tempChance = 0.5;
    float tempSpellChance = 0.9;

    damage = tempDamage;
    spellDamage = tempSpell;
    speed = tempSpeed;
    defense = tempDefense;
    willPower = tempWill;
    mana = tempMana;
    hitChance = tempChance;
    spellChance = tempSpellChance;
}

void Templar::setAttributes()
{
    int tempDamage = 90;
    int tempSpeed = 15;
    int tempDefense = 80;
    int tempWill = 75;
    float tempChance = 0.2;

    damage = tempDamage;
    speed = tempSpeed;
    defense = tempDefense;
    willPower = tempWill;
    hitChance = tempChance;
}

void Rogue::setAttributes()
{
    int tempDamage = 20;
    int tempSpeed = 95;
    int tempDefense = 30;
    int tempWill = 70;
    float tempChance = 0.9;

    damage = tempDamage;
    speed = tempSpeed;
    defense = tempDefense;
    willPower = tempWill;
    hitChance = tempChance;
}

void BerserkerKnight::setAttributes()
{
    int tempDamage = 95;
    int tempSpeed = 5;
    int tempDefense = 95;
    int tempWill = 85;
    float tempChance = 0.1;

    damage = tempDamage;
    speed = tempSpeed;
    defense = tempDefense;
    willPower = tempWill;
    hitChance = tempChance;
}

void PikeMan::setAttributes()
{
    int tempDamage = 50;
    int tempSpeed = 45;
    int tempDefense = 60;
    int tempWill = 50;
    float tempChance = 0.5;

    damage = tempDamage;
    speed = tempSpeed;
    defense = tempDefense;
    willPower = tempWill;
    hitChance = tempChance;
}

void Archer::setAttributes()
{
    int tempDamage = 70;
    int tempSpeed = 60;
    int tempDefense = 50;
    int tempWill = 70;
    float tempChance = 0.7;

    damage = tempDamage;
    speed = tempSpeed;
    defense = tempDefense;
    willPower = tempWill;
    hitChance = tempChance;
}

void WyvernRider::setAttributes()
{
    int tempDamage = 80;
    int tempSpeed = 80;
    int tempDefense = 20;
    int tempWill = 70;
    float tempChance = 0.75;

    damage = tempDamage;
    speed = tempSpeed;
    defense = tempDefense;
    willPower = tempWill;
    hitChance = tempChance;
}

void Griffions::setAttributes()
{
    int tempDamage = 70;
    int tempSpeed = 90;
    int tempDefense = 50;
    int tempWill = 60;
    float tempChance = 0.80;

    damage = tempDamage;
    speed = tempSpeed;
    defense = tempDefense;
    willPower = tempWill;
    hitChance = tempChance;
}

void Infantry::setAttributes()
{
    int tempDamage = 50;
    int tempSpeed = 50;
    int tempDefense = 50;
    int tempWill = 50;
    float tempChance = 0.5;

    damage = tempDamage;
    speed = tempSpeed;
    defense = tempDefense;
    willPower = tempWill;
    hitChance = tempChance;
}

void Defenders::setAttributes()
{
    int tempDamage = 60;
    int tempSpeed = 30;
    int tempDefense = 90;
    int tempWill = 60;
    float tempChance = 0.6;

    damage = tempDamage;
    speed = tempSpeed;
    defense = tempDefense;
    willPower = tempWill;
    hitChance = tempChance;
}
Alright, I've updated my main post and I had to make another comment just to fit this cpp file. Now I guess the next step is displaying their stats so that the user can see it.
Last edited on
I think I 've hit a brick wall because I don't how I could display the correct stats for each unit given the way that I structured my code.
Display them as in display them in the console? Or are you trying to make a GUI?
Display them in the console window like this....

After I've shown what units are available, I show the stats for each unit like this.

Arcane Mage: Damage 50, Speed 40, Hit chance 20 etc etc... and do that for each character.

This will help the user pick the units that he wants for his team.
Last edited on
Pages: 12