1.11M Members

Game of Life - Pointer / Dynamic_cast / Method problem

 
0
 

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];
}
 
0
 

dynamic_cast < type-id > ( expression )

<ant *> would be a type-id correct?

 
0
 

I would also like to point out that line 8 doesnt work.

(world->getOrganism(i, j-1) != NULL)

crashes when the value is returned.

 
0
 

you need to make sure you are in the bounds of the array before you call getOrganism(). You could also add some coe to you getOrganism() function and have it check to see if the posistion asked for is within the array. If it is not then it should return null and it it is then it should return what was at the location.

getOrganism(int x, int y)
{
    if(x < 0 || y < 0)
        return NULL;
    if (x > numberOfRows - 1 || y > numberOfCols - 1)
        return NULL;
    return grid[x][y];
}
 
0
 

Yes but the code breaks even if I force feed it good values. It seems like maybe the problem is caused because the grid doesn't transfer over into the world class properly.

I'll post some more of my code to give you a better idea of whats going on.

Main

#include <iostream>
#include <ctime>
#include "World.h"
#include "Ant.h"
#include "Organism.h"
#include "Lion.h"
#include <cstdlib>
#include <time.h>
#include <Windows.h>



using namespace std;


int main()
{   
    clock_t t1,t2;
    t1=clock();



    int intTotal=0;
    string random;
    World theWorld;
    ant theAnt;
    lion theLion;
    Organism *oAnt = &theAnt;
    Organism *oLion = &theLion;

    int k=0;

    // Populate grid full of ants
    for( int i=0;  i<GRID_WIDTH; i++ )
    {
        for( int j=0; j<GRID_HEIGHT; j++ )
        {
            theWorld.setOrganism(oAnt,i,j);
        }

    }

    //set the 5 lions.
    theWorld.setOrganism(oLion,0,0);
    theWorld.setOrganism(oLion,3,2);
    theWorld.setOrganism(oLion,15,19);
    theWorld.setOrganism(oLion,19,2);
    theWorld.setOrganism(oLion,17,11);






    //while(true){
    //intTotal++;
    //Sleep (500);


    theWorld.move();

    cout << theWorld << endl;
    cout << "#:" << intTotal << endl;
    //}











    cin.get ();

    return 0;

}

World.cpp

#include <iostream>
#include "World.h"
#include "Organism.h"
#include "Ant.h"
#include "Lion.h"
using namespace std;
World::World()
{   
    for( int i=0; i<GRID_WIDTH; i++ )
    {
        for( int j=0; j<GRID_HEIGHT; j++ )
        {
            grid[i][j] = NULL;
        }
    }
}
World::~World()
{
    //free up allocated memory
    for( int i=0; i<GRID_WIDTH; i++ )
    {
        for( int j=0; j<GRID_HEIGHT; j++ )
        {
            if( grid[i][j] != NULL )
            {
                delete grid[i][j];
            }
        }
    }
}

Organism *World::getOrganism( int x, int y )
{
    return grid[15][15];
}


void World::setOrganism( Organism *organism, int x, int y )
{
    grid[x][y] = organism;
}
void World::move()
{
    // object method call priority:
    // 1 - Move all lions


    for( int i=0; i<GRID_WIDTH; i++ )
    {
        for( int j=0; j<GRID_HEIGHT; j++ )
        {
            // verify organism is a lion and that the lion
            // has not already moved once, before moving
            if( (dynamic_cast<lion *>(grid[i][j]) != NULL) && (grid[i][j]->isTurn()) )
            {
                grid[i][j]->move();
            }
        }
    }
    // 2 - Move all ants
    for( int i=0; i<GRID_WIDTH; i++ )
    {
        for( int j=0; j<GRID_HEIGHT; j++ )
        {
            // verify organism is an ant and that the ant
            // has not already moved once, before moving
            if( (dynamic_cast<ant *>(grid[i][j]) != NULL) &&
                (grid[i][j]->isTurn()) )
            {
                grid[i][j]->move();
            }
        }
    }
    // 3 - Breed all eligible lions
    for( int i=0; i<GRID_WIDTH; i++ )
    {
        for( int j=0; j<GRID_HEIGHT; j++ )
        {
            // verify organism is a lion and that it's the lion's
            // turn before attempting to breed
            if( (dynamic_cast<lion *>(grid[i][j]) != NULL) &&
                (grid[i][j]->isTurn()) )
            {
                grid[i][j]->breed();
            }
        }
    }
    // 4 - Breed all eligible ants
    for( int i=0; i<GRID_WIDTH; i++ )
    {
        for( int j=0; j<GRID_HEIGHT; j++ )
        {
            // verify organism is an ant and that it's the ant's
            // turn before attempting to breed
            if( (dynamic_cast<ant *>(grid[i][j]) != NULL) &&
                (grid[i][j]->isTurn()) )
            {
                grid[i][j]->breed();
            }
        }
    }
    // 6 - Starve all eligible lions
    for( int i=0; i<GRID_WIDTH; i++ )
    {
        for( int j=0; j<GRID_HEIGHT; j++ )
        {
            // verify organism is a lion and that it's the lion's
            // turn before attempting to starve
            if( (dynamic_cast<lion *>(grid[i][j]) != NULL) &&
                (grid[i][j]->isTurn()) )
            {
                (dynamic_cast<lion *>(grid[i][j])->starve());
            }
        }
    }
    // 7 - End all the organism's turns
    for( int i=0; i<GRID_WIDTH; i++ )
    {
        for( int j=0; j<GRID_HEIGHT; j++ )
        {
            // verify presence of organism
            if( grid[i][j] != NULL )
            {
                grid[i][j]->endTurn();
            }
        }
    }
    // 8 - All remaining organisms have "survived" this step. Increment each organism's counter by one.
    for( int i=0; i<GRID_WIDTH; i++ )
    {
        for( int j=0; j<GRID_HEIGHT; j++ )
        {
            // verify presence of organism
            if( grid[i][j] != NULL )
            {
                grid[i][j]->incCounter();
            }
        }
    }
}
ostream& operator<<( ostream &output, World &world )
{
    // display grid
    for( int i=0; i<GRID_WIDTH; i++ )
    {
        for( int j=0; j<GRID_HEIGHT; j++ )
        {
            output << world.grid[i][j];
        }
        output << endl;
    }
    return output;
}

world.h

#ifndef _WORLD_H
#define _WORLD_H
#include <iostream>
using namespace std;
class Organism;
const int GRID_WIDTH = 20;
const int GRID_HEIGHT = 20;
class World

{
public:
    Organism *grid[GRID_WIDTH][GRID_HEIGHT];
public:
    World();
    virtual ~World();
    Organism *getOrganism( int x, int y );
    void setOrganism( Organism *organism, int x, int y );
    void move();
    friend ostream& operator<<( ostream &output, World &world );
};
#endif
You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article