## Lab 4-2 - The Duel## Due: Feb 28, 11:55pm |

- Turn in the code (a cpp file or ideone.com link), and the run outputs as requested below.
- Remember to format the code as described and the book and text, and to include comments including complete commetns at the beginning of the program.

Requirement | Grading Comments | Points | Score |
---|---|---|---|

Run examples with correct output | 70 | ||

Good clean, easy to understand input and output operations | 10 | ||

Code is appropriartely broken up into functions. | 10 | ||

Complete source code with good formatting and organization, variables names, and comments | 10 | ||

Total | 100 |

In the land of Puzzlevania, Aaron, Bob, and Charlie had an argument over which one of them was the greatest puzzler of all time. To end the argument once and for all, they agreed on a duel to the death. Aaron is a poor shooter and only hits his target with a probability of 1/3. Bob is a bit better and hits his target with a probability of 1/2. Charlie is an expert marksman and never misses. A hit means a kill and the person hit drops out of the duel.

To compensate for the inequities in their marksmanship skills, it is decided that the contestants would fire in turns starting with Aaron, followed by Bob, then by Charlie, then back to Aaron. The cycle would repeat until there was only one man standing. And that man would be remembered as the greatest puzzler of all time.

a. Write a function to simulate a single shot. It should use the following declaration:

voidshoot(bool& targetAlive,doubleaccuracy);

This would simulate someone shooting at targetAlive with the given accuracy by generating a random number between 0 and 1. If the random number is less than accuracy , then the target is hit and targetAlive should be set to false . Below is a link that illustrates how to generate random numbers.

You should write a function to seed the random number generator, and another to return the next random number between 0 and 1.

So, for example, if Bob is shooting at Charlie, this could be invoked as:

shoot(charlieAlive, 0.5);

Here, charlieAlive is a Boolean variable that indicates if Charlie is alive. *Test your function using a driver program before moving on to step b*.

b. An obvious 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. Write a second function named startDuel that uses the shoot function to simulate an entire duel using this strategy. The function will cycle through the men in the given order (Aaron, Bob, Charlie), dropping each men out of the conetest when they die.

It should loop until only one contestant is left, invoking the shoot function with the proper target and probability of hitting the target according to who is shooting. The function should return a variable that indicates who won the duel.

For example, you might have the series:

- Aaron shoots at Charlie, misses
- Bob shoots at Charlie, misses
- Charlie shoots at Bob, hits (Bob dies)
- Aaron shoots at Charlie, hits (Charlie dies)
- Aaron wins!

- Aaron 354 wins
- Bob 452 wins
- Charlie 194 wins

Your code should be broken up into functions to simplify it. I would suggest the following routines to start:

voidseedRandom();// Seed the random number generatordoublenextRandom();// Get the next random number (between 0.0 and 1.0)voidshoot(bool& targetAlive,doubleaccuracy);// Shoot with accuracy given, and set targetAlive to false if you hit.

You can either write two similar programs that use the different strategies, or you can write a single program with two versions of duel, e.g. duel1 (the first strategy) and duel2 (the second strategy), and have it first run the test with duel1, then with duel2.

After each set of 1000 duels, print the probability that each contestant will win for that strategy. Example Run:

Strategy one: always shoot at the best shot. Out of a 1000 duels: Aaron wins 374 times, probability of winning 0.374 Bob wins 419 times, probability of winning 0.419 Charlie wins 207 times, probability of winning 0.207 Strategy two: always shoot at the best shot. Aaron misses first shot Out of a 1000 duels: Aaron wins 356 times, probability of winning 0.356 Bob wins 422 times, probability of winning 0.422 Charlie wins 222 times, probability of winning 0.222

(The above answers are examples, not the correct numbers).

- A run of each simluation strategy
- All source code

- Yevgeny_Onegin_by_Repin.jpg:

This topic: SCMP118/Fall2016 > WebHome > Lab4-2

Topic revision: r5 - 2017-01-17 - JimSkon

Copyright © 2008-2020 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.

Ideas, requests, problems regarding TWiki? Send feedback

Ideas, requests, problems regarding TWiki? Send feedback