Lesson 2 | A simple ecological Simulation |
Objective | Describe the purpose and general specifications for the simulation. |
C++ Ecological Simulation
Let's look at a simple ecological simulation program that is an extension of the predator simulation used as the course project in
Building Classes in C++, the previous course in the C++ for C Programmers series.
The ecological simulation program displays a "world," which is actually a group of squares that are either empty or occupied by a lifeform.
This world has a one-square border that is always empty. The simulation goes through a series of cycles. At each cycle, the squares of the
world are populated by a variety of lifeforms, who then interact with neighboring lifeforms according to a series of rules.
In the simulation we'll be looking at, the lifeforms consist of predators, prey, and plants. The predators feed on the prey, who feed on
the plants. If a predator lifeform is in a square that adjoins a square occupied by a prey lifeform, the predator eats the prey. Lifeforms
can also die of old age or overcrowding.
In the example below, you see a 10 by 10 "world," occupied by predators (X), prey (Y), and plants (Z). Notice the empty, 1-square border
around the "world."
0 0 0 0 0 0 0 0 0 0
0 X X Y X 0 0 0 Z 0
0 Z Z Y 0 0 0 0 X 0
0 0 0 0 Y X 0 0 Y 0
0 Y 0 Z X X 0 Z Y 0
0 X X 0 0 Z X X Y 0
0 X 0 0 Z Z 0 0 0 0
0 0 X Y 0 Y X 0 Z 0
0 0 Z Z 0 X X 0 Y 0
0 0 0 0 0 0 0 0 0 0
Let us begin our examination of the simulation by looking at the abstract base class we'll be using for the lifeforms.