How would i randomly generate a number, and then choose a monster name for that? I know it's simple... but what I tried doesn't work... at all.
How would i randomly generate a number, and then choose a monster name for that? I know it's simple... but what I tried doesn't work... at all.
Maybe you should show us what you've tried to do so far. Random number generation is covered in quite a few posts on these boards.Originally posted by drdroid
How would i randomly generate a number, and then choose a monster name for that? I know it's simple... but what I tried doesn't work... at all.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
One solution is to create an emu of the monster names and randomly generate an integer representing the monster ID.
KuphrynCode:emu monster {A = 0x0001, B, C, D, E}; unsigned int nNum = 1 + rand() % 5; switch (nNum) { case A : { break; } ... }
I dont mean to be rude, but theres an example of correct, yet extremely overly complex code.
What was wrong with:
?Code:switch ( (rand() % 5) + 1 ) { case 1 : break; //theres no need for enclosing braces in a case ... }
"There's always another way"
-lightatdawn (lightatdawn.cprogramming.com)
... and one of the more common links I post is...
http://www.eskimo.com/~scs/C-faq/q13.16.html
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
I'm not talking about random number generation... I know how to do that... I'm talking about applying that to choosing a monster for my battle engine. I'm more concerned about the string part... not the number.
use a random number to index a static array of strings.Arrays start at array[0] and finish at array[x] so you need rand(x). Simple.
Free the weed!! Class B to class C is not good enough!!
And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi
They are needed on some compilers in some situations. It doesn't hurt to put them there, it prevents scope errors.Originally posted by lightatdawn
/theres no need for enclosing braces in a case
>>They are needed on some compilers in some situations
AFAIK, it should be all compilers, but the only situation (again, AFAIK) is when attempting to declare variables inside the scope of the case. This seems like a bad coding practice anyhow, so I dont see why one would want to condone it. Though your right, it doesnt _technically_ hurt.
>>use a random number to index a static array of strings.Arrays start at array[0] and finish at array[x] so you need rand(x). Simple.
Yup. Listen to the man.
Monster->SetupStats(rand() % Max_Monsters);
"There's always another way"
-lightatdawn (lightatdawn.cprogramming.com)