Unexpected results from function

This is a discussion on Unexpected results from function within the C++ Programming forums, part of the General Programming Boards category; Hey I'm trying to create a function that controls a battle between two characters. For testing purposes each character is ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    21

    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.

    Thanks in advance

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    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;
    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.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    21
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 06:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21