Tags:
create new tag
view all tags

Lab 4-2 - The Duel

Due: Oct 10, 11:55pm

Moodle Link

Yevgeny_Onegin_by_Repin.jpg

Instructions

  • 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.

Grading Table

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  

Problem - Duel Simulation

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:

void shoot(bool& targetAlive, double accuracy);
 

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.

Generating Random Numbers

Click here to see how to generate a random number between 0 and 1;

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!
c. In your main function, invoke the startDuel function 1,000 times in a loop, keeping track of how many times each contestant wins. Output the probability that each contestant will win when everyone uses the strategy of shooting at the most accurate shooter left alive. Thus you end up with a solution that shows the win scores for each (fake results here):
  • Aaron 354 wins
  • Bob 452 wins
  • Charlie 194 wins
d. A counterintuitive strategy is for Aaron to intentionally miss on his first shot. Thereafter, everyone uses the strategy of shooting at the most accurate shooter left alive. This strategy means that Aaron is guaranteed to live past the first round, since Bob and Charlie will fire at each other. Modify the program to accommodate this new strategy and output the probability of winning for each contestant.
Functions

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

void seedRandom();   // Seed the random number generator
double nextRandom(); // Get the next random number (between 0.0 and 1.0)
void shoot(bool& targetAlive, double accuracy); // Shoot with accuracy given, and set targetAlive to false if you hit.

What to do

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).

Turn In

  1. A run of each simluation strategy
  2. All source code

Solutions

  • Yevgeny_Onegin_by_Repin.jpg:
Topic attachments
I Attachment History Action Size Date Who Comment
JPEGjpg Yevgeny_Onegin_by_Repin.jpg r1 manage 134.2 K 2016-09-30 - 18:13 JimSkon  
Edit | Attach | Watch | Print version | History: r7 < r6 < r5 < r4 < r3 | Backlinks | Raw View | Raw edit | More topic actions
Topic revision: r7 - 2017-10-10 - JimSkon
 
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2018 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback