Hello, I'm working on an assignment and I'm having a bit of trouble.
Around line 12 it calls a world->getOrganism(int,int)
However no matter what value of grid[][] I return it crashes.
The if() should be checking to see if that particular grid cordinate is of the ant class.
(dynamic_cast<ant *>(world->getOrganism(i, j-1)) != NULL) should be = to false
However it just crashes as soon as I return the value.
I've been trying to debug this using breakpoints for the last 5 hours, and I'm still no where.
Lion problem Method
void lion::move()
{ int i=0;
int j=0;
int direction;
// eat the first ant that the lion comes across, and move into its space
if( // the space is occupied,
(world->getOrganism(i, j-1) != NULL) &&
// the space isn't over the edge of the grid,
(i >= 0) && (j-1 >=0) && (i < GRID_WIDTH) && (j-1 < GRID_HEIGHT) &&
// and the space is occupied by an ant,
(dynamic_cast<ant *>(world->getOrganism(i, j-1)) != NULL) )
{
// replace the ant with the lion
eat(i, j-1);
eaten = true;
}
else if((world->getOrganism(x, y+1) != NULL) && (x >= 0) && (y+1 >=0) &&
(x < GRID_WIDTH) && (y+1 < GRID_HEIGHT) && (dynamic_cast<ant *>(world->getOrganism(x, y+1)) != NULL)) // SOUTH
{
eat(x, y+1);
eaten = true;
}
else if((world->getOrganism(x+1, y) != NULL) && (x+1 >= 0) && (y >=0) &&
(x+1 < GRID_WIDTH) && (y < GRID_HEIGHT) && (dynamic_cast<ant *>(world->getOrganism(x+1, y)) != NULL)) // EAST
{
eat(x+1, y);
eaten = true;
}
else if((world->getOrganism(x-1, y) != NULL) && (x-1 >= 0) && (y >=0) &&
(x-1 < GRID_WIDTH) && (y < GRID_HEIGHT) && (dynamic_cast<ant *>(world->getOrganism(x-1, y)) != NULL)) // WEST
{
eat(x-1, y);
eaten = true;
}
else if((world->getOrganism(x+1, y-1) != NULL) && (x+1 >= 0) && (y-1 >=0) &&
(x+1 < GRID_WIDTH) && (y-1 < GRID_HEIGHT) && (dynamic_cast<ant *>(world->getOrganism(x+1, y-1)) != NULL)) // NORTHEAST
{
eat(x+1, y-1);
eaten = true;
}
else if((world->getOrganism(x-1, y-1) != NULL) && (x-1 >= 0) && (y-1 >=0) &&
(x-1 < GRID_WIDTH) && (y-1 < GRID_HEIGHT) && (dynamic_cast<ant *>(world->getOrganism(x-1, y-1)) != NULL)) // NORTHWEST
{
eat(x-1, y-1);
eaten = true;
}
else if((world->getOrganism(x+1, y+1) != NULL) && (x+1 >= 0) && (y+1 >=0) &&
(x+1 < GRID_WIDTH) && (y+1 < GRID_HEIGHT) && (dynamic_cast<ant *>(world->getOrganism(x+1, y+1)) != NULL)) // SOUTHEAST
{
eat(x+1, y+1);
eaten = true;
}
else if((world->getOrganism(x-1, y+1) != NULL) && (x-1 >= 0) && (y+1 >=0) &&
(x-1 < GRID_WIDTH) && (y+1 < GRID_HEIGHT) && (dynamic_cast<ant *>(world->getOrganism(x-1, y+1)) != NULL)) // SOUTHWEST
{
eat(x-1, y+1);
eaten = true;
}
else // move normally
{
direction = rand() % NUM_DIRECTIONS;
switch( direction )
{
case NORTH:
newX = x;
newY = y - 1;
break;
case SOUTH:
newX = x;
newY = y + 1;
break;
case EAST:
newX = x + 1;
newY = y;
break;
case WEST:
newX = x - 1;
newY = y;
break;
default:
break;
}
// check limits of the grid
if( newX < 0 ) newX = 0;
if( newY < 0 ) newY = 0;
if( newX >= GRID_WIDTH ) newX = GRID_WIDTH - 1;
if( newY >= GRID_HEIGHT ) newY = GRID_HEIGHT - 1;
// move lion to new location
if( world->getOrganism( newX, newY ) == NULL )
{
world->setOrganism( this, newX, newY );
world->setOrganism( NULL, x, y );
x = newX;
y = newY;
}
}
}
The world->getOrganism Method
Organism *World::getOrganism( int x, int y )
{
return grid[x][y];
}