Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You will be creating a simple two-dimensional predator-prey simulation. In this

ID: 3604316 • Letter: Y

Question

You will be creating 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 x20 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. Doodlebugs eat Ants. 2 The ants behave according to the following model Move. Every time step, randomly try to move up, down, left, or right. (hints: you can use a random number generator to generate integer from 1 to 4, 1-right, 2-down, 3 = left, and 4 -up). 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 creating 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 offspring is produced, an ant cannot produce an offspring until three more time steps have elapsed The doodlebugs behave according to the following model

Explanation / Answer

#include <iostream>
#include <vector>
#include <stdlib.h>
#include <time.h>

class Organism{

private:
   int x;
   int y;
   int id;
   int roundsSurvived;
   bool hasMoved;
   bool hasEaten;
public:
   Organism(int x, int y, int id){
       this->x = x;
       this->y = y;
       this->id = id;
       this->roundsSurvived = 0;
       this->hasMoved = false;
       this->hasEaten = false;
   }

   virtual ~Organism() {}

   virtual void move(Organism* grid[20][20]) = 0;

   virtual char getType()const = 0;

   virtual void breed(Organism* grid[20][20], int&) = 0;

   virtual void starve(Organism* grid[20][20]){
       if (getRoundsSurvived() == 3){
           if (getIfAte() == false){
               delete grid[getX()][getY()];
               grid[getX()][getY()] = NULL;
           }
       }
   }

   // Getters
   int getX()const{ return x; }
   int getY()const{ return y; }
   int getID()const{ return id; }
   int getRoundsSurvived()const{ return roundsSurvived; }
   bool getIfMoved()const{ return hasMoved; }
   bool getIfAte()const{ return hasEaten; }

   // Setters
   void setX(int x){ this->x = x; }
   void setY(int y){ this->y = y; }
   void setID(int id){ this->id = id; }
   void setRoundsSurvived(int roundsSurvived){ this->roundsSurvived = roundsSurvived; }
   void setIfMoved(bool condition){ this->hasMoved = condition; }
   void setIfAte(bool condition){ this->hasEaten = condition; }

};

class Doodlebug: public Organism{
private:
   char type;
public:
   Doodlebug(int x, int y, int id): Organism(x, y, id), type('X') {}

   virtual ~Doodlebug(){}

   char getType()const{ return type; }

   void move(Organism* grid[20][20]);

   void breed(Organism* grid[20][20], int& doodlebugID);

   void starve(Organism* grid[20][20]);

};

void Doodlebug::move(Organism* grid[20][20]){
   /*
   1. Randomize a direction
       (.) Let's define the directions by the following integers
           (a) Up: 1
           (b) Down: 2
           (c) Left: 3
           (d) Right: 4
   2. Depending on which direction you're going make sure it's a valid direction
       (a) A direction is valid if the doodlebug doesn't fall off the grid
   3. Swap x, y with that point in the grid by creating a new object and marking
   that object as one that moved
   //*/

   int direction = rand() % 4 + 1;

  
   if (direction == 1 && getX()-1 >= 0 && getIfMoved() == false){

       if (grid[getX()-1][getY()] == NULL){
           int newX = getX()-1;
           int newY = getY();
           int newID = getID();
           int newRoundsSurvived = getRoundsSurvived()+1;

           delete grid[getX()][getY()];
           grid[getX()][getY()] = NULL;

           grid[newX][newY] = new Doodlebug(newX, newY, newID);
           grid[newX][newY]->setRoundsSurvived(newRoundsSurvived);
           grid[newX][newY]->setIfMoved(true);
      
           return;
       }

       else if (grid[getX()-1][getY()]->getType() == 'o'){
           int newX = getX()-1;
           int newY = getY();
           int newID = getID();
           int newRoundsSurvived = getRoundsSurvived()+1;

           delete grid[getX()][getY()];
           delete grid[newX][newY];
           grid[getX()][getY()] = NULL;

           grid[newX][newY] = new Doodlebug(newX, newY, newID);
           grid[newX][newY]->setRoundsSurvived(newRoundsSurvived);
           grid[newX][newY]->setIfMoved(true);
           grid[newX][newY]->setIfAte(true);
          
           return;
       }
   }

   if (direction == 2 && getX()+1 <= 19 && getIfMoved() == false){

       if (grid[getX()+1][getY()] == NULL){
           int newX = getX()+1;
           int newY = getY();
           int newID = getID();
           int newRoundsSurvived = getRoundsSurvived()+1;

           delete grid[getX()][getY()];
           grid[getX()][getY()] = NULL;

           grid[newX][newY] = new Doodlebug(newX, newY, newID);
           grid[newX][newY]->setRoundsSurvived(newRoundsSurvived);
           grid[newX][newY]->setIfMoved(true);

           return;
       }
       else if (grid[getX()+1][getY()]->getType() == 'o'){
           int newX = getX()+1;
           int newY = getY();
           int newID = getID();
           int newRoundsSurvived = getRoundsSurvived()+1;

           delete grid[getX()][getY()];
           delete grid[newX][newY];
           grid[getX()][getY()] = NULL;

           grid[newX][newY] = new Doodlebug(newX, newY, newID);
           grid[newX][newY]->setRoundsSurvived(newRoundsSurvived);
           grid[newX][newY]->setIfMoved(true);
           grid[newX][newY]->setIfAte(true);

           return;
       }
   }

   if (direction == 3 && getY()-1 >= 0 && getIfMoved() == false){

       if (grid[getX()][getY()-1] == NULL){
           int newX = getX();
           int newY = getY()-1;
           int newID = getID();
           int newRoundsSurvived = getRoundsSurvived()+1;

           delete grid[getX()][getY()];
           delete grid[newX][newY];
           grid[getX()][getY()] = NULL;

           grid[newX][newY] = new Doodlebug(newX, newY, newID);
           grid[newX][newY]->setRoundsSurvived(newRoundsSurvived);
           grid[newX][newY]->setIfMoved(true);

           return;
       }
       else if (grid[getX()][getY()-1]->getType() == 'o'){
           int newX = getX();
           int newY = getY()-1;
           int newID = getID();
           int newRoundsSurvived = getRoundsSurvived()+1;

           delete grid[getX()][getY()];
           delete grid[newX][newY];
           grid[getX()][getY()] = NULL;

           grid[newX][newY] = new Doodlebug(newX, newY, newID);
           grid[newX][newY]->setRoundsSurvived(newRoundsSurvived);
           grid[newX][newY]->setIfMoved(true);
           grid[newX][newY]->setIfAte(true);
      
           return;
       }
   }

   if (direction == 4 && getY()+1 <= 19 && getIfMoved() == false){

       if (grid[getX()][getY()+1] == NULL){
           int newX = getX();
           int newY = getY()+1;
           int newID = getID();
           int newRoundsSurvived = getRoundsSurvived()+1;

           delete grid[getX()][getY()];
           grid[getX()][getY()] = NULL;

           grid[newX][newY] = new Doodlebug(newX, newY, newID);
           grid[newX][newY]->setRoundsSurvived(newRoundsSurvived);
           grid[newX][newY]->setIfMoved(true);
          
           return;
       }
       else if (grid[getX()][getY()+1]->getType() == 'o'){
           int newX = getX();
           int newY = getY()+1;
           int newID = getID();
           int newRoundsSurvived = getRoundsSurvived()+1;

           delete grid[getX()][getY()];
           delete grid[newX][newY];
           grid[getX()][getY()] = NULL;

           grid[newX][newY] = new Doodlebug(newX, newY, newID);
           grid[newX][newY]->setRoundsSurvived(newRoundsSurvived);
           grid[newX][newY]->setIfMoved(true);
           grid[newX][newY]->setIfAte(true);
      
           return;
       }
   }

   setRoundsSurvived(getRoundsSurvived()+1);
}

void Doodlebug::breed(Organism* grid[20][20], int& doodlebugID){

   setRoundsSurvived(0);

   if (grid[getX()-1][getY()] == NULL){
       ++doodlebugID;
       grid[getX()-1][getY()] = new Doodlebug(getX()-1, getY(), doodlebugID);
   }
   else if (grid[getX()+1][getY()] == NULL){
       ++doodlebugID;
       grid[getX()+1][getY()] = new Doodlebug(getX()+1, getY(), doodlebugID);
   }
   else if (grid[getX()][getY()-1] == NULL){
       ++doodlebugID;
       grid[getX()][getY()-1] = new Doodlebug(getX(), getY()-1, doodlebugID);
   }
   else if (grid[getX()][getY()+1] == NULL){
       ++doodlebugID;
       grid[getX()][getY()+1] = new Doodlebug(getX(), getY()+1, doodlebugID);
   }
}


void Doodlebug::starve(Organism* grid[20][20]){
   /*
   Implement this after implementing the ant class
   //*/
   if (getRoundsSurvived() == 3){
       if (getIfAte() == false){
           delete grid[getX()][getY()];
           grid[getX()][getY()] = NULL;
       }
   }
}

class Ant: public Organism{
private:
   char type;
public:
   Ant(int x, int y, int id): Organism(x, y, id), type('o'){}

   virtual ~Ant(){}

   char getType()const{ return type; }

   void move(Organism* grid[20][20]);

   void breed(Organism* grid[20][20], int&);
};

void Ant::move(Organism* grid[20][20]){
   int direction = rand() % 4 + 1;

  
   if (direction == 1 && getX()-1 >= 0 && getIfMoved() == false){

       if (grid[getX()-1][getY()] == NULL){
           int newX = getX()-1;
           int newY = getY();
           int newID = getID();
           int newRoundsSurvived = getRoundsSurvived()+1;

           delete grid[getX()][getY()];
           grid[getX()][getY()] = NULL;

           grid[newX][newY] = new Ant(newX, newY, newID);
           grid[newX][newY]->setRoundsSurvived(newRoundsSurvived);
           grid[newX][newY]->setIfMoved(true);
          
           return;
       }
   }

   if (direction == 2 && getX()+1 <= 19 && getIfMoved() == false){

       if (grid[getX()+1][getY()] == NULL){
           int newX = getX()+1;
           int newY = getY();
           int newID = getID();
           int newRoundsSurvived = getRoundsSurvived()+1;

           delete grid[getX()][getY()];
           grid[getX()][getY()] = NULL;

           grid[newX][newY] = new Ant(newX, newY, newID);
           grid[newX][newY]->setRoundsSurvived(newRoundsSurvived);
           grid[newX][newY]->setIfMoved(true);

           return;
       }
   }

   if (direction == 3 && getY()-1 >= 0 && getIfMoved() == false){

       if (grid[getX()][getY()-1] == NULL){
           int newX = getX();
           int newY = getY()-1;
           int newID = getID();
           int newRoundsSurvived = getRoundsSurvived()+1;

           delete grid[getX()][getY()];
           grid[getX()][getY()] = NULL;

           grid[newX][newY] = new Ant(newX, newY, newID);
           grid[newX][newY]->setRoundsSurvived(newRoundsSurvived);
           grid[newX][newY]->setIfMoved(true);

           return;
       }
   }

   if (direction == 4 && getY()+1 <= 19 && getIfMoved() == false){

       if (grid[getX()][getY()+1] == NULL){
           int newX = getX();
           int newY = getY()+1;
           int newID = getID();
           int newRoundsSurvived = getRoundsSurvived()+1;

           delete grid[getX()][getY()];
           grid[getX()][getY()] = NULL;

           grid[newX][newY] = new Ant(newX, newY, newID);
           grid[newX][newY]->setRoundsSurvived(newRoundsSurvived);
           grid[newX][newY]->setIfMoved(true);

           return;
       }
   }
   setRoundsSurvived(getRoundsSurvived()+1);
}

void Ant::breed(Organism* grid[20][20], int& antID){
   setRoundsSurvived(0);

   if (grid[getX()-1][getY()] == NULL){
       ++antID;
       grid[getX()-1][getY()] = new Ant(getX()-1, getY(), antID);
   }
   else if (grid[getX()+1][getY()] == NULL){
       ++antID;
       grid[getX()+1][getY()] = new Ant(getX()+1, getY(), antID);
   }
   else if (grid[getX()][getY()-1] == NULL){
       ++antID;
       grid[getX()][getY()-1] = new Ant(getX(), getY()-1, antID);
   }
   else if (grid[getX()][getY()+1] == NULL){
       ++antID;
       grid[getX()][getY()+1] = new Ant(getX(), getY()+1, antID);
   }
}


void initialize(Organism* grid[20][20]){

   for (int i = 0; i < 20; i++){
       for (int j = 0; j < 20; j++){
           grid[i][j] = NULL;
       }
   }
}

void render(Organism* grid[20][20]){
   for (int i = 0; i < 20; i++){
       for (int j = 0; j < 20; j++){
           if (grid[i][j] == NULL){
               std::cout << "-" << " ";
           }
           else if (grid[i][j]->getType() == 'X'){
               std::cout << "X" << " ";
           }
           else if (grid[i][j]->getType() == 'o'){
               std::cout << "o" << " ";
           }
       }
       std::cout << std::endl;
   }

   std::cout << std::endl;
}

int countTheDoodles(Organism* grid[20][20]){

   int count = 0;

   for (int i = 0; i < 20; i++){
       for (int j = 0; j < 20; j++){
           if (grid[i][j] != NULL){
               if (grid[i][j]->getType() == 'D'){
                   count++;
               }
           }
       }
   }

   return count;
}

int countTheAnts(Organism* grid[20][20]){

   int count = 0;

   for (int i = 0; i < 20; i++){
       for (int j = 0; j < 20; j++){
           if (grid[i][j] != NULL){
               if (grid[i][j]->getType() == 'o'){
                   count++;
               }
           }
       }
   }

   return count;
}

int main(){

   Organism* grid[20][20];

   initialize(grid);

   std::srand(time(NULL));

   // Create 5 doodlebugs on the heap
   int doodlebugCount = 0;
   int doodlebugID = 0;

   while (doodlebugCount < 5){
       // Get a random x and y coordinate for the grid
       int randomX = rand() % 20;
       int randomY = rand() % 20;

       if (grid[randomX][randomY] == NULL){
           grid[randomX][randomY] = new Doodlebug(randomX, randomY, doodlebugID);
           doodlebugCount++;
           doodlebugID++;
       }
   }

   // Create 100 ants on the heap
   int antCount = 0;
   int antID = 100;

   while (antCount < 100){
       int randomX = rand() % 20;
       int randomY = rand() % 20;

       if (grid[randomX][randomY] == NULL){
           grid[randomX][randomY] = new Ant(randomX, randomY, antID);
           antCount++;
           antID++;
       }
   }

   std::cout << "Intial Grid" << std::endl;
   render(grid);

   bool flag = true;

   int step = 0;

   while (flag){

       std::cout << "Press Enter to continue..." << std::endl;
       std::cin.ignore();

       ++step;

       std::cout << "******Time Step: " << step << "******" << std::endl;

       for (int i = 0; i < 20; i++){
           for (int j = 0; j < 20; j++){
               if (grid[i][j] != NULL){
                   if (grid[i][j]->getType() == 'D' && !grid[i][j]->getIfMoved()){
                       grid[i][j]->move(grid);
                   }
               }
              
           }
       }

       for (int i = 0; i < 20; i++){
           for (int j = 0; j < 20; j++){
               if (grid[i][j] != NULL){
                   if (grid[i][j]->getType() == 'o' && !grid[i][j]->getIfMoved()){
                       grid[i][j]->move(grid);
                   }
               }
              
           }
       }

       for (int i = 0; i < 20; i++){
           for (int j = 0; j < 20; j++){
               if (grid[i][j] != NULL){
                   if (grid[i][j]->getType() == 'D' || grid[i][j]->getType() == 'o'){
                       grid[i][j]->setIfMoved(false);
                   }
               }
              
           }
       }

       for (int i = 0; i < 20; i++){
           for (int j = 0; j < 20; j++){
               if (grid[i][j] != NULL){
                   if (grid[i][j]->getType() == 'D'){
                       grid[i][j]->starve(grid);
                   }
               }
              
           }
       }

       for (int i = 0; i < 20; i++){
           for (int j = 0; j < 20; j++){
               if (grid[i][j] != NULL){
                   if (grid[i][j]->getType() == 'D' && grid[i][j]->getRoundsSurvived() == 8){
                       grid[i][j]->breed(grid, doodlebugID);
                   }
               }
              
           }
       }

       for (int i = 0; i < 20; i++){
           for (int j = 0; j < 20; j++){
               if (grid[i][j] != NULL){
                   if (grid[i][j]->getType() == 'o' && grid[i][j]->getRoundsSurvived() == 3){
                       grid[i][j]->breed(grid, antID);
                   }
               }
              
           }
       }

       render(grid);

       if (countTheAnts(grid) == 400 || countTheDoodles(grid) == 400){
           flag = false;
       }

   }


   for (int i = 0; i < 20; i++){
       for (int j = 0; j < 20; j++){
           delete grid[i][j];
           grid[i][j] = NULL;
       }
   }

   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote