An Interesting C++ Assignment
November 27th, 2006 LukeRecently, while lurking the Dreamincode forums, I found a post on the forums regarding someone’s C++ assignment. I wasn’t quite sure how to solve it, but I’ve copied the assignment, and here it is:
Problem:
Create a simulation of doodlebugs life
Description of the Problem:
The goal for this programming project is to create a simple two-dimensional predator-prey simulation. In this simulation the prey are ants and the predators are doodlebugs. These critters live in a world composed of a 20x20 grid of cells. Only one critter may occupy a cell at a time. The grid is enclosed, so a critter is not allowed to move off the edges of the world. Time is simulated in time steps. Each critter performs some action every time step.
The ants behave according to the following model:
â?¢ Move. Every time step randomly try to move up, down, left, or right. If the neighbouring cell in the selected direction is occupied or would move the ant off the grid, then the ant stays in the current cell.
â?¢ Breed. If an ant survives for three time steps, then at the end of the time step (that is; after moving) the ant will breed. This is simulated by creating a new ant in adjacent (up, down, left, or right) cell that is empty. If there is no empty cell available, then no breeding occurs. Once an offspring is produced an ant cannot produce an offspring until three more steps have elapsed.
The doodlebugs behave according to the following model:
â?¢ Move. Every time step, if there is an adjacent ant (up, down, left, or right), then the doodlebug will move to that cell and eat the ant. Otherwise, the doodlebug moves according to the same rules as the ant. Note that a doodlebug cannot eat other doodlebugs.
â?¢ Breed. If a doodlebug survives for eight-time step, then at the end of time step it will spawn off a new doodlebug in the same manner as the ant.
â?¢ Starve. If a doodlebug has not eaten an ant within the last three time steps, then at the end of the third time step it will starve and die. The doodlebug should then be removed from the grid of cells.
During one turn, the doodlebugs should move before the ants do.
Write a program to implement this simulation and draw the world using ASCII characters of â??oâ? for an ant and â??Xâ? for a doodlebug. Create a class named Organism that encapsulates basic data common to both ants and doodlebugs.
Initialize the world with 5 doodlebugs and 100 ants. After each time step, prompt the user to press Enter to move to the next time step. You should see a cyclical pattern between population of predators and prey, although random perturbations may lead to the elimination of one or both species.
Classes to be created:
(use the concept of objects, classes, constructors and destructors with or without overloading, Data encapsulation, Data abstraction and Inheritance).
Create an inheritance hierarchy to represent doodlebugs and ants that derived from Organism class.
I couldn’t help the original poster, but it looks like an interesting project to try. I’m going to get to working on that.