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

Program must be written in C++: The goal for this Programming Project is to crea

ID: 3691237 • Letter: P

Question

Program must be written in C++:

The goal for this Programming Project is to create a simple two-dimensional predator-prey simulation. In this simulation the prey are ants and the predator are doodlebugs. These critters live in a world composed of 20 x 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 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:

Move. Every time step, if there is an adjacent ant (up, down, left, or right), then the doodlebug will move to the 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 doodlebugs 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. 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 cyclical pattern between the population of predators and prey, although random perturbations may lead to the elimination of one or both species.

Explanation / Answer

test.cpp


#include "hsu_Organism.hpp"
#include "hsu_Ant.hpp"
#include "hsu_Doodlebug.hpp"
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
#include <vector>
#include <algorithm>

using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::vector;
using std::random_shuffle;

/***************************************************************
* Function: fillWithNull
* Description: Fills 20x20 grid with null pointers
* ************************************************************/
void fillWithNull(Organism* grid[][20], int numRows);

/***************************************************************
* Function: initializeGrid
* Description: Initializes 20x20 grid with 100 Ants and 5 Doodlebugs
* ************************************************************/
void initializeGrid(Organism* grid[][20], int numRows);

/***************************************************************
* Function: printGrid
* Description: Prints values of 20x20 grid
* ************************************************************/
void printGrid(Organism* grid[][20], int numRows);

/***************************************************************
* Function: advanceTime
* Description: Goes through 20x20 grid. Each non NULL pointer is placed in
*              a vector randomVec. randomVec is shuffled into random order
*              and then iterated through. All Organisms move (if
*              appropriate), then all Organisms breed (if appropriate),
*              then all Doodlebugs starve(if appropriate). Any Organisms
*              that have died are then deleted.
* ************************************************************/
void advanceTime(Organism* grid[][20], int numRows);

int main(){

     string cont;
     Organism* world[20][20];   //Create 20x20 grid of Organism pointers

     srand(time(NULL));       //Seed random number generator
     initializeGrid(world,20); //Fill grid with Organisms and NULL pointers

     do{
   
      printGrid(world, 20);
      advanceTime(world, 20);

      cout << "Press ENTER key to continue. Enter 'q' to quit. ";
      getline(cin, cont);

     }while(cont != "q" && cont != "Q");

     return 0;

}

