In this assignment, you will create a simple 2D 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 a grid. Time is simulated in time steps. Each critter performs some action(s) 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 cell in the selected direction in occupied or would move the ant off the grid, then the ant stays in the current cell.
Breed: If an ant survives for 3 time steps, then at the end of the 3rd time step (i.e., after moving) the ant will breed. This is simulated by creating a new ant in the adjacent (up, down, left, or right) cell that is empty. If there is not adjacent empty cell available, no breeding occurs. Once an offspring is produced, the ant cannot produce an offspring until 3 more time steps have elapsed.
The doodlebugs behave according to the following model:
Move: Every time step, if there is an adjacent cell ((up, down, left, or right) occupied by an ant 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 8 time steps, then at the end of the time step it will spawn off a new doodlebug in the same manner as an ant.
Starve: If a doodlebug has not eaten an ant within the last 3 time steps, then at the end of the 3rd time step it will starve and die. The doodlebug should then be removed from the grid of cells.
During each time step, all doodlebugs should move before the ants.
Write a program to implement this simulation and the world (the 20x20 grid of cells) 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. This class should have an overridden method named move that is defined in the derived classes of Ant and Doodlebug. You may need additional data structures to keep track of which critters have moved.
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 the population of predators and prey, although random perturbation may lead to the elimination of one or both species.
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference
and should not be submitted as is. We are not held liable for any misuse of the solutions.
Please see the frequently asked questions page
for further questions and inquiries.
Kindly complete the form.
Please provide a valid email address and we will get back to you within 24 hours.
Payment is through PayPal, Buy me a Coffee
or Cryptocurrency.
We are a nonprofit organization however we need funds to keep this organization operating
and to be able to complete our research and development projects.