Using C you will use cellular automata to create a 2D predat
Using C++ you will use cellular automata to create a 2D predator–prey simulation in your program. The preys are ants and the predators are doodlebugs. These critters live in a 20 * 20 grid of cells. Only one critter may occupy a cell at a time. The grid is enclosed and no critter may move off the grid. Time is simulated in steps. Each critter performs some action every time step. The ants behave according to the following model: Move: For every time step, the ants 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 (not been eaten by doodlebugs), at the end of the time step (i.e., 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 randomly. If there is no empty cell available, no breeding occurs. Once an offspring is produced, an ant cannot produce an offspring again until it has survived three more time steps.
The doodlebugs behave according to the following model:
Move: For every time step, the doodlebug will firstly try to move to an adjacent cell containing an ant and eat the ant (you can decide if there are several ants in the adjacent cells, how the doodlebug will choose to move). If there are no ants in adjacent cells, 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, 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 three time steps, at the end of the third time step it will starve and die. The doodlebug should then be removed from the grid of cells. Initialize the world with 5 doodlebugs and 100 ants. You will randomly place them. 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. You will prompt the user to enter the number of time steps to run.
Create a class named Critter that contains data and functions common to ants and doodlebugs. This class should have a virtual function named move that is defined in the derived classes of Ant and Doodlebug. Each class will be in its own source file.
For each time step, do the following in your program: after moves, when breeding, eating, and starving are resolved, display the resulting grid. Draw the world using ASCII characters of “O” for an ant, “X” for a doodlebug and “E” for an empty space (the characters should be arranged to look like a grid). The doodlebugs will move before the ants in each time step. When you reach the time steps entered by the user, ask them to enter another number and start to run the simulation again or to exit the program. You must maintain the state of the current grid while creating the next display. You will use a dynamic array to represent the grid. Each array element will be a pointer to a Critter. Get your program running and tested. For debugging your program, you should save the random placement until you have everything else working properly. In general, “random” is bad for testing and debugging.
Solution
Solution:
Critter.h:
#ifndef CRITTER_H
#define CRITTER_H
#pragma once
#include \"Loct.h\"
#include <iostream>
namespace DBGame
{
class Critter
{
public:
Critter( );
Critter(Loct post, char spics);
Critter(const Critter& othr);
Critter& operator=(const Critter& othr);
void set_location(Loct new_loc);
virtual Loct& get_loc( );
virtual char get_specs( );
private:
Loct m_pts;
char m_speis;
};
}//DBGame
#endif // CRITTER_H
Critter.cpp:
#include \"stdafx.h\"
#include \"Critter.h\"
namespace DBGame
{
Critter::Critter( )
{ }
Critter::Critter(
Loct pnts,
char spics):
m_pts(pnts),
m_speis(spics)
{ }
Critter::Critter(const Critter& othr)
{
this->m_pts = othr.m_pts;
this->m_speis = othr.m_speis;
}
Critter& Critter::operator=(const Critter& othr)
{
this->m_pts = othr.m_pts;
this->m_speis = othr.m_speis;
return *this;
}
char Critter::get_specs( )
{
return m_speis;
}
Loct& Critter::get_loc( )
{
return m_pts;
}
//validate x and y are >= 0 && <= 20
void Critter::set_location(Loct new_loc)
{
((new_loc.get_xpoint( ) - 1) >= 0 && (new_loc.get_xpoint( ) + 1) <= 19) ?
m_pts.set_xpoint(new_loc.get_xpoint( )) : m_pts.set_xpoint(m_pts.get_xpoint( ));
((new_loc.get_ypoint( ) - 1) >= 0 && (new_loc.get_ypoint( ) + 1) <= 19) ?
m_pts.set_ypoint(new_loc.get_ypoint( )) : m_pts.set_ypoint(m_pts.get_ypoint( ));
}
}//DoodleBugsGameOrtell
Anat.h:
#ifndef ANT_H
#define ANT_H
#pragma once
#include \"Critter.h\"
namespace DBGame
{
class Ant : public Critter
{
public:
Ant( );
Ant(Loct post, char spics, int st_breed);
char get_specs( );
Loct& get_loc( );
void st_stp_tillbreed(const int count);
int gt_stps_tlbreed( ) const;
private:
int s_stp_tlbreed;
};
} // DBGame
#endif //ANT_H
Ant.cpp:
#include \"stdafx.h\"
#include \"Ant.h\"
#include <cstdlib>
namespace DBGame
{
Ant::Ant( )
{ }
Ant::Ant( Loct pnts,
char spics,
int st_breed):
Critter(pnts, spics),
s_stp_tlbreed(st_breed)
{ }
char Ant::get_specs( )
{
return \'O\';
}
Loct& Ant::get_loc( )
{
return Critter::get_loc( );
}
void Ant::st_stp_tillbreed(const int count)
{
s_stp_tlbreed = count;
}
int Ant::gt_stps_tlbreed( ) const
{
return s_stp_tlbreed;
}
}//DBGame
Doodlebug.h:
#ifndef DOODLEBUG_H
#define DOODLEBUG_H
#pragma once
#include \"Critter.h\"
namespace DBGame
{
class DoodleBug : public Critter
{
public:
DoodleBug( );
DoodleBug(Loct post, char spics, int st_breed, int steps_till_death);
char get_specs( );
Loct& get_loc( );
void st_stp_tillbreed(const int count);
void st_stps_tildeath(const int count);
int gt_stps_tlbreed( ) const;
int gt_stps_tildeath( ) const;
private:
int s_stp_tlbreed;
int m_stps_tldeath;
};
} // DBGame
#endif //DOODLEBUG_H
Doodlebug.cpp:
#include \"stdafx.h\"
#include \"Doodlebug.h\"
namespace DBGame
{
DoodleBug::DoodleBug( )
{ }
DoodleBug::DoodleBug( Loct post,
char spics,
int tl_breed,
int tl_dth):
Critter(post, spics),
s_stp_tlbreed(tl_breed),
m_stps_tldeath(tl_dth)
{ }
char DoodleBug::get_specs( )
{
return \'X\';
}
Loct& DoodleBug::get_loc( )
{
return Critter::get_loc( );
}
void DoodleBug::st_stp_tillbreed(const int count)
{
s_stp_tlbreed = count;
}
void DoodleBug::st_stps_tildeath(const int count)
{
m_stps_tldeath = count;
}
int DoodleBug::gt_stps_tlbreed( ) const
{
return s_stp_tlbreed;
}
int DoodleBug::gt_stps_tildeath( ) const
{
return m_stps_tldeath;
}
}//DoodlebugGameOrtell
Gameboard.h:
#ifndef GAMEBOARD_H
#define GAMEBOARD_H
#pragma once
#include <iostream>
#include \"Loct.h\"
#include \"Constants.h\"
#include \"Ant.h\"
#include \"DoodleBug.h\"
namespace DBGame
{
class GameBoard
{
public:
GameBoard( );
Loct chk_empty_cell(Loct& curnt_cell);
Loct chk_ant_cell(Loct& curnt_cell);
Loct pk_randomsur_cell(Loct& curnt_cell);
char gt_contntof_chosencell(Loct& chosen_cell);
void rmov_organism_gameBoard(Loct& cell);
void ad_ant_gmBoard(Ant& ant);
void ad_doodlebug_gmBoard(DoodleBug& player);
private:
Critter m_gmBoard[RWs][COLs];
};
}//DBGame
#endif //GAMEBOARD_H
Gameboard.cpp:
#include \"stdafx.h\"
#include \"GameBoard.h\"
namespace DBGame
{
GameBoard::GameBoard( ) // populates gameBoard with empty organisms
{
for(int i =0; i < RWs; i++)
for(int j =0; j < COLs; j++)
m_gmBoard[i][j] = Critter(Loct(i, j),\'E\');
}
void GameBoard::rmov_organism_gameBoard(Loct& cell)
{
m_gmBoard[cell.get_xpoint( )][cell.get_ypoint( )] = Critter(cell, \'E\');
}
void GameBoard::ad_ant_gmBoard(Ant& ant)
{
m_gmBoard[ant.get_loc( ).get_xpoint( )][ant.get_loc( ).get_ypoint( )] = ant;
}
void GameBoard::ad_doodlebug_gmBoard(DoodleBug& doodleBug)
{
m_gmBoard[doodleBug.get_loc( ).get_xpoint( )][doodleBug.get_loc( ).get_ypoint( )] = doodleBug;
}
Loct GameBoard::pk_randomsur_cell(Loct& curnt_cell)
{
switch(rand( ) % 4)
{
case 0: //<--- UP
if(curnt_cell.get_xpoint( ) -1 >= GAMEBOARD_BOTTOM_SIDE )
return Loct((curnt_cell.get_xpoint()-1), curnt_cell.get_ypoint());
case 1: //<--- DOWN
if (curnt_cell.get_xpoint( ) +1 <= GAMEBOARD_HIGH_SIDE)
return Loct((curnt_cell.get_xpoint( )+1), curnt_cell.get_ypoint( ));
case 2: //<--- LEFT
if (curnt_cell.get_ypoint( ) -1 >= GAMEBOARD_BOTTOM_SIDE)
return Loct((curnt_cell.get_xpoint( )), curnt_cell.get_ypoint( )-1);
case 3: //<--- RIGHT
if(curnt_cell.get_xpoint( ) +1 <= GAMEBOARD_HIGH_SIDE)
return Loct((curnt_cell.get_xpoint( )), curnt_cell.get_ypoint( )+1);
}
return curnt_cell;
}
char GameBoard::gt_contntof_chosencell(Loct& chosen_cell)
{
return m_gmBoard[chosen_cell.get_xpoint( )][chosen_cell.get_ypoint( )].get_specs( );
}
Loct GameBoard:: chk_empty_cell(Loct& curnt_cell)
{
if(curnt_cell.get_xpoint( ) -1 >= GAMEBOARD_BOTTOM_SIDE) //<--- Up
if(m_gmBoard[curnt_cell.get_xpoint( )-1][curnt_cell.get_ypoint( )].get_specs( ) == \'E\')
return Loct((curnt_cell.get_xpoint( )-1), curnt_cell.get_ypoint( ));
if(curnt_cell.get_xpoint( ) +1 <= GAMEBOARD_HIGH_SIDE) //<--- Down
if(m_gmBoard[curnt_cell.get_xpoint( )+1][curnt_cell.get_ypoint( )].get_specs( ) == \'E\')
return Loct((curnt_cell.get_xpoint( )+1), curnt_cell.get_ypoint( ));
if(curnt_cell.get_ypoint( ) -1 >= GAMEBOARD_BOTTOM_SIDE) //<--- Left
if(m_gmBoard[curnt_cell.get_xpoint( )][curnt_cell.get_ypoint( )-1].get_specs( ) == \'E\')
return Loct((curnt_cell.get_xpoint( )), curnt_cell.get_ypoint( )-1);
if(curnt_cell.get_ypoint( ) +1 <= GAMEBOARD_HIGH_SIDE) //<--- Right
if(m_gmBoard[curnt_cell.get_xpoint( )][curnt_cell.get_ypoint( )+1].get_specs( ) == \'E\')
return Loct((curnt_cell.get_xpoint( )), curnt_cell.get_ypoint( )+1);
return curnt_cell; // All are Occupied
}
Loct GameBoard::chk_ant_cell(Loct& curnt_cell)
{
if(curnt_cell.get_xpoint( ) -1 >= GAMEBOARD_BOTTOM_SIDE) //<--- Up
if(m_gmBoard[curnt_cell.get_xpoint( )-1][curnt_cell.get_ypoint( )].get_specs( ) == \'O\')
return Loct((curnt_cell.get_xpoint( )-1), curnt_cell.get_ypoint( ));
if(curnt_cell.get_xpoint( ) +1 <= GAMEBOARD_HIGH_SIDE) //<--- Down
if(m_gmBoard[curnt_cell.get_xpoint( )+1][curnt_cell.get_ypoint( )].get_specs( ) == \'O\')
return Loct((curnt_cell.get_xpoint( )+1), curnt_cell.get_ypoint( ));
if(curnt_cell.get_ypoint( ) -1 >= GAMEBOARD_BOTTOM_SIDE) //<--- Left
if(m_gmBoard[curnt_cell.get_xpoint( )][curnt_cell.get_ypoint( )-1].get_specs( ) == \'O\')
return Loct((curnt_cell.get_xpoint( )), curnt_cell.get_ypoint( )-1);
if(curnt_cell.get_ypoint( ) +1 <= GAMEBOARD_HIGH_SIDE) //<--- Right
if(m_gmBoard[curnt_cell.get_xpoint( )][curnt_cell.get_ypoint( )+1].get_specs( ) == \'O\')
return Loct((curnt_cell.get_xpoint( )), curnt_cell.get_ypoint( )+1);
return curnt_cell; // All are Occupied
}
}//DBGame
Gameengine.h:
#ifndef GAMEENGINE_H
#define GAMEENGINE_H
#pragma once
#include <vector>
#include \"Ant.h\"
#include \"DoodleBug.h\"
#include \"GameBoard.h\"
#include \"Constants.h\"
namespace DBGame
{
class GameEngine
{
public:
GameEngine( );
Ant& get_ant_frm_ants(int index);
DoodleBug& gt_doodleBug_frm_doodlebugs(int index);
void mv_ant(Ant& ant_2_move);
void mv_ant(Ant& ant_2_move);
void mv_doodleBug(DoodleBug& doodleBug_2_move);
void et_ant(Loct& location_of_ant_2_eat);
void brd_doodleBug(DoodleBug& doodleBug_2_breed);
void kill_dodlebg(DoodleBug& doodleBug_2_kill);
void poplte_gameBoard( );
void drw_gmBoard_2_console( );
int get_num_ofants( ) const;
int get_numof_doodleBugs( ) const;
void set_num_of_ants(const int number_of_ants);
void set_numof_doodleBugs(const int number_of_doodleBugs);
private:
int m_number_ofants;
int m_number_ofdoodleBugs;
GameBoard m_gmBoard;
Ant m_ants[ANTS_SIZE];
DoodleBug m_doodleBugs[DOODLEBUGS_SIZE];
};
}//DoodlebugGameOrtell
#endif //GAMEENGINE_H
Gameengine.cpp:
#include \"stdafx.h\"
#include \"GameEngine.h\"
namespace DBGame
{
GameEngine::GameEngine( )
{
m_number_ofants =0;
m_number_ofdoodleBugs =0;
poplte_gameBoard( );
}
int GameEngine::get_num_ofants( ) const
{
return m_number_ofants;
}
int GameEngine::get_numof_doodleBugs( ) const
{
return m_number_ofdoodleBugs;
}
void GameEngine::set_num_of_ants(const int number_of_ants)
{
m_number_ofants = number_of_ants;
}
void GameEngine::set_numof_doodleBugs(const int number_of_doodleBugs)
{
m_number_ofdoodleBugs = number_of_doodleBugs;
}
Ant& GameEngine::get_ant_frm_ants(int index)
{
return m_ants[index];
}
DoodleBug& GameEngine::gt_doodleBug_frm_doodlebugs(int index)
{
return m_doodleBugs[index];
}
void GameEngine::poplte_gameBoard( )
{
int count =0;
while(m_number_ofants < 100)
{
int x = rand( ) % 20;
int y = rand( ) % 20;
if (m_gmBoard.gt_contntof_chosencell(Loct(x, y)) == \'*\')
{
m_ants[count] = (Ant(Loct(x, y), \'O\', 0));
m_gmBoard.ad_ant_gmBoard(Ant(Loct(x, y), \'O\', 0));
m_number_ofants++;
count++;
}
}
count =0;
while (m_number_ofdoodleBugs < 5)
{
int x = rand( ) % 20;
int y = rand( ) % 20;
if (m_gmBoard.gt_contntof_chosencell(Loct(x, y)) == \'*\')
{
m_doodleBugs[count] = (DoodleBug(Loct(x, y), \'X\', 0, 0));
m_gmBoard.ad_doodlebug_gmBoard(DoodleBug(Loct(x, y), \'X\', 0, 0));
m_number_ofdoodleBugs++;
count++;
}
}
}
void GameEngine::mv_ant(Ant& ant_2_move)
{
Loct desired_cell = m_gmBoard.pk_randomsur_cell(ant_2_move.get_loc( ));
if (m_gmBoard.gt_contntof_chosencell(desired_cell) == \'*\')
{
m_gmBoard.rmov_organism_gameBoard(ant_2_move.get_loc( ));
ant_2_move.set_location(desired_cell);
m_gmBoard.ad_ant_gmBoard(ant_2_move);
(ant_2_move.gt_stps_tlbreed( ) == 3) ? mv_ant(ant_2_move) :
ant_2_move.st_stp_tillbreed(ant_2_move.gt_stps_tlbreed( )+1);
}
}
void GameEngine::mv_ant(Ant& ant_2_move)
{
Loct spawns_cell = m_gmBoard. chk_empty_cell(ant_2_move.get_loc( ));
if (spawns_cell != ant_2_move.get_loc( ))
{
m_ants[m_number_ofants] = (Ant(spawns_cell, \'O\', 0));
m_gmBoard.ad_ant_gmBoard(Ant(spawns_cell, \'O\', 0));
ant_2_move.st_stp_tillbreed(0);
m_number_ofants++;
}
}
void GameEngine::mv_doodleBug(DoodleBug& doodleBug_2_move)
{
Loct desired_cell = m_gmBoard.chk_ant_cell(doodleBug_2_move.get_loc( ));
if (m_gmBoard.gt_contntof_chosencell(desired_cell) == \'O\')
{
m_gmBoard.rmov_organism_gameBoard(desired_cell);
m_gmBoard.rmov_organism_gameBoard(doodleBug_2_move.get_loc());
doodleBug_2_move.set_location(desired_cell);
m_gmBoard.ad_doodlebug_gmBoard(doodleBug_2_move);
m_number_ofants--;
doodleBug_2_move.st_stps_tildeath(0);
(doodleBug_2_move.gt_stps_tlbreed( ) == 8) ? brd_doodleBug(doodleBug_2_move) :
doodleBug_2_move.st_stp_tillbreed(doodleBug_2_move.gt_stps_tlbreed( )+1);
}
else
{
desired_cell = m_gmBoard. chk_empty_cell(doodleBug_2_move.get_loc( ));
if (desired_cell != doodleBug_2_move.get_loc( ))
{
m_gmBoard.rmov_organism_gameBoard(doodleBug_2_move.get_loc( ));
doodleBug_2_move.set_location(desired_cell);
m_gmBoard.ad_doodlebug_gmBoard(doodleBug_2_move);
(doodleBug_2_move.gt_stps_tildeath( ) == 3) ? kill_dodlebg(doodleBug_2_move) :
doodleBug_2_move.st_stps_tildeath(doodleBug_2_move.gt_stps_tildeath( )+1);
(doodleBug_2_move.gt_stps_tlbreed( ) == 8) ? brd_doodleBug(doodleBug_2_move) :
doodleBug_2_move.st_stp_tillbreed(doodleBug_2_move.gt_stps_tlbreed( )+1);
}
}
}
void GameEngine::brd_doodleBug(DoodleBug& doodleBug_2_breed)
{
Loct spawns_cell = m_gmBoard. chk_empty_cell(doodleBug_2_breed.get_loc( ));
if (spawns_cell != doodleBug_2_breed.get_loc( ))
{
m_doodleBugs[m_number_ofdoodleBugs] = (DoodleBug(spawns_cell, \'X\', 0, 0));
m_gmBoard.ad_doodlebug_gmBoard(DoodleBug(spawns_cell, \'X\', 0, 0));
doodleBug_2_breed.st_stp_tillbreed(0);
doodleBug_2_breed.st_stps_tildeath(0);
m_number_ofdoodleBugs++;
}
}
void GameEngine::kill_dodlebg(DoodleBug& doodleBug_2_kill)
{
m_gmBoard.rmov_organism_gameBoard(doodleBug_2_kill.get_loc());
}
void GameEngine::drw_gmBoard_2_console( )
{
for(int i =0; i < 20; i++)
{
for(int j =0; j < 20; j++)
{
std::cout << m_gmBoard.gt_contntof_chosencell(Loct(i, j)) << \" \";
}
std::cout << std::endl;
}
}
}//DoodlebugGameOrtell
DBGame.h:
#ifndef CONSTANTS_H
#define CONSTANTS_H
namespace DBGame
{
const int RWs = 20;
const int COLs = 20;
const int ANTS_SIZE =400;
const int DOODLEBUGS_SIZE =200;
const int GAMEBOARD_HIGH_SIDE = 19;
const int GAMEBOARD_BOTTOM_SIDE =0;
}//DBGame
#endif //CONSTANTS_H
Game.cpp:
#include \"stdafx.h\"
#include <Windows.h>
#include \"GameEngine.h\"
using namespace DBGame;
int _tmain(int argc, _TCHAR* argv[])
{
GameEngine test_engine;
test_engine.drw_gmBoard_2_console( );
int count =0;
while (true)
{
for (int i =0; i < test_engine.get_num_ofants( ); i++)
{
test_engine.mv_ant(test_engine.get_ant_frm_ants(i));
}
for (int i =0; i < test_engine.get_numof_doodleBugs( ); i++)
{
test_engine.mv_doodleBug(test_engine.gt_doodleBug_frm_doodlebugs(i));
std::cout << \"DoodleBugs Loop ran \" << i << std::endl;
}
std::cout << std::endl;
std::cout << \"Outer Loop \" << count << std::endl;
std::cout << \"Ants: \"<< test_engine.get_num_ofants( ) << \" DoodleBugs: \"
<< test_engine.get_numof_doodleBugs( ) << std::endl;
std::cout << std::endl;
test_engine.drw_gmBoard_2_console( );
Sleep(1000);
count++;
}
system(\"PAUSE\");
return 0;
}










