Is there a way to write a general chunk of code and then have the program determine which variables to use depending on some condition?
One thing I'm trying to experiment with is a combat system for a text RPG; I have all the enemy variables in a structure like
and then several kinds of monsters declared with different values for each variable.Code:struct enemy { int eatk; int edef; int maxehp; int ehp; int XPworth; };
What I'd like to do is have the program generate a random number and then send out a monster based on that; I have a general "battle mode" that has stuff like
How could I make it so if, for example, a function returned a random number '2' (I know how to do this part), all the instances of 'edef' would be set equal to (for example) 'rat.edef'? I know I can have a switch or if statements than set edef = rat.edef but if I have more than a few monsters that seems like it would be quite hugeCode:... printf("You attack the enemy for %d damage!\n", patk-edef); ehp = ehp - patk + edef; printf("The enemy has %d HP remaining.\n", ehp); ...