Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // Created by Julia Sanguinetti on 2019-10-16.
- //
- #include <iostream>
- #include <string>
- #include <vector>
- #include <cstdlib>
- #include <time.h>
- using namespace std;
- // Constants
- const int WORLDSIZE = 20;
- const int INITIAL_ANTS = 20;
- const int INITIAL_DOODLEBUGS = 5;
- const int DOODLEBUG = 1;
- const int ANT = 2;
- const int ANT_BREED = 3;
- const int DOODLE_BREED = 8;
- const int DOODLE_STARVE =3;
- //Declarations of classes
- class Organism;
- class Doodlebug;
- class Ant;
- //World class
- class World {
- friend class Organism;
- friend class Doodlebug;
- friend class Ant;
- public:
- //Constructor
- World ();
- //Destructor
- ~World();
- Organism* getAt (int x, int y);
- void setAt(int x, int y, Organism *org);
- void display();
- void oneTurn ();
- private:
- Organism* grid [WORLDSIZE] [WORLDSIZE];
- };
- // Definition for the Organism base class
- class Organism {
- friend class World;
- public:
- //Constructors
- Organism ();
- Organism (World *wrld, int x, int y);
- //Destructor
- virtual ~Organism() = default;
- //Methods
- virtual void breed () = 0;
- virtual void move () = 0;
- virtual int getType () = 0;
- virtual bool starve ()= 0;
- protected:
- int x,y;
- bool moved;
- int breedCount;
- World *world;
- };
- //Constructor definition
- World::World (){
- int i,j;
- for (i=0; i<WORLDSIZE;i++){
- for (j=0; j<WORLDSIZE;j++)
- grid [i][j] = nullptr;
- }
- }
- // Destructor definition
- World::~World () {
- //Release any allocated memory
- for (int i = 0; i < WORLDSIZE; i++) {
- for (int j = 0; j < WORLDSIZE; j++) {
- if (grid[i][j] != nullptr)
- delete (grid[i][j]);
- }
- }
- }
- // Member function definitions
- Organism* World::getAt(int x, int y) {
- if ((x >= 0) && (x < WORLDSIZE) && (y >= 0) && (y < WORLDSIZE))
- return grid[x][y];
- return nullptr;
- }
- // Method definition of setAt
- void World::setAt(int x, int y, Organism *org) {
- if ((x>=0) && (x<WORLDSIZE) && (y>=0) && (y<WORLDSIZE))
- grid [x][y] = org;
- }
- //Method definition of display
- void World::display() {
- for (int j = 0; j<WORLDSIZE;j++){
- for (int i=0;i<WORLDSIZE;i++){
- if (grid[i][j]== nullptr)
- cout<<'-';
- else if (grid [i][j]->getType() == ANT)
- cout<<'o';
- else
- cout << "X";
- }
- cout<<endl;
- }
- }
- // Method definition of oneTurn
- void World::oneTurn(){
- //First reset all organism to not move
- for (int i =0; i<WORLDSIZE;i++)
- for (int j=0;j<WORLDSIZE;j++){
- if (grid [i][j] != nullptr)
- grid [i][j] ->moved = false;
- }
- //Loop through cells in order and move if its Doodlebug
- for (int i=0; i<WORLDSIZE;i++)
- for (int j=0;j<WORLDSIZE;j++){
- if ((grid [i][j])!=nullptr) && (grid[i][j]->getType() == DOODLEBUG)){
- if (!grid[i][j]->moved) {
- grid[i][j]->moved = true;
- grid[i][j]->move();
- }
- }
- }
- //Loop through cells in order and move if its an Ant
- for (int i=0;i<WORLDSIZE;i++)
- for (int j=0;j<WORLDSIZE;j++){
- if ((grid [i][j] != nullptr) && (grid[i][j]->getType()==ANT)){
- if (!grid [i][j] -> moved){
- grid[i][j]->moved = true;
- grid [i][j]->move();
- }
- }
- }
- // Kill off any doodlebugs that haven't eaten
- for (int i=0; i<WORLDSIZE;i++)
- for (int j=0;j<WORLDSIZE;j++){
- if ((grid [i][j])!= nullptr) && (grid[i][j]->getType() == DOODLEBUG)){
- if (grid[i][j]->starve()) {
- delete (grid[i][j]);
- grid[i][j] = nullptr;
- }
- }
- }
- // Check if organism should breed
- for (int i=0; i<WORLDSIZE;i++)
- for (int j=0; j<WORLDSIZE;j++){
- if ((grid [i][j] != nullptr) && grid [i][j]->moved)
- grid [i][j]->breed();
- }
- }
- // Default constructor of Organism
- Organism::Organism() {
- world = nullptr;
- moved = false;
- breedCount = 0;
- x=0;
- y=0;
- }
- // Constructor with parameters
- Organism::Organism(World *wrld, int x, int y) {
- this->world = wrld;
- moved = false;
- breedCount = 0;
- this->x=x;
- this->y=y;
- wrld->setAt(x,y,this);
- }
-
- // Ant class
- class Ant:public Organism{
- public:
- friend class World;
- //Constructors
- Ant() = default;
- Ant (World *wrld,int x, int y):Organism (wrld,x,y){};
- //Methods
- void breed () override;
- void move () override;
- int getType () override;
- bool starve () override {
- return false;
- }
- };
- // Method definition of move
- void Ant::move() {
- //Pick random direction to move
- int dir = rand() % 4;
- //Try to move up, if not at an edge and empty spot
- if (dir == 0) {
- if ((y > 0) && (world->getAt(x, y - 1) == nullptr)) {
- world->setAt(x, y - 1, world->getAt(x, y));
- //Move to new spot
- worl->setAt(x, y, nullptr);
- y--;
- }
- }
- //Try to move down
- else if (dir == 1) {
- if ((y < WORLDSIZE - 1) && (world->getAt(x, y + 1) == nullptr)) {
- world->setAt(x, y - 1, world->getAt(x, y));
- //Move to new spot
- world->setAt(x, y, nullptr);
- y++;
- }
- }
- //Try to move left
- else if (dir == 2) {
- if ((x > 0) && (world->getAt(x - 1, y) == nullptr)) {
- world->setAt(x - 1, y, world->getAt(x, y));
- //Move to new spot
- world->setAt(x, y, nullptr);
- x--;
- }
- }
- //Try to move right
- else {
- if ((x < WORLDSIZE - 1) && (world->getAt(x + 1, y) == nullptr)) {
- world->setAt(x + 1, y, world->getAt(x, y));
- //Move to new spot
- world->setAt(x, y, nullptr);
- x++;
- }
- }
- }
- //Method definition of getType
- int Ant::getType() {
- return ANT;
- }
- // Method definition of breed
- void Ant::breed() {
- breedCount++;
- if (breedCount ==ANT_BREED){
- breedCount = 0;
- //Try to make a new any either above, left, right, below
- if ((y>0) && (world->getAt(x,y-1) == nullptr))
- auto *newAnt = new Ant (world,x,y-1);
- else if ((y<WORLDSIZE-1) && (world->getAt(x,y+1) == nullptr))
- auto *newAnt = new Ant (world,x, y + 1);
- else if ((x>0) && (world->getAt(x-1,y) == nullptr))
- auto *newAnt = new Ant (world, x -1 ,y);
- else if ((x<WORLDSIZE -1) && (world->getAt(x+1,y)== nullptr))
- auto *newAnt = new Ant (world, x+1, y);
- }
- }
- // Create a class Doodlebug
- class Doodlebug:public Organism {
- public:
- friend class World;
- public:
- Doodlebug ();
- Doodlebug (World *world, int x, int y);
- void breed () override;
- void move () override;
- int getType() override;
- bool starve () override;
- private:
- int starveTricks;
- };
-
- // Default constructor
- Doodlebug::Doodlebug():Organism () {
- starveTricks = 0;
- }
- //Constructor with parameters
- Doodlebug::Doodlebug(World *world, int x, int y)
- : Organism(world, x, y) {
- starveTricks = 0;
- }
- // Member functions
- void Doodlebug::move() {
- if ((y > 0) && (world->getAt(x, y - 1) != nullptr) && (world->getAt(x, y - 1)->getType() == ANT)) {
- delete (world->grid[x][y - 1]);
- world->grid[x][y - 1] = this;
- world->setAt(x, y, nullptr);
- starveTricks = 0;
- y--;
- return;
- }
- else if ((y < WORLDSIZE - 1) && (world->getAt(x, y + 1) != nullptr) && (world->getAt(x, y + 1)->getType() == ANT)) {
- delete (world->grid[x][y + 1]);
- world->grid[x][y + 1] = this;
- world->setAt(x, y, nullptr);
- starveTricks = 0;
- y++;
- return;
- }
- else if ((x > 0) && (world->getAt(x - 1, y) != nullptr) && (world->getAt(x - 1, y)->getType() == ANT)) {
- delete(world->grid[x - 1][y]);
- world->grid[x - 1][y] = this;
- world->setAt(x, y, nullptr);
- starveTricks = 0;
- x--;
- return;
- }
- else if ((x < WORLDSIZE - 1) && (world->getAt(x + 1, y) != nullptr) && (world->getAt(x + 1, y)->getType() == ANT)) {
- delete(world->grid[x + 1][y]);
- world->grid[x + 1][y] = this;
- world->setAt(x, y, nullptr);
- starveTricks = 0;
- x++;
- return;
- }
- int dir = rand() % 4;
- // Try to move up, if not at an edge and empty spot
- if (dir == 0) {
- if ((y > 0) && (world->getAt(x, y - 1) == nullptr)) {
- // Move to new spot
- world->setAt(x, y - 1, world->getAt(x, y));
- // Set current spot to empty
- world->setAt(x, y, nullptr);
- y--;
- }
- }
- // Try to move down
- else if (dir == 1) {
- if ((y < WORLDSIZE - 1) && (world->getAt(x, y + 1) == nullptr)) {
- // Move to new spot
- world->setAt(x, y + 1, world->getAt(x, y));
- // Set current spot to empty
- world->setAt(x, y, nullptr);
- y++;
- }
- }
- // Try to move left
- else if (dir == 2) {
- if ((x > 0) && (world->getAt(x - 1, y) == nullptr)) {
- // Move to new spot
- world->setAt(x - 1, y, world->getAt(x, y));
- // Set current spot to empty
- world->setAt(x, y, nullptr);
- x--;
- }
- }
- // Try to move right
- else {
- if ((x < WORLDSIZE - 1) && (world->getAt(x + 1, y) == nullptr)) {
- // Move to new spot
- world->setAt(x + 1, y, world->getAt(x, y));
- // Set current spot to empty
- world->setAt(x, y, nullptr);
- x++;
- }
- }
- // Increment starve tick since didn't eat on this turn
- starveTricks++;
- }
- // Method definition of getType
- int Doodlebug::getType() {
- return DOODLEBUG;
- }
- // Method definition of breed
- void Doodlebug::breed() {
- breedCount++;
- if (breedCount == DOODLE_BREED) {
- breedCount = 0;
- // Try to make a new ant either above, left, right, or down
- if ((y > 0) && (world->getAt(x, y - 1) == nullptr)) {
- auto *newDoodle = new Doodlebug(world, x, y - 1);
- }
- else if ((y < WORLDSIZE - 1) && (world->getAt(x, y + 1) == nullptr)) {
- auto *newDoodle = new Doodlebug(world, x, y + 1);
- }
- else if ((x > 0) && (world->getAt(x - 1, y) == nullptr)) {
- auto *newDoodle = new Doodlebug(world, x - 1, y);
- }
- else {
- auto *newDoodle = new Doodlebug(world, x + 1, y);
- }
- }
- }
- // Method definition of starve
- bool Doodlebug::starve() {
- // Starve if no food eaten in last DOODLE_STARVE time ticks
- return starveTricks > DOODLE_STARVE;
- }
- int main (){
- // User answer
- char userInput = 'N';
- // Seed random number generator
- srand(time(nullptr));
- World w;
- // Randomly create 100 ants
- int antCount = 0;
- while (antCount < INITIAL_ANTS) {
- int x = rand() % WORLDSIZE;
- int y = rand() % WORLDSIZE;
- // Check for empty spots
- if (w.getAt(x, y) == nullptr) {
- auto *a = new Ant(&w, x, y);
- antCount++;
- }
- }
- // Randomly create 5 doodlebugs
- int doodleCount = 0;
- while (doodleCount < INITIAL_DOODLEBUGS) {
- int x = rand() % WORLDSIZE;
- int y = rand() % WORLDSIZE;
- // Check for empty spots
- if (w.getAt(x, y) == nullptr) {
- auto *d = new Doodlebug(&w, x, y);
- doodleCount++;
- }
- }
- // Run simulation until user cancels
- do {w.display();
- w.oneTurn();
- std::cout << std::endl << "Would you like to run another step?\n";
- std::cin>> userInput;
- } while ((userInput == 'Y') || (userInput == 'y'));
-
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement