In C++ Create a simple two-dimensional predator-prey simulation. In this simulat
ID: 3602619 • Letter: I
Question
In C++
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 20 ×20 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. Doodlebugs eat Ants.
The ants behave according to the following model:
Move. Every time step, randomly try to move up, down, left, or right. (hints: you can use a random number generator to generate integer from 1 to 4, 1 = right, 2 = down, 3 = left, and 4 = up). If the neighboring 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 an 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 time 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 steps, then at the end of the 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, all the doodlebugs should move before the ants do.
Write a program to implement this simulation and draw the world using ASCII characters of “A” for an ant, “D” for a doodlebug and “.” A dot represent an empty location. This is an example for 10 x 10 world (we want 20 x 20).
....D....A
...A......
A.DA....A.
.....A...A
.D...D..AD
Create a class named Organism that encapsulates basic data common to both ants and doodlebugs. This class should have a virtual function 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 perturbations may lead to the elimination of one or both species.
Explanation / Answer
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int a[20][20],p=0,q=0;
void step(int g,int h,int i,int &u,int &v);
/* Organism is the parent class here */
class Organism
{
public :
int x,y,life;
};
/* Both Bugs and ants inherit from Organism */
class Ant:public Organism
{
public:
void birth(int e,int f)
{
x=e;
y=f;
life=0;
a[e][f]=2;
p++;
}
/* these are there three operations required to be performed according to the problem statement */
void move();
void breed();
void death()
{
a[x][y]=0;
}
}Ants[1000]; // this is a bugs objects array of size 1000
/* DoodleBugs also inherit from */
class DoodleBugs:public Organism
{
public:
int steps;
void birth(int e,int f)
{
x=e;
y=f;
life=3;
steps=0;
a[e][f]=1;
q++;
}
void move();
void breed();
void death()
{
a[x][y]=0;
}
}DoodleBugsle[1000];
/* function definitions */
void Ant::move()
{
int k=0,t1,t2,i;
life++;
for(i=0;i<4;i++)
{
step(x,y,i,t1,t2);
if(a[t1][t2]==0)
k++;
}
if(k>0)
{
if(k!=1)
k=(rand()%k+1);
for(i=0;i<4,k>0;i++)
{
step(x,y,i,t1,t2);
if(a[t1][t2]==0)
k--;
}
a[x][y]=0;
a[t1][t2]=2;
x=t1;
y=t2;
if(life>=3)
{
this->breed();
life=0;
}
}
}
void Ant::breed()
{
int i,k=0,t1,t2;
for(i=0;i<4;i++)
{
step(x,y,i,t1,t2);
if(a[t1][t2]==0)
k++;
}
if(k!=1)
k=(rand()%k+1);
for(i=0;i<4,k>0;i++)
{
step(x,y,i,t1,t2);
if(a[t1][t2]==0)
k--;
}
Ants[p].birth(t1,t2);
}
void shiftb();
void shiftd();
void DoodleBugs::move()
{
int k=0,t1,t2;
steps ++;
for(int i=0;i<4;i++)
{
step(x,y,i,t1,t2);
if(a[t1][t2]==2)
k++;
}
if(k==0)
life--;
else
{
if(k!=1)
k=(rand()%k+1);
for(int i=0;i<4,k>0;i++)
{
step(x,y,i,t1,t2);
if(a[t1][t2]==2)
k--;
}
life=3;
for(int j=0;j<p;j++)
{
if((Ants[j].x==t1)&&(Ants[j].y==t2))
{ Ants[j].death();
shiftb();
}
}
a[this->x][this->y]=0;
a[t1][t2]=1;
this->x=t1;
this->y=t2;
}
if(steps>=8)
this->breed();
if(life<=0)
this->death();
}
void DoodleBugs::breed()
{
int i,k=0,t1,t2;
for(i=0;i<4;i++)
{
step(x,y,i,t1,t2);
if(a[t1][t2]==0)
k++;
}
if(k>0)
{
if(k!=1)
k=(rand()%k+1);
for(i=0;i<4,k>0;i++)
{
step(x,y,i,t1,t2);
if(a[t1][t2]==0)
k--;
}
DoodleBugsle[q].birth(t1,t2);
}
}
/* Display function */
void display()
{
int i,j;
char c;
system("cls");
for(i=0;i<20;i++)
{
for(j=0;j<20;j++)
{
if(a[i][j]==0)
c='-';
if(a[i][j]==1)
c='x';
if(a[i][j]==2)
c='o';
cout<<c<<" ";
}
cout<<" ";
}
}
/* steps function */
void step(int g,int h,int i,int &u,int &v)
{
u=g;
v=h;
if(i==0)
u--;
if(i==1)
v++;
if(i==2)
u++;
if(i==3)
v--;
if(u<0||v<0||u>19||v>19)
{
u=g;
v=h;
}
}
/* shiftb and shiftd functions */
void shiftb()
{
int j,k;
if(p!=1)
for( j=0;j<p;)
{
if(a[Ants[j].x][Ants[j].y]==0)
{
p--;
for(int k=j;k<p;k++)
Ants[k]=Ants[k+1];
}
else
j++;
}
if(p==1)
if(a[Ants[0].x][Ants[0].y]==0)
p--;
}
void shiftd()
{
int j,k;
if(q!=1)
for( j=0;j<q;)
{
if(a[DoodleBugsle[j].x][DoodleBugsle[j].y]==0)
{
q--;
for(int k=j;k<q;k++)
DoodleBugsle[k]=DoodleBugsle[k+1];
}
else
j++;
}
if(q==1)
if(a[DoodleBugsle[0].x][DoodleBugsle[0].y]==0)
q--;
}
/* Driver program */
int main()
{
int m,n,i,j,s,t;
char ch='y';
s = 5;
t = 100;
for(i=0;i<20;i++)
for(j=0;j<20;j++)
a[i][j]=0;
srand(time(NULL));
for(i=0;i<s;i++)
{
do {
m=rand()%20;
n=rand()%20;
}while(a[m][n]!=0);
DoodleBugsle[i].birth(m,n);
}
for(i=0;i<t;i++)
{
do{
m=rand()%20;
n=rand()%20;
}while(a[m][n]!=0);
Ants[i].birth(m,n);
}
display ();
ch=cin.get();
while(true)
{
for(i=0;i<q;i++)
DoodleBugsle[i].move();
shiftd();
for(i=0;i<p;i++)
Ants[i].move();
display();
cout<<"Press Enter for next";
ch=cin.get();
}
}