DoodleBug

DoodleBug Project

DoodleBug_Image

DoodleBug

Bug simulation program in c++
Program outline

The goal for this Project is to create a simple two-dimensional predator-prey simulation. In this simulation the prey are ants and the predators are doodlebugs. These critters live in a world composed of a 20 × 20 grid of cells. Only one critter may occupy a cell at a time. The grid is enclosed, so a critter is not allowed to move off the edges of the world. Time is simulated in time steps. Each critter performs some action every time step.

The ants behave according to the following model:

Move. Every time step, randomly try to move up, down, left, or right. If the neighboring cell in the selected direction is occupied or would move the ant off the grid, then the ant stays in the current cell.

Breed. If an ant survives for three-time steps, then at the end of the time step (that is; after moving) the ant will breed. This is simulated by creat-ing a new ant in an adjacent (up, down, left, or right) cell that is empty. If there is no empty cell available, then no breeding occurs. Once an off-spring is produced, an ant cannot produce an offspring until three more time steps have elapsed. The doodlebugs behave according to the following model:

Move. Every time step, if there is an adjacent ant (up, down, left, or right), then the doodlebug will move to that cell and eat the ant. Otherwise, the doodlebug moves according to the same rules as the ant. Note that a doodlebug cannot eat other doodlebugs.

Breed. If a doodlebug survives for eight time steps, then at the end of the time step it will spawn off a new doodlebug in the same manner as the ant.

Starve. If a doodlebug has not eaten an ant within the last three time steps, then at the end of the third time step it will starve and die. The doodlebug should then be removed from the grid of cells.

During one turn, all the doodlebugs should move before the ants do.

Write a program to implement this simulation and draw the world using ASCII characters of “o” for an ant and “X” for a doodlebug or “-” for an empty space. Create a class named Organism that encapsulates basic data common to both ants and doodlebugs. This class should have a virtual function named move that is defined in the derived classes of Ant and Doodlebug. You may need additional data structures to keep track of which critters have moved.

Initialize the world with 5 doodlebugs and 100 ants. After each time step, prompt the user to press Enter to move to the next time step. You should see a cyclical pattern between the population of predators and prey, although random perturbations may lead to the elimination of one or both species.

				
					#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <time.h>

using namespace std;

const int GARDENSIZE = 20;
const int STARTING_ANTS = 100;
const int STARTING_BUGS = 5;
const int DOODLEBUG_ID = 2;
const int ANT_ID = 1;
const int DOODLEBUG_BREED = 8;
const int DOODLEBUG_STARVE = 3;
const int ANT_BREED = 3;
const char ANT_CHAR = 'O';
const char DOODLEBUG_CHAR = 'X';
const char EMPTY_SPACE = '-';

class Organism;
class Ant;
class Doodlebug;

class Garden{
    friend class Organism;
    friend class Doodlebug;
    friend class Ant;
    public:
        Garden();
        void printGarden();
        Organism* getAtLoc(int x, int y);
        void setAtLoc(int x, int y, Organism *org);
    private:
        Organism* grid[GARDENSIZE][GARDENSIZE];
};

Garden::Garden(){
    for(int i = 0; i < GARDENSIZE; i++){
        for(int j = 0; j < GARDENSIZE; j++){
            grid[i][j] = 0;
        }
    }
}

class Organism{
    friend class Garden;
    public:
        Organism();
        ~Organism();
        int getType(){return type;};
        virtual bool getStarved() = 0;
        virtual int getStarveLevel() = 0;
        virtual void setStarved() = 0;
        virtual void increaseStarveLevel(){};
        virtual void resetStarveLevel(){};
        bool getMoved(){return moved;};
        void setMoved(bool didMove){moved = didMove;};
        virtual void move(int i, int j, Organism* currentLoc, Garden &g){};
        void incAliveFor(){aliveFor++;};
        int getAliveFor(){return aliveFor;};
        virtual void breed(int i, int j, Organism* currentLoc, Garden &g){};
    protected:
        int aliveFor;
        int type;
        bool moved;
};

Organism::Organism(){
    aliveFor = 0;
    moved = false;
}

Organism::~Organism(){}

class Ant : public Organism{
    public:
        Ant();
        bool getStarved(){return false;};
        int getStarveLevel(){return 0;};
        void setStarved(){};
        void increaseStarveLevel(){};
        void breed(int i, int j, Organism* currentLoc, Garden &g);
        void move(int i, int j, Organism* currentLoc, Garden &g);
};

