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.
#include "SwordArmy.h"
#include "Attributes.h"
#include <iostream>
#include <vector>
#include <random>
#include <ctime>
#include <limits>
usingnamespace 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!!! ";
}
elseif(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.
}
#include "SwordArmy.h"
#include <iostream>
#include <vector>
usingnamespace 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(unsignedint i=0; i<unitName.size(); i++)
{
cout << i+1 << ".) " << unitName[i] << endl;
}
}
}
#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
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.
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.
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.
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:
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.
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.
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?
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 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.
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.