void fillWithNull(Organism* grid[][20], int numRows){

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

void initializeGrid(Organism* grid[][20], int numRows){

     fillWithNull(grid, numRows);

     // Fill grid with 100 Ants
     for(int i = 0; i < numRows; i++){
      for(int j = 0; j < 20; j++){

           if(i % 2 == 0){   // Fill in rows with even index
          if((j + 1) % 4 == 0){

               grid[i][j] = new Ant(i, j);

          }
           }else{       // Fill in rows with odd index
          if((j + 2) % 4 == 0){

               grid[i][j] = new Ant(i, j);

          }
           }
      }
     }

     // Fill grid with 5 Doodlebugs
     grid[0][0] = new Doodlebug(0,0);
     grid[19][19] = new Doodlebug(19,19);
     grid[6][5] = new Doodlebug(6,5);
     grid[8][17] = new Doodlebug(8,17);
     grid[15][12] = new Doodlebug(15,12);

}

void printGrid(Organism* grid[][20], int numRows){
   
     for(int i = 0; i < numRows; i++){

      for(int j = 0; j < 20; j++){

           if(grid[i][j] == NULL){
            cout << " ";
           }else if((*grid[i][j]).getSpecies() == "Ant"){
            cout << "O";
           }else if((*grid[i][j]).getSpecies() == "Doodlebug"){
            cout << "X";
           }
      }

   cout << endl;
   
     }
}

void advanceTime(Organism* grid[][20], int numRows){

     vector<Organism*> randVec;

     //Fill vector with Organism pointers
     for(int i = 0; i < numRows; i++){
      for(int j = 0; j < 20; j++){

           if(grid[i][j] != NULL){
            randVec.push_back(grid[i][j]);
           }
      }
     }

     //Randomly shuffle order of Organism pointers
     random_shuffle(randVec.begin(), randVec.end());

     //Iterate through vector and call move on each living Organism
     for(int i = 0; i < randVec.size(); i++){
  
      if(!randVec[i]->hasDied()){
           randVec[i]->move(grid, 20);
      }
     }

     //Randomly shuffle order of Organism pointers
     random_shuffle(randVec.begin(), randVec.end());

     //Iterate through vector and call breed on each living Organism
     for(int i = 0; i < randVec.size(); i++){

      if(!randVec[i]->hasDied()){
           randVec[i]->breed(grid, 20);
      }
     }

     //Iterate through vector and call starve on Doodlebugs
     for(int i = 0; i < randVec.size(); i++){

      if(randVec[i]->getSpecies() == "Doodlebug"){
           randVec[i]->starve(grid, 20);
      }
     }

     // Delete eaten Ants and starved Doodlebugs.
     // (These were already removed from the grid by
     // Doodlebug::move and Doodlebug::starve)
     for(int i = 0; i < randVec.size(); i++){
  
      if(randVec[i]->hasDied()){
           delete randVec[i];
           randVec[i] = NULL;
      }
     }
}
  
hsu_Ant.cpp
#include "hsu_Ant.hpp"
#include <ctime>
#include <iostream>
#include <cstdlib>

using std::cout;

Ant::Ant() : Organism(){
   
     species = "Ant";

}

Ant::Ant(int r, int c) : Organism(r,c){

     species = "Ant";

}
  
void Ant::move(Organism* grid[][20], int numRows){

     int direction = rand() % 4;
     int rowUp = row - 1;
     int rowDown = row + 1;
     int colLeft = col - 1;
     int colRight = col + 1;

     switch(direction){
      case 0:   // Move left
           if(colLeft >= 0 && grid[row][colLeft] == NULL){
          
            grid[row][colLeft] = grid[row][col];
            grid[row][col] = NULL;
            col = colLeft;
            moved = true;

           }
           break;

      case 1:   // Move up
           if(rowUp >= 0 && grid[rowUp][col] == NULL){
          
            grid[rowUp][col] = grid[row][col];
            grid[row][col] = NULL;
            row = rowUp;
            moved = true;
         
           }
           break;

      case 2:   // Move right
           if(colRight < 20 && grid[row][colRight] == NULL){
          
            grid[row][colRight] = grid[row][col];
            grid[row][col] = NULL;
            col = colRight;
            moved = true;
         
           }
           break;

      case 3:   // Move down
           if(rowDown < 20 && grid[rowDown][col] == NULL){
          
            grid[rowDown][col] = grid[row][col];
            grid[row][col] = NULL;
            row = rowDown;
            moved = true;
         
           }
           break;

      default:
           cout << "ERROR: INVALID DIRECTION. Exit program. ";
           exit(1);
     }

}

void Ant::breed(Organism* grid[][20], int numRows){

     if(stepsNotLucky == 3){
  
      int rowUp = row - 1;
      int rowDown = row + 1;
      int colLeft = col - 1;
      int colRight = col + 1;

      //Breed to left
      if(colLeft >= 0 && grid[row][colLeft] == NULL){
         
           grid[row][colLeft] = new Ant(row, colLeft);
           stepsNotLucky = 0;

      //Breed upwards
      }else if(rowUp >= 0 && grid[rowUp][col] == NULL){
         
           grid[rowUp][col] = new Ant(rowUp, col);
           stepsNotLucky = 0;

      //Breed to right
      }else if(colRight < 20 && grid[row][colRight] == NULL){
         
           grid[row][colRight] = new Ant(row, colRight);
           stepsNotLucky = 0;

      //Breed downwards
      }else if(rowDown < 20 && grid[rowDown][col] == NULL){
         
           grid[rowDown][col] = new Ant(rowDown, col);
           stepsNotLucky = 0;

      }

     }else{

      stepsNotLucky++;

     }

}

string Ant::getSpecies(){

     return species;

}

hsu_Ant.hpp
#ifndef HSU_ANT_HPP
#define HSU_ANT_HPP

#include "hsu_Organism.hpp"
#include <string>

using std::string;

class Ant : public Organism{

     public:

      // Constructors
      Ant();
      Ant(int r, int c);

      /************************************************************
       * Function: move
       * Description: Has ant move randomly up, down, left, or right if
       *              space available. Otherwise Ant stays in position.
       * **********************************************************/
      void move(Organism* grid[][20], int numRows);
  
      /************************************************************
       * Function: breed
       * Description: Ant creates a new adjacent Ant if survived 3
       *              timesteps. Ant prefers to create new Ant to left
       *              and working clockwise. If no space, then doesn't
       *              breed. Ant must survive another 3 timesteps to
       *              breed again if offspring produced.
       * **********************************************************/
      void breed(Organism* grid[][20], int numRows);

      /***********************************************************
       * Function: getSpecies
       * Description: Returns "Ant"
       * *********************************************************/
      string getSpecies();


     protected:

      string species; // holds the species: Ant

};

#endif

hsu_Doodlebug.cpp
#include "hsu_Doodlebug.hpp"
#include <iostream>
#include <cstdlib>

using std::cout;

Doodlebug::Doodlebug() : Organism(){

     species = "Doodlebug";
     stepsNotEaten = 0;

}

Doodlebug::Doodlebug(int r, int c) : Organism(r,c){
   
     species = "Doodlebug";
     stepsNotEaten = 0;

}
  
void Doodlebug::move(Organism* grid[][20], int numRows){

     int rowUp = row - 1;
     int rowDown = row + 1;
     int colLeft = col - 1;
     int colRight = col + 1;

     // Eat Ant to left
     if(colLeft >= 0 &&
   grid[row][colLeft] != NULL &&
   (*grid[row][colLeft]).getSpecies() == "Ant"){
  
      grid[row][colLeft]->setDied(true);
      grid[row][colLeft] = grid[row][col];
      grid[row][col] = NULL;
      col = colLeft;
      moved = true;
      stepsNotEaten = 0;

     // Eat Ant above
     }else if(rowUp >= 0 &&
          grid[rowUp][col] != NULL &&
          (*grid[rowUp][col]).getSpecies() == "Ant"){
  
      grid[rowUp][col]->setDied(true);
      grid[rowUp][col] = grid[row][col];
      grid[row][col] = NULL;
      row = rowUp;
      moved = true;
      stepsNotEaten = 0;

     // Eat Ant to right  
     }else if(colRight < 20 &&
          grid[row][colRight] != NULL &&
          (*grid[row][colRight]).getSpecies() == "Ant"){
  
      grid[row][colRight]->setDied(true);
      grid[row][colRight] = grid[row][col];
      grid[row][col] = NULL;
      col = colRight;
      moved = true;
      stepsNotEaten = 0;

     // Eat Ant below
     }else if(rowDown < 20 &&
           grid[rowDown][col] != NULL &&
          (*grid[rowDown][col]).getSpecies() == "Ant"){
  
      grid[rowDown][col]->setDied(true);
      grid[rowDown][col] = grid[row][col];
      grid[row][col] = NULL;
      row = rowDown;
      moved = true;
      stepsNotEaten = 0;

     // Move randomly without eating
     }else{
   
      int direction = rand() % 4;
      switch(direction){
         
           // Move left
           case 0:
            if(colLeft >= 0 && grid[row][colLeft] == NULL){
          
           grid[row][colLeft] = grid[row][col];
           grid[row][col] = NULL;
           col = colLeft;
           moved = true;
          
            }
            break;
  
           // Move up      
           case 1:
            if(rowUp >= 0 && grid[rowUp][col] == NULL){
          
           grid[rowUp][col] = grid[row][col];
           grid[row][col] = NULL;
           row = rowUp;
           moved = true;
          
            }
            break;

           // Move right
           case 2:
            if(colRight < 20 && grid[row][colRight] == NULL){
          
           grid[row][colRight] = grid[row][col];
           grid[row][col] = NULL;
           col = colRight;
           moved = true;
          
            }
            break;

               // Move down
           case 3:
            if(rowDown < 20 && grid[rowDown][col] == NULL){
          
           grid[rowDown][col] = grid[row][col];
           grid[row][col] = NULL;
           row = rowDown;
           moved = true;
          
            }
            break;

           default:
            cout << "ERROR: INVALID DIRECTION. Exit program. ";
            exit(1);
      }

      stepsNotEaten++;

     }

}

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

     if(stepsNotLucky == 8){
  
      int rowUp = row - 1;
      int rowDown = row + 1;
      int colLeft = col - 1;
      int colRight = col + 1;

      // Breed to the left
      if(colLeft >= 0 && grid[row][colLeft] == NULL){
         
           grid[row][colLeft] = new Doodlebug(row, colLeft);
           stepsNotLucky = 0;
  
      // Breed above
      }else if(rowUp >= 0 && grid[rowUp][col] == NULL){
         
           grid[rowUp][col] = new Doodlebug(rowUp, col);
           stepsNotLucky = 0;

      // Breed to the right
      }else if(colRight < 20 && grid[row][colRight] == NULL){
         
           grid[row][colRight] = new Doodlebug(row, colRight);
           stepsNotLucky = 0;

      // Breed below   
      }else if(rowDown < 20 && grid[rowDown][col] == NULL){
         
           grid[rowDown][col] = new Doodlebug(rowDown, col);
           stepsNotLucky = 0;
  
      }

     }else{

      stepsNotLucky++;

     }

}

