Posted on

This is study resource 13 for the 24-week cohort during the summer of 2019. There is only one question for this study resource.

Click here to see all the study resources.

Question #1

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.

This is a rough answer – I did not get full credit for it. Please work it out.

#include <iostream>
#include <string>
#include <cstdlib>
#include <time.h>
using namespace std;


const int GARDENSIZE = 20;
const int INITIALANTS = 100;
const int INITALBUGS = 5;
const int DOODLEBUG = 1;
const int ANT = 2;
const int ANTBREED = 3;
const int DOODLEBREED = 8;
const int DOODLESTARVE = 3;
const char ANTCHAR = 'O';
const char BUGCHAR = 'X';

class Organism;
class Doodlebug;
class Ant;

class Garden{
  friend class Organism;
  friend class Doodlebug;
  friend class Ant;
  public:
    Garden();
    ~Garden();

    Organism* getAt(int x, int y);
    void setAt(int x, int y, Organism *org);
    void Display();
    void SimulateOneStep();

  private:
    Organism* grid[GARDENSIZE][GARDENSIZE];
};

class Organism{
  friend class Garden;
  public:
    Organism();
    Organism(Garden *garden, int x, int y);
    virtual ~Organism();

    virtual void breed() = 0;
    virtual void move() = 0;
    virtual int getType() = 0;
    virtual bool starve() = 0;
  protected:
    int x, y;
    bool moved;
    int breedTicks;
    Garden *garden;

};

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

Garden::~Garden(){
  int i, j;
  for (i = 0; i < GARDENSIZE; i++){
    for(j = 0; j < GARDENSIZE; j++){
      if(grid[i][j] != NULL)
        delete (grid[i][j]);
    }
  }
}

Organism* Garden::getAt(int x, int y){
  if((x >= 0) && (x < GARDENSIZE) && (y >= 0) && (y < GARDENSIZE)){
    return grid[x][y];
  }
  return NULL;
}

void Garden::setAt(int x, int y, Organism *org){
  if((x >= 0) && (x < GARDENSIZE) && (y >= 0) && (y < GARDENSIZE)){
    grid[x][y] = org;
  }
}

void Garden::Display(){
  cout << endl << endl;
  for(int j=0; j < GARDENSIZE; j++){
    for(int i=0; i < GARDENSIZE; i++){
      if(grid[i][j] == NULL){
        cout << ".";
      }else if(grid[i][j]->getType() == ANT){
        cout << ANTCHAR;
      }else{
        cout << BUGCHAR;
      }
    }
    cout << endl;
  }
}

void Garden::SimulateOneStep(){
  for (int i=0; i<GARDENSIZE; i++){
    for(int j=0; j<GARDENSIZE; j++){
      if (grid[i][j] != NULL){
        grid[i][j]->moved = false;
      }
    }
  }

  for(int i=0; i<GARDENSIZE; i++){
    for(int j=0; j<GARDENSIZE; j++){
      if((grid[i][j] != NULL) && (grid[i][j]->getType() == DOODLEBUG)){
        if (grid[i][j]->moved == false){
          grid[i][j]->moved = true;
          grid[i][j]->move();
        }
      }
    }
  }

  for(int i=0; i<GARDENSIZE; i++){
    for(int j=0; j<GARDENSIZE; j++){
      if((grid[i][j] != NULL) && (grid[i][j]->getType() == ANT)){
        if (grid[i][j]->moved == false){
          grid[i][j]->moved = true;
          grid[i][j]->move();
        }
      }
    }
  }

  for(int i=0; i<GARDENSIZE; i++){
    for(int j=0; j<GARDENSIZE; j++){
      if((grid[i][j] != NULL) && (grid[i][j]->getType() == DOODLEBUG)){
        if (grid[i][j]->starve()){
          delete (grid[i][j]);
          grid[i][j] = NULL;
        }
      }
    }
  }

  for(int i=0; i<GARDENSIZE; i++){
    for(int j=0; j<GARDENSIZE; j++){
      if((grid[i][j] != NULL) && (grid[i][j]->moved == true)){
          grid[i][j]->breed();
      }
    }
  }

}

Organism::Organism(){
  garden = NULL;
  moved = false;
  breedTicks = 0;
  x = 0;
  y = 0;
}

Organism::Organism(Garden *grden, int x, int y){
  this->garden = grden;
  moved = false;
  this->x = x;
  this->y = y;
  grden->setAt(x, y, this);
}

Organism::~Organism(){}

//Ant class
class Ant: public Organism{
  friend class Garden;

  public:
    Ant();
    Ant(Garden *garden, int x, int y);

    void breed();
    void move();
    int getType();
    bool starve(){ return false; }
};

Ant::Ant() : Organism() {}
Ant::Ant(Garden *garden, int x, int y): Organism(garden, x, y) {}
void Ant::move(){
  int direction = rand() % 4;

  if(direction == 0){
    if((y > 0) && (garden->getAt(x, y-1) == NULL)){
      garden->setAt(x, y-1, garden->getAt(x, y));
      garden->setAt(x, y, NULL);
      y--;
    }
  }else if (direction == 1){
    if((y < GARDENSIZE - 1) && (garden->getAt(x, y+1) == NULL)){
      garden->setAt(x, y, garden->getAt(x, y));
      garden->setAt(x, y, NULL);
      y++;
    }
  }else if(direction == 2){
    if ((x > 0) && (garden->getAt(x-1, y) == NULL)){
      garden->setAt(x-1, y, garden->getAt(x, y));
      garden->setAt(x, y, NULL);
      x--;
    }
  }else{
    if((x < GARDENSIZE-1) && (garden->getAt(x+1, y) == NULL)){
      garden->setAt(x+1, y, garden->getAt(x, y));
      garden->setAt(x, y, NULL);
      x++;
    }
  }
}

