Topical Information

This program should help clarify to you the use of inheritance (and possibly polymorphism -- see the options below).

Program Information

Watching 'Wild Kingdom' reruns on Animal Planet has taken its toll on your psyche. You have predators and prey on the brain! You feel driven to write a predator/prey simulation to alleviate the mental cacophany.

You see that all predators share certain traits and all prey share certain traits. (Heck, from biology class you realize that all creatures on Earth share some common traits.) So you decide to make a hierarchy (or two) to power your simulation.

Think simply here. Predators have a hunger which drives them to be predatory. They have a speed/cunning factor which helps them catch prey. They can eat prey.

Prey are worth a certain amount of food to a predator (a single rabbit won't fill a wolf's appetite, but 5 or 6 might). They also have speed/cunning that help them escape. They can be eaten/killed by predators (some prey isn't killed by being eaten -- grass will simply reduce in amount of food if a cow chomps on it some).

All creatures can procreate. They might die of starvation or being eaten, but they can all re-populate.

Create your hierarchy(ies) in a library for easy re-use.

Make a sample simulation application which allows the user to choose names for the predators and prey, give them ranges of values for speed, birth rate, hunger, and food worth -- all wolves are equally fast or hungry and all rabbits aren't equally fast and food-worthy. Randomly create creatures to fill your population. Then simulate their interaction:

      each creature gets a chance to 'spawn'
      then predators attack (a random prey) and prey flees (when attacked)
      if the attack succeeds, the predator 'eats' the prey
      remove those who have died of starvation and/or being eaten
      report the counts of predators and prey left
   repeat until _______?

Common stopping conditions are the user tells you how many 'generations' to do, everything is dead, everything from either predator or prey is dead, or the system seems to be stable (the count of both populations over the past n generations is the same or oscillating -- this can be checked by keeping track of the recent maxes/mins of the population counts -- last 5-10 generations or so -- and seeing if the difference between them is converging). I'd implement the first three -- they aren't too hard to combine or code.

Thought Provoking Questions

  1. How many hierarchies do you have? Why?

  2. How many classes are in the hierarchy(ies)? How many levels does this hierarchy(ies) have?

  3. What members did you make protected? Private?

  4. What kind of inheritance mode(s) did you use for your inheritance?

  5. Have you used any composition? Or is your entire program dependant on inheritance?

  6. How many arrays do you have to hold your simulation's population? How 'weildy' is this?

  7. What sort of initial counts cause predators to reign? Prey? A stable population (not by code -- by inspection)?

This assignment is (Level 4).


Add (Level 2) to extract the common interfaces and make the hierarchy polymorphic.

More Thought Provoking Questions

  1. How many arrays will you now need to store all of the creatures in your population?

  2. What methods did you make virtual?

  3. How do you commonalize the interface to both predators and prey (prey eating, predators being eaten)?

  4. Do you now have to worry about a predator eating another predator?

Add (Level 1.5) to code the stability stop condition in your simulation. If you find/implement a sound and better/easier method of stability checking than hinted at above, let me know and it could be worth another (Level 1.5).