Game using rand()

I had a lab where we had to write a program to simulate 10,000 duels for Aaron who hit his target with a probability of 1/3. Bob who has a probability of 1/2. Charlie who never missed. A hit means a kill and the person hit drops out of the duel. Your program should use random numbers and the probabilities given in the problem to determine whether a shooter hits his target. To compensate for the inequities in their marksmanship skills, the three decided that they would fire in turns, starting with Aaron, followed by Bob, and then by Charlie. The cycle would repeat until there was one man standing.

(An obvious and reasonable strategy is for each man to shoot at the most accurate shooter still alive, on the grounds that this shooter is the deadliest and has the best chance of hitting back.)

I started writing this code and at first I was having problems with generating my random numbers that coincided with the players accuracy but now I am having trouble with my inner loop that eliminates a player only if they are hit, and you have to keep in mind a player might miss.

Sorry for the long problem. Hope you can help :\

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
 #include <iostream>
#include <stdlib.h>
#include <cstdlib>
#include <ctime>

using namespace std;

double rand1 (double accuracy);
bool aaron_shoot (double a, bool& charlie, bool& bob);
bool bob_shoot(double b, bool& charlie, bool& aaron);
bool charlie_shoot (double c, bool& bob, bool& aaron);

int main() {
 bool aaron;
 bool bob;
 bool charlie;
 int aaron_wins = 0, bob_wins = 0, charlie_wins = 0;
 
 srand(time(0));
    
    for ( int battles = 1; battles <= 10000; battles++){
     
    int fighters = 3; 
    while ( fighters > 1){   
         aaron = aaron_shoot(1.0/3.0, charlie, bob); 
               
         bob = bob_shoot (1.0/2.0, charlie, aaron);
         charlie = charlie_shoot (1.0, bob, aaron);   
         fighters--;
     }
     aaron_wins = aaron_wins + aaron;
     bob_wins = bob_wins + bob;  
//keeps track of the win at the end of each round of battles when 
//there is one player left standing           
charlie_wins = charlie_wins + charlie;                  
    }
cout << "Aaron won " << aaron_wins<< "/10000 duels or " << (aaron_wins/100)<< "%.\n";
cout << "Bob won " << bob_wins << "/10000 duels or " << (bob_wins/100)<<"%.\n";
cout << "Charlie won " << charlie_wins << "/10000 duels or " << (charlie_wins/100)<<"%.\n";

system ("Pause"); 
return 0;

}


bool aaron_shoot (double a, bool& charlie, bool& bob){    
     if (charlie == true){ //is alive
                 if (rand1 (a) >= a){
                           charlie = false; 
                                                     
                           }
                 }
     else if (bob == true){
                  if (rand1 (a) >= a){
                            bob = false;                                                 
                           }
                  }          
} 
bool bob_shoot (double b, bool& charlie, bool& aaron){
     if (charlie == true){
                 if (rand1 (b) >= b){
                            charlie = false;                           
                           }
                 }
     else if (aaron == true){
                  if (rand1 (b) >= b){
                            aaron = false;                           
                            }
                  }      
}   

bool charlie_shoot (double c, bool& bob, bool& aaron){
     if (bob == true){
                 if (rand1 (c) >= c){
                         bob = false;
                           }
                 }
     else if (aaron == true){
                  if (rand1 (c) >= c){
                          aaron = false;                            
                            }
                  }          
}

double rand1 (double accuracy){
       double r = rand ();            
       return (r / RAND_MAX) < accuracy;
       
}          
<_<

did you even RESEARCH rand?

You use it by: random_number = std::rand() % max_random_number;
closed account (1CfG1hU5)
in an older dos language I have, "#define RAND_MAX 0x7FFFU". where the U at the
end is unsigned. 7FFF is 32,767

