Dodgeball Game (Charlie never wins)
Oct 13, 2016 at 1:57am UTC
I made this dodgeball game and it keeps printing out that Charlie never wins. It prints "Charlie won 0% of the time". He is supposed to get around 22% wins and I can't figure out why he keeps getting 0%.
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
#include <iostream>
#include <cstdlib>
#include <ctime>
void shoot(bool & targetAlive, double accuracy);
char startDuel();
int main()
{
srand((unsigned int ) time(0));
// for (int i = 0; i < 10; i++)
// {
// bool targetAlive = true;
// shoot(targetAlive, 0.5);
// std::cout << targetAlive << std::endl;
// }
int aaronWins = 0;
int bobWins = 0;
int charlieWins = 0;
for (int i = 0; i < 1000; i++)
{
char winner = startDuel();
switch (winner)
{
case 'a' :
aaronWins++;
break ;
case 'b' :
bobWins++;
break ;
case 'c' :
charlieWins++;
break ;
}
}
std::cout << "Aaron won " << (aaronWins / 10.0) << " % of the time" << std::endl;
std::cout << "Bob won " << (bobWins / 10.0) << " % of the time" << std::endl;
std::cout << "Charlie won " << (charlieWins / 10.0) << " % of the time" << std::endl;
char ch;
std::cin >> ch;
return 0;
}
void shoot(bool & targetAlive, double accuracy)
{
double r = (rand() % 1000) / 1000.0;
if (r < accuracy)
{
targetAlive = false ;
}
}
char startDuel()
{
bool aaron = true ;
bool bob = true ;
bool charlie = true ;
while ((aaron && bob) || (aaron && charlie) || (bob && charlie))
{
if (aaron)
{
if (charlie)
{
shoot(charlie, 0.3333);
}
else if (bob)
{
shoot (bob, 0.3333);
}
}
if (bob)
{
if (charlie)
{
shoot(charlie, 0.5);
}
else if (aaron)
{
shoot (aaron, 0.5);
}
}
if (charlie)
{
if (bob)
{
shoot(bob, 1.0);
}
else if (aaron)
{
shoot (aaron, 1.0);
}
}
if (aaron)
{
return 'a' ;
}
else if (bob)
{
return 'b' ;
}
else
{
return 'c' ;
}
}
}
Oct 13, 2016 at 7:50am UTC
Move the if
on line 99 outside of the while
loop (after line 111).
Topic archived. No new replies allowed.