Ant::Ant() : Organism() {
    type = ANT_ID;
}

void Ant::move(int i, int j, Organism* currentLoc, Garden &g){
    int randDir = rand() % 4 + 1; //North = 1, East = 2, South = 3, West = 4;
    switch (randDir){
        case 1: //North
            if(i == 0){
                break;
            }
            else{
                if(g.getAtLoc(i - 1, j) == NULL){
                    g.setAtLoc(i - 1, j, currentLoc);
                    currentLoc = g.getAtLoc(i - 1, j);
                    g.setAtLoc(i, j, NULL);
                }
            }
            break;
        case 2: //East
            if(j == GARDENSIZE - 1){
                break;
            }
            else{
                if(g.getAtLoc(i, j + 1) == NULL){
                    g.setAtLoc(i, j + 1, currentLoc);
                    currentLoc = g.getAtLoc(i, j + 1);
                    g.setAtLoc(i, j, NULL);
                }
            }
            break;
        case 3: //South
            if(i == GARDENSIZE - 1){
                break;
            }
            else{
                if(g.getAtLoc(i + 1, j) == NULL){
                    g.setAtLoc(i + 1, j, currentLoc);
                    currentLoc = g.getAtLoc(i + 1, j);
                    g.setAtLoc(i, j, NULL);
                }
            }
            break;
        case 4: //West
            if(j == 0){
                break;
            }
            else{
                if(g.getAtLoc(i, j - 1) == NULL){
                    g.setAtLoc(i, j - 1, currentLoc);
                    currentLoc = g.getAtLoc(i, j - 1);
                    g.setAtLoc(i, j, NULL);
                }
            }
        break;
    }
}

void Ant::breed(int i, int j, Organism* currentLoc, Garden &g){
    if(currentLoc->getAliveFor() % ANT_BREED == 0){
        Ant *a = new Ant();
        if(i > 0){//Up
            if(g.getAtLoc(i - 1, j) == NULL){
                g.setAtLoc(i - 1, j, a);
            }
        }
        else if (i < GARDENSIZE - 1){//Down
            if(g.getAtLoc(i + 1, j) == NULL){
                g.setAtLoc(i + 1, j, a);
            }
        }
        else if (j > 0){//Left
            if(g.getAtLoc(i, j - 1) == NULL){
                g.setAtLoc(i, j - 1, a);
            }
        }
        else if (i < GARDENSIZE - 1){//Right
            if(g.getAtLoc(i, j + 1) == NULL){
                g.setAtLoc(i, j + 1, a);
            }
        }
    }
}

class Doodlebug : public Organism{
    public:
        Doodlebug();
        void increaseStarveLevel(){starveLevel++;};
        int getStarveLevel(){return starveLevel;};
        void setStarved();
        bool getStarved(){return starved;};
        void move(int i, int j, Organism* currentLoc, Garden &g);
        void resetStarveLevel(){starveLevel = 0;};
        void breed(int i, int j, Organism* currentLoc, Garden &g);
    private:
        bool starved;
        int starveLevel;
};

Doodlebug::Doodlebug() : Organism() {
    type = DOODLEBUG_ID;
    starveLevel = 0;
}

