3 way duel problem

Adam, Bob, and Charlie agreed on a duel to the death (using paintball guns). Adam was a poor shot and only hit his target with a probability of 1/3. Bob hit his target with a probability of 1/2. Charlie never missed. A hit means a "kill" and the person hit drops out of the duel. They decided to fire in turns, staring with Adam, followed by Bob, and then Charlie. The cycle would repeat until one man was standing. An obvious and reasonably strategy is for each man to shoot at the most accurate opponent still alive.

Write a program to simulate the duel and this strategy. Your program should use this pseudo random number function (Xn+1 =(a*Xn)+c) where a = 22695477, c = 1, and Xn is a single unsigned integer parameter and the probabilities given in the problem to determine whether a shooter hits his target. Once you can simulate a single duel, add an outer loop that simulates 10,000 duels. Count the number of times that each contestant wins and display his winning percentage

Hint: Use the % operator to convert the pseudo-random numbers to a hit or miss
I did. Is this right then? I don't have C++ compilier on my laptop to check it.

#include <iostream>
using namespace std;

unsigned int pseudorand(unsigned int x);
char duel(int times);

int main()

{
int i;

char win;

double winners[3]={0,0,0};

int times;

cout<< "How many times do you want to play?"<<endl;

cin>> times;

for(i=0; i<times; i++)

{

win= duel(times);

if (win=='A')

{

winners[0] += 1;

}

else if (win=='B')

{

winners[1] += 1;

}

else

winners[2] += 1;

}

cout<< "Adam won " << (winners[0]/times)*100 << " percent of the time." << endl;

cout<< "Bob won " << (winners[1]/times)*100 << " percent of the time." << endl;

cout<< "Charlie won " << (winners[2]/times)*100 << " percent of the time." << endl;

}

char duel(int times)

{

int x, odds;

x=pseudorand(x);

odds=x%6;

if(odds<0)

{

odds=odds*(-1);

}

{

Winner="none";

while (Winner=="none")

{

x=pseudorand(x);

odds=x%6;

if(odds<0)

{

odds=odds*(-1);

}

if (odds<3)//Bob

{

Winner = "Bob";

return 'B';

}

else

{

x=pseudorand(x);

odds=x%6;

if(odds<0)

{

odds=odds*(-1);

}

{

return 'A';

}

}

}

}

else

{

x=pseudorand(x);

odds=x%6;

if(odds<0)

{

odds=odds*(-1);

}

if (odds<3)//Bob

{

Winner= "none";

while (Winner=="none")

{

x=pseudorand(x);

odds=x%6;

if(odds<0)

{

odds=odds*(-1);

}

{

return 'A';

}

else

{

x=pseudorand(x);

odds=x%6;

if(odds<0)

{

odds=odds*(-1);

}

if (odds<3)//Bob

{

Winner = "Bob";

return 'B';

}

}

}

}

else

{

x=pseudorand(x);

odds=x%6;

if(odds<0)

{

odds=odds*(-1);

}

{

return 'A';

}

else//Charlie

{

Winner = "Chralie";

return 'C';

}

}

}

}

unsigned int pseudorand( unsigned int x)
{
x=(22695477*x+1);
return x;
}
Sorry it got a little messed up when i copied it to this forum.
you can use the opening and closing code tags to post your code. This makes it a lot more readable and also keeps the formatting you used like follows:
 12345678910111213141516171819202122232425262728293031323334353637383940414243 int main() { int i; char win; double winners[3]={0,0,0}; int times; cout<< "How many times do you want to play?"<> times; for(i=0; i
If you are studying C++ consider downloading a C++ compiler. I use one of either MinGW, Visual C++ or GNU using Cygwin for projects I need to compile via Linux.

PS: Dont forget to return an integer in your main function return 0;
Last edited on
ok thank you!
Topic archived. No new replies allowed.