I am currently trying an Ant simulation.
The area looks like this:
class Area
{
public:
Area(Area *north, Area *east, Area *south, Area *west);
~Area();
Area* getSouth();
Area* getEast();
Area* getWest();
Area* getNorth();
void setSouth(Area* south);
void setEast(Area* east);
void setWest(Area* west);
void setNorth(Area* north);
void printInfo();
std::list<Item*> Liste;
private:
Area *south, *east, *west, *north;
};
And inside the list I store the objects that are currently on the field, like ants, food, anthill.
When I want to move an Ant from one field to another I used this code:
void Ant::move(){
// Random movement
srand(time(NULL))
int direction = rand() % 4; // 0.. north, 1.. east, 2.. south, 3.. west
cout << direction << endl;
if(direction == 0 && moved == 0){
Area* help = this->area->getNorth();
cout << "I want to move to " << help << endl;
if(help != NULL){
this->area->Liste.remove(this);
this->area = this->area->getNorth();
this->age++;
this->moved=1;
this->area->Liste.push_back(this);
cout << "moved north to " << this->area << endl;
}
}
However this doesn't work, and I think the problem lies within the list and my approach to move the ant to a neighboring field. The first ant can change the field (or so it seems by the console output) but the next field procudes a segmentation fault during execution.
How can I make this movement better? I am grateful for any tips.