fight simulation

everything seems to be going as expected except player1 always wins ALL of the fights, I have no clue what I'm doing wrong here, please help if you can




#include <cstdlib>
#include <ctime>
#include <iostream>

using namespace std;

int main()
{
srand((unsigned)time(NULL));

//Game Variables
bool player1Turn = false;
bool player2Turn = false;
int numberOfFights = 0;
int player1Wins = 0;
int player2Wins = 0;

//player 1 stats//
char player1Name[20] = "Player1";
int player1Health = 100;
int player1Damage = 10;
int player1Dodge = 10;

//player 2 stats//
char player2Name[20] = "Player2";
int player2Health = 100;
int player2Damage = 10;
int player2Dodge = 10;

cout << "Please enter the name, health, damage, dodge rating for player 1" << endl;
cin >> player1Name >> player1Health >> player1Damage >> player1Dodge;

cout << "Please enter the name, health, damage, dodge rating for player 2" << endl;
cin >> player2Name >> player2Health >> player2Damage >> player2Dodge;

cout << "Please enter number of fights desired: " << endl;
cin >> numberOfFights;

for (int i = 0; i < numberOfFights; i++)
{
// coin toss
int coinToss=rand()%100 + 1;
if (coinToss <= 50)
{
player1Turn = true;
player2Turn = false;
}
else if (coinToss > 50)
{
player1Turn = false;
player2Turn = true;
}
while (player1Health > 0 && player2Health > 0)
{
//swinging
if (player1Turn == false)
{
int hitChance=rand()%100;
if(hitChance > player2Dodge)
{
player2Health -= player1Damage;
}
player1Turn = false;
player2Turn = true;
}
else
{
int hitChance=rand()%100;
if(hitChance > player1Dodge)
{
player1Health -= player2Damage;
}
player1Turn = true;
player2Turn = false;
}
}
if (player1Health > 0)
{
player1Wins++;
}
else if (player2Health > 0)
{
player2Wins++;
}
}

cout << "Player 1 Wins: " << player1Wins << endl;
cout << "Player 2 wins: " << player2Wins << endl;
system ("pause");
}
Last edited on
Firstly whoever has the first turn will always win since you never swap turns:

1
2
3
4
5
6
7
8
9
10
if (player1Turn == false)
{
   int hitChance=rand()%100;
   if(hitChance > player2Dodge)
   {
      player2Health -= player1Damage;
   }
   player1Turn = false;
   player2Turn = true;
}


Also you never reset the health levels so whoever wins the first match will win every other match. (Actually they don't do any fighting after the first match.)
Topic archived. No new replies allowed.