#include "World.h" // ====================== // World constructor, destructor // These classes initialize the array and // releases any classes created when destroyed. // ====================== World::World() { // Initialize world to empty spaces int i,j; for (i=0; i=0) && (x=0) && (y=0) && (x=0) && (ygetType()==ANT) cout << "o"; else cout << "X"; } cout << endl; } } // ====================== // SimulateOneStep // This is the main routine that simulates one turn in the world. // First, a flag for each organism is used to indicate if it has moved. // This is because we iterate through the grid starting from the top // looking for an organism to move . If one moves down, we don't want // to move it again when we reach it. // First move doodlebugs, then ants, and if they are still alive then // we breed them. // ====================== void World::SimulateOneStep() { int i,j; // First reset all organisms to not moved for (i=0; imoved = false; } // Loop through cells in order and move if it's a Doodlebug for (i=0; igetType()==DOODLEBUG)) { if (grid[i][j]->moved == false) { grid[i][j]->moved = true; // Mark as moved grid[i][j]->move(); } } } // Loop through cells in order and move if it's an Ant for (i=0; igetType()==ANT)) { if (grid[i][j]->moved == false) { grid[i][j]->moved = true; // Mark as moved grid[i][j]->move(); } } } // Loop through cells in order and check if we should breed for (i=0; igetType()==DOODLEBUG)) { if (grid[i][j]->starve()) { delete (grid[i][j]); grid[i][j] = NULL; } } } // Loop through cells in order and check if we should breed for (i=0; imoved==true)) { grid[i][j]->breed(); } } }