Homework #3 Solution

$30.00

Description

Description:

In the land of Puzzlevania, Aaron, Bob, and Charlie had an argument over which one of them was the greatest puzzle-solver of all time. To end the argument once and for all, they agreed on a duel to the death (does this make sense?). Aaron was a poor shot and only hit his target with a probability of 1/3. Bob was a bit better and hit his target with a probability of 1/2. Charlie was an expert marksman and never missed. A hit means a kill and the person hit drops out of the duel.

To compensate for the inequities in their marksmanship skills, the three decided that they would fire in turns, starting with Aaron, followed by Bob, and then by Charlie. The cycle would repeat until there was one man standing. That man would be remembered for all time as the Greatest Puzzle-Solver of All Time.

An obvious and reasonable 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 program to simulate the duel using this strategy. Your program should use random numbers and the probabilities given in the problem to determine whether a shooter hits his target. You will likely want to create multiple functions to complete the problem. My solution had only one function to simulate the duels and it passed in the odds and the three guys as call-by-reference parameters. Once you can simulate a duel, add a loop to your program that simulates 10,000 duels. Count the number of times that each contestant wins and print the probability of winning for each contestant (e.g., for Aaron yours might output “Aaron won 3612/10000 duels or 36.12%).

Strategy 2: An alternate strategy is for Araon to intentionally miss on his first shot. Write a function to simulate Strategy 2. Your program will determine which strategy is better for Aaron.

Page 1 of 8

Note: You must provide the following user interface. Red text might be different for different runs due to the random number. You do not need to display text in red. Please replace “Aubie” with your name.

*** Welcome to Aubie’s Duel Simulator ***

Unit Testing 1: Function – at_least_two_alive()

Case 1: Aaron alive, Bob alive, Charlie alive

Case passed …

Case 2: Aaron dead, Bob alive, Charlie alive

Case passed …

Case 3: Aaron alive, Bob dead, Charlie alive

Case passed …

Case 4: Aaron alive, Bob alive, Charlie dead

Case passed …

Case 5: Aaron dead, Bob dead, Charlie alive

Case passed …

Case 6: Aaron dead, Bob alive, Charlie dead

Case passed …

Case 7: Aaron alive, Bob dead, Charlie dead

Case passed …

Case 8: Aaron dead, Bob dead, Charlie dead

Case passed …

Press any key to continue…

Unit Testing 2: Function Aaron_shoots1(Bob_alive, Charlie_alive)

Case 1: Bob alive, Charlie alive

Aaron is shooting at Charlie

Aaron misses.

Case 2: Bob dead, Charlie alive

Aaron is shooting at Charlie

Charlie is dead.

Case 3: Bob alive, Charlie dead

Aaron is shooting at Bob

Aaron misses.

Press any key to continue…

Unit Testing 3: Function Bob_shoots(Aaron_alive, Charlie_alive)

Case 1: Aaron alive, Charlie alive

Bob is shooting at Charlie

Charlie is dead.

Case 2: Aaron dead, Charlie alive

Bob is shooting at Charlie

Bob missed

Case 3: Aaron alive, Charlie dead

Bob is shooting at Aaron

Aaron is dead.

Press any key to continue…

Page 2 of 8

Unit Testing 4: Function Charlie_shoots(Aaron_alive, Charlie_alive)

Case 1: Aaron alive, Bob alive

Charlie is shooting at Bob

Bob is dead.

Case 2: Aaron dead, Bob alive

Charlie is shooting at Bob

Bob is dead

Case 3: Aaron alive, Bob dead

Charlie is shooting at Aaron

Aaron is dead.

Press any key to continue…

Unit Testing 5: Function Aaron_shoots2(Bob_alive, Charlie_alive)

Case 1: Bob alive, Charlie alive

Aaron intentionally misses his first shot Both Bob and Charlie are alive.

Case 2: Bob dead, Charlie alive

Aaron is shooting at Charlie

Charlie is dead.

Case 3: Bob alive, Charlie dead

Aaron is shooting at Bob

Aaron misses.

Press any key to continue…

Ready to test strategy 1 (run 10000 times):

Press any key to continue…

Aaron won 3612/10000 duels or 36.12%

Bob won 4137/10000 duels or 41.37%

Charlie won 2251/10000 duels or 22.51%

Ready to test strategy 2 (run 10000 times):

Press any key to continue…

Aaron won 4099/10000 duels or 40.99%

Bob won 2552/10000 duels or 25.52%

Charlie won 3349/10000 duels or 33.49%

Strategy 2 is better than strategy 1.

Page 3 of 8

Requirements:

  1. You must follow the above user interface to implement your program.

  1. You must implement the following functions:

  1. bool at_least_two_alive(bool A_alive, bool B_alive, bool C_alive)

/* Input: A_alive indicates whether Aaron is alive */

/*

B_alive

indicates

whether

Bob is alive */

/*

C_alive

indicates

whether

Charlie is alive */

/* Return: true if at least

two are

alive */

/*

otherwise return

false */

  1. void Aaron_shoots1(bool& B_alive, bool& C_alive) /* Strategy 1: Use call by reference

    • Input: B_alive indicates whether Bob alive or dead

    • C_alive indicates whether Charlie is alive or dead

    • Return: Change B_alive into false if Bob is killed.

    • Change C_alive into false if Charlie is killed.

*/

  1. void Bob_shoots(bool& A_alive, bool& C_alive) /* Call by reference

    • Input: A_alive indicates if Aaron is alive or dead

    • C_alive indicates whether Charlie is alive or dead

    • Return: Change A_alive into false if Aaron is killed.

    • Change C_alive into false if Charlie is killed.

*/

  1. void Charlie_shoots(bool& A_alive, bool& B_alive) /* Call by reference

    • Input: A_alive indicates if Aaron is alive or dead

    • B_alive indicates whether Bob is alive or dead

    • Return: Change A_alive into false if Aaron is killed.

    • Change B_alive into false if Bob is killed.

*/

    1. void Aaron_shoots2(bool& B_alive, bool& C_alive) /* Strategy 2: Use call by reference

      • Input: B_alive indicates whether Bob is alive or dead

      • C_alive indicates whether Charlie is alive or dead

      • Return: Change B_alive into false if Bob is killed.

      • Change C_alive into false if Charlie is killed.

*/

  1. You must implement five unit test drivers (five functions) to test the above five functions. (see also the user interface on pages 2 and 3)

  1. You must use the assert macro in your test driver.

  1. You must define four constants in your implementation. For example, the total number (i.e., 10,000) of runs can be defined as a constant.

Hints:

  1. How to implement Press any key to continue…”

cout << “Press any key to continue…”;

cin.ignore().get(); //Pause Command for Linux Terminal

Note: you can implement the above two lines as a function to be repeatedly called by other functions in your program.

  1. You may need to use the following libraries.

    • include <iostream>

    • include <cstdlib>

    • include <cassert>

    • include <ctime>

  1. Initialize your random number generator as below:

srand(time(0));

4. A sample code of generating a random number is given below:

/* Assume that the probabilty of hit a target is 25 percent */ int shoot_target_result;

shoot_target_result = rand()%100;

if (shoot_target_result <= 25) {

cout<<“Hit the target\n”;

/* add more code here */

}

5. Please follow the following sample test driver to implement all the test drivers.

void test_at_least_two_alive(void) {

cout << “Unit Testing 1: Function – at_least_two_alive()\n”;

cout << “Case 1: Aaron alive, Bob alive, Charlie alive\n”; assert(true == at_least_two_alive(true, true, true)); cout << “Case passed …\n”;

cout << “Case 2: Aaron dead, Bob alive, Charlie alive\n”; assert(true == at_least_two_alive(false, true, true)); cout << “Case passed …\n”;

cout << “Case 3: Aaron alive, Bob dead, Charlie alive\n”; assert(true == at_least_two_alive(true, false, true)); cout << “Case passed …\n”;

Page 5 of 8

/* add test cases 4-6 below */

}

  1. If your strategy 1 is better than your strategy 2, then please check your source code. In this case, it is likely that you have incorrectly implemented strategy 2. Please ensure that strategy 2 is better than strategy 1.

  1. The framework of the source code is given below:

  • include <iostream>

  • include <cstdlib>

  • include <cassert>

  • include <ctime> using namespace std;

//prototypes

bool at_least_two_alive(bool A_alive, bool B_alive, bool C_alive); /* Input: A_alive indicates whether Aaron is alive */

/*

B_alive indicates

whether Bob is alive */

/*

C_alive indicates

whether Charlie is alive */

/* Return: true if at least

two are alive */

/*

otherwise return

false */

/*

  • Add other function prototypes below */

void test_at_least_two_alive(void);

/* This is a test driver for at_least_two_alive() */

/*

  • Add more prototypes for other test drivers below */

int main()

{

/*

  • This is the main function

*/

}

/* Implementation of at_least_two_alive() */

bool at_least_two_alive(bool a_alive, bool b_alive, bool c_alive) {

/* add the implementation below */

}

Page 6 of 8

/* Implementation of the test driver for at_least_two_alive() */ void test_at_least_two_alive(void) {

cout << “Unit Testing 1: Function – at_least_two_alive()\n”;

cout << “Case 1: Aaron alive, Bob alive, Charlie alive\n”; assert(true == at_least_two_alive(true, true, true));

cout << “Case passed …\n”;

cout << “Case 2: Aaron dead, Bob alive, Charlie alive\n”; assert(true == at_least_two_alive(false, true, true));

cout << “Case passed …\n”;

/* add test cases 3-6 below */

}

Programming Environment:

Write a program in C++. Compile and run it using the g++ compiler on a Linux box (either in computer labs in Shelby, your home Linux machine, a Linux box on a virtual machine, or using an emulator like Cygwin).

Grading:

  1. (5 points) Use comments to provide a heading at the top of your code containing your name, Auburn Userid, filename, and how to compile your code. Also describe any help or sources that you used (as per the syllabus).

  2. (5 points) Your source code file should be named as “hw3.cpp”.

  3. (20 points) You must follow the specified user interface.

  4. (5 points) Define at least four constants.

  5. (25 points) Implement five functions.

  6. (20 points) Implement five test drivers for the functions.

  7. (5 points) You must use assert() in one of your test drivers.

  8. (5 points) Your program must compare strategy 1 with strategy 2.

  1. (10 points) Quality of your source code.

Note: You will lose at least 40 points if there are compilation errors or warning messages when we compile your source code. You will lose points if you: do not use the specific program file name, or do not have comments, or do not have a comment block on EVERY program you hand in.

Deliverables:

  • Submit your source code file named as “hw3.cpp” through Canvas.

Late Submission Penalty:

  • 10 points penalty per day for late submission. For example, an assignment submitted after the deadline but up to 1 day (24 hours) late can achieve a maximum of 90% of points allocated for the assignment. An assignment submitted after the deadline but up to 2 days (48 hours) late can achieve a maximum of 80% of points allocated for the assignment.

  • Assignment submitted more than 3 days (72 hours) after the deadline will not be graded.

Rebuttal period:

  • You will be given a period of 7 days to read and respond to the comments and grades of your homework or project assignment. The TA may use this opportunity to address any concern and question you have. The TA also may ask for additional information from you regarding your homework or project.

Page 8 of 8


error: Content is protected !!