Assignment 10

Due: Wednesday April 27, 2011, at noon, 100 points

For this assignment you will submit (in the usual way) multiple files. These files will contain a C++ program that you have named appropriately, each file with an appropriate name and appropriate extension. Be sure that your program compiles using the g++ compiler before you submit it.

Background: Dragons are important and are in a class of their own. But as you know, dragons need a place to be and things to do. Peasants and their huts also need a place to be, but not really much to do .... except work and starve....and burn. So, now your dragon(s) and peasants and huts will be placed in a countryside and the dragon(s) will wingaling about, burninating peasants and huts, and fighting other dragons....maybe.

Specifications: You are going to create new types to make the above happen. Here's how it will work. We'll give you suggestions below on how you will want to implement these things.

You will have a countryside class that will contain an array of Dragons, and array of Peasants, an array of Huts, and an N x N grid of sectors, each able to hold a character. (You will do this with a 2-D array of chars...but more on that later.) There will be placed at random in the countryside X number of peasants and Y number of huts. Up to four Dragons will be placed in the corners of the grid. Then, the "game" will begin. It's actually a simulation since you have no control over the actors. Of course, you can change the parameters (that we will provide initially) to get different results, but you'd do that on your own time. The dragons will take turns moving about the countryside burninating peasants and huts and, well, anything! One dragon will "move" and burninate, then the other will do so, then each peasant has a chance to "move". Then the Huts do their thing (described below). Then each of the dragons, peasants, and huts (in that order) will place (a character representation of) themselves in the countryside grid.

The foregoing movements represent one timeslice, or turn, in the simulation. The simulation will continue until all peasants and their huts are dead/gone via burninations, of course, or until both dragons are dead. It's easy to understand how peasants and huts burninate, and it's not because peasants are smoking in bed! So how does a dragon die? Well, there are two ways: (1) one dragon kills another; or (2) a dragon runs out of burnination fuel and thus becomes defenseless against the peasants. Thus, if a defenselss dragon lands on a Peasant, the Peasant can kill (by stabbing it with a pitchfork, the only weapon any self-respecting peasant owns!) the dragon. A Peasant can die in one of two ways: (1) A Peasant can starve to death. Each turn a Peasant will have a 67% chance of starving and 33% chance of eating. If they lose weight down to that critical mass of 50 lbs, they die. (2) Burnination! If a dragon burns a Peasant, he/she/it will stay on fire for 2 more turns and die. Durning this time, they move just as any other Peasant. If they get burninated again, they can no longer apply for Medicare, but it has no other affect; they die as scheduled. And where do Peasants come from? Huts will randomly (15% chance for each turn) create another Peasant and place it in the same location as the Hut.

The output from the program will be printouts of the state of affairs in the simulation. You will output each dragon at the initial state (starting point of the simulation), at the 10th time-slice of the simulation, and finally at the end of the simulation. Your printout will state all relevent info about the dragons and the countryside, including how the simulation ended.

Details: Reference hw 9. Here we list some additions/deletions/modifications to the classes you've already built. You may have to add others; it is up to you. BUT, don't add something to a object that doesn't belong! Each data member should be valid, reasonable, and needed. Thus, you have a certain amount of leeway in how you design your classes.

  • dragon class additions/deletions/modifications:

    • member data:

      1. A member for location in the countryside (grid). You can either use two members for x and y coordinates, or you can create yet another struct for "point" to locate things in the countryside.

    • member functions:

      1. a copy constructor - REMOVE THIS CONSTRUCTOR

      2. a print() function REMOVE THIS FUNCTION and replace it with an overloaded insertion operator

      3. a move() function that a dragon calls when it wants to travel to another location in the grid. If the dragon is a wingaling dragon, then he/she/it will move to a randomly chosen location (simulating near-earth-orbit flight). If not a wingaling, then the dragon can only move to an adjacent cell in the grid. How you choose to have such a dragon move through the countryside is up to you. This function, or another function it will call, will handle all the interactions of the dragon with whatever it runs into or lands on top of.

      4. a place() function that will place an appropriate letter ('D' for Dragon is good) in the countryside's grid where the Dragon resides.

    • peasant class additions/deletions/modifications:

    • member data:

      1. a member for location on the grid.

    • member functions:

      1. a print() function REMOVE THIS FUNCTION and replace it with an overloaded insertion operator

      2. a move() function that has the object shift to another location in the countryside and interact with any other actor in the simulation.

      3. a place() function that will place an appropriate letter ('P' for Peasant is good) in the countryside's grid where the Peasant resides.

    • Notes on peasants:

      • The peasant's name is to be chosen at random from a file full of names:

        • Cletus Daryl BillyBob (NO spaces!) Frank Tibor Lupita Giselle Burnidette Payne Diego Dennis Cheeseman Gregory Godfrey Dunn Bigge Broomhilda Corpsen Rodney Ali

      • When a peasant is burninated, they will scream out (to be printed) an "oath" randomly chosen from a file of such oathes:

        • Gov'ner, I'm on fire!! AhhhhhH!!!!! FIRE, FIRE, FIRE oh no, now how am I gonna pay the mortgage?! I'd gladly pay you Tuesday for an extinguisher today. Let it rain, let it rain. Why doesn't it ever rain when you want it to?!! 'b'lieve I need a shower! Oh I wish I were an Oscar Meyer wiener...

More Details: Here are additional classes that you will need:

  • Countryside class:

    • member data:

      1. An N x N array of characters. This will be the representation of the countryside. Make N a global constant.

      2. A short to represent what part of N x N array you will be using for that particular countryside. To make it clear, suppose you make N 50, call this member m_worldsize and make it 15 in your constructor, then that particular countryside object will only be using a 15 x 15 part of the 50 x 50 2-D array allocated.

      3. A short for the number of Peasants in the countryside

      4. A short for the number of Huts in the countryside

      5. A short for the number of Dragons in the countryside

      6. An array of Dragons (max 4)

      7. An array of Peasants (max 30)

      8. An array of Huts (max 5)

    • member functions:

      1. At least one constructor. You may have more. There needs to be one that will take the number of Dragons, Peasants, and Huts you wish to "scatter" about the countryside.

      2. An overloaded insertion operator for outputting the state of the countryside. This is sort of like a "state of the Union" function, but not really.

  • Hut class:

    • member data:

      1. A location in the countryside.

      2. A member to indicate whether the hut is "functional" (not if a dragon burninated it).

    • member functions:

      1. A generate() function that will periodically generate a new Peasant and place that Peasant in the very same location as the hut.

      2. a place() function that will place an appropriate letter ('H' for Hut is good) in the countryside's grid where the Hut is.

Details upon the Details: There are several parameters that you need to know for the program. We'll list them below here. There is also a lot of leeway for you for implementations. In other words, you have to make a lot of the decisions as to how you will code this up. Think carefully about what you do.

  • Your program will first need to declare a countryside object for everything to exist in. It will automatically create the member arrays of Peasants, Huts, and Dragons. Anytime you declare an array of a user-defined type, the compiler calls the default constructor for that type. Thus, in order for it to do so, each of these types needs a default constructor. You have that. But you need each to place the default Peasant, Hut, and Dragon in some location. Make that default (0, 0). The next thing your countryside constructor needs to do is give the dragons a location: (0, 0), then (n-1, n-1), etc. It then scatters the Peasants in randomly chosen locations in the grid by putting an appropriate letter in that grid location, and assigns that Peasant's location member that location. Then do the same for the Huts.

  • When scattering Huts and Peasants, be sure that you don't put a Hut on top of a Peasant nor a Peasant on top of a Hut. Put a 'P' for Peasant, etc. Be sure your code disallows scattering too many "things" - more than it can hold. (Overcrowding of the countryside leads to civil unrest, and thus, more burninations.)

  • You will find it wise to create a private "clear_field()" function that clears a coutnryside object that the constructor calls initially. If you didn't do this, your countryside would initially be littered with all sorts of stuff!

  • You will decide how to implement a "burning" Peasant. You already have a "isOnfire" member. You will also need a member to keep track of how long the Peasant has been burning. If you want to discard the "isOnfire" member for another implementation of this behavior, go ahead and do it.

  • When a dragon lands in a cell where a Peasant resides, Peasant goes "SQUISH" and is dead. When a dragon lands, he will burninate any Peasant in a cell that adjoins the dragon's cell N, S, E, or W; not diagonally. Whether a Peasant object actually catches fire will depend on the majesty of the offending Dragon object (as described in hw 9).

  • When a dragon lands on a Hut, the dragon destroys it ... no questions asked.

  • When a dragon lands on another dragon, one will survive and the other will not. Compare attributes in this order: majesty, number of V's, number of beefy arms, number of burninations. If at some point, one dragon "out ranks" the other, it wins and the loser is killed. If they are equal at every point, then "flip a coin" and kill one at random.

  • Every time slice, a Hut has a 15% chance of generating another Peasant. Give the new Peasant the same location as the Hut. When mapping the Peasants and Huts to the grid, follow the order stated so that the Hut will be printed, not the Peasant. This behavior simply mimics the Peasant hiding in the Hut. They frequently do this, Peasants do, .... trying to avoid Dragons.

  • Every time slice, a Peasant can move one position up or down, left or right. You make the decision how the Peasant decides this. Or not. Two Peasants cannot occupy the same location simultaneously. If a Peasant finds himself completely surrounded by other Peasants, ...

  • If a Dragon can burninate more than one object in a turn, it will. They're not socially well adjusted nor picky.

For this assignment, you are welcome to work in pairs.1 You and your partner will submit only one submission for the both of you. Make sure that both names are listed at the top. You don't have to work with another person, but you if you do, your partner must be in section A. Also, the work must be done by both partners equally.

When you submit: Your code will require no user interaction.

As always, if you have questions, don't hesitate to ask your instructor or the LEAD GUY if you need help.

1If you work in pears, you'd better find a big pear.