Skip to content
Sign upLog in
Using a random variable in C++
Profile icon

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 = 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.

You are viewing a single comment. View All
Profile icon

You could store those as an array rather than giving them names, or if you prefer to use these predefined names:

switch(randomInteger(1, 3)) { case 1: //do something with monsterAttacks.attack1 case 2: //do something with monsterAttacks.attack2 case 3: //do something with monsterAttacks.attack3 }

Strongly recommend the array though.