a way to have massive selection list without huge nesting

Is there anyway to have a huge list of stuff that program will auto choose from without using huge nesting. I'm trying to make a Monster Manager for a combat routine.

something like

int MonsterSelect = 0;



then on the area that triggers the combat have

int MonsterSelect = # of monster stored in a seperate function

so

Game() > MonsterManager() > Combat()


id rather have seperate small functions for each monster type or even a single function if possible that stores all monster information. I don't want tons of if else what about switchs is there a way to do switchs without user input?
Last edited on
is this a legal use of if?

if (MonsterSelect = 1)
{
cout << "Monster Encounter 1\n";
}
else if (MonsterSelect = 2)
{
cout << "Monster Encounter 2\n";
}
else if (MonsterSelect = 3)
{
cout << "Monster Encounter 3\n";
}
How aboutcout << "Monster encounter " << MonsterSelect <<'\n';?
If your monsters only differ in some properties, like power and health you could do
1
2
3
4
struct monster{
    int pow, hp;
};
monster my_monsters[50] = {/*you can define your monsters quickly here*/};
then in combat function, do sth like my_monsters[MonsterSelect].hp-=50;.

If your monsters differ drastically and really need different function each, you should go for polymorphism, though I have a feeling this is not the case.
This is how i was planning on doing it:




Global Monster Variables:

// Monster #1
string MonsterName1 = "Fiddler_Crab";
int MonsterLevel1 = 1;
int MonsterHitPoints1 = 10;
int MonsterAttack1 = 7;
int MonsterDefense1 = 4;
int MonsterHitRate1 = 50;
int MonsterExperience1 = 20;


// Global Monster Variables - Combat System
string MonsterName = "Monster";
int MonsterLevel = 0;
int MonsterHitPoints = 0;
int MonsterAttack = 0;
int MonsterDefense = 0;
int MonsterHitRate = 0;
int MonsterExperience = 0;
int MonsterSelect = 0;




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

MonsterManager()
{
if (MonsterSelect = 1)
{ 
MonsterName = MonsterName1;
MonsterHitPoints = MonsterHitPoints1;
MonsterAttack = MonsterHitPoints1;
MonsterDefense = MonsterDefense1;
MonsterHitRate = MonsterDefense1;
MonsterExperience = MonsterExperience1;
Combat();
}
else if (MonsterSelect = 2)
{
 ANOTHER ONE HERE
}
else if (MonsterSelect = 3)
{
AND HERE ETC
}
return 0;
}




then in there area the encounter is triggered from, i just put MonsterSelect = #;
Last edited on
Which it seems for some odd reason its not registering my code not sure where the problem is but im working on it......

the difference is


Name
Level
HP
Attack
Defense
Hitrate
Experience (Awarded)
Gold (Awarded)
Use a container for this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
struct Monster
{
  string name;
  int hp;
  //... etc
};

Monster MonsterList[] = {
  {"Fiddler Crab",15, /*...*/},
  {"Great Moose",96, /*...*/}
};


void somefunction()
{
  // select a monster
  int monsterID = rand() % number_of_monsters;

  Monster mymonster = MonsterList[monsterID];  // no need for any ifs, just use the ID to index
}


Another (arguably better) way to go would be to have the monster details stored in an external file that you load at runtime. But the general idea is the same -- have a big array (or some other container, like a vector/list/deque), and use that as like master list of all the enemies, and just pull from it what you need.
and guess what my way worked 100% perfect and flawless!!! Just i had a homer simpson moment doh!!! I put MonsterCombat() and triggered the combat function with 0 initialized variables instead of MonsterManager() > MonsterCombat()


xD
That doesn't make it a good way though. Do as Disch (and I) said.
oh jeez I didn't even notice that you said that already hamsterman XD
Topic archived. No new replies allowed.