void Doodlebug::starve(Organism* grid[][20], int numRows){

     if(stepsNotEaten == 3){

      died = true;
      grid[row][col] = NULL;

     }
}

string Doodlebug::getSpecies(){

     return species;

}

hsu_Doodlebug.hpp

#ifndef HSU_DOODLEBUG_HPP
#define HSU_DOODLEBUG_HPP

#include "hsu_Organism.hpp"
#include <string>

using std::string;

class Doodlebug : public Organism{

     public:
        
      // Constructors
      Doodlebug();
      Doodlebug(int r, int c);

      /************************************************************
       * Function: move
       * Description: Has Doodlebug move to an adjacent cell with an ant
       *           and eat the ant. It has a preference to eat from
       *           the left and move clockwise. If no adjacent ant,
       *           then Doodlebug moves randomly up, down, left, or
       *           right if space available. Otherwise Doodlebug
       *           stays in position.
       * **********************************************************/
      void move(Organism* grid[][20], int numRows);

      /************************************************************
       * Function: breed
       * Description: Doodlebug creates a new adjacent Doodlebug if
       *           survived 8 timesteps. Doodlebug prefers to create
       *           new Doodlebug to left and working clockwise. If
       *           no space, then doesn't breed. Doodlebug must
       *           survive another 8 timesteps to breed again if
       *           offspring was produced.
       * **********************************************************/
      void breed(Organism* grid[][20], int numRows);

