Assignment 10 - final project

Due: Wednesday, May 3, 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.

WHEN YOU SUBMIT: Unless your particular instructor says differently....have your program output to the screen the town (grid) after each step of Milhouse until he has stepped 35 times along with anything any character says, and then the final town grid with all the final stats.

Resources: www.cplusplus.com

Background: homework #9 This gives you the basic foundation upon which to build your program. There will be modifications and additions as described below. And, in this assignment, we highly recommend that you work with a partner. There is a lot of code and a lot of intricacies to deal with. Take advantage of this offer. And, as before with hw 8, submit only under one name, but put both names on all files.

Specifications: Starting with the code you generated for hw 9, you will make the following additions and modifications. NOTE: for town and tailor classes, I've included here that which is from the hw 9 version that is retained.

town class:

member variables:

    • a 2-dim array of loc objects declared for MAX size of 25 x 25. loc is described below.

    • a short integer describing the actual size of the town ( <= MAX). Your particular town object may not be as big as MAX x MAX, thus using only part of the array.

    • a short for the number of houses in the town.

    • a short for the number of bullies in the town.

  • member functions:

    • a constructor to which you pass the size (better be greater than 4 and less than or equal to MAX), the number of bullies, and the number of houses. This constructor should set up the town by calling the two functions below.

    • a clear() function that empties the town.

    • a build() function that will place the walls and then scatter the bullies and buildings.

    • a print function or the an overloaded insertion operator.

    • appropriate accessor/mutator (set/get) functions.

NOTES on town: Your town grid will no longer be a 2-D array of characters. You will need to create a struct called loc (that's short for location - I couldn't think of anything better). loc will have just two members: one for a char, and one for a bool to represent whether or not it has thrown a pair of pants at Milhouse, the tailor. What? Ok, so each cell of the grid will be empty if nothing is there. A space character suffices for that. But a cell can also be a piece of wall. Use 'W' for that. It could also have a bully standing there; use a 'B' for that. It could also have a house on that location. Use a 'H' for that. The way life in this little grid is going to work is that when Milhouse (the tailor) moves past a house, there is some chance that the occupant of the house will want to exchange a pair of pants with one of Milhouse's high-water pants he's carrying. Once that happens for a particular house, it will never happen again. And, so, in that case, the bool member of that loc will be set to false - they no longer have pants to exchange. This bool member ONLY has meaning for a 'H' in that spot. Walls, bullies, empty spots just don't have pants to alter. Thus, when writing your clear() and build() functions, think about what they should do.

Have your build() function scatter 40 houses, and then scatter 10 bullies, to randomly chosen spots in town. The houses are to be scattered in this manner: pick a random (empty) location in town and put a house there. Then (if there is at least one more house to place) put the next house next to the one you placed randomly (just before it). "Next to it" means up, down, left or right. Then, place the next house randomly, and another next to it (if possible). Continue scattering the houses in this manner, so as to create "clumps" or neighborhoods of houses, until you have placed all the houses. Scatter the bullies with no consideration to the placement of other bullies (bullies are not sociable).

loc struct:

member variables:

  • one for a char

  • one for a bool (if char is 'H', this represents whether the house has a pants or not)

tailor class:

member variables:

    • a member for the tailor's name - ntca or string.

    • a member to represent how much money he/she has.

    • appropriate variable(s) for his/her location on the grid (town).

    • a char for how he/she is to be represented (in the town array).

    • state variable to indicate whether he/she is alive.

    • a short indicating his "health" value - ranges from 0 -> 100. (0 indicates death).

    • a short indicating how many pants he has in his possession.

member functions:

    • a constructor that takes arguments for a name and a representing character (default to 'M'). This constructor should also set the money variable to a random amount between $20 and $40, the health to 100, location to (-1, -1), aliveness to true, pants to 30.

    • a place_me() function that has a reference parameter of type town. This function must first determine the dimensions of the town, and from that will place the tailor in the town's grid and change the tailor's (private) coordinates. Of course, activist always prefers to be in the middle of things (places the object as close to the middle of the town as possible). Place the tailor at a random unoccupied location in the town.

    • a rand_walk function() that has a reference to a town parameter. It will pick a random direction (up/down/left/right - no diagonal movement in Springfield!) in which to take a step, and it will change its own coordinates and the location it occupies in the town passed to it. This function should prevent movement into any new location in the town in which there is a character other than 'blank'. So, if it randomly chooses to move left and there is a char in that spot in the town grid, such as a 'W' for wall, then it has to move in some other direction.

    • a print() function or overloaded insertion operator. This should output on one line the object's name, aliveness, and location in town, etc. (e.g. "Milhouse is alive at (4, 5) with $44 and health 34" )

NOTES on tailor: The tailor (Milhouse) will move about town in a random fashion, one step at a time. When he passes by a house, it might happen that an occupant in the house will exchange his/her pants with one of Milhouse's high-water pants. If that happens, that particular house will no longer have pants to exchange in the future, and Milhouse's money will increase by $10. So, you will need to include a function in the tailor class that will be called at each step. It will have to look at its neighboring locations (not the one it just came from) to either side and ahead. If that spot is a house, then with probability of 70%, pants are exchanged. If that spot contains a bully, the bully will call its punch() function and .... well, that's described below (it gets ugly from here).

bully class:

member variables:

  • a name

  • a short to represent the power of a punch he will deliver (to some poor sap trying to sell high-water pants).

  • a short for the probability of throwing a punch.

member functions:

  • a default constructor that will draw a name from a file called bullies.dat (names below) and assign their name member variable, randomly assign a power value between and including 30 and 50, and assign their probability of punching to be 80%.

  • a punch() function to which you will pass a tailor object.

NOTES on the bully: When a bully punches Milhouse, he is sent flying and the bully takes power percentage of Milhouse's money. (If the bully's power is 50, he gets 50% of Milhouse's money. You may round down the money taken to make the computation easy.) Bummer. Not only that, it reduces his health by 10. You can implement Milhouse's "flight" from the punch by using the place-me() function you defined above. In other words, the bully will knock Milhouse to some random (open) spot in the grid. Now, if Milhouse gets lucky and the 20% chance the bully doesn't hit him occurs, then the bully will output to the screen a message drawn at random from a message.dat file. (messages below)

phantom_pants class:

member variables:

  • member(s) for location (in the grid).

member functions:

  • a constructor to which will locate the object to (-1, -1) by default.

  • a place_me() function that will be passed the town; it will give the pants location a random spot in town that is empty and it will place a 'P' in the town in that location.

  • a chase() function to which is passed the tailor object.

  • a kill() function to which is passed the tailor object.

NOTES on the pants: Phantom pants are not good. They hate Milhouse. C'mon! Pant mutilation is illegal in some states. Whenever Milhouse gets punched by a bully, he will drop a pair of his high-waters during his flight across town. If this happens, the pants will come alive and seek revenge! It will chase Milhouse and kill() him. You will pass Milhouse to the chase() function so the pants know which way to go. Also, phantom_pants are sorta like zombies. They are slow and stupid. So, they will move slower than Milhouse. In fact, it works like this: for every step Milhouse takes, there is only a 25% chance that a phantom_pants will take a step. And this applies to every phantom_pants in play. (This will give Milhouse a decent chance at survival.)

The General Plan: This program will simulate a day in the life of a enterprising tailor, plying his trade - the alteration of normal pants into high-water pants. Let's run this simulation for a town sized 17 X 17. Milhouse, the tailor, will walk around town (presumably Springfield) with a sack full of high-waters in a random fashion. Each step he takes is in a randomly chosen direction. He can't step into a cell of the town grid unless it is empty.

  • When he passes a house (adjacent to a cell with a 'H' in it), the owner might exchange their normal pants for Milhouse's high-waters. Milhouse gets $10 if so.

  • When he steps in a cell adjacent to a bully, 'B', the bully might hit him and he might not (see above).

  • Once in a while, Milhouse will drop pants in the street (no, not the ones he's wearing!), and it will turn into a phantom that will chase him and maybe kill him.

All of this you will code in your program by starting a loop that has Milhouse taking steps, houses throwing pants at him, bullies bullying, phantom pants phantomizing....until it all stops. So what is the stopping condition(s)? They are:

  • a maximum number of steps of 1000 (to be sure you don't end up with a infinite loop.

  • Milhouse trades all his pants.

  • Milhouse is murdered by a pair of phantom pants.

  • Milhouse's health declines to 0 as a result of being hit by too many bullies.

And once the simulation stops, your program is to state why it stopped and tally some stats: how much money Milhouse is left with, how many pants he still has to trade, and what his health value is.

A few details: Where do the bullies come from and how do they interact with Milhouse, the tailor? Well, each step Milhouse takes, he will have to first "look" to see what is in the cell chosen. If there is anything there, he can't move there. But once he moves to a new cell, he then has to look at the adjacent cells. If there is a 'H', you know what to do. But if there is a 'B' in a cell, then he has to interact with a bully. What bully; the 'B' in the cell is only a character, not a bully. So, at the beginning of the simulation, you will create an array of 4 bully objects. Note: to create an array of any user-defined type, it must have a default constructor. So, now you have an array of 4 default bullies. But because your default constructor chose a name for the bully, he is "different". Have the names chosen for the 4 bullies in order from a file of names (below). Now, when Milhouse meets up with a bully, just choose one from the array and use it. You can choose them in order or at random. Yeah, only 4 bullies to choose from and 10 bullies in town? Ok, some get repeated.....they have "territory". They move. Get it?

Also, phantom_pants that come alive in the town after Milhouse has been hit, will need to be chosen from an array of phantoms that should also be created at the beginning of the simulation. (Think of them as waiting outside town to be "activated"!) You will choose them in order from the array and no more than the number of phantoms in the array can be created. Declare an array of 3 phantom pants. When you activate them, they will have to call their place_me() function.

Thus, in pseudo-code, I think your main function will look something like this:

begin main

declare town

declare tailor

declare array of bullies

tailor.place_me(town)

simulation loop

tailor step

pant step (if they exist)

while tailor alive or has pants to sell

collect and present stats

end main

The files:

bully names bully messages

-------------- -------------------

Dolph "This is your lucky day, punk!"

Kearney "Ha ha .... I'm not going to hit you, weasel"

Jimbo "You're such a loser, I don't need to hit you!"

Nelson "I'm off-duty bullying now....you get away easy this time."

"Ok, so no punch today. But don't come back!"

"Gimme your lunch money and I won't hit ya."

"Only because I'm a gentleman....I'm not hittin' ya"

Finally: Above we have laid out a lot of the required code ideas to make this simulation work. You might have to add more to any given class. Feel free to do so - additional functions and/or variables. And if you want to add more interesting and fun things for Milhouse to do in town, go ahead. Also and important: You'd better set up constants for this program! We may change the parameters of this simulation to make it more interesting. If you hardcode the values, we will have ZERO sympathy for you (and you can make that a constant!).

When you submit, just do it. No wait, do 16 push ups and 55 sit ups also. But don't submit those.

Finally finally: If you get all this done and you're only taking 6 hrs this semester and are bored, then you are welcome to add more funny stuff....like trees that come alive and eat Milhouse, or bullies wearing phantom pants who become super villains or ..... If you do, write a BIG note to the grader at the beginning of your program explaining what you've done.

A summary of parameters: Your simulation will scatter 10 bullies, 40 houses, have town dim of 17 (square), tailor carries 30 pants to exchange, 25% chance phantoms step for each tailor step, 80% chance bully punches Milhouse, $10 for a pants "upgrade", power of punch between 30 and 50.