run time error in my code

June 9, 2011 at 07:56:11
Specs: Windows 7
It is required to create a simple two-dimensional predator-prey game. In this game the prey are ants
and the predators are doodlebugs. These critters live in a world composed of a 20x20 grid cells.
Only one critter may occupy a cell at a time. The grid is enclosed, so the critter is not allowed to
move off the edges of the world. This is done in time steps.
The ants behave according to the following model:
1. Move: Each one turn, it randomly (direction is generated randomly) try to move up, down,
left, or right. If the neighboring cell in the selected direction is busy or outside the grid,
then the ant stays in the current cell.
2. Breed: If an ant survives for three turns, then it will breed another ant. This is done by
creating an ant in the adjacent cell (up, down, right, left) that is empty. If there is no empty
adjacent cell then no breeding occurs. Once it breeds, it can't breed another ant until other 3
moves.
The doodlebugs behave according to the following model:
1. Move: Each turn, if there is an adjacent ant (up, down, left, or right) then the doodlebug
will move to that cell and eat the ant (ant will be died and removed from world). Otherwise
the doodlebug moves according to the same rules as ant. Note that a doodlebug can't eat
other doodlebug.
2. Breed: If a doodlebug survives for eight turns, it will spawn a new doodlebug in the same
manner as the ant done.
3. Starve: If the doodlebug does not eat an ant for three successive turns, then after the third
move it will starve and die. The doodlebug then must be removed from world (grid of
cells).
During on turn all the doodlebugs should be moved before the ants do.
Write a program that implement this game and draw the world using ASCII characters of "o" for
an ant and "x" for a doodlebug. Implement a class named Organism that encapsulates common
data 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 moved.
Initialize the world with 5 doodlebugs and 100 ants in randomly places of the grid (use random
number generation). After each step wait until user press Enter to do the next turn.
my code was
#include <iostream>
#include<vector>
#include<cstdlib>
#include<ctime>
using namespace std;
char g[20][20];
void printg()
{
for(int i=0; i<20; i++)
{
for(int j=0; j<20; j++)
cout<<g[i][j]<<" ";
cout<<"\n";
}
}
void reset(int x,int y)
{
g[x][y]='.';
}
void set(int x,int y,char data)
{
g[x][y]=data;
}
class organism
{
public:
bool dead()
{
if(life==0)
return true;
else
return false;
}
void sdie();
virtual void move()=0;
virtual void breed()=0;
protected:
int x,y,life,breeds;
char data;
};
class ant :public organism
{
public:
void move();
void breed();
ant()
{
do
{
x=rand()%20;
y=rand()%20;
life=1;
breeds=3;
data='O';
}
while(g[x][y]!='.');
set (x,y,data);
}
};
void ant::move()
{
if(life!=0)
{
int step,tx=x,ty=y;
step=rand()%4;
if(step==0)
tx=x+1;
if(step==1)
tx=x-1;
if(step==2)
ty=y+1;
if(step==3)
ty=y-1;
if(tx<0||tx>19||ty<0||ty>19)
return;
if(g[tx][ty]=='.')
{
reset(x,y);
x=tx;
y=ty;
set(x,y,data);
}
breeds--;
}
}