is max_random_number same hex range? or a higher value?
Yes, I researched rand() and we learned about it in class. I thought my rand() function was correct.
nice problem.
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
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;
int main ()
{
	srand(time(0));
	bool aaron=1, bob =1, charlie=1; //alive by default
	int a, b, c;
	int ca=0, cb=0, cc=0;

	for (int i=0; i<10000; i++) {  //10,000 samples
		
		while (true) //true untill two are dead.
		{
		a=rand()%3; //aaron's accracy 1/3
		if(a==0) //kills if 0
			{
				if(charlie) charlie = 0; // kill charlie if he is alive
				else bob = 0; // otherwise kill bob
				if(bob==0 && charlie ==0) {	ca++; break;	}
			}
		
	
		b=rand()%2;
		if(b==0)
			{
				if(charlie) charlie=0;
				else aaron =0;
				if(aaron ==0 && charlie ==0) {	cb++; break;	}
			}			
		
			
		if(bob) {bob =0; continue;}
		else {aaron = 0; cc++; break;}		
		
		}
			
	} 
	cout << "nos winner\n";
	cout << "aaron: "<< ca << ", bob: " << cb << ", charlie: " << cc << endl;
	
	return 0;	
}
anup30 thanks, it is cool to see the program a different way.
I have a question thought, if (a==0) then it goes into the if statement, but why if the rand() is 0 if his accuracy is 1/3? and will will charlie automatically get hit? And how come charlie does not have an if statement or generate a random number?

What does nos stand for?

Thanks for your time, I am just starting out in programming and want to make sure I understand everything.
Last edited on
by line 17: a= 0 or 1 or 2. if you take either one of them (ie a=2) then by random number generation, aaron has 1/3 possiblity.
by taking a=0 only, i discarded a=2 and a=3.
so, a=0 makes sure that arron kills someone. (charlie is first choice)

line 26, bob has 50% probability, so b= 0 or 1.

charlie surely kills someone(3/3 probablity), so he doesn't need random number.
line 35 continues, because aaron is alive at that moment.
line 36 says charlie is the winner of that round.

thanks for bringing up the question.

nos (http://www.thefreedictionary.com/nos.)
With this program is there a chance that aaron or bob won't even hit a player?
the minimum possible shot triggerd in each round, is 2:
1. aaron kills charlie //a==0
2. bob kills aaron //b==0

the maximum possible shot in each round, is 5:
1. aaron misses //a==1 or a==2 // loop1
2. bob misses // b==1
3. charlie kills bob
4. aaron misses //a==1 or a==2 // loop2
// bob is dead
5. charlie kills aaron

so,
With this program is there a chance that aaron or bob won't even hit a player?

- when charlie wins.
Last edited on
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
//================================================================================
// Main.cpp
//--------------------------------------------------------------------------------
// 
//================================================================================
#include <iostream>
#include <cstdlib>
#include <vector>
#include <string>
#include <ctime>

using namespace std;

struct Duelist{
	string name;
	int prob;
	bool alive = true;
	float wins = 0;
	float losses = 0;
};



//================================================================================
// main()
//--------------------------------------------------------------------------------
// 
//================================================================================
int main()
{
	bool DuelOver = false;
	int target = -1;

	srand( time(0) );

	Duelist killaz[3];
	killaz[0].name = "Argyle";
	killaz[0].prob = 3;

	killaz[1].name = "Banjo";
	killaz[1].prob = 2;

	killaz[2].name = "Cooter";
	killaz[2].prob = 1;


	for( int i = 0; i < 10000; i++ )
	{
		// reset for next duel
		for( int j = 0; j < 3; j++ )
			killaz[j].alive = true;
		DuelOver = false;

		while( !DuelOver )
		{
			for( int j = 0; j < 3; j++ )
			{
				if( killaz[j].alive )
				{
					// pick target
					target = -1;
					for( int k = 0; k < 3; k++ )
					{
						if( k != j  &&  killaz[k].alive )
							target = k;
					}
					if( target == -1 )
					{
						killaz[j].wins++; // last man standing
						DuelOver = true;
						break;
					}

					// take the shot
					if( (rand() % killaz[0].prob) == 0 )
					{
						killaz[target].alive = false;
						killaz[target].losses++;
					}
				}
			}
		}
	}


	float pct;
	for( int i = 0; i < 3; i++ )
	{
		pct = 100 / ((killaz[i].wins + killaz[i].losses) / killaz[i].wins);
		cout << killaz[i].name << "  Wins: " << killaz[i].wins << "  Losses: " << killaz[i].losses << "  Win %:" << pct << endl;
	}
}
oops, i forgot to check if one is alive!
corrected:
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
// shooting game: whos alive
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;
int main ()
{
	srand(time(0));
	bool aaron=1, bob =1, charlie=1; //alive by default
	int a, b, c;
	int ca=0, cb=0, cc=0;

	for (int i=0; i<10000; i++)  //10,000 samples
	{		
		while (true) //true untill two are dead.
		{		
		if(aaron)
		{
		a=rand()%3; //aaron's accracy 1/3 
		if(a==0) //kills if 0
			{
				if(charlie) charlie = 0; // kill charlie if he is alive
				else bob = 0; // otherwise kill bob
				if(bob==0 && charlie ==0) {	ca++; break;	}
			}
		}		
	
		if(bob) 
		{
		b=rand()%2;
		if(b==0)
			{
				if(charlie) charlie=0;
				else aaron =0;
				if(aaron ==0 && charlie ==0) {	cb++; break;	}
			}		
		}
		
		if(charlie)
		  {
		   if(bob) {bob =0; }
		   else {aaron = 0; cc++; break;}	
		  }		
		}
		aaron=1, bob =1, charlie=1;
			
	}
		
	cout << "times alive count:\n";
	cout << "aaron: "<< ca << ", bob: " << cb << ", charlie: " << cc << endl;
	
	return 0;	
}
Guess the game theory analysis boils down to: "Practice makes you the prime target of all the mediocrities, so is pays to be a slacker"
Last edited on
Oddly enough, if you add more people to the mix using the same rules, the odds for the worst shooter stays about the same (even though he's only hitting 1 in 5 in this example). This seems to explain a lot about my chain of command while I was in the Navy. :)

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
//================================================================================
// Main.cpp
//--------------------------------------------------------------------------------
// 
//================================================================================
#include <iostream>
#include <cstdlib>
#include <vector>
#include <string>
#include <ctime>

using namespace std;

struct Duelist{
	string name;
	int prob;
	bool alive = true;
	float wins = 0;
	float losses = 0;
};

const int numKillaz = 5;


//================================================================================
// main()
//--------------------------------------------------------------------------------
// 
//================================================================================
int main()
{
	bool DuelOver = false;
	int target = -1;

	srand( time(0) );

	Duelist killaz[numKillaz];
	killaz[0].name = "Argyle";
	killaz[0].prob = 5;

	killaz[1].name = "Banjo";
	killaz[1].prob = 4;

	killaz[2].name = "Cooter";
	killaz[2].prob = 3;

	killaz[3].name = "Morton";
	killaz[3].prob = 2;

	killaz[4].name = "Quincy";
	killaz[4].prob = 1;

	for( int i = 0; i < 100000; i++ )
	{
		// reset for next duel
		for( int j = 0; j < numKillaz; j++ )
			killaz[j].alive = true;
		DuelOver = false;

		while( !DuelOver )
		{
			for( int j = 0; j < numKillaz; j++ )
			{
				if( killaz[j].alive )
				{
					// pick target
					target = -1;
					for( int k = 0; k < numKillaz; k++ )
					{
						if( k != j  &&  killaz[k].alive )
							target = k;
					}
					if( target == -1 )
					{
						killaz[j].wins++; // last man standing
						DuelOver = true;
						break;
					}

					// take the shot
					if( (rand() % killaz[0].prob) == 0 )
					{
						killaz[target].alive = false;
						killaz[target].losses++;
					}
				}
			}
		}
	}


	float pct;
	for( int i = 0; i < numKillaz; i++ )
	{
		pct = 100 / ((killaz[i].wins + killaz[i].losses) / killaz[i].wins);
		cout << killaz[i].name << "  Wins: " << killaz[i].wins << "  Losses: " << killaz[i].losses << "  Win %:" << pct << endl;
	}
}
Last edited on
hello Esslercuffi, my program shows survival percentage approx. 36% 41% 22% for aaron bob charlie respectively. but you program shows approx. 50% 41% 8%
evidently one algorithm is wrong.(or both)

what about 2 player match? player1 has 50% accuracy, player2 has 100%.
player1 shoots first. its easy to understand both have 50% chance of survival.
does your program shows it ??

// shooting game: two player verson of anup30's code
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
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;
int main ()
{
	srand(time(0));
	bool aaron=1, bob =1;  //alive by default
	int a, b;
	int ca=0, cb=0;
double sample = 1e7;
	for (int i=0; i<sample; i++)  
	{		
		while (true) //true untill two are dead.
		{		
		if(aaron)
		{
		a=rand()%2; //aaron's accracy 1/2
		if(a==0) //kills if 0
			{				
			 bob = 0; // kill bob
			 ca++; break;
			}
		}		
	
		if(bob) 
		{
			 aaron =0;
			cb++; break;				
		}		
		
		}
		aaron=1, bob =1;
			
	}
	
if(!(ca+cb)==sample)	cout << "program error!\n";	
	cout << "alive count :\n\n";
	cout << "aaron: \t "<< ca << "\t alive percentage: " << ca*100/sample
		<< "\nbob: \t " << cb << "\t alive percentage: " << cb*100/sample	
		<< endl;
		
return 0;	
}

output:
aaron 49.9996 %
bob   50.0004 %



// shooting game: two player verson of Esslercuffi's code
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
#include <iostream>
#include <cstdlib>
#include <vector>
#include <string>
#include <ctime>

using namespace std;

struct Duelist{
	string name;
	int prob;
	bool alive;
	float wins;
	float losses;
};

// main()
int main()
{
	bool DuelOver = false;
	int target = -1;

	srand( time(NULL) );

	Duelist killaz[3];
	killaz[0].name = "Argyle";
	killaz[0].prob = 2;
	killaz[0].wins = 0;
	killaz[0].losses = 0;
	killaz[0].alive = true;

	killaz[1].name = "Banjo";
	killaz[1].prob = 1;
	killaz[1].wins = 0;
	killaz[1].losses = 0;	
	killaz[1].alive = true;
	

double sample = 1e7;
	for( int i = 0; i < sample; i++ )
	{
		// reset for next duel
		for( int j = 0; j < 2; j++ )
			killaz[j].alive = true;
		DuelOver = false;

		while( !DuelOver )
		{
			for( int j = 0; j < 2; j++ )
			{
				if( killaz[j].alive )
				{
					// pick target
					target = -1;
					for( int k = 0; k < 2; k++ )
					{
						if( k != j  &&  killaz[k].alive )
							target = k;
					}
					if( target == -1 )
					{
						killaz[j].wins++; // last man standing
						DuelOver = true;
						break;
					}

					// take the shot
					if( (rand() % killaz[0].prob) == 0 )
					{
						killaz[target].alive = false;
						killaz[target].losses++;
					}
				}
			}
		}
	}

if( !(killaz[0].wins + killaz[1].wins  )==sample )	cout << "program error!\n";	
	float pct;
	for( int i = 0; i < 2; i++ )
	{
		pct = 100 / ((killaz[i].wins + killaz[i].losses) / killaz[i].wins);
		cout << killaz[i].name << "  Wins: " << killaz[i].wins << "  Losses: " << killaz[i].losses << "  Win %:" << pct << endl;
	}
		
return 0;	
}


output:
Argyle  66.6103 %
Banjo   33.3897 %
hmmm... that's peculiar.
Somehow, Banjo is missing.

<edit> found it... problem is killaz[0].prob in hit test should be killaz[j].prob.

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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
//================================================================================
// Main.cpp
//--------------------------------------------------------------------------------
// 
//================================================================================
#include <iostream>
#include <cstdlib>
#include <vector>
#include <string>
#include <ctime>
using namespace std;

const int numKillaz = 2;

struct Duelist{
	string name;
	int prob;
	bool alive = true;
	float wins = 0;
	float losses = 0;
	int hits = 0;
	int misses = 0;
};

int TargetBest( Duelist killaz[], int self );
int TargetWorst( Duelist killaz[], int self );
int TargetRandom( Duelist killaz[], int self );
int GetRand( int min, int max );


//================================================================================
// main()
//--------------------------------------------------------------------------------
// 
//================================================================================
int main()
{
	bool DuelOver = false;
	int target = -1;

	srand( time(0) );

	Duelist killaz[numKillaz];
	killaz[0].name = "Argyle";
	killaz[0].prob = 2;
	killaz[1].name = "Banjo";
	killaz[1].prob = 1;
/*	killaz[2].name = "Cooter";
	killaz[2].prob = 8;
	killaz[3].name = "Morton";
	killaz[3].prob = 7;
	killaz[4].name = "Quincy";
	killaz[4].prob = 6;
	killaz[5].name = "Lester";
	killaz[5].prob = 5;
	killaz[6].name = "Purvis";
	killaz[6].prob = 4;
	killaz[7].name = "Franco";
	killaz[7].prob = 3;
	killaz[8].name = "Cleetus";
	killaz[8].prob = 2;
	killaz[9].name = "Ralph";
	killaz[9].prob = 1; */

	for( int i = 0; i < 100000; i++ )
	{
		// reset for next duel
		for( int j = 0; j < numKillaz; j++ )
			killaz[j].alive = true;
		DuelOver = false;

		while( !DuelOver )
		{
			for( int j = 0; j < numKillaz; j++ )
			{
				if( killaz[j].alive )
				{
					target = -1;
					target = TargetBest( killaz, j );
					//target = TargetWorst( killaz, j );
					//target = TargetRandom( killaz, j );
					if( target == -1 ) // No targets left, j wins.
					{
						killaz[j].wins++;
						DuelOver = true;
						break;
					}

					// take the shot
					if( (rand() % killaz[0].prob) == 0 )
					{
						killaz[j].hits++;
						killaz[target].alive = false;
						killaz[target].losses++;
					}
					else
						killaz[j].misses++;
				}
			}
		}
	}


	float pct;
	for( int i = 0; i < numKillaz; i++ )
	{
		pct = 100 / ((killaz[i].wins + killaz[i].losses) / killaz[i].wins);
		cout << killaz[i].name 
			<< "  Wins: "   << killaz[i].wins 
			<< "  Losses: " << killaz[i].losses 
			<< "  Win %: "  << pct 
			<< "  Hits: " << killaz[i].hits
			<< "  Misses: " << killaz[i].misses
			<< endl;
	}
}


//================================================================================
// TargetBest()
//--------------------------------------------------------------------------------
// Pick the best shooter still standing as target
//================================================================================
int TargetBest( Duelist killaz[], int self )
{
	int target = -1;
	for( int k = 0; k < numKillaz; k++ )
	{
		if( k != self  &&  killaz[k].alive )
			target = k;
	}

	return target;
}


//================================================================================
// TargetWorst()
//--------------------------------------------------------------------------------
// Pick the worst shooter still standing as target
//================================================================================
int TargetWorst( Duelist killaz[], int self )
{
	int target = -1;
	for( int k = numKillaz-1; k >= 0; k-- )
	{
		if( k != self  &&  killaz[k].alive )
			target = k;
	}

	return target;
}


//================================================================================
// TargetRandom()
//--------------------------------------------------------------------------------
// Pick a random target
//================================================================================
int TargetRandom( Duelist killaz[], int self )
{
	int target = -1;
	bool valid = false;

	// check if there are valid targets left
	for( int i = 0; i < numKillaz; i++ )
	{
		if( i != self && killaz[i].alive )
			valid = true;
	}
	if( !valid )
		return -1;

	int tmp = -1;
	while( target == -1 )
	{
		tmp = GetRand( 0, numKillaz-1 );
		if( tmp != self  && killaz[tmp].alive )
			target = tmp;
	}

	return target;
}


//=============================================================================
// GetRand()                                                                   
//-----------------------------------------------------------------------------
// Return a random integer in the range of min to max                          
//=============================================================================
int GetRand( int min, int max )
{
	return (rand() % (max - min + 1)) + min;
}
Last edited on
My code isn't that neat, sorry don't have a whole lot of time to spare but here is a sample I have created. There are probably a few tricks you can to improve it :)

http://coliru.stacked-crooked.com/a/b2791bca1f007ca9
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


By the way there was actually the same question about 1 year ago :P
http://www.cplusplus.com/forum/beginner/99976/
so, the solution is 36% 42% 22% (approx.)
at last three method have been correct: me, Esslercuffi and giblit.
thanks bs319 for bringing this up. sorry didn't try how to make your code work - but you get the ideas.
lol... ended up wasting the whole evening playing with this silly thing and ignored the OP entirely.

btw, anup

the maximum possible shot in each round, is 5:
1. aaron misses //a==1 or a==2 // loop1
2. bob misses // b==1
3. charlie kills bob
4. aaron misses //a==1 or a==2 // loop2
// bob is dead
5. charlie kills aaron


not quite true, if bob kills charlie, aaron and bob could end up trading misses for a while.
you are right! it has been a sleepless night. its 9:30am here - going to ....sleep
Topic archived. No new replies allowed.