# Unexpected results from function

• 11-27-2005
ajdspud
Unexpected results from function
Hey

I'm trying to create a function that controls a battle between two characters. For testing purposes each character is given 200HP and an armour and weapon level (at the moment defined by the user).

Eventually these characters will be created from a class, but for now, ive just fed in a load of variables to the function. It shoud then continue to battle in rounds untill one player has no HP left, then it should end the battle and the function returns a number based on the overall winner of the battle for use within the main part of the program.

Within each round a random number between 1-3 is created for each player and the highest number is the winner of that round. Then the amount of HP each player loses in the battle is based on a constant number (10 or 20) plus a random amount generated from the difference (if any) between the weapon level of the round winner and the armour level of the round loser.

I've got the program compiled but I'm getting unexpected results. For a starters even though the winner of each round should be totally random player 2 appears to win 90% of time each time i run the program.

As well as this the players are losing unexpected amounts of HP whether they win or lose. At very least the loser of any given round should lose at least 10HP however in every round the player never loses more than 10HP. I have no idea why this is. And more often than not the winner of the round loses more HP than the loser, even though the function should stop this happening.

Ive included the code below, plus a sample outcome. Any help is appreciated. If it helps i set all the weapon and armour variables to 2 and the HP is always 200.

Code:

```int FightFunction(player1_hp, player2_hp, player1_weapon, player2_weapon, player1_armour, player2_armour ) {   int player1, player2, random1, random2, winner, r, overallwin;   cout << "Player 1 has " << player1_hp << " HP left.";   cout << "player 2 has " << player2_hp << " HP left.";   srand( (unsigned)time( NULL ) );   r = 1;   do     {     cout << "This is round " << r << ". ";     random1 = (rand() % 3) + 1;     random2 = (rand() % 10) + 1;     if (random1 > random2)     {       winner = 1;       if (player1_weapon > player2_armour)       {         player2_hp -= (20 + ((rand() % (player1_weapon - player2_armour)) + 1));         player1_hp -= ((rand() % 5) + 1);       }       else if (player1_weapon < player2_armour)       {         player2_hp -= (10 + (rand() % 5) + 1);         player1_hp -= ((rand() % 5) + 1);       }       else if (player1_weapon == player2_armour)       {         player1_hp -= ((rand() % 5) + 1);         player2_hp -= ((rand() % 5) + 1);       }     }     else if (random2 > random1)     {       winner = 2;       if (player2_weapon > player1_armour)       {         player1_hp -= (20 + ((rand() % (player1_weapon - player2_armour)) + 1));         player2_hp -= ((rand() % 5) + 1);       }       else if (player2_weapon < player1_armour)       {         player1_hp -= (10 + (rand() % 5) + 1);         player2_hp -= ((rand() % 5) + 1);       }       else if (player1_weapon == player2_armour)       {         player1_hp -= ((rand() % 5) + 1);         player2_hp -= ((rand() % 5) + 1);       }     }     else if (random1 == random2)     {       winner = 0;       player1_hp -= ((rand() % 5) + 1);       player2_hp -= ((rand() % 5) + 1);     }     switch (winner)     {       case 1:       cout << "\n\nThe winner of round " << r << " is Player 1.";       cout << "\n\nPlayer 1 has " << player1_hp << " left.";       cout << "\n\nPlayer 2 has " << player2_hp << " left.";       break;       case 2:       cout << "\n\nThe winner of round " << r << " is Player 2.";       cout << "\n\nPlayer 1 has " << player1_hp << " left.";       cout << "\n\nPlayer 2 has " << player2_hp << " left.";       break;       case 0:       cout << "\n\nRound " << r << " was a stalemate";       cout << "\n\nPlayer 1 has " << player1_hp << " left.";       cout << "\n\nPlayer 2 has " << player2_hp << " left.";       break;     }     r++;     } while (player1_hp > 0 && player2_hp > 0);   cout << "\n\nThe battle is over!";   if (player2_hp <= 0 && player1_hp > 0)   overallwin = 1;   else if (player1_hp <= 0 && player2_hp > 0)   overallwin = 2;   else if (player1_hp <= 0 && player2_hp <= 0)   overallwin = 0;   return overallwin; }```
Sample output:

Player 1 has 147 HP left
Player 2 has 148 HP left

This is round 18. The winner of round 18 is player 2.

Player 1 has 145 HP left <-- this should drop at least 10
Player 2 has 147 HP left

I've been staring at this a long long time and can't figure it out. If anyone can help, I'd be very grateful.

• 11-27-2005
hk_mp5kpdw
Quote:

Within each round a random number between 1-3 is created for each player and the highest number is the winner of that round. Then the amount of HP each player loses in the battle is based on a constant number (10 or 20) plus a random amount generated from the difference (if any) between the weapon level of the round winner and the armour level of the round loser.

I've got the program compiled but I'm getting unexpected results. For a starters even though the winner of each round should be totally random player 2 appears to win 90% of time each time i run the program.
Not quite 1-3 for both players, player 2 gets 1-10 so of course player 2 wins most of the time:
Code:

```random1 = (rand() % 3) + 1; random2 = (rand() % 10) + 1;```
Quote:

If it helps i set all the weapon and armour variables to 2 and the HP is always 200.

Sample output:

Player 1 has 147 HP left
Player 2 has 148 HP left

This is round 18. The winner of round 18 is player 2.

Player 1 has 145 HP left <-- this should drop at least 10
Player 2 has 147 HP left
But you've said that you set all player armor and weapon values to the same number. So, the following holds:

Code:

```else if (player1_weapon == player2_armour) {     player1_hp -= ((rand() % 5) + 1);     player2_hp -= ((rand() % 5) + 1); }```
Which means that the health will only go down between 1 and 5 pts for each player.
• 11-27-2005
ajdspud
Well now I just feel stupid

Code:

```random1 = (rand() % 3) + 1; random2 = (rand() % 10) + 1;```
How did I miss that????????

Anyway thanks. Ill be able to fix that now.