Assignment 10

Due: Wednesday, December 7, 2016, at noon, 100 points

For this assignment, you will submit multiple files containing a program written in C++. You know how to use the submit program by now; I needn't tell you. And, as stated in the hw #8 posting, you may work in a group of two for this project. Be sure to divide the work equitably; we absolutely do not want to hear of people not contributing.

Resources: www.cplusplus.com

Background: homework #9 This gives you the basic foundation upon which to build your program. You have the town, activist, and polluter classes.

Foreground: In general, the activist will chase the polluter around town, possibly bumping into walls, getting caught by the EPA, eating roots, getting intoxicated, losing dignity, etc.

Specifications: You are going to add another class. It is the:

  • root class: (you remember hw 4 don't you)

    • member variables:

      • a member string for description (truffle or square1)

      • a float member for "effect". The value of this member will be determined for each object by the constructor. It will be interpreted this way: if the value is negative, it is square; if it is positive, it is truffle. Zero is an unacceptable value. This value will affect its consumer by increasing or decreasing his (the activist, of course) toxicity by that amount.

    • member function:

      • A constructor that will assign to its "effect" variable at random from the following intervals: [-.010, -0.001] and [0.05, 0.10]. [Hint: randomly choose positve or negatve first. Then choose from the appropriate interval]. This will obviously determine the name of root, too.

Though this is the only new class you will add to the mix, it is certainly not the only new construction for the program. Let's describe the big picture first. Your main function will loop on a call to a simulation() function; this function represents one day for an activist. And the simulation runs until the activist object becomes inactive on that day. The loop will then represent multiple days. This is described below. Your main will collect stats and report

  • percentage of exits (which does not kill the activist, he/she just spends his/her remaining days in a EPA holding cell in the basement of some govt building .... in Shelbyville.)

  • percentage of becoming wacked out, i.e. "gone", on roots (truffles, of course)

  • percentage of successes in catching the polluter

  • percentage of death by the loss of all his/her dignity (when dignity goes to zero, death occurs....this does NOT apply to politicians)

  • and average toxicity when the day is done

Thus, a day finishes if the activist becomes inactive which means

  • the activist exits the town

  • catches the polluter

  • dies of supreme indignity

  • becomes over-intoxicated by roots

Notice that the activist doesn't have a set number of steps anymore. If the activist

  • steps "into" a wall, he/she doesn't move but loses a given number of dignity points

  • steps into an exit, he/she falls into the hands of the EPA

  • steps into a cell where the polluter is, he/she wins for the day

  • Now,

  • if he/she steps on a cell where there is a root, he/she will then consume that root and suffer the effect (toxicity is adjusted accordingly). That root is removed from the grid.

  • if he/she steps on a cell where there is a cop who is waiting for the polluter, he/she will engage in meaningless banter and lose dignity points because he/she has to admit to being related to the polluter.

So, the polluter will move about the town at random. The activist and polluter take turns stepping. The polluter moves at random like Homer usually does after leaving Moe's a polluter usually does. But the activist in this program will have two modes of moving: one is random like before; and one is semi-intelligent. As the activist moves, he/she loses and gains toxicity and dignity points depending on what happens. His/her toxicity will determine his/her method of movement. After the simulation runs to completion, the stats are reported and the program ends. So, pseudo-code for your main could look like this:

12 4 6 1000 1 7 10 20This is the town's grid size, num roots, num cops, num days, dignity points lost for bumping into a wall, dignity points for making excuses to cops. Keep this order. Do not put comments or anything else in the file. Take these numbers above as your starting point for the parameters of this simulation. We may change them later, though. Then, in your main function you will create a stream to connect to the config file and stream into local variables those parameters. Cool, eh? (Yes, cool.) All other constant-kinda-stuff should be constants in an appropriate header file. By the way, make the default symbol for a polluter a constant 'P' and put it in a header. That way you can code activist to look for a specific constant symbol. see requirement #3 below in the added requirements.

Details upon details: There is the issue of the roots. Clearly, you will have two kinds of roots, truffles and squares. Since the town is a grid of chars, you can't actually place different root objects out there. That would require really advanced c++ techniques - c+++++ concepts! So, you're going to do this in a novel way. You see in the pseudo-code in the sim function that you declare an array of roots of CONSTANT size. You must do that, as you know, because the compiler must know the size of an array at compile time. That MAX_ROOTS can be set by you to something like 20. But, you will only use a subset of them. That value is the value read in from the config file: num roots. So, you will scatter at random that number of 'R's over the grid. Then, whenever the activist lands on a root, he/she will choose the (num_root-1)th root from the array of roots to consume and be affected by, and then decrement number of roots. In this way there is a 1-1 correspondence between the R's on the grid and the roots in the array. Also, you must understand this: When you declare an array of user-defined objects (like roots), the compiler will automatically call the default constructor for each of those objects in the array. So, you need to build into your own default constructor the random assignment of "effect" member for that root. Finally, in my pseudo-code, I have the array of roots outside the town class. You might want to include the array of roots as part of the town class. This decision is up to you.

Now, as far as cops go, they're out there to watch for polluters.....and eat donuts....but mostly watch for polluters. You will represent a cop using a 'C' on the town grid. Cops don't move, but they will indeed stop a polluter. They will also talk to any activist about town. If an activist lands on a cop, the symbol for the activist will dominate (remove the cop symbol and display the activist symbol). But once the activist moves on, the cop symbol is re-displayed.

The activist will start2 with a toxicity of 0.05. At this level, he/she is considered normal. He/she will move one step at a time with semi-intelligence. That is, his/her step function will be passed the polluter object so that he/she can "see" where it is. Knowing its location, he/she can move toward the quadrant it's located in. Once his/her toxicity reaches "cool" threshold (0.08), he/she reverts back to random steps. If his/her toxicity reaches "gone" level (0.25), he/she quits the chase because he/she is going to attend the Stones concert, and that run and the simulation ends.

The polluter will always move about randomly. If, however, he/she lands on a cell with a cop, he/she has to remain there until the end of the day. In that case, remove the symbol for cop and replace it with the 'P' symbol for that polluter. You might want to include in your polluter class a member variable to indicate whether or not it is caught by a cop. This and other design decisions are yours to make. If the polluter hits anything else (including the activist! (stupid Homer polluter)) he/she doesn't move anywhere that turn.

Not Optional: For this assignment, you are required to overload either the '+' or the '+=' operator so as to be able to add a root to an activist. How it works...you can figure it out. Where it should be defined, that's up to you. Make it a good, wise decision.

These are two important addendums: First, we are changing the parameters of the ‘experiment’ or ‘simulation’. This will be VERY EASY for you to change if you have already started. These are the numbers in the config file. The new values will make the outcomes a little more interesting. They are:

Grid size: 17

Number of roots: 12

Number of cops: 6

Number of days: 1000

Dignity loss for bump: 7

Dignity loss for cop: 20

Next, you are to code the following:

  1. A polluter object can walk on a root object and will simply ignore it. Thus, if Homer steps on a root, his symbol shows and not the root’s symbol. Once he steps off the root, the root’s symbol returns.

  2. Your code will simulate 1000 days, BUT it will only output the town for the first TWO (2) days. The remaining days will be simulated, but you won’t output to the screen the town as that would take to many pages to print. This is very simple code to implement.

  3. Also, you will code the constructor for the polluter to take not just a name, but also a symbol to represent it. Likewise with the activist. Create your polluter with name “Homer” and symbol ‘H’, and your activist with name “Lisa” and symbol ‘L’.

Optional: Now, after you get all this stuff done, you have the option to add "your own". That's right, if you can think of fun stuff to add to this, do so. You will be judged on how much you make your instructor laugh. If you really impress them with something innovative and goofy, then maybe they'll give you points for it. Good luck!!

The information read in from the config file (explained below) will be parameters of the program runs. They are

    • the size of the town (grid size)

  • the number of roots in the town

  • the number of cops in the town

  • the number of days in the simulation

  • dignity loss for hitting a wall

  • dignity loss for lying to cops

The number of days will be used in the loop that controls how many times the simulation function is called. The other values will be used in the sim function to declare the town. You might find it necessary to modify your town class to accommodate this. Pseudo-code for the simulation() function might look like this:

begin sim(.....) // represents one day/run/chase/whatever...and hopefully many many truffles declare town declare activist declare polluter declare array of MAX_ROOTS roots place the activist place the polluter randomly place num_roots roots randomly place num_cops cops loop polluter steps activist steps while activist active end sim

Details: So what's this "config file" business? Well, you have had many constants in your programs in the past, and you will certainly have constants in this program. But one of the reasons to have them is that they may be parameters to change as you run your program in different ways. In order to change the parameters, though, you have to change the constants and then recompile your program. This works, but there is another way. For parameters that are going to change enough times that you are annoyed by recompiling, you can put those values in a "config.dat" file, just like a data file. Then, for each different run of your program, you can just open the file and change one of the parameters, then run again. You don't have to recompile. (This is not a big deal with little programs like ours, but some programs can take hours to compile!) So, create a config.dat file and put this data in it:

15 17 6

begin main read sim parameters from config file into local variables loop simulation(the sim parameters) collect stats end loop (days passed) report stats end main

was caused by consuming too many roots: ginko, mandrake, ginseng and square. In the 50s, these guys were known as "squares". Most grew up to become politicians. I had a truffle once, but I swear I didn't inhale.

2I suppose this needs some explanation. Activists start their day with a big bowl of General Mills "Truffle-Os".

When you submit, submit under only one name and be sure to include in your comment header the names of your partners on this project.

As always, if you have questions, don't hesitate to ask your instructor, or ask the lab help in the LEAD guys in the evenings.

1It should be noted that truffles also grow underground. But they are a mushroom. And, as you know, mushrooms can have a clearly intoxicating effect. Truffles, in particular, commonly grown and eaten in the UK, have a .... well ... just look. For all you youngin's, this is Keith Richards, member of The (Rolling) Stones.....stones, you get it? Stay away from truffles! Roots, on the other hand, particularly the 'square' variety, usually have a "sobering" effect. Maybe because they taste so bad. Evidence: check out Buddy Holly. Clearly a goober. If you see this guy in the building, offer him a mushroom. This condition