GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 06-Jan-2008, 09:03
qoofish qoofish is offline
New Member
 
Join Date: Jan 2008
Posts: 1
qoofish is on a distinguished road

Problems about Ant and Doodlebug.


Following is the question.
The goal for this programming project is to create a simple 2D predator-prey simulation. In this simulation, the prey are ants and the predators are doodlebugs. The critters live in 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 steps. Each critter performs some action every time step. During one turn, all the doodlebugs should move before the ants.

The ants behave according to the following model:
 Move. For every time step, the ants randomly try to move up, down, left, or right. 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, at the end of the time step (i.e., 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, no breeding occurs. Once an offspring is produced, an ant cannot produce an offspring again until it has survived three more time steps.

The doodlebugs behave according to the following model.
 Move. For every time step, the doodlebug will move to an adjacent cell containing an ant and eat the ant. If there are no ants in adjoining cells, the doodlebug moves according to the same rules as the ant. Note that a doodlebug cannot eat other doodlebug.
 Breed. If a doodlebug survives for eight time steps, 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 three time steps, at the end of the third time step it will starve and die. The doodlebug should then be removed from the grid of cells.

Write a program to implement this simulation and draw the world using ASCII character “O” for an ant, and “X” for a doodlebug. Create a class named Organism that encapsulates basic data common to 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

I've been working on it and have wrote some code,
when the compile OK,It seems that there's some logical or somwhere else I didn't thought about that the model runs not exactly right.So I found this one: (not written by me)
CPP / C++ / C Code:
#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);

class org
  {
     public :
         int x,y,life;
  };
  class bug:public org
   {
     public:
        void birth(int e,int f)
         {
           x=e;
           y=f;
           life=0;
           a[e][f]=2;
           p++;
         }
        void move();
        void breed();
        void death()
         {
           a[x][y]=0;
         }
   }bugs[400];
   void bug::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 bug::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--;
		}
		bugs[p].birth(t1,t2);
    }
  class dood:public org
  {
    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;
       }
  }doodle[400];
  void shiftb();
  void shiftd();
  void dood::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((bugs[j].x==t1)&&(bugs[j].y==t2))
       { bugs[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 dood::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--;
		}
		doodle[q].birth(t1,t2);
	}
}
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<<"\n";
	 }
}
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;
	}
}
void shiftb()
{
	int j,k;
	if(p!=1)
	for( j=0;j<p;)
	{
	  if(a[bugs[j].x][bugs[j].y]==0)
	  {
		p--;
		for(int k=j;k<p;k++)
			bugs[k]=bugs[k+1];
	  }
	  else
	   j++;
	}
	if(p==1)
	 if(a[bugs[0].x][bugs[0].y]==0)
	   p--;
}
void shiftd()
{ 
	int j,k;
	if(q!=1)
	for( j=0;j<q;)
	 {
	  if(a[doodle[j].x][doodle[j].y]==0)
	   {
	q--;
	for(int k=j;k<q;k++)
	doodle[k]=doodle[k+1];
	   }
	  else
	   j++;
	 }
	if(q==1)
	 if(a[doodle[0].x][doodle[0].y]==0)
	   q--;
}
void main()
{
	system("cls");
	int m,n,i,j,s,t;
	char ch='y';
	cout<<"enter how many doodles & bugs do u wany?"<<endl;
	cin>>s>>t;
	for(i=0;i<20;i++)
	for(j=0;j<20;j++)
	 a[i][j]=0;
	srand(time(0));
	for(i=0;i<s;i++)
	{
	 do {
	 m=rand()%20;
	 n=rand()%20;
	   }while(a[m][n]!=0);
	 doodle[i].birth(m,n);
	}
	for(i=0;i<t;i++)
	{
	 do{
	m=rand()%20;
	n=rand()%20;
	   }while(a[m][n]!=0);
	 bugs[i].birth(m,n);
	}
	display ();
	ch=cin.get();
	while(true)
	{
	 for(i=0;i<q;i++)
	  doodle[i].move();
	 shiftd();
	for(i=0;i<p;i++)
		bugs[i].move();

		display();
	cout<<"Press Enter for next";
	ch=cin.get();
	}
}
What I'd like to ask is that:
Code:
class bug:public org { public: void birth(int e,int f) { x=e; y=f; life=0; a[e][f]=2; p++; } void move(); void breed(); void death() { a[x][y]=0; } }bugs[400];
What does that " bugs[400] " means?
Is it a object array of bug?
And why could it use like this?
Does that means when I want to declare an object array,I just need to add an 's' to the class name?
  #2  
Old 06-Jan-2008, 09:36
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,648
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Problems about Ant and Doodlebug.


Quote:
Originally Posted by qoofish
...I found this one...What does that " bugs[400] " means?

That statement defines a class named bug, derived from class org, and creates a global variable named bugs. The variable is declared to be an array of 400 objects of type bug

Notice that, in addition to the 400 bugs allocated by that statement, the program may have other bugs as well.

Regards,

Dave
 
 

Recent GIDBlogFlickr uploads of IA pictures by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 00:13.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.