void Doodlebug::move(int i, int j, Organism* currentLoc, Garden &g){
    int randDir = rand() % 4 + 1; //North = 1, East = 2, South = 3, West = 4;
    switch (randDir){
        case 1: //North
            if(i == 0){
                currentLoc->increaseStarveLevel();
                break;
            }
            else{
                if(g.getAtLoc(i - 1, j) == NULL){
                    g.setAtLoc(i - 1, j, currentLoc);
                    currentLoc = g.getAtLoc(i - 1, j);
                    g.setAtLoc(i, j, NULL);
                    currentLoc->increaseStarveLevel();
                }
                else if(g.getAtLoc(i - 1, j)->getType() == ANT_ID){
                    g.setAtLoc(i - 1, j, currentLoc);
                    currentLoc = g.getAtLoc(i - 1, j);
                    g.setAtLoc(i, j, NULL);
                    currentLoc->resetStarveLevel();
                }
                else{
                    currentLoc->increaseStarveLevel();
                }
            }
            break;
        case 2: //East
            if(j == GARDENSIZE - 1){
                currentLoc->increaseStarveLevel();
                break;
            }
            else{
                if(g.getAtLoc(i, j + 1) == NULL){
                    g.setAtLoc(i, j + 1, currentLoc);
                    currentLoc = g.getAtLoc(i, j + 1);
                    g.setAtLoc(i, j, NULL);
                    currentLoc->increaseStarveLevel();
                }
                else if(g.getAtLoc(i, j + 1)->getType() == ANT_ID){
                    g.setAtLoc(i, j + 1, currentLoc);
                    currentLoc = g.getAtLoc(i, j + 1);
                    g.setAtLoc(i, j, NULL);
                    currentLoc->resetStarveLevel();
                }
                else{
                    currentLoc->increaseStarveLevel();
                }
            }
            break;
        case 3: //South
            if(i == GARDENSIZE - 1){
                currentLoc->increaseStarveLevel();
                break;
            }
            else{
                if(g.getAtLoc(i + 1, j) == NULL){
                    g.setAtLoc(i + 1, j, currentLoc);
                    currentLoc = g.getAtLoc(i + 1, j);
                    g.setAtLoc(i, j, NULL);
                    currentLoc->increaseStarveLevel();
                }
                else if(g.getAtLoc(i + 1, j)->getType() == ANT_ID){
                    g.setAtLoc(i + 1, j, currentLoc);
                    currentLoc = g.getAtLoc(i + 1, j);
                    g.setAtLoc(i, j, NULL);
                    currentLoc->resetStarveLevel();
                }
                else{
                    currentLoc->increaseStarveLevel();
                }
            }
            break;
        case 4: //West
            if(j == 0){
                currentLoc->increaseStarveLevel();
                break;
            }
            else{
                if(g.getAtLoc(i, j - 1) == NULL){
                    g.setAtLoc(i, j - 1, currentLoc);
                    currentLoc = g.getAtLoc(i, j - 1);
                    g.setAtLoc(i, j, NULL);
                    currentLoc->increaseStarveLevel();
                }
                else if(g.getAtLoc(i, j - 1)->getType() == ANT_ID){
                    g.setAtLoc(i, j - 1, currentLoc);
                    currentLoc = g.getAtLoc(i, j - 1);
                    g.setAtLoc(i, j, NULL);
                    currentLoc->resetStarveLevel();
                }
                else{
                    currentLoc->increaseStarveLevel();
                }
            }
            break;
    }
}

void Doodlebug::breed(int i, int j, Organism* currentLoc, Garden &g){
    if(currentLoc->getAliveFor() % DOODLEBUG_BREED == 0){
        Doodlebug *b = new Doodlebug();
        if(i > 0){//Up
            if(g.getAtLoc(i - 1, j) == NULL){
                g.setAtLoc(i - 1, j, b);
            }
        }
        else if (i < GARDENSIZE - 1){//Down
            if(g.getAtLoc(i + 1, j) == NULL){
                g.setAtLoc(i + 1, j, b);
            }
        }
        else if (j > 0){//Left
            if(g.getAtLoc(i, j - 1) == NULL){
                g.setAtLoc(i, j - 1, b);
            }
        }
        else if (i < GARDENSIZE - 1){//Right
            if(g.getAtLoc(i, j + 1) == NULL){
                g.setAtLoc(i, j + 1, b);
            }
        }
    }
}

void Doodlebug::setStarved(){
    starved = true;
}

void Garden::printGarden(){
    for(int i = 0; i < GARDENSIZE; i++){
        for(int j = 0; j < GARDENSIZE; j++){
            if(grid[i][j] == NULL){
                cout << EMPTY_SPACE;
            }
            else{
                switch(grid[i][j]->getType()){
                    case ANT_ID:
                        cout << ANT_CHAR;
                        break;
                    case DOODLEBUG_ID:
                        cout << DOODLEBUG_CHAR;
                        break;
                }
            }
        }
        cout << endl;
    }
}

Organism* Garden::getAtLoc(int x, int y){
    return grid[x][y];
}

void Garden::setAtLoc(int x, int y, Organism *org){
    grid[x][y] = org;
}

void resetMoved(Garden &g);
void killStarved(Garden &g);
void moveBugs(Garden &g);
void moveAnts(Garden &g);
void breedAnts(Garden &g);
void breedBugs(Garden &g);

