I'd make two classes: Doodlebug and Ant.
Each class should have a linked list structure. (doubly-linked would be better suited to this problem.) Whenever a new ant is born, you insert a new ant into the linked list. Whenever an ant dies, you likewise delete the ant from the linked list. (This is where the double-linking comes in handy.)
Each class should have integer variables like "StepsUntilDeath" and "StepsUntilBreeding". You decrement each at the end of a time step, or reset them after successful breeding or eating.
A good structure might be something like this:
-
class Doodlebug{
-
private:
-
Doodlebug* pPreviousDoodleBug;
-
Doodlebug* pNextDoodleBug;
-
-
int StepsUntilBreeding;
-
int StepsUntilDeath;
-
public:
-
int PositionX;
-
int PositionY;
-
-
Doodlebug();
-
~Doodlebug();
-
bool MoveDoodlebug();
-
bool BreedDoodlebug();
-
bool UpdateGridWithPosition();
-
bool UpdateDoodlebug();
-
}
-
I'd use the "UpdateDoodlebug()" function to do everything: call the "move" function and try to move, try to eat, try to breed, and update the grid. (remove any markers from the previous cell, and add a marker to the new cell.)
You could update every ant and every doodlebug by running through both linked lists. One caution, though: running through the linked list in the same order every time can introduce bias that you'll observe in the results! It might be better to update in a random order. (This problem has been observed in cellular automata and agent-based scientific modeling/computation.)
It looks like you'll want an array to keep track of what's occupying each square. You could just as well make it a character array with entries "E" (empty), "A" (ant), or "D" (doodlebug). Or you could make your 2D array a
bitmap image and save it as every time step, so you could easily view your simulation and create animations later. Of course, more complex structures are possible. (e.g., an array of Ant memory addresses, and an array of Doodlebug memory addresses. If there's no ant, it's NULL, and otherwise, it has a pointer to the precise ant occupying that cell. This would make it easier to determine which ant is eaten by a Doodlebug.)
Simpler structures are possible, too, of course. You could just go with an integer array. 0-99 denotes an ant, and 100-199 denotes a doodlebug. Any negative number is unoccupied. Then, different values in that range can encode both the time until the next breeding and the time until death.
Have fun with your project--it sounds like a blast!! -- Paul