I'm messing with classes and I'm wondering what would be a good way to create a list of monsters like Pokemon.
I could have a Pokemon class and make them from that like this:
1 2 3 4 5 6 7 8 9 10
class Pokemon {
string name;
string type
int HP;
int str;
int def;
string skills[20];
public:
Pokemon(string nam,string typ,int hp,int str,int def);
};
Since pokemon have a different number of skills, I can't have an exact number constant to the class, so how would I go about labeling the skills?
Would I have to go pkm0.skills[0] = "x", pkmn[0].skills[1] = "xx".
Or maybe I could have the base Pokemon class with subclasses for each pokemon with the exact numbers. But with that, it would be much more lengthy and would probably take up a lot of unnecessary space.
if you're goal is to make a small game i would make a subclass for every pokemon. Unless you're goal is to model all pokemon, in which case i'd make a way to store sort of a.. database of pokemon attributes, and load them into your class.
If you were to do it like that, yes you'd need to have the skill entered individually, which is why i mentioned a 'database'. Your program would load the database file, and read every entry, which would feed into your data. Afterwards, you could use a list to store multiple instances of Pokemon.
std::list<Pokemon*>::iterator i = pokemonList.begin()
while(i != pokemonList.end())
{
//do whatever code to pull data from your database
(*i)->AddSkill(/*skill name you pulled from database*/) //or something of that sort.
++i;
}
To delete a pokemon you'd
1 2 3
//inside a loop
delete *i;
i = pokemonList.erase(i);
Thumper is right - unless you are writing a small app with a limited number of Pokemon (I suspect it would be hard to stop creating them once you start), database-driven code would be your best bet.
If you stuff your Pokemon data in Mysql, for example, you will be able to query on various Pokemon types or instantiate your objects based on a SQL statement. If you prefer more structure, Postgresql will allow you to create a OODB where you could define your Objects in the database itself. On the other end of the scale, you could always stuff this data in comma-delimited or pipe-delimited text file.
It all depends on your requirements, but putting all the data in one place (not in your code) will make for much easier maintenance.
(*i)->AddSkill("Tackle"); assumes you're using an iterator to access it, and your list is made of Pokemon pointers. When you store objects in lists, you use an 'iterator' to move through it.
It was my bad, I had the AddSkill part commented out on accident haha.
Anyways I have this set up now:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
void PokemonManager::SkillAssigner()
{
std::list<Pokemon*>::iterator i = pokemonList.begin();
int x = 0;
while(i != pokemonList.end())
{
string atk[3] = { "Tackle","Growl","Thundershock" };
while( x < 3 )
{
(*i)->AddSkill(atk[x],x);
++x;
}
++i;
}
}
Where AddSkill is this:
1 2 3 4
void Pokemon::AddSkill(string str, int x)
{
skills[x] = str;
}
Now that I pretty much see how it works, I'll try to expand on it and implement the database.
Do you guys have any pointers on improving the structure of the code?
If you aren't using polymorphism, why did you made a list of pointers instead of a list of objects?
Erase a member will be easier, because you don't have to worry about delete.
And instead of (*it)->memfunc(); you will have it->memfunc();
Well, I do not know too much about Pokemon, but I suspect there are penalties and bonuses based on attacks vs creature-type. For example (I am guessing here), Water vs Fire +10%, etc... So you may want to create some kind of method which would return a double (eg 1.1) based on AttackType vs DefenseType. Maybe call it:
Of course, if you know all the attackTypes and defenseTypes, an enumeration would be even more type-safe. You can make this table data-driven by storing it in a 2D matrix of attackType vs defensType.
BTW, ne555 has a good point - if you do not intend to use polymorphism, it is better to store the entire object in the list. Or if you want to access them by name:
Would having a list of pointers be more efficient?
I made a text based pokemon game a while back, but the coding was just awful lol. I'm trying to rewrite it clean with classes and to be more efficient, and add graphics as well.
Also to find a pokemon object I was wondering maybe I could have a list of the objects to store them, then use a map to have the <name, pointer to a pokemon object> so I could easily locate it by name.
If you aren't using polymorphism, why did you made a list of pointers instead of a list of objects
Works either way. He might later add some polymorphic(?) function.
AdventWolf wrote:
then use a map to have the <name, pointer to a pokemon object>
Possible, you could also use the name you defined in your pokemon class. Make a getname function or something, and iterate through the list and find the pokemon with the name you're looking for. That's how i'd do it. Mostly because i don't have experience with maps, and i'm not sure what their purpose is :P