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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
void shoot(bool& targetalive, double accuracy);
//simulates one shot
int startDuel();
// returns a winner for each iteration
bool charlielive, boblive, aaronlive; //global variables for ease of use in functions
double characc= 1, bobacc= 0.5, aaronacc= (1/3.0); //whether the player is alive or dead and their accuracies
int main()
{
srand(time(0)); //seeds for the randomizer in void function
double charpercent,bobpercent,aaronpercent;
int charwin=0, bobwin=0,aaronwin=0,i,winner,alldead=0;
for (i=1;i<=1000;i++) //loops 1000 times
{
charlielive=true; //resets all players as living
boblive=true;
aaronlive=true;
winner= startDuel(); //plays one round
if (winner == 1) //adds to an accumulator the winner
aaronwin += 1;
if (winner == 2)
bobwin += 1;
if (winner == 3)
charwin += 1;
if (winner == 4)
alldead += 1;
}
charpercent=(charwin / 1000.0) * 100; //calculates percentage
bobpercent=(bobwin / 1000.0) * 100;
aaronpercent=(aaronwin / 1000.0) * 100;
cout<< "Aaron's Wins: " << aaronwin << endl; //displays results
cout<< "Bob's Wins: " << bobwin << endl;
cout<< "Charlie's Wins: " << charwin << endl << endl;
cout<< "Aaron's Win Percentage: " << aaronpercent << endl;
cout<< "Bob's Win Percentage: " << bobpercent << endl;
cout<< "Charlie's Win Percentage: " << charpercent << endl << endl;
return 0;
}
int startDuel()
{
int result=0;
//while loop checking if all or at least 2 players are alive
while (((charlielive==true) && (aaronlive==true) && (boblive==true)) || ((charlielive==true) && (aaronlive==true) && (!boblive==true)) ||
((aaronlive==true) && (boblive==true) && (!charlielive==true)) || ((charlielive==true) && (boblive==true) && (!aaronlive==true)))
{
if ((charlielive==true) && (aaronlive==true) && (boblive==true))
{
shoot(charlielive, aaronacc); // if all alive aaron shoots charlie if charlie lives bob shoots charlie if aaron kills charlie
if (charlielive==true) // bob shoots at aaron
shoot(charlielive, bobacc);
else
shoot(aaronlive,bobacc);
if (charlielive==true)
shoot(boblive, characc);
}
if ((!charlielive==true) && (boblive==true) && (aaronlive==true))
{
shoot(boblive, aaronacc); //for the rest if two players live than the first shoots and
if (boblive==true) // if the second is alive he shoots back
shoot(aaronlive, bobacc); //breaks loop if only one is alive (recently added didn't affect results)
else
break;
}
if ((charlielive==true) && (!boblive==true) && (aaronlive==true))
{
shoot(charlielive, aaronacc);
if (charlielive==true)
shoot(aaronlive, characc);
else
break;
}
if ((charlielive) && (!aaronlive==true) && (boblive==true))
{
shoot(charlielive, bobacc);
if (charlielive==true)
shoot(boblive, characc);
else
break;
}
}
if ((!charlielive==true) && (!boblive==true) && (aaronlive==true))
result=1;
else if ((!charlielive==true) && (boblive==true) && (!aaronlive==true))
result=2;
else if ((charlielive==true) && (!boblive==true) && (!aaronlive==true))
result=3;
return result;
}
void shoot(bool& targetalive, double accuracy)
{
double attack;
attack=(rand() % 100) * .01; //picks random number between 0 and 1
if (attack < accuracy) //if attack is less than accuracy targetplayer is dead
targetalive= false;
}
|