      /************************************************************
       * Function: starve
       * Description: Doodlebug dies if it has not eaten within 3
       *            timesteps.
       * **********************************************************/
      void starve(Organism* grid[][20], int numRows);

      /***********************************************************
       * Function: getSpecies
       * Description: Returns "Doodlebug"
       * *********************************************************/
      string getSpecies();

     protected:

      string species;
      int stepsNotEaten; // # timesteps Doodlebug has not eaten
  
};

#endif


hsu_Organism.cpp

#include "hsu_Organism.hpp"

Organism::Organism(){

     stepsNotLucky = 0;
     moved = false;
     died = false;
     row = -1;
     col = -1;
}

Organism::Organism(int r, int c){
   
     stepsNotLucky = 0;
     moved = false;
     died = false;
     row = r;
     col = c;
}

void Organism::move(Organism* grid[][20], int numRows){

     // Intentionally undefined

}

void Organism::breed(Organism* grid[][20], int numRows){

     // Intentionally undefined

}

void Organism::starve(Organism* grid[][20], int numRows){

     // Intentionally undefined

}

string Organism::getSpecies(){

     // Intentionally undefined

}

bool Organism::hasMoved() const{
   
     return moved;

}

void Organism::setMoved(bool doneMoving){
   
     moved = doneMoving;

}

bool Organism::hasDied() const{

     return died;

}

void Organism::setDied(bool death){

     died = death;

}

hsu_Organism.hpp

#ifndef HSU_ORGANISM_HPP
#define HSU_ORGANISM_HPP

#include <string>

using std::string;

class Organism{

     public:

      // Default constructor
      Organism();

      // Constructor
      Organism(int r, int c);

      // Virtual move function undefined
      virtual void move(Organism* grid[][20], int numRows);

      // Virtual breed function undefined
      virtual void breed(Organism* grid[][20], int numRows);

      // Virtual starve function undefined
      virtual void starve(Organism* grid[][20], int numRows);

      // Virtual getSpecies function undefined
      virtual string getSpecies();

      /************************************************************
       * Function name: hasMoved
       * Description: Tells whether an Organism has moved in a timestep
       * **********************************************************/
      bool hasMoved() const;

      /************************************************************
       * Function name: setMoved
       * Description: Sets the moved member variable to true or false
       * **********************************************************/
      void setMoved(bool doneMoving);

      /************************************************************
       * Function name: hasDied
       * Description: Tells whether an Organism has died in a timestep
       * **********************************************************/
      bool hasDied() const;

      /************************************************************
       * Function name: setDied
       * Description: Sets the died member variable to true or false
       * **********************************************************/
      void setDied(bool death);

     protected:

      int stepsNotLucky; //# timesteps without breeding
      bool moved; // whether Organism has moved within a timestep
      bool died; // whether Organism has died
      int row; // row position in grid
      int col; // column position in grid

};

#endif

sample output

Project
X O   O   O   O   O                                                                                                                                        
O   O   O   O   O                                                                                                                                         
   O   O   O   O   O                                                                                                                                        
O   O   O   O   O                                                                                                                                         
   O   O   O   O   O                                                                                                                                        
O   O   O   O   O                                                                                                                                         
   O X O   O   O   O                                                                                                                                        
O   O   O   O   O                                                                                                                                         
   O   O   O   O X O                                                                                                                                        
O   O   O   O   O                                                                                                                                         
   O   O   O   O X O                                                                                                                                        
O   O   O   O   O                                                                                                                                         
   O   O   O   O   O                                                                                                                                        
O   O   O   O   O                                                                                                                                         
   O   O   O   O   O                                                                                                                                        
O   O   O   O   O                                                                                                                                         
   O   O   O   O   O                                                                                                                                        
O   O   O X O   O                                                                                                                                         
   O   O   O   O   O                                                                                                                                        
O   O   O   O   O                                                                                                                                         
   O   O   O   O   O                                                                                                                                        
O   O   O   O   OX                                                                                                                                        
Press ENTER key to continue. Enter 'q' to quit. q                                                                                                          

Dr Jack
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote