Using a random variable in C++
I'm working on the first level of my game and I want to know how I can make the monster use a random attack on the player AND use the correct number of damage. Here is the data that I wrote for this part (I'm not including the whole code because the rest is useless):
void LevelStart(int XP, int Lvl, int NewXP, int AddXP, int NewLvl){ int playerAttack; struct Hero player; struct Attacks playerAttacks; struct Attacks monsterAttacks; struct Monster easyLevelMonster; // Player attacks and stats player.health = 150; playerAttacks.attack1 = "FireBall"; playerAttacks.attack2 = "Fire Slash"; playerAttacks.attack3 = "Fire Barrage"; playerAttacks.attack4 = "Blue Flame Bomb"; playerAttacks.damageAttack1 = 4; playerAttacks.damageAttack2 = 9; playerAttacks.damageAttack3 = 14; playerAttacks.damageAttack4 = 19; // Monster attacks and stats easyLevelMonster.monsterCount = 1; easyLevelMonster.monsterType = "monster"; easyLevelMonster.monsterHealth = 100; monsterAttacks.attack1 = "bash"; monsterAttacks.attack2 = "Spin attack"; monsterAttacks.attack3 = "Heavy punch"; monsterAttacks.damageAttack1 = 10; monsterAttacks.damageAttack2 = 15; monsterAttacks.damageAttack3 = 20;
Tell me if you need more of the code to answer this. Please help me with this.
Voters
Do you want to pick a random attack? You can generate a random number using
rand
fromcstdlib
, and use that to choose. Here's a function which generates a random integer frommin
tomax
inclusive:Thank you! How can I implement this into my code? I looked stuff like this up but I don't want a random number, I'm looking for a random variable like
@19winterspStrongly recommend the array though.
Okay, I'll try, I suck at arrays
@19winterspI ran into a problem seeding an integer inside a loop due to it most of the time being seeded with time (which doesn't change fast enough for loops).
srand
does, but usingrand
seems fine.Yeah, I was thinking "hmmm, that command doesn't seem like the ones I used".
I made the Arrays for
attacks
andattack damage
. How do I pick a random attack to be used?@19wintersp
so for the
attacks
array, with a length of 3: