Classes and Objects

Started by
10 comments, last by alvaro 10 years, 1 month ago

This isn't complete code, but it should get you pointed in the right direction. If you're still having trouble working with arrays, post the code you're trying to get working with a description of the problems you're having.


//Best to declare the size of your arrays for a variety of reasons
//This size will get used in a variety of locations, as you'll see
const int numMonsters = 4;
monsterClass monsters[numMonsters];

//Initialize all monsters
monsters[0].setMonster("Pig", 5, 5, rand() % 5 + 1, rand() % 1);
...
monsters[numMonsters-1].setMonster...


//No adding 1 here.  Arrays are zero based
//An array of 4 monsters will have indices 0, 1, 2, and 3
int randomMonster = rand() % numMonsters;

//Now combat can commence on monsters[randomMonster]
//And you have only one block of code for fighting monsters
//This can be pushed to a separate function as well
while(monsters[randomMonster].isAlive() && player.isAlive())
{


}
Advertisement

I originally had in mind code like jHaskell's when I mentioned arrays. But then I read the code more carefully and realized that he doesn't have four monsters that the player is fighting: There seem to be 4 monster types, and a new instance of the monster is created in each attack. That's why my code is more involved.

This topic is closed to new replies.

Advertisement