3 way duel problem

Oct 30, 2012 at 11:27pm
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
Oct 31, 2012 at 12:47am
Do your own homework.
Oct 31, 2012 at 12:52am
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)

{

string Adam, Bob, Charlie, Winner;

int x, odds;



x=pseudorand(x);

odds=x%6;

if(odds<0)

{

odds=odds*(-1);

}


if (odds<2)//Adam

{

Charlie= "Dead";


Winner="none";

while (Winner=="none")

{

x=pseudorand(x);

odds=x%6;

if(odds<0)

{

odds=odds*(-1);

}

if (odds<3)//Bob

{

Adam = "Dead";

Winner = "Bob";

return 'B';

}

else

{

x=pseudorand(x);

odds=x%6;

if(odds<0)

{

odds=odds*(-1);

}

if (odds<2)//Adam

{

Bob = "Dead";

Winner = "Adam";

return 'A';

}

}

}

}

else

{

x=pseudorand(x);

odds=x%6;

if(odds<0)

{

odds=odds*(-1);

}

if (odds<3)//Bob

{

Charlie = "Dead";

Winner= "none";

while (Winner=="none")

{

x=pseudorand(x);

odds=x%6;

if(odds<0)

{

odds=odds*(-1);

}

if (odds<2)//Adam

{

Bob = "dead";

Winner = "Adam";

return 'A';

}

else

{

x=pseudorand(x);

odds=x%6;

if(odds<0)

{

odds=odds*(-1);

}

if (odds<3)//Bob

{

Adam = "Dead";

Winner = "Bob";

return 'B';

}

}

}

}

else

{

Bob = "Dead";

x=pseudorand(x);

odds=x%6;

if(odds<0)

{

odds=odds*(-1);

}

if (odds<2)//Adam

{

Charlie = "dead";

Winner = "Adam";

return 'A';

}

else//Charlie

{

Adam = "dead";

Winner = "Chralie";

return 'C';

}

}

}


}








unsigned int pseudorand( unsigned int x)
{
x=(22695477*x+1);
return x;
}
Oct 31, 2012 at 12:57am
Sorry it got a little messed up when i copied it to this forum.
Oct 31, 2012 at 1:07am
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:
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
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;


}
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 Oct 31, 2012 at 1:09am
Oct 31, 2012 at 1:11am
ok thank you!
Topic archived. No new replies allowed.