1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
struct Gunman
{
std::string name;
unsigned hitOneInEvery;
bool aliveStatus;
unsigned kills;
unsigned wins;
};
bool aimAndShoot(Gunman &shooter, Gunman &target1, Gunman &target2);
int main()
{
std::srand(std::time(nullptr));
unsigned const rounds = 10000u;
//you could use an array here if you want
//the name parameter isn't as useful without array but oh well
Gunman aaron{"aaron", 3u, true, 0u, 0u};
Gunman bob{"bob", 2u, true, 0u, 0u};
Gunman charlie{"charlie", 1u, true, 0u, 0u};
for(unsigned round = 0u; round < rounds; ++round)
{
aaron.aliveStatus = true;
bob.aliveStatus = true;
charlie.aliveStatus = true;
unsigned totalAlive = 3u;
//if you don't want total alive you could alternatively do (aaron.aliveStatus && bob.aliveStatus) || (aaron.aliveStatus && charlie.aliveStatus) || (bob.aliveStatus && charlie.aliveStatus)
while(totalAlive > 1) //2 or 3 are alive
{
totalAlive -= aimAndShoot(aaron, charlie, bob);
totalAlive -= aimAndShoot(bob, charlie, aaron);
totalAlive -= aimAndShoot(charlie, bob, aaron);
}
aaron.wins += aaron.aliveStatus;
bob.wins += bob.aliveStatus;
charlie.wins += charlie.aliveStatus;
}
std::cout << "\tAaron\tBob\tCharlie\n"
"Ratio:\t" << static_cast<double>(aaron.wins) / rounds << '\t' << static_cast<double>(bob.wins) / rounds << '\t' << static_cast<double>(charlie.wins) / rounds
<< "\nWins:\t" << aaron.wins << '\t' << bob.wins << '\t' << charlie.wins
<< "\nKills:\t" << aaron.kills << '\t' << bob.kills << '\t' << charlie.kills << std::endl;
return 0;
}
bool aimAndShoot(Gunman &shooter, Gunman &target1, Gunman &target2) //returns if they killed anyone
{
bool missed = true;
if(shooter.aliveStatus)
{
missed = rand() % shooter.hitOneInEvery;
if(!missed)
{
++shooter.kills;
if(target1.aliveStatus) //bob is alive
{
target1.aliveStatus = false;
}
else
{
target2.aliveStatus = false;
}
}
}
return !missed;
}
|
Aaron Bob Charlie
Ratio: 0.36 0.4183 0.2217
Wins: 3600 4183 2217
Kills: 6882 7546 5572 |