Question
Hey guys I keep getting this error: Thread 1: EXC_BAD_ACCESS (code=1, address=0x0) When I call grid[x][y]->getType() The first of these errors comes up on line
Hey guys I keep getting this error:
Thread 1: EXC_BAD_ACCESS (code=1, address=0x0)
When I call grid[x][y]->getType()
The first of these errors comes up on line 242 as shown here:
Note: If I try to replace this->grid[x[[y] with grid[x][y] it does not work either
My code is below, please help thanks!
#include
#include
#include
#include
#include
using namespace std;
class board;
class creature;
class ant;
class doodlebug;
//Indicates the integer that represents each creature
const int antInd= 1;
const int doodlebugInd = 2;
int ants = 0;
int doodlebugs = 0;
int empty=0;
class board {
friend class creature;
friend class ant;
friend class doodlebug;
public:
board(){};
creature* grid[20][20];
void createGrid();
void initialize(int& ants, int& doodlebugs, int& empty);
void simTurn();
void printGrid();
protected:
board* thisBoard;
};
class creature: public board {
friend class board;
public:
creature(): turnsWithoutFood(0), turnsSurvived(0){}
creature(board* boardptr,int x, int y) {};
creature(const creature &creatureTwo)
: x(creatureTwo.x), y(creatureTwo.y), turnsSurvived(creatureTwo.turnsSurvived), turnsWithoutFood(creatureTwo.turnsWithoutFood){};
creature &operator=(const creature & creatureTwo) {
x = creatureTwo.x;
y = creatureTwo.y;
turnsWithoutFood = creatureTwo.turnsWithoutFood;
turnsSurvived = creatureTwo.turnsSurvived;
return *this;
}
// virtual ~ creature();
void breed(int x, int y);
void starve(int x, int y);
// if(this->type==1)
// return 1;
// else {
// return 2;
// }
// };
private:
int type;
virtual int getType() = 0;
int x,y;
int turnsWithoutFood;
int turnsSurvived;
};
class ant: public creature {
friend class board;
public:
ant(): creature(), turnsSurvived(0){}
private:
int x,y;
ant(board* boardptr, int x,int y);
int getType() {
return 1;
}
int turnsWithoutFood;
int turnsSurvived;
int type=1;
void breed(int x, int y) ;
};
class doodlebug: public creature {
friend class board;
public:
doodlebug(): creature(), turnsWithoutFood(0), turnsSurvived(0){}
private:
int turnsWithoutFood;
int turnsSurvived;
int type=2;
doodlebug(board* boardptr, int x, int y) {};
int getType() {
return 2;
}
void breed(int x, int y);
void starve(int x, int y);
void eat() ;
};
void board::createGrid() {
srand(time(NULL));
for(int i=0;i
for(int j=0;j
grid[i][j]= NULL;
}
}
}
void board::initialize(int& ants, int& doodlebugs, int& empty) {
this->createGrid();
while(doodlebugs
int x = rand()%20;
int y= rand()%20;
// creature* thisCreature = grid[x][y];
if(grid[x][y] == NULL)
// delete thisCreature;
grid[x][y] = new doodlebug(this,x,y);
doodlebugs++;
}
while(ants
int x = rand()%20;
int y= rand()%20;
// creature* thisCreature = grid[x][y];
if(grid[x][y] == NULL) {
// delete thisCreature;
grid[x][y] = new ant(this,x,y);
ants++;
}
}
}
void board::printGrid() {
for(int i=0;i
cout
for(int j=0;j
if(grid[i][j] == NULL) {
cout
}
else if(grid[i][j]->getType() == 2) {
cout
}
else {
cout
}
}
}
cout
}
ant::ant(board*, int, int)
{
/othing
}
void ant::breed( int x, int y) {
if(grid[x+1][y] == NULL) {
grid[x+1][y] = new ant(this,x+1,y);
ant *thisAnt = (ant *)grid[x+1][y];
thisAnt->turnsSurvived=0;
ants++;
}
else if(grid[x-1][y] == NULL) {
grid[x-1][y] = new ant(this,x-1,y);
ants++;
ant *thisAnt = (ant *)grid[x-1][y];
thisAnt->turnsSurvived=0;
}
else if(grid[x][y+1] == NULL) {
grid[x][y+1] = new ant(this,x,y+1);
ants++;
ant * thisAnt = (ant *)grid[x][y+1];
thisAnt->turnsSurvived=0;
}
else if(grid[x][y-1] == NULL) {
grid[x][y-1] = new ant(this,x,y-1);
ants++;
ant * thisAnt = (ant *)grid[x][y-1];
thisAnt->turnsSurvived=0;
}
}
void doodlebug::breed(int x, int y) {
if(grid[x+1][y]==NULL) {
grid[x+1][y] = new doodlebug(this,x+1,y);
doodlebugs++;
doodlebug* thisDoodlebug = (doodlebug *)grid[x+1][y];
thisDoodlebug->turnsSurvived=0;
thisDoodlebug->turnsWithoutFood=0;
}
else if(grid[x-1][y]==NULL) {
grid[x-1][y] = new doodlebug(this,x-1,y);
doodlebugs++;
doodlebug* thisDoodlebug = (doodlebug *)grid[x-1][y];
thisDoodlebug->turnsSurvived=0;
thisDoodlebug->turnsWithoutFood=0;
}
else if(grid[x][y+1] == NULL) {
grid[x][y+1] = new doodlebug(this,x,y+1);
doodlebugs++;
doodlebug* thisDoodlebug = (doodlebug *)grid[x][y+1];
thisDoodlebug->turnsSurvived=0;
thisDoodlebug->turnsWithoutFood=0;
}
else if(grid[x][y-1]== NULL) {
grid[x][y+1] = new doodlebug(this,x,y-1);
doodlebugs++;
doodlebug* thisDoodlebug = (doodlebug *)grid[x][y-1];
thisDoodlebug->turnsSurvived=0;
thisDoodlebug->turnsWithoutFood=0;
}
}
void doodlebug::starve(int x, int y) {
delete this;
doodlebugs--;
empty++;
}
void board::simTurn() {
int counter=0;
int right = 1;
int left = 2;
int up = 3;
int down = 4;
int x = 0;
int y = 0;
while(counter
if((this->grid[x][y])->getType() == antInd) {
ant* thisAnt = (ant* )grid[x][y];
int dir = rand()%4;
if(x==0) {
if(dir==2) {
dir=1;
}
}
if(y==0) {
if(dir==4) {
dir=3;
}
}
if(x==19) {
if(dir==1) {
dir=2;
}
}
if(y==19) {
if(dir==3) {
dir=4;
}
}
if(thisAnt->turnsSurvived==3) {
thisAnt->breed(x,y);
}
if(dir==right) {
if(grid[x+1][y] == NULL) {
// thisAnt = (ant*)grid[x+1][y];
// thisAnt->x+=1;
grid[x+1][y] = thisAnt;
ant* newAnt= thisAnt;
delete thisAnt;
newAnt->turnsSurvived++;
}
else if(grid[x+1][y]->getType() == doodlebugInd){
delete thisAnt;
//ant is eaten
ants--;
thisAnt->turnsSurvived=0;
}
else {
thisAnt->turnsSurvived++;
}
}
if(dir==left) {
if(grid[x-1][y] == NULL) {
// thisAnt = (ant*)grid[x-1][y];
// thisAnt->x-=1;
// delete grid[x][y];
// thisAnt->turnsSurvived++;
// thisAnt = (ant*)grid[x+1][y];
// thisAnt->x+=1;
grid[x-1][y] = thisAnt;
ant* newAnt= thisAnt;
delete thisAnt;
newAnt->turnsSurvived++;
}
else if(grid[x-1][y]->getType() == doodlebugInd){
delete thisAnt;
//ant is eaten
ants--;
thisAnt->turnsSurvived=0;
}
else {
thisAnt->turnsSurvived++;
}
}
if(dir==up) {
if(grid[x][y+1] == NULL) {
// thisAnt = (ant*)grid[x][y+1];
// thisAnt->y+=1;
// delete grid[x][y];
// thisAnt->turnsSurvived++;
// thisAnt = (ant*)grid[x+1][y];
// thisAnt->x+=1;
grid[x][y+1] = thisAnt;
ant* newAnt= thisAnt;
delete thisAnt;
newAnt->turnsSurvived++;
}
else if(grid[x][y+1]->getType() == doodlebugInd){
delete thisAnt;
//ant is eaten
ants--;
thisAnt->turnsSurvived=0;
}
else {
thisAnt->turnsSurvived++;
}
}
if(dir==down) {
if(grid[x][y-1] == NULL) {
// thisAnt = (ant*)grid[x][y-1];
// thisAnt->y-=1;
// delete grid[x][y];
// thisAnt->turnsSurvived++;
// thisAnt = (ant*)grid[x+1][y];
// thisAnt->x+=1;
grid[x][y-1] = thisAnt;
ant* newAnt= thisAnt;
delete thisAnt;
newAnt->turnsSurvived++;
}
else if(grid[x][y-1]->getType() == doodlebugInd){
delete thisAnt;
//ant is eaten
ants--;
thisAnt->turnsSurvived=0;
}
else {
thisAnt->turnsSurvived++;
}
}
// }
if(x==19) {
y++;
}
else {
x++;
}
counter++;
}
else if(grid[x][y]->getType() == doodlebugInd) {
doodlebug* thisDoodlebug = (doodlebug *)grid[x][y];
if(thisDoodlebug->turnsSurvived==8) {
thisDoodlebug->breed(x,y);
}
if(thisDoodlebug->turnsWithoutFood==3) {
thisDoodlebug->starve(x,y);
}
int dir = rand()%4;
if(x==0) {
if(dir==2) {
dir=1;
}
}
if(y==0) {
if(dir==4) {
dir=3;
}
}
if(x==19) {
if(dir==1) {
dir=2;
}
}
if(y==19) {
if(dir==3) {
dir=4;
}
}
if(dir==right) {
if(grid[x+1][y]== NULL) {
// thisDoodlebug = (doodlebug *)grid[x][y];
// thisDoodlebug->x+=1;
// delete grid[x][y];
// thisDoodlebug->turnsWithoutFood++;
// thisDoodlebug->turnsSurvived++;
grid[x+1][y] = thisDoodlebug;
doodlebug* newDoodlebug= thisDoodlebug;
delete thisDoodlebug;
newDoodlebug->turnsSurvived++;
newDoodlebug->turnsWithoutFood++;
}
else if(grid[x+1][y]->getType() == antInd) {
thisDoodlebug->turnsWithoutFood=0;
thisDoodlebug->turnsSurvived++;
}
else {
//do nothing
thisDoodlebug->turnsWithoutFood++;
thisDoodlebug->turnsSurvived++;
}
}
else if(dir==left) {
if(grid[x-1][y]== NULL) {
// thisDoodlebug = (doodlebug *)grid[x][y];
// thisDoodlebug->x-=1;
// delete grid[x][y];
// thisDoodlebug->turnsWithoutFood++;
// thisDoodlebug->turnsSurvived++;
grid[x-1][y] = thisDoodlebug;
doodlebug* newDoodlebug= thisDoodlebug;
delete thisDoodlebug;
newDoodlebug->turnsSurvived++;
newDoodlebug->turnsWithoutFood++;
}
else if(grid[x-1][y]->getType() == antInd) {
thisDoodlebug->turnsWithoutFood=0;
thisDoodlebug->turnsSurvived++;
}
else {
//do nothing
thisDoodlebug->turnsWithoutFood++;
thisDoodlebug->turnsSurvived++;
}
}
else if(dir==up) {
if(grid[x][y+1]== NULL) {
// thisDoodlebug = (doodlebug *)grid[x][y];
// thisDoodlebug->y+=1;
// delete grid[x][y];
// thisDoodlebug->turnsWithoutFood++;
// thisDoodlebug->turnsSurvived++;
grid[x][y+1] = thisDoodlebug;
doodlebug* newDoodlebug= thisDoodlebug;
delete thisDoodlebug;
newDoodlebug->turnsSurvived++;
newDoodlebug->turnsWithoutFood++;
}
else if(grid[x][y+1]->getType() == antInd) {
thisDoodlebug->turnsWithoutFood=0;
thisDoodlebug->turnsSurvived++;
}
else {
//do nothing
thisDoodlebug->turnsWithoutFood++;
thisDoodlebug->turnsSurvived++;
}
}
if(dir==down) {
if(grid[x][y-1]== NULL) {
// thisDoodlebug = (doodlebug *)grid[x][y];
// thisDoodlebug->y-=1;
// delete grid[x][y];
// thisDoodlebug->turnsWithoutFood++;
// thisDoodlebug->turnsSurvived++;
grid[x][y-1] = thisDoodlebug;
doodlebug* newDoodlebug= thisDoodlebug;
delete thisDoodlebug;
newDoodlebug->turnsSurvived++;
newDoodlebug->turnsWithoutFood++;
}
else if(grid[x][y-1]->getType() == antInd) {
thisDoodlebug->turnsWithoutFood=0;
thisDoodlebug->turnsSurvived++;
}
else {
//do nothing
thisDoodlebug->turnsWithoutFood++;
thisDoodlebug->turnsSurvived++;
}
}
if(x==19) {
y++;
}
else {
x++;
}
counter++;
}
}
}
int main() {
srand(time(NULL));
board Board;
Board.initialize(ants,doodlebugs,empty);
Board.printGrid();
bool gameOver = false;
while(gameOver==false) {
cout
string key;
cin>>key;
if(key=="enter") {
Board.simTurn();
Board.printGrid();
}
else if(key=="stop") {
gameOver=true;
}
}
}
88hw)main.cpp) M board:simTurn intxe int y- while (countergridlx]Iy])->getType)antInd) Thread 1: EXC_BAD_ACCE. ant* thisAnt-(ant* )grid[x][y]; int dir- rand ()964 ; if (x0) { if(dir:#2 ) dir-1; if(y 0) if(dir4) 1 dir-3; if(x--19) if(dir--1) ( dir-2
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started