void organism::sdie()
{
if(g[x][y]=='.')
life=0;
}
class bug :public organism
{
public:
void move();
void breed();
bug()
{
do
{
x=rand()%20;
y=rand()%20;
life=3;
breeds=8;
data='X';
}
while(g[x][y]!='.');
set (x,y,data);
}
};
void bug::move()
{
int tx=x,ty=y;
if(life!=0)
{
if(g[x+1][y]=='O')
{
reset(x,y);
reset(x+1,y);
tx=x+1;
if(tx<0||tx>19||ty<0||ty>19)
return;
x=tx;
y=ty;
set(x,y,data);
life=3;
}
else if(g[x-1][y]=='O')
{
reset(x,y);
reset(x-1,y);
tx=x-1;
if(tx<0||tx>19||ty<0||ty>19)
return;
x=tx;
y=ty;
set(x,y,data);
life=3;
}
else if(g[x][y+1]=='O')
{
reset(x,y);
reset(x,y+1);
ty=y+1;
if(tx<0||tx>19||ty<0||ty>19)
return;
x=tx;
y=ty;
set(x,y,data);
life=3;
}
else if(g[x][y-1]=='O')
{
reset(x,y);
reset(x,y-1);
ty=y-1;
if(tx<0||tx>19||ty<0||ty>19)
return;
x=tx;
y=ty;
set(x,y,data);
life=3;
}
else
{
int step,tx=x,ty=y;
step=rand()%4;
if(step==0)
tx=x+1;
if(step==1)
tx=x-1;
if(step==2)
ty=y+1;
if(step==3)
ty=y-1;
if(tx<0||tx>19||ty<0||ty>19)
return;
if(g[tx][ty]=='.')
{
reset(x,y);
x=tx;
y=ty;
set(x,y,data);
}
life--;
}
breeds--;
}
}
vector<ant>ants;
vector<bug>bugs;
void play();
int main()
{
srand(time(NULL));
for(int i=0; i<20; i++)
for(int j=0; j<20; j++)
g[i][j]='.';
int n1,n2;
cout<<"enter the ants number"<<endl;
cin>>n1;
cout<<"enter the bugs number"<<endl;
cin>>n2;
for(int i=0; i<n1; i++)
{
ant a;
ants.push_back(a);
}
for(int i=0; i<n2; i++)
{
bug b;
bugs.push_back(b);
}
printg();
system("cls");
play();
return 0;
}
void ant::breed()
{
int tx,ty;
if (breeds<=0)
{
if(g[x+1][y]=='.')
{
tx=x+1;
ty=y;
if(tx<0||tx>19||ty<0||ty>19)
return;
ant bant;
reset(bant.x,bant.y);
bant.x=tx;
bant.y=ty;
set(bant.x,bant.y,data);
ants.push_back(bant);
breeds=3;
}
else if(g[x-1][y]=='.')
{
tx=x-1;
ty=y;
if(tx<0||tx>19||ty<0||ty>19)
return;
ant bant;
reset(bant.x,bant.y);
bant.x=tx;
bant.y=ty;
set(bant.x,bant.y,data);
ants.push_back(bant);
breeds=3;
}
else if(g[x][y+1]=='.')
{
tx=x;
ty=y+1;
if(tx<0||tx>19||ty<0||ty>19)
return;
ant bant;
reset(bant.x,bant.y);
bant.x=tx;
bant.y=ty;
set(bant.x,bant.y,data);
ants.push_back(bant);
breeds=3;
}
else if(g[x][y-1]=='.')
{
tx=x;
ty=y-1;
if(tx<0||tx>19||ty<0||ty>19)
return;
ant bant;
reset(bant.x,bant.y);
bant.x=tx;
bant.y=ty;
set(bant.x,bant.y,data);
ants.push_back(bant);
breeds=3;
}
}
}
void bug::breed()
{
int tx,ty;
if (breeds<=0)
{
if(g[x+1][y]=='.')
{
tx=x+1;
ty=y;
if(tx<0||tx>19||ty<0||ty>19)
return;
bug bbug;
reset(bbug.x,bbug.y);
bbug.x=tx;
bbug.y=ty;
set(bbug.x,bbug.y,data);
bugs.push_back(bbug);
breeds=8;
}
else if(g[x-1][y]=='.')
{
tx=x-1;
ty=y;
if(tx<0||tx>19||ty<0||ty>19)
return;
bug bbug;
reset(bbug.x,bbug.y);
bbug.x=tx;
bbug.y=ty;
set(bbug.x,bbug.y,data);
bugs.push_back(bbug);
breeds=8;
}
else if(g[x][y+1]=='.')
{
tx=x;
ty=y+1;
if(tx<0||tx>19||ty<0||ty>19)
return;
bug bbug;
reset(bbug.x,bbug.y);
bbug.x=tx;
bbug.y=ty;
set(bbug.x,bbug.y,data);
bugs.push_back(bbug);
breeds=8;
}
else if(g[x][y-1]=='.')
{
tx=x;
ty=y-1;
if(tx<0||tx>19||ty<0||ty>19)
return;
bug bbug;
reset(bbug.x,bbug.y);
bbug.x=tx;
bbug.y=ty;
set(bbug.x,bbug.y,data);
bugs.push_back(bbug);
breeds=8;
}
}
}
void play()
{
int x=0;
while((ants.size()!=0)||(bugs.size()!=0))
{
for (int i=0; i<bugs.size(); i++)
{
if (bugs[i].dead()==true)
{
bugs.erase(bugs.begin()+i);
if(bugs.size()==0)
{
break;
}
}
else
{
bugs[i].sdie();
bugs[i].move();
bugs[i].breed();
}
}
for (int i=0; i<ants.size(); i++)
{
if (ants[i].dead()==true)
{
ants.erase(ants.begin()+i);
if(ants.size()==0)
{
break;
}
}
else
{
ants[i].sdie();
ants[i].move();
ants[i].breed();
}
}
printg();
system("pause");
system("cls");
x++;
}
cout<<"game over after no "<<x<<" turns"<<endl;
}
just help me run-time error and logical errors


See More: run time error in my code

Report •


#1
June 12, 2011 at 10:58:43
#include <iostream>
#include<vector>
#include<cstdlib>
#include<ctime>
using namespace std;
char g[20][20];
void printg()
{
for(int i=0; i<20; i++)
{
for(int j=0; j<20; j++)
cout<<g[i][j]<<" ";
cout<<"\n";
}
}
void reset(int x,int y)
{
g[x][y]='.';
}
void set(int x,int y,char data)
{
g[x][y]=data;
}
class organism
{
public:
bool dead()
{
if(life==0)
{
reset(x,y);
return true;
}
else
return false;
}
void sdie();
virtual void move()=0;
virtual void breed()=0;
protected:
int x,y,life,breeds;
char data;
};
class ant :public organism
{
public:
void move();
void breed();
ant()
{
do
{
x=rand()%20;
y=rand()%20;
life=1;
breeds=3;
data='O';
}
while(g[x][y]!='.');
set (x,y,data);
}
};
void ant::move()
{
if(life!=0)
{
int step,tx=x,ty=y;
step=rand()%4;
if(step==0)
tx=x+1;
if(step==1)
tx=x-1;
if(step==2)
ty=y+1;
if(step==3)
ty=y-1;
if(tx<0||tx>19||ty<0||ty>19)
return;
if(g[tx][ty]=='.')
{
reset(x,y);
x=tx;
y=ty;
set(x,y,data);
}
breeds--;
}
}
void organism::sdie()
{
if(g[x][y]!=data)
life=0;
}
class bug :public organism
{
public:
void move();
void breed();
bug()
{
do
{
x=rand()%20;
y=rand()%20;
life=3;
breeds=8;
data='X';
}
while(g[x][y]!='.');
set (x,y,data);
}
};
void bug::move()
{
int tx=x,ty=y;
if(g[x+1][y]=='O')
{
reset(x,y);
reset(x+1,y);
tx=x+1;
if(tx<0||tx>19||ty<0||ty>19)
return;
x=tx;
y=ty;
set(x,y,data);
life=3;
}
else if(g[x-1][y]=='O')
{
reset(x,y);
reset(x-1,y);
tx=x-1;
if(tx<0||tx>19||ty<0||ty>19)
return;
x=tx;
y=ty;
set(x,y,data);
life=3;
}
else if(g[x][y+1]=='O')
{
reset(x,y);
reset(x,y+1);
ty=y+1;
if(tx<0||tx>19||ty<0||ty>19)
return;
x=tx;
y=ty;
set(x,y,data);
life=3;
}
else if(g[x][y-1]=='O')
{
reset(x,y);
reset(x,y-1);
ty=y-1;
if(tx<0||tx>19||ty<0||ty>19)
return;
x=tx;
y=ty;
set(x,y,data);
life=3;
}
else
{
int step,tx=x,ty=y;
step=rand()%4;
if(step==0)
tx=x+1;
if(step==1)
tx=x-1;
if(step==2)
ty=y+1;
if(step==3)
ty=y-1;
if(tx<0||tx>19||ty<0||ty>19)
return;
if(g[tx][ty]=='.')
{
reset(x,y);
x=tx;
y=ty;
set(x,y,data);
}
life--;
}
breeds--;
}
class human :public organism//human only stomp nearst organism
{
public:
void move();
void breed()
{

}
human()
{
do
{
x=rand()%20;
y=rand()%20;
life=0;
breeds=0;
data='H';
}
while(g[x][y]!='.');
set (x,y,data);
}
};
void human::move()
{
int tx=x,ty=y;
if(g[x+1][y]=='O'||g[x+1][y]=='X')
{
reset(x,y);
reset(x+1,y);
tx=x+1;
if(tx<0||tx>19||ty<0||ty>19)
return;
x=tx;
y=ty;
set(x,y,data);
}
else if(g[x-1][y]=='O'||g[x-1][y]=='X')
{
reset(x,y);
reset(x-1,y);
tx=x-1;
if(tx<0||tx>19||ty<0||ty>19)
return;
x=tx;
y=ty;
set(x,y,data);
}
else if(g[x][y+1]=='O'||g[x][y+1]=='X')
{
reset(x,y);
reset(x,y+1);
ty=y+1;
if(tx<0||tx>19||ty<0||ty>19)
return;
x=tx;
y=ty;
set(x,y,data);
}
else if(g[x][y-1]=='O'||g[x][y-1]=='X')
{
reset(x,y);
reset(x,y-1);
ty=y-1;
if(tx<0||tx>19||ty<0||ty>19)
return;
x=tx;
y=ty;
set(x,y,data);
life=3;
}
}
vector<ant>ants;
vector<bug>bugs;
void play();
int main()
{
srand(time(NULL));
for(int i=0; i<20; i++)
for(int j=0; j<20; j++)
g[i][j]='.';
int n1,n2;
cout<<"enter the ants number"<<endl;
cin>>n1;
cout<<"enter the bugs number"<<endl;
cin>>n2;
for(int i=0; i<n1; i++)
{
ant a;
ants.push_back(a);
}
for(int i=0; i<n2; i++)
{
bug b;
bugs.push_back(b);
}
printg();
system("cls");
play();
return 0;
}
void ant::breed()
{
if(life!=0)
{
int tx,ty;
if (breeds==0)
{
if(g[x+1][y]=='.')
{
tx=x+1;
ty=y;
if(tx<0||tx>19||ty<0||ty>19)
return;
ant bant;
reset(bant.x,bant.y);
bant.x=tx;
bant.y=ty;
set(bant.x,bant.y,data);
ants.push_back(bant);
breeds=3;
}
else if(g[x-1][y]=='.')
{
tx=x-1;
ty=y;
if(tx<0||tx>19||ty<0||ty>19)
return;
ant bant;
reset(bant.x,bant.y);
bant.x=tx;
bant.y=ty;
set(bant.x,bant.y,data);
ants.push_back(bant);
breeds=3;
}
else if(g[x][y+1]=='.')
{
tx=x;
ty=y+1;
if(tx<0||tx>19||ty<0||ty>19)
return;
ant bant;
reset(bant.x,bant.y);
bant.x=tx;
bant.y=ty;
set(bant.x,bant.y,data);
ants.push_back(bant);
breeds=3;
}
else if(g[x][y-1]=='.')
{
tx=x;
ty=y-1;
if(tx<0||tx>19||ty<0||ty>19)
return;
ant bant;
reset(bant.x,bant.y);
bant.x=tx;
bant.y=ty;
set(bant.x,bant.y,data);
ants.push_back(bant);
breeds=3;
}
}
}
}
void bug::breed()
{
int tx,ty;
if (breeds==0)
{
if(g[x+1][y]=='.')
{
tx=x+1;
ty=y;
if(tx<0||tx>19||ty<0||ty>19)
return;
bug bbug;
reset(bbug.x,bbug.y);
bbug.x=tx;
bbug.y=ty;
set(bbug.x,bbug.y,data);
bugs.push_back(bbug);
breeds=8;
}
else if(g[x-1][y]=='.')
{
tx=x-1;
ty=y;
if(tx<0||tx>19||ty<0||ty>19)
return;
bug bbug;
reset(bbug.x,bbug.y);
bbug.x=tx;
bbug.y=ty;
set(bbug.x,bbug.y,data);
bugs.push_back(bbug);
breeds=8;
}
else if(g[x][y+1]=='.')
{
tx=x;
ty=y+1;
if(tx<0||tx>19||ty<0||ty>19)
return;
bug bbug;
reset(bbug.x,bbug.y);
bbug.x=tx;
bbug.y=ty;
set(bbug.x,bbug.y,data);
bugs.push_back(bbug);
breeds=8;
}
else if(g[x][y-1]=='.')
{
tx=x;
ty=y-1;
if(tx<0||tx>19||ty<0||ty>19)
return;
bug bbug;
reset(bbug.x,bbug.y);
bbug.x=tx;
bbug.y=ty;
set(bbug.x,bbug.y,data);
bugs.push_back(bbug);
breeds=8;
}
}
}
void play()
{
human H;
int x=0;
while((ants.size()!=0)&&(bugs.size()!=0))
{
for (int i=0; i<bugs.size(); i++)
{
if (bugs[i].dead()==true)
{
bugs.erase(bugs.begin()+i);
if(bugs.size()==0)
{
cout<<"game over after no "<<x<<" turns"<<endl;
return;
}
}
else
{
bugs[i].move();
bugs[i].breed();
}
}
for (int j=0; j<ants.size(); j++)
{
if (ants[j].dead()==true)
{
ants.erase(ants.begin()+j);
if(ants.size()==0)
{
cout<<"game over after no "<<x<<" turns"<<endl;
return;
}
}
else
{
ants[j].sdie();
ants[j].move();
ants[j].breed();
}
}
H.move();
printg();
system("pause");
system("cls");
x++;
}
cout<<"game over after no "<<x<<" turns"<<endl;
}
that is the answer to my own problem the site was no help at all fixed it myself


Report •
Related Solutions


Ask Question