Assignment 06

Due: Friday, Oct. 13, 2017 at noon 100 pts

For this assignment, you will submit a single C++ compilable file containing a program written in C++. Remember, to submit a file for this course electronically, from the directory in which the file resides type in at the UNIX prompt the command: cssubmit 1570 section_letter assignment_number. Be sure that only the file you want to submit is in that directory - make a directory for every assignment! The submit system will deliver every .cpp file in the current directory to me. Name your file a meaningful name and give it a .cpp extension since you will be compiling it. Also, make sure that you compile and run your program using the GNU (g++) compiler before submitting to make sure that it will work for the submit script.

Background: hw4Foreground: So, you know the back-story. Hans went for walks and got lost, found he got nowhere in life, or actually accomplished a goal. But that was his life in hw 4. As it is with the forward movement of time, things do indeed get better! You are going to re-code hw 4 using functions. However, things are changing in Hans's life in more that one way. Hans has a pet penguin. And we all know how much penguins cherish lemonade (it reminds them of balmy days, laying out on the beaches of Antarctica). The penguin occasionally escapes the confines of the house to go look for lemonade, forcing Hans to go in search of his lost "vertical duck". (Hans thought he was buying a duck from that shady character on the street corner!) Well, you can imagine how difficult this is. A penguin wandering the streets of Springfield on his escape for ade. I guess you could call that an "escapade" - get it, escape-ade.....escapade. And Hans Moleman wanders about searching for him??!! What a disaster. Fortunately, Hans has a dart/tranquilizer gun that allows him to stop the penguin if he gets close enough.

Specifications: First, you are to use functions to code this assignment. YOU decide what the functions are this time. Make good decisions as we will grade you on how easy it is to read, understand and adapt your code. Make sure you use the good programming practices of good variable and function names! Now, your program will be a simulation of 500 walks. A walk entails Hans leaving home in search of his penguin. The end of the walk can be one of several outcomes and you will keep and output stats on each. Here's what can happen:

  1. Hans will walk unimpeded until he makes 500 turns, not reaching home or finding his penguin or being picked up by the cops ... at which point he dies. (Now that is love for a penguin!)

  2. Hans will see his penguin, capture him and head home. He either makes it home or walks unimpeded until our tolerance of 500 turns is exhausted .... at which point he dies. (bummer! but at least he has a friend with him)....or a cop picks him up and his penguin up and takes them both home.

  3. Hans will walk for a while and then the cop stops him and takes him to jail as a vagrant....where he dies (bummer!).

  4. Hans will find his penguin, head home and then get caught by a cop who has pity on him (the penguin, not Hans) and takes him (the penguin) home with his pet man. (You can assume that when the cop catches them, that is the end of that particular walk since they get a free ride home.)

  5. The cop finds the penguin before Hans does, at which point the two of them just hang out together talking about Hans who is stumbling around town looking for the penguin and avoiding cops. In other words, those two stop moving, but Hans continues until situation #1 results (dies of exhaustion ... bummer) or he finds both cop and penguin (at the penguin's location) and thus gets a free ride home.

Details: The devil is in the details....The penguin is in the details.....

  1. Hans will walk randomly as long as he isn't with the penguin. Every leg of his journey (even from the front door) is a random number of steps between 50 and 100, inclusive. Each time he stops, he randomly chooses to turn either left or right. In your code, choose the random direction first, then choose the random distance to walk. (Note: all the distances in this assignment are the same unit. Let's make them steps or paces....and a step = a pace.)

  2. Once Hans finds the penguin, he walks not totally randomly, but with a bias towards home. That's because he has the penguin helping him; penguins have a homing capability .... that I just can't explain. So, after capturing the penguin, with a 40% chance Hans will turn in one of the two directions which will get him closer to home; with a 10% chance Hans will turn in one of the two directions which will take him further away from home. (e.g. Suppose home is (0, 0) and Hans is in the 2nd quadrant. Then there is a 40% chance he will go south, a 40% chance he will go east, a 10% chance he will go north, and a 10% chance he will go west.)

  3. To make this easy, place Hans's home (his starting point) at (0, 0).

  4. The penguin will move randomly just like Hans, only he takes between 20 and 50, inclusive, steps each time...until he is caught or hangs out with the cop.....or Hans dies. Which is really sad because he'll probably end up on skid row panhandling for fish.

  5. Start the penguin at (200, 200) and have his first move be westerly.

  6. Always start Hans moving north.

  7. Place the cop at (75, 0). This is his post for the day; the corner of 12th str. and Vine.

  8. Make Hans move first, the penguin second, and the cop third IF he moves.

  9. The cop will stay at his post for 10 moves of the other characters, but then he moves to (0, 75) where the doughnut shop is, and moves back to his post on the next turn. He eats his doughnut for 10 more turns and then repeats this pattern until the end of the walk.

  10. If either the cop or Hans come with in 175 steps of penguin, your code will assume Hans captures him (at the penguin's location - Hans darts him and then magically jumps to the penguin!) or the cop hangs out with him (at the penguin's location). If Hans is within 10 steps of home after returning with the penguin, he jumps through one of the windows and stays there!

  11. The cop sleeps a lot....you know, the "donut downer" effect. So, Hans has to be with in 5 steps of the cop before the cop notices him and arrests him.1

Your output should be the statistics of what percentage of each of the halting conditions occur during the simulation.

Details upon details: To imbue some consistency in this program, let's establish the following standards:

  • move all your actors (Hans, the penguin, the cop) before calculating any distances and checking any walk-termination criteria. Thus, you don't move Hans and then see if he's close enough to the penguin; you move Hans and the penguin (and the cop) and then see if Hans is close enough....

  • check your actor-to-actor distances in this order: distance Hans to penguin, distance Hans to cop, distance copy to penguin. This implies that, if Hans was close enough to both the cop and the penguin, Hans would catch the penguin rather than get caught by the cop. Of course, this does reflect reality as any cop would be dumbfounded to see any man who resembles a mole to be shooting a penguin. Don't ya think?

Special note: Since you have all learned to document your functions correctly, this is indeed a requirement for your code.

When you submit: To insure consistency in output for the graders, do the following:

  • seed your random number generator with 2.

As always, don't hesitate to ask if you have any questions about this assignment.

1You might think this is inconsistent with the 175-step required before the cop notices the penguin before he can stop that animal. It's not inconsistent....penguins are really stinky.