int tick = 0;

int main(){
    srand(time(NULL));
    Garden g;
    int totalAnts = 0;
    int totalBugs = 0;
    while(totalAnts < STARTING_ANTS){
        int randX = rand() % (GARDENSIZE - 1) + 0;
        int randY = rand() % (GARDENSIZE - 1) + 0;
        Ant *a = new Ant();
        if(g.getAtLoc(randX, randY) == 0){
            g.setAtLoc(randX, randY, a);
            totalAnts++;
        }
    }
    while(totalBugs < STARTING_BUGS){
        int randX = rand() % (GARDENSIZE - 1) + 0;
        int randY = rand() % (GARDENSIZE - 1) + 0;
        Doodlebug *b = new Doodlebug();
        if(g.getAtLoc(randX, randY) == NULL){
            g.setAtLoc(randX, randY, b);
            totalBugs++;
        }
    }

    g.printGarden();

    string userInput;

    cout << endl << "To exit type \"Exit\"" << endl;
    getline(cin, userInput);

    while(userInput != "Exit"){
        resetMoved(g);
        killStarved(g);
        moveBugs(g);
        breedBugs(g);
        moveAnts(g);
        breedAnts(g);
        tick++;
        g.printGarden();
        cout << endl << "Tick: " << tick;
        cout << endl << "To exit type \"Exit\"" << endl;
        getline(cin, userInput);
    }
}

void resetMoved(Garden &g){
    for(int i = 0; i < GARDENSIZE; i++){
        for(int j = 0; j < GARDENSIZE; j++){
            Organism* currentLoc = g.getAtLoc(i, j);
            if(currentLoc != NULL){
                currentLoc->setMoved(false);
            }
        }
    }
}

void moveBugs(Garden &g){
    for(int i = 0; i < GARDENSIZE; i++){
        for(int j = 0; j < GARDENSIZE; j++){
            Organism* currentLoc = g.getAtLoc(i, j);
            if(currentLoc != NULL){
                if(currentLoc->getType() == DOODLEBUG_ID && currentLoc->getMoved() == false){
                    currentLoc->move(i, j, currentLoc, g);
                    currentLoc->setMoved(true);
                    currentLoc->incAliveFor();
                    if(currentLoc->getStarveLevel()%DOODLEBUG_STARVE == 0 && currentLoc->getStarveLevel() > 0){
                        currentLoc->setStarved();
                    }
                }
            }
        }
    }
}

void killStarved(Garden &g){
    for(int i = 0; i < GARDENSIZE; i++){
        for(int j = 0; j < GARDENSIZE; j++){
            Organism* currentLoc = g.getAtLoc(i, j);
            if(currentLoc != NULL){
                if(currentLoc->getType() == DOODLEBUG_ID){
                    if(currentLoc->getStarved() == true){
                        g.setAtLoc(i,j,NULL);
                    }
                }
            }
        }
    }
}

void moveAnts(Garden &g){
    for(int i = 0; i < GARDENSIZE; i++){
        for(int j = 0; j < GARDENSIZE; j++){
            Organism* currentLoc = g.getAtLoc(i, j);
            if(currentLoc != NULL){
                if(currentLoc->getType() == ANT_ID && currentLoc->getMoved() == false){
                    currentLoc->move(i, j, currentLoc, g);
                    currentLoc->setMoved(true);
                    currentLoc->incAliveFor();
                }
            }
        }
    }
}

void breedBugs(Garden &g){
    for(int i = 0; i < GARDENSIZE; i++){
        for(int j = 0; j < GARDENSIZE; j++){
            Organism* currentLoc = g.getAtLoc(i, j);
            if(currentLoc != NULL){
                if(currentLoc->getType() == DOODLEBUG_ID){
                    currentLoc->breed(i, j, currentLoc, g);
                }
            }
        }
    }
}

void breedAnts(Garden &g){
    for(int i = 0; i < GARDENSIZE; i++){
        for(int j = 0; j < GARDENSIZE; j++){
            Organism* currentLoc = g.getAtLoc(i, j);
            if(currentLoc != NULL){
                if(currentLoc->getType() == ANT_ID){
                    currentLoc->breed(i, j, currentLoc, g);
                }
            }
        }
    }
}
				
			
Comments are closed.