Question

In: Computer Science

Please USE C++ The goal for this Project is to create a simple two-dimensional predator-prey simulation....

Please USE C++

The goal for this Project is to create a simple two-dimensional predator-prey simulation. In this simulation the prey are ants and the pred-ators 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.

Please USE C++

Solutions

Expert Solution

#include <iostream>

               #include <cstdlib>

               #include <ctime>

              

               using namespace std;

              

               const int numOfColumns = 20;

               const int numOfRows = 20;

              

               class Organism{

               public:

                   Organism();

                   Organism(int fIndex, int sIndex, int myAge, int myIdNumber);

                   Organism(int myIdNumber);

                   int get_first_index() const;

                   int get_second_index() const;

                   int get_age() const;

                   int get_idNumber() const;

                   bool get_life_status() const;

                   virtual char get_symbol();

                   void set_first_index(int newFirstIndex);

                   void set_second_index(int newSecondIndex);

                   void set_age(int newAge);

                   void set_id_number(int newIdNumber);

                   void set_symbol(char newSymbol);

                   void set_life_status(bool newStatus);

                   virtual void out_symbol();

                   virtual void move();

                   virtual void breed(){};

                  

               private:

                   char symbol;

                   int firstIndex;

                   int secondIndex;

                   int age;

                   int idNumber;

                   bool isAlive;

               };

              

               class Space: public Organism

               {

               public:

                   Space();

                   virtual void out_symbol();

                   virtual char get_symbol();

                  

                  

               private:

                   char symbol;

               };

              

               class Ant: public Organism{

               public:

                   Ant();

                   virtual void out_symbol();

                   virtual char get_symbol();

                   virtual void move(Organism* gridArray[numOfRows][numOfColumns], Space spacesArray[]);

                   virtual void breed(Organism* gridArray[numOfRows][numOfColumns], Ant antArray[], int &antCounter);

                  

                  

               private:

                   char symbol;

               };

              

               class Doodlebug: public Organism{

               public:

                   Doodlebug();

                  

                   void set_time_since_meal(int newTime);

                   int get_time_since_meal() const;

                   virtual void out_symbol();

                   virtual char get_symbol();

                   virtual void move(Organism* gridArray[numOfRows][numOfColumns], Doodlebug doodleBugArray[], Space spacesArray[]);

                   virtual void breed(Organism* gridArray[numOfRows][numOfColumns], Doodlebug doodleBugArray[], int &doodleBugCounter);

                   void starve(Organism* gridArray[numOfRows][numOfColumns], Space spacesArray[]);

                  

                  

               private:

                   char symbol;

                   int timeSinceMeal;

               };

              

               void initiate_time_step(Organism* gridArray[numOfRows][numOfColumns], Ant antArr[], Space spaceArr[], Doodlebug doodlebugArr[], int &antCounter, int &doodlebugCounter);

              

               int find_next_available_index(Doodlebug arr[]);

               int find_next_available_index(Ant arr[]);

               int main(){

                   srand(time(0)%100);

                   int doodlebugCount = 0;

                   int antCount = 0;

                   int tempFirstI, tempSecondI;

                   Space spaceArray[1];

                   Doodlebug doodlebugArray[400];

                   Ant antArray[400];

              

                  

                   Organism* gameSpace[20][20];

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

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

                           gameSpace[c][i] = nullptr;

                       }

                   }

                   while (doodlebugCount < 5){

                       bool spawned = false;

                       while(spawned == false){

                           tempFirstI = rand() % 20;

                           tempSecondI = rand() % 20;

                           if(gameSpace[tempFirstI][tempSecondI] == nullptr){

                               gameSpace[tempFirstI][tempSecondI] = &doodlebugArray[doodlebugCount];

                               gameSpace[tempFirstI][tempSecondI]->set_first_index(tempFirstI);

                               gameSpace[tempFirstI][tempSecondI]->set_second_index(tempSecondI);

                               gameSpace[tempFirstI][tempSecondI]->set_life_status(true);

                               doodlebugCount++;

                               spawned = true;

                           }

                       }

                   }

              

                  

                   while (antCount < 100){

                       bool spawned = false;

                       while(spawned == false){

                           tempFirstI = rand() % 20;

                           tempSecondI = rand() % 20;

                           if(gameSpace[tempFirstI][tempSecondI] == nullptr){

                               gameSpace[tempFirstI][tempSecondI] = &antArray[antCount];

                               gameSpace[tempFirstI][tempSecondI]->set_first_index(tempFirstI);

                               gameSpace[tempFirstI][tempSecondI]->set_second_index(tempSecondI);

                               gameSpace[tempFirstI][tempSecondI]->set_life_status(true);

                               antCount++;

                               spawned = true;

                           }

                       }

                   }

              

              

                  

                   for(int c = 0; c < numOfColumns; c++){

                       for (int i = 0; i < numOfRows; i++){

                           if (gameSpace[c][i] == nullptr){

                               gameSpace[c][i] = &spaceArray[0];

                           }

                       }

                   }

                   for(int c = 0; c < numOfColumns; c++){

                       for (int i = 0; i < numOfRows; i++){

                           gameSpace[c][i]->out_symbol();

                           cout<<" ";

                       }

                       cout<<endl;

                   }

                   cout<<"World has been initialized with 100 ants and 5 doodlebugs."<<endl;

                   char test;

                   bool seenEnd = false;

                   while(seenEnd == false){

                       cout<<"Press enter to initiate time step: ";

                       test = fgetc(stdin);

                       if(test == '\n'){

                           initiate_time_step(gameSpace, antArray, spaceArray, doodlebugArray, antCount, doodlebugCount);

                           seenEnd = false;

                       }

                       else

                           seenEnd = true;

                   }

              

              

                   return 0;

               }

              

              

               Organism::Organism(){

                   firstIndex = -1;

                   secondIndex = -1;

                   age = 0;

                   idNumber = -1;

                   isAlive = false;

               }

              

               Organism::Organism(int fIndex, int sIndex, int myAge, int myIdNumber){

                   firstIndex = fIndex;

                   secondIndex = sIndex;

                   age = myAge;

                   idNumber = myIdNumber;

                  

               }

              

               Organism::Organism(int myIdNumber){

                   firstIndex = -1;

                   secondIndex = -1;

                   age = 0;

                   idNumber = myIdNumber;

                  

               }

              

               int Organism::get_first_index() const{

                   return firstIndex;

               }

              

               int Organism::get_second_index() const{

                   return secondIndex;

               }

              

               int Organism::get_age() const{

                   return age;

               }

              

               int Organism::get_idNumber() const{

                   return idNumber;

               }

              

               bool Organism::get_life_status() const{

                   return isAlive;

               }

              

               void Organism::set_first_index(int newFirstIndex){

                   firstIndex = newFirstIndex;

               }

               void Organism::set_second_index(int newSecondIndex){

                   secondIndex = newSecondIndex;

               }

               void Organism::set_age(int newAge){

                   age = newAge;

               }

              

               void Organism::set_id_number(int newIdNumber){

                   idNumber = newIdNumber;

                  

               }

              

               void Organism:: set_life_status(bool newStatus){

                   isAlive = newStatus;

               }

              

               char Organism::get_symbol(){

                   return symbol;

               }

              

              

               void Organism::out_symbol(){

                   cout<<"here's your problem"<<endl;

               };

              

               void Organism::move(){

                   cout<<"move"<<endl;

               };

              

              

               Space::Space():Organism(), symbol('-'){};

              

               void Space:: out_symbol() {

                   cout<<symbol;

               }

              

               char Space:: get_symbol(){

                   return symbol;

               }

              

               Ant::Ant():Organism(), symbol('o'){};

              

               void Ant:: out_symbol() {

                   cout<<symbol;

               }

              

               char Ant:: get_symbol(){

                   return symbol;

               }

              

              

               void Ant::move(Organism* gridArray[numOfRows][numOfColumns], Space spacesArray[]){

                   int decision = rand() % 4;

                   set_age(get_age() + 1);

                   if (decision == 0){

                       if ((get_first_index() - 1 >= 0) && (gridArray[get_first_index() - 1][get_second_index()]->get_symbol() == '-'))

                       {

                           gridArray[get_first_index() - 1][get_second_index()] = this;

                           gridArray[get_first_index()][get_second_index()] = &spacesArray[0];

                           set_first_index(get_first_index() - 1);

                       }

                   }

                   else if (decision == 1){

                       if ((get_second_index() + 1 <= 19) && (gridArray[get_first_index()][get_second_index() + 1]->get_symbol() == '-'))

                       {

                           gridArray[get_first_index()][get_second_index() + 1] = this;

                           gridArray[get_first_index()][get_second_index()] = &spacesArray[0];

                           set_second_index(get_second_index() + 1);

                       }

                   }

                  

                   else if (decision == 2){

                       if ((get_first_index() + 1 <= 19) && (gridArray[get_first_index() + 1][get_second_index()]->get_symbol() == '-'))

                       {

                           gridArray[get_first_index() + 1][get_second_index()] = this;

                           gridArray[get_first_index()][get_second_index()] = &spacesArray[0];

                           set_first_index(get_first_index() + 1);

                       }

                   }

                  

                   else if (decision == 3){

                       if ((get_second_index() - 1 >= 0) && (gridArray[get_first_index()][get_second_index() - 1]->get_symbol() == '-'))

                       {

                           gridArray[get_first_index()][get_second_index() - 1] = this;

                           gridArray[get_first_index()][get_second_index()] = &spacesArray[0];

                           set_second_index(get_second_index() - 1);

                       }

                   }

                   else{};

               }

               void Ant::breed(Organism* gridArray[numOfRows][numOfColumns], Ant antArray[], int &antCounter){

                   if(get_age() > 0 && get_age() % 3 == 0){

                       bool seenFlag = false;

                       int nextAvailIndex = find_next_available_index(antArray);

                      bool northCell = false, eastCell = false, southCell = false, westCell = false;

                       while(seenFlag == false && ((northCell == false) || (eastCell == false) || (southCell == false) || (westCell == false))){

                           int decision = rand() % 4;

                           if(decision == 0){

                               northCell = true;

                               if ((get_first_index() - 1 >= 0) && (gridArray[get_first_index() - 1][get_second_index()]->get_symbol() == '-'))

                               {

                                   gridArray[get_first_index() - 1][get_second_index()] = &antArray[nextAvailIndex];

                                   antArray[nextAvailIndex].set_first_index(get_first_index() - 1);

                                   antArray[nextAvailIndex].set_second_index(get_second_index());

                                   antArray[nextAvailIndex].set_life_status(true);

                                   seenFlag = true;

                               }

                           }

                           else if(decision == 1){

                               eastCell = true;

                               if ((get_second_index() + 1 <= 19) && (gridArray[get_first_index()][get_second_index() + 1]->get_symbol() == '-')){

                                   gridArray[get_first_index()][get_second_index() + 1] = &antArray[nextAvailIndex];

                                   antArray[nextAvailIndex].set_first_index(get_first_index());

                                   antArray[nextAvailIndex].set_second_index(get_second_index() + 1);

                                   antArray[nextAvailIndex].set_life_status(true);

                                   seenFlag = true;

                               }

                           }

                           else if (decision == 2){

                               southCell = true;

                               if ((get_first_index() + 1 <= 19) && (gridArray[get_first_index() + 1][get_second_index()]->get_symbol() == '-')){

                                   gridArray[get_first_index() + 1][get_second_index()] = &antArray[nextAvailIndex];

                                   antArray[nextAvailIndex].set_first_index(get_first_index() + 1);

                                   antArray[nextAvailIndex].set_second_index(get_second_index());

                                   antArray[nextAvailIndex].set_life_status(true);

                                   seenFlag = true;

                               }

                           }

                           else if (decision == 3){

                               westCell = true;

                               if ((get_second_index() - 1 >= 0) && (gridArray[get_first_index()][get_second_index() - 1]->get_symbol() == '-')){

                                   gridArray[get_first_index()][get_second_index() - 1] = &antArray[nextAvailIndex];

                                   antArray[nextAvailIndex].set_first_index(get_first_index());

                                   antArray[nextAvailIndex].set_second_index(get_second_index() - 1);

                                   antArray[nextAvailIndex].set_life_status(true);

                                   seenFlag = true;

                               }

                           }

                       }

                   }

               }

              

              

               Doodlebug::Doodlebug(): Organism(), symbol('X'), timeSinceMeal(0){};

              

               void Doodlebug:: out_symbol() {

                   cout<<symbol;

               }

              

               char Doodlebug:: get_symbol(){

                   return symbol;

               }

              

               void Doodlebug:: set_time_since_meal(int newTime){

                   timeSinceMeal = newTime;

               }

               int Doodlebug:: get_time_since_meal() const{

                   return timeSinceMeal;

               }

              

               void Doodlebug:: move(Organism* gridArray[numOfRows][numOfColumns], Doodlebug doodleBugArray[], Space spacesArray[]){

                   set_age(get_age() + 1);

                   bool seenFlag = false;

                   bool northCell = false, eastCell = false, southCell = false, westCell = false;

                   while(seenFlag == false && ((northCell == false) || (eastCell == false) || (southCell == false) || (westCell == false))){

                       int decision = rand() % 4;

                       if(decision == 0){

                           northCell = true;

                           if ((get_first_index() - 1 >= 0) && (gridArray[get_first_index() - 1][get_second_index()]->get_symbol() == 'o'))

                           {

                               gridArray[get_first_index() - 1][get_second_index()]->set_life_status(false);

                               gridArray[get_first_index() - 1][get_second_index()]->set_age(0);

                               gridArray[get_first_index() - 1][get_second_index()] = this;

                               gridArray[get_first_index()][get_second_index()] = &spacesArray[0];

                               set_first_index(get_first_index() - 1);

                               set_time_since_meal(0);

                               seenFlag = true;

                           }

                       }

                       else if(decision == 1){

                           eastCell = true;

                           if ((get_second_index() + 1 <= 19) && (gridArray[get_first_index()][get_second_index() + 1]->get_symbol() == 'o')){

                               gridArray[get_first_index()][get_second_index() + 1]->set_life_status(false);

                               gridArray[get_first_index()][get_second_index() + 1]->set_age(0);

                               gridArray[get_first_index()][get_second_index() + 1] = this;

                               gridArray[get_first_index()][get_second_index()] = &spacesArray[0];

                               set_second_index(get_second_index() + 1);

                               set_time_since_meal(0);

                               seenFlag = true;

                           }

                       }

                       else if(decision == 2){

                           southCell = true;

                           if ((get_first_index() + 1 <= 19) && (gridArray[get_first_index() + 1][get_second_index()]->get_symbol() == 'o')){

                               gridArray[get_first_index() + 1][get_second_index()]->set_life_status(false);

                               gridArray[get_first_index() + 1][get_second_index()]->set_age(0);

                               gridArray[get_first_index() + 1][get_second_index()] = this;

                               gridArray[get_first_index()][get_second_index()] = &spacesArray[0];

                               set_first_index(get_first_index() + 1);

                               set_time_since_meal(0);

                               seenFlag = true;

                           }

                       }

                       else if(decision == 3){

                           westCell = true;

                           if ((get_second_index() - 1 >= 0) && (gridArray[get_first_index()][get_second_index() - 1]->get_symbol() == 'o')){

                               gridArray[get_first_index()][get_second_index() - 1]->set_life_status(false);

 


Related Solutions

Studies of mimicry in predator-prey systems often use plastic/clay replicas of prey species to determine if...
Studies of mimicry in predator-prey systems often use plastic/clay replicas of prey species to determine if predators preferentially avoid the mimic and model, just the model, just the mimic, or even additional species not thought to be the model or the mimic. Replicas are placed for days to weeks in habitats thought to contain predators, models, and mimic, and researchers then count the number of “wounds” or missing replicas as an indication of whether models and mimics are equally avoided/attacked....
C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test...
C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test data. The program should have the following functions: getTotal - This function should accept two-dimensional array as its argument and return the total of all the values in the array. getAverage - This function should accept a two-dimensional array as its argument and return the average of values in the array. getRowTotal - This function should accept a two-dimensional array as its first argument...
Create a new Java project named Program 4 Three Dimensional Shapes. In this project, create a...
Create a new Java project named Program 4 Three Dimensional Shapes. In this project, create a package named threeDimensional and put all 3 of the classes discussed below in this package Include a header comment to EACH OF your files, as indicated in your instructions. Here's a link describing the header. Note that headers are not meant to be in Javadoc format. Note that Javadoc is a huge part of your grade for this assignment. Javadoc requires that every class,...
Create a new Java project named Program 4 Three Dimensional Shapes. In this project, create a...
Create a new Java project named Program 4 Three Dimensional Shapes. In this project, create a package named threeDimensional and put all 3 of the classes discussed below in this package Include a header comment to EACH OF your files, as indicated in your instructions. Here's a link describing the header. Note that headers are not meant to be in Javadoc format. Note that Javadoc is a huge part of your grade for this assignment. Javadoc requires that every class,...
write and explain two dimensional parity check code and hamming codes in simple way please
write and explain two dimensional parity check code and hamming codes in simple way please
Create a C++ project in visual studio. You can use the C++ project that I uploaded...
Create a C++ project in visual studio. You can use the C++ project that I uploaded to complete this project. 1. Write a function that will accept two integer matrices A and B by reference parameters, and two integers i and j as a value parameter. The function will return an integer m, which is the (i,j)-th coefficient of matrix denoted by A*B (multiplication of A and B). For example, if M = A*B, the function will return m, which...
you will create a dynamic two dimensional array of mult_div_values structs (defined below). The two dimensional...
you will create a dynamic two dimensional array of mult_div_values structs (defined below). The two dimensional array will be used to store the rows and columns of a multiplication and division table. Note that the table will start at 1 instead of zero, to prevent causing a divide-by-zero error in the division table! struct mult_div_values { int mult; float div; }; The program needs to read the number of rows and columns from the user as command line arguments. You...
Create a simple C++ application that will exhibit concurrencyconcepts. Your application should create two threads...
Create a simple C++ application that will exhibit concurrency concepts. Your application should create two threads that will act as counters. One thread should count up to 20. Once thread one reaches 20, then a second thread should be used to count down to 0. For your created code, provide a detailed analysis of appropriate concepts that could impact your application. Specifically, address:Performance issues with concurrencyVulnerabilities exhibited with use of stringsSecurity of the data types exhibited.
Please follow the instructions and solve it by c++ Close the project and create a new...
Please follow the instructions and solve it by c++ Close the project and create a new one called 'Lab0-Part3' with the same options as in the previous step. Write a C++ program that does the following: Creates a 100-element array, either statically or dynamically Fills the array with random integers between 1 and 100 inclusive Then, creates two more 100-element arrays, one holding odd values and the other holding even values. Prints both of the new arrays to the console.
Please write in C++ as simple as possible I want you to create a Book Class...
Please write in C++ as simple as possible I want you to create a Book Class for a bookstore. I am not going to tell you what variables should go into it, that is for you to figure out (but you should have at least 5+). And then you must create a UML with all the variables and methods (like: the getters and setters, a default constructor, and a constructor that takes all the variables and finally the printValues() )....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT