I had a lab where we had to write a program to simulate the duel, for Aaron who a has probability of 1/3, Bob 1/2, Charlie never misses. The program should use random numbers and the probabilities given in the problem to determine whether a shooter hits his target, simulate 10,000 duels.
First time with rand and having some problems with my functions, my professor told me to put the %accuracy so you would only need one function and it would input the players accuracy from the function call, but I am getting an error: invalid operands of types int' and
double' to binary `operator%', I am confused because accuracy is a number.
I am also having trouble with the while loop because 'fighters' should only subtract one when a player is shot and there will be times when a player is not shot.
#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, bob, 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(0.333, charlie, bob);
bob = bob_shoot (0.5, charlie, aaron);
charlie = charlie_shoot (1.0, bob, aaron);
//need an argument to make sure a shooter does not shoot if they are dead, and to count how many are left if there is a hit
} //keeps track of the win at the end of each round of battles when
//there is one player left standing
aaron_wins = aaron_wins + aaron;
bob_wins = bob_wins + bob;
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){
return (charlie = false);
}
}
else if (bob == true){
if (rand1 (a) >= a){
return (bob = false);
}
}
}
bool bob_shoot (double b, bool& charlie, bool& aaron){
if (charlie == true){
if (rand1 (b) >= b){
return (charlie = false);
}
}
else if (aaron == true){
if (rand1 (b) >= b){
return (aaron = false);
}
}
}
bool charlie_shoot (double c, bool& bob, bool& aaron){
if (bob == true){
if (rand1 (c) >= c){
return (bob = false);
}
}
else if (aaron == true){
if (rand1 (c) >= c){
return (aaron = false);
}
}
}
double rand1 (double accuracy){
double r = rand ();
return (r / RAND_MAX) < accuracy;
}
double rand1 (double accuracy){
srand(time(0));
int a = rand ()% accuracy; <-- THIS
double b = ((double)a / RAND_MAX);
return b;
}
I'm not sure what you think that line does, but it doesn't make any sense. If you want to keep the accuracies as fractions, use:
double r = rand ();
if ( (r / RAND_MAX) < accuracy)
Also, don't call srand
more than once. Otherwise, two calls to rand1
in the same second will get the same return value from rand
.