Megan
Megan

Reputation: 129

Vector iterator not looping through entire vector

Edit: The problem was solved by both iterating by index and passing the vectors to the functions as a reference.

I am creating a cellular automata-like program. There are two classes - ants and doodlebugs. Ants are represented by char 'O' and doodlebugs are represented by char 'X'. Empty spaces on the 2D char array are '.'

I have a dynamic vector to hold pointers to objects as the amount of objects increases and decreases as time goes on.

I'm currently testing my Ant class. After three movement steps, the Ant breeds. These two functions work, so I'm testing the last Ant function - dying.

I was testing an initial function to kill an ant (simply by flipping it from a letter to '.' in the array) but it doesn't seem to be iterating through my entire vector of ants. It works as expected if I do not have the ants 'breed.'

For example, I start with 6 Ants. I have them move around, then kill them all. It works.

I start with 6 Ants, let them move 3 times, breed, then try to kill them all. Only some of the objects "die" (actually turn to a '.')

I'm assuming the issue is the breeding function - is something about how I'm adding a new object interfering with the iteration?

Here are the relevant bits of code:

class Ant : public Organism {
    Ant(char array[][20]) {
            this->x = rand() % 20;
            this->y = rand() % 20;
            array[x][y] = 'O';
            this->count = 0;
    }
    Ant(char array[][20], int itsX, int itsY) {
            this->x = itsX;
            this->y = itsY;
            array[x][y] = 'O';
            this->count = 0;
    }

    // If adjacent cell = '.', push back a new Ant object
    void breed(char array[][20], std::vector<Ant*> colony) {
            if (this->count == 2) {
                    if(!occupied_down(array)) { 
                            Ant* temp = new Ant(array, x+1, y);
                            colony.push_back(temp);
                    } else if(!occupied_up(array)) {
                            Ant* temp = new Ant(array, x-1, y);
                            colony.push_back(temp);
                    } else if(!occupied_right(array)) {
                            Ant* temp = new Ant(array, x, y+1);
                            colony.push_back(temp);
                    } else if(!occupied_left(array)) {
                            Ant* temp = new Ant(array, x, y-1);
                            colony.push_back(temp);
                    }
                    this->count = 0;
            }

    }
    void die(char array[][20]) {
            array[this->x][this->y] = '.';
    }
};

 void moveAnts(char step[][20], std::vector<Ant*> colony) {

    std::vector<Ant*>::iterator itr;
    for(itr = colony.begin(); itr < colony.end(); ++itr) {
            Ant* temp = *itr;
            temp->move(step);
    }
 }

void breedAnts(char step[][20], std::vector<Ant*> colony) {

    std::vector<Ant*>::iterator itr;
    for(itr = colony.begin(); itr < colony.end(); ++itr) {
           Ant* temp = *itr;
           temp->breed(step, colony);
   }
 }

void killAnts(char step[][20], std::vector<Ant*> colony) {

    std::vector<Ant*>::iterator itr;
    for(itr = colony.begin(); itr < colony.end(); ++itr) {
            Ant* temp = *itr;
            temp->die(step);
    }
 }

 int main() {

    srand(time(NULL));

    char step[20][20];

    for(int i = 0; i < 20; i++) {
            for(int j = 0; j < 20; j++) {
                    step[i][j] = '.';
            }
    }

    std::vector<Ant*> colony;

    for(int i = 0; i < 6; i++) {
            Ant* test = new Ant(step);
            colony.push_back(test);
    }

    for(int i = 0; i < 4; i++) {
            print(step);
            moveAnts(step, colony);
            breedAnts(step, colony);
    }

    killAnts(step, colony);

    print(step);

    return 0;
 }

Upvotes: 0

Views: 231

Answers (1)

Nipun Talukdar
Nipun Talukdar

Reputation: 5387

Change your functions to take reference of the vectors. You seem to pass them by value. Eg. change

void moveAnts(char step[][20], std::vector<Ant*> colony)

to

void moveAnts(char step[][20], std::vector<Ant*>& colony)

Upvotes: 3

Related Questions