int Ant::getType() {return ANT; }
void Ant::breed(){
  breedTicks++;
  if(breedTicks == ANTBREED){
    breedTicks = 0;
    if((y>0) && (garden->getAt(x, y-1) == NULL)){
      Ant *newAnt = new Ant(garden, x, y-1);
    }else if ((y<GARDENSIZE-1) && (garden->getAt(x, y+1) == NULL)){
      Ant *newAnt = new Ant(garden, x, y+1);
    }else if((x>0) && (garden->getAt(x-1, y) == NULL)){
      Ant *newAnt = new Ant(garden, x-1, y);
    }else if((x<GARDENSIZE-1) && (garden->getAt(x+1, y) == NULL)){
      Ant *newAnt = new Ant(garden, x+1, y);
    }
  }
}

//Doodlebug class
class Doodlebug : public Organism{
  friend class Garden;
  public:
    Doodlebug();
    Doodlebug(Garden *garden, int x, int y);
    void breed();
    void move();
    int getType();
    bool starve();
  private:
    int starveTicks;
};

Doodlebug::Doodlebug(): Organism() { starveTicks = 0; }
Doodlebug::Doodlebug(Garden *garden, int x, int y): Organism(garden, x, y) { starveTicks = 0; }

void Doodlebug::move(){
  if((y>0) && (garden->getAt(x, y-1) != NULL) && (garden->getAt(x, y-1)->getType() == ANT)){
    delete (garden->grid[x][y-1]);
    garden->grid[x][y-1] = this;
    garden->setAt(x, y, NULL);
    starveTicks = 0;
    y--;
    return;
  }else if((y<GARDENSIZE-1) && (garden->getAt(x, y+1) != NULL) && (garden->getAt(x, y+1)->getType() == ANT)){
    delete (garden->grid[x][y+1]);
    garden->grid[x][y+1] = this;
    garden->setAt(x, y, NULL);
    starveTicks = 0;
    y++;
    return;
  }else if((x>0) && (garden->getAt(x-1, y) != NULL) && (garden->getAt(x-1, y)->getType() == ANT)){
    delete (garden->grid[x-1][y]);
    garden->grid[x-1][y] = this;
    garden->setAt(x, y, NULL);
    starveTicks = 0;
    x--;
    return;
  }else if((x<GARDENSIZE-1) && (garden->getAt(x+1, y) != NULL) && (garden->getAt(x+1, y)->getType() == ANT )){
    delete (garden->grid[x+1][y]);
    garden->grid[x+1][y] = this;
    garden->setAt(x, y, NULL);
    starveTicks = 0;
    x++;
    return;
  }

  int direction = rand() % 4;

  if(direction == 0){
    if((y>0) && (garden->getAt(x, y-1) == NULL)){
      garden->setAt(x, y-1, garden->getAt(x, y));
      garden->setAt(x, y, NULL);
      y--;
    }
  }else if(direction == 1){
    if((y<GARDENSIZE-1) && (garden->getAt(x, y+1) == NULL)){
      garden->setAt(x, y+1, garden->getAt(x, y));
      garden->setAt(x, y, NULL);
      y++;
    }
  }else if(direction == 2){
    if((x>0) && (garden->getAt(x-1, y) == NULL)){
      garden->setAt(x-1, y, garden->getAt(x, y));
      garden->setAt(x, y, NULL);
      x--;
    }
  }else{
    if((x<GARDENSIZE-1) && (garden->getAt(x+1, y) == NULL)){
      garden->setAt(x+1, y, garden->getAt(x, y));
      garden->setAt(x, y, NULL);
      x++;
    }
  }

  starveTicks++;
}

int Doodlebug::getType() { return DOODLEBUG; }

void Doodlebug::breed(){
  breedTicks++;
  if(breedTicks == DOODLEBREED){
    breedTicks = 0;
    if((y>0) && (garden->getAt(x, y-1) == NULL)){
      Doodlebug * newDoodle = new Doodlebug(garden, x, y-1);
    }else if ((y<GARDENSIZE -1) && (garden->getAt(x, y+1) == NULL)){
      Doodlebug *newDoodle = new Doodlebug(garden,x,y+1);
    }else if((x>0) && (garden->getAt(x-1, y) == NULL)){
      Doodlebug *newDoodle = new Doodlebug(garden,x-1,y);
    }else{
      Doodlebug *newDoodle = new Doodlebug(garden, x+1, y);
    }
  }
}

bool Doodlebug::starve(){
  return (starveTicks > DOODLESTARVE) ? true : false;
}

int main() {
  string s;
  srand(time(NULL));
  Garden g;

  int antCount = 0;
  int doodleCount = 0;

  while(antCount < INITIALANTS){
    int x = rand() % GARDENSIZE;
    int y = rand() % GARDENSIZE;

    if(g.getAt(x, y) == NULL){
      antCount++;
      Ant *a1 = new Ant(&g, x, y);
    }
  }

  while(doodleCount < INITALBUGS){
    int x = rand() % GARDENSIZE;
    int y = rand() % GARDENSIZE;

    if(g.getAt(x, y) == NULL){
      doodleCount++;
      Doodlebug *d1 = new Doodlebug(&g, x, y);
    }
  }

  //run program
  while (true){
    g.Display();
    g.SimulateOneStep();
    cout << endl << "Press enter for next step" << endl;
    getline(cin, s);
  }

  return 0;
}

Photo by Nahel Abdul Hadi on Unsplash

One Reply to “Study Resource 13 – NYU Bridge to Tandon Program”

  1. Hey Xstudent – these rock. Question for you – what do you mean by you didn’t get full credit? Is there like practice questions beyond just exams that get graded?

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.