I made a simple battle simulator program to practice my loops and such but when I run the program the human forces always win with no losses. at first I figured this was cause I was missing, turn = 'E'; after humans turn was processed however when I added the line the problem persisted.
#include<iostream>
#include<cstdlib>
#include<string>
#include<random>
#include<ctime>
usingnamespace std;
void battleSim(int x, int y);
//initializing and seeding random number generator.
mt19937 randEng(time(NULL));
uniform_real_distribution<float> attack(0.0f, 1.0f);
int main()
{
//variables for to store user input
int humans, elves;
cout << "--------Basic Human vs. Elf Battle Sim.------------ \n\n" << endl;
//request user to enter number of humans. prompt them to reenter in case of bad entry
while(cout << "How many humans will battle: " && cin >> humans){
if(humans <= 0 || humans >3000){
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid number please stay within the range of 1-3000" << endl;
}elsebreak;
}
//request user to enter number of elves. prompt them to reenter in case of bad entry
while(cout << "How many elves will battle: " && cin >> elves){
if(elves <= 0 || elves >3000){
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid number please stay within the range of 1-3000" << endl;
}elsebreak;
}
cout << "\nLet the Battle BEGIN!!!! \n\n" << endl;
cout << "Results: \n"<< endl;
battleSim(humans, elves);
system("PAUSE");
return 0;
}
//x = humans, y = elves
void battleSim(int x, int y)
{
//turn - used to keep track of the turns. atkRoll - used to store the attack result
char turn = 'E';
float atkRoll;
//defined human properties curHumHP - used to hold the currently fighting humans health
float curHumHP = 75.0f;
float humHP = 75.0f;
float humATK = 0.6f;
float humDMG = 70.0f;
//defined elf properties curElfHP - ^^^
float curElfHP = 60.0f;
float elfHP = 60.0f;
float elfATK = 0.75f;
float elfDMG = 60.0f;
//perform battle until one army has 0 forces
while(x > 0 && y > 0){
//getting attack result
atkRoll = attack(randEng);
//Elves turn
if(turn == 'E'){
//checking for elves hit success or failure.
if(atkRoll <= elfATK){
//damage the current opposing enemy
curHumHP -= elfDMG;
//checking current enemy health and reacting
if(curElfHP <= 0){
//reducing total forces and reseting current enemy health
x--;
curHumHP = humHP;
}
}
//changing turns
turn = 'H';
}else{//Humans turn
//checking for humans hit success or fail.
if(atkRoll <= humATK){
//damage the current enemy
curElfHP -= humDMG;
//check current enemy health
if(curElfHP <= 0){
//reducing total forces and reseting current enemy health
y--;
curElfHP = elfHP;
}
}
//changing turns
turn = 'E';
}
}
//Display results of battle loop
if(x > 0){
cout << "The Humans are victorious today. The remaining soldiers will rejoice. \n" << endl;
cout << "Human Forces Left: " << x << endl;
}else{
cout << "The Elves are victorious today. The remaining soldiers will rejoice. \n" << endl;
cout << "Elf Forces Left: " << y << endl;
}
}