C++ Object-Oriented Programming - I wrote all of this code in one file; however,
ID: 3708246 • Letter: C
Question
C++ Object-Oriented Programming - I wrote all of this code in one file; however, I need it all broken up into separate files for encapsulation. (header and cpp for every class)
If you could do that and fix any issues that come up with "private within this context" then I'll be good to go. I'll give you an upvote, thanks.
CODE:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Game;
class Organism;
class Doodlebug;
class Ant;
//valid moves for each organism
const int LEFT = 1;
const int RIGHT = 2;
const int DOWN = 3;
const int UP = 4;
const int WORLD_DIMENSION = 20;
//the number of each critter we initialize the world with
const int INIT_DOODLEBUG_COUNT = 5;
const int INIT_ANT_COUNT = 100;
//the time steps it takes for each critter to breed
const int DOODLEBUG_BREED_TIME = 8;
const int ANT_BREED_TIME = 3;
//the time steps it takes for a doodlebug to starve
const int DOODLEBUG_STARVE_TIME = 3;
//number to indicate the type of each critter
const int DOODLEBUG = 1;
const int ANT = 2;
typedef Organism* OrganismPtr;
typedef Game* GamePtr;
class Game {
friend class Organism;
friend class Ant;
friend class Doodlebug;
private:
OrganismPtr world[WORLD_DIMENSION][WORLD_DIMENSION];
int timeStepCount;
int generateRandomNumber(int startRange, int endRange) const;
public:
Game();
void startGame();
void takeTimeStep();
void printWorld() const;
};
class Organism {
protected:
GamePtr currGame;
int x;
int y;
int timeTillBreed;
int timeStepCount;
//given a coordinate of the cell (x,y),
//returns a list of valid moves to adjacent empty cells
vector<int> getMovesToEmptyCells(int x, int y) const;
bool isValidCoordinate(int x, int y) const;
//given a valid move from grid[x][y],
//updates x and y according to the move
void getCoordinate(int& x, int& y, int move) const;
public:
Organism() : currGame(nullptr), x(0), y(0), timeTillBreed(0), timeStepCount(0) {}
Organism(GamePtr currGame, int x, int y);
virtual void breed() = 0;
virtual void move();
virtual int getType() = 0;
virtual bool starves() { return false; }
};
class Doodlebug : public Organism {
private:
int timeTillStarve;
//given a coordinate of the cell (x,y),
//returns a list of valid moves to adjacent ants
vector<int> getMovesToAnts(int x, int y) const;
public:
Doodlebug() : Organism(), timeTillStarve(0) {}
Doodlebug(GamePtr currGame, int x, int y);
void breed();
void move();
int getType() { return DOODLEBUG; }
bool starves() { return timeTillStarve == 0; }
};
class Ant : public Organism {
public:
Ant() : Organism() {}
Ant(GamePtr currGame, int x, int y);
void breed();
int getType() { return ANT; }
};
int Game::generateRandomNumber(int startRange, int endRange) const {
return rand() % (endRange - startRange + 1) + startRange;
}
Game::Game() {
srand(time(NULL));
timeStepCount = 0;
for (int x = 0; x < WORLD_DIMENSION; x++)
for (int y = 0; y < WORLD_DIMENSION; y++)
world[x][y] = nullptr;
}
void Game::startGame() {
int x, y;
int doodlebugCount = 0;
int antCount = 0;
while (doodlebugCount < INIT_DOODLEBUG_COUNT) {
x = generateRandomNumber(0, WORLD_DIMENSION - 1);
y = generateRandomNumber(0, WORLD_DIMENSION - 1);
if (world[x][y] != nullptr) continue;
world[x][y] = new Doodlebug(this, x, y);
doodlebugCount++;
}
while (antCount < INIT_ANT_COUNT) {
x = generateRandomNumber(0, WORLD_DIMENSION - 1);
y = generateRandomNumber(0, WORLD_DIMENSION - 1);
if (world[x][y] != nullptr) continue;
world[x][y] = new Ant(this, x, y);
antCount++;
}
}
void Game::takeTimeStep() {
timeStepCount++;
for (int x = 0; x < WORLD_DIMENSION; x++) {
for (int y = 0; y < WORLD_DIMENSION; y++) {
if (world[x][y] == nullptr) continue;
if (world[x][y]->getType() == DOODLEBUG)
world[x][y]->move();
}
}
for (int x = 0; x < WORLD_DIMENSION; x++) {
for (int y = 0; y < WORLD_DIMENSION; y++) {
if (world[x][y] == nullptr) continue;
if (world[x][y]->getType() == ANT)
world[x][y]->move();
}
}
for (int x = 0; x < WORLD_DIMENSION; x++) {
for (int y = 0; y < WORLD_DIMENSION; y++) {
if (world[x][y] == nullptr) continue;
world[x][y]->breed();
}
}
for (int x = 0; x < WORLD_DIMENSION; x++) {
for (int y = 0; y < WORLD_DIMENSION; y++) {
if (world[x][y] == nullptr) continue;
if (world[x][y]->starves()) {
delete world[x][y];
world[x][y] = nullptr;
}
}
}
}
void Game::printWorld() const {
for (int x = 0; x < WORLD_DIMENSION; x++) {
for (int y = 0; y < WORLD_DIMENSION; y++) {
if (world[x][y] == nullptr)
cout << '-';
else if (world[x][y]->getType() == ANT)
cout << 'o';
else //world[x][y]->getType() == DOODLEBUG
cout << 'X';
}
cout << endl;
}
}
vector<int> Organism::getMovesToEmptyCells(int x, int y) const {
vector<int> movesToEmptyCells;
int tempX, tempY;
for (int move = LEFT; move <= UP; move++) {
tempX = x;
tempY = y;
getCoordinate(tempX, tempY, move);
if (!isValidCoordinate(tempX, tempY)) continue;
if (currGame->world[tempX][tempY] == nullptr)
movesToEmptyCells.push_back(move);
}
return movesToEmptyCells;
}
bool Organism::isValidCoordinate(int x, int y) const {
if (x < 0 || x >= WORLD_DIMENSION || y < 0 || y >= WORLD_DIMENSION)
return false;
return true;
}
void Organism::getCoordinate(int& x, int& y, int move) const {
if (move == LEFT) x--;
if (move == RIGHT) x++;
if (move == DOWN) y--;
if (move == UP) y++;
}
Organism::Organism(GamePtr currGame, int x, int y) {
this->currGame = currGame;
this->x = x;
this->y = y;
timeTillBreed = 0;
timeStepCount = currGame->timeStepCount;
}
void Organism::move() {
if (timeStepCount == currGame->timeStepCount) return;
timeStepCount++;
timeTillBreed--;
int randomMove = currGame->generateRandomNumber(LEFT, UP);
int newX = x;
int newY = y;
getCoordinate(newX, newY, randomMove);
if (isValidCoordinate(newX, newY)) {
if (currGame->world[newX][newY] != nullptr) return;
currGame->world[x][y] = nullptr;
currGame->world[newX][newY] = this;
x = newX;
y = newY;
}
}
vector<int> Doodlebug::getMovesToAnts(int x, int y) const {
vector<int> movesToAnts;
int tempX, tempY;
for (int move = LEFT; move <= UP; move++) {
tempX = x;
tempY = y;
getCoordinate(tempX, tempY, move);
if (!isValidCoordinate(tempX, tempY)) continue;
if (currGame->world[tempX][tempY] == nullptr) continue;
if (currGame->world[tempX][tempY]->getType() == ANT)
movesToAnts.push_back(move);
}
return movesToAnts;
}
Doodlebug::Doodlebug(GamePtr currGame, int x, int y) : Organism(currGame, x, y) {
timeTillStarve = DOODLEBUG_STARVE_TIME;
timeTillBreed = DOODLEBUG_BREED_TIME;
}
void Doodlebug::breed() {
if (timeTillBreed > 0) return;
vector<int> validMoves = getMovesToEmptyCells(x, y);
if (validMoves.size() == 0) return;
int randomMove = validMoves[currGame->generateRandomNumber(0, validMoves.size() - 1)];
int newX = x;
int newY = y;
getCoordinate(newX, newY, randomMove);
currGame->world[newX][newY] = new Doodlebug(currGame, newX, newY);
timeTillBreed = DOODLEBUG_BREED_TIME;
}
void Doodlebug::move() {
if (timeStepCount == currGame->timeStepCount) return;
vector<int> movesToAnts = getMovesToAnts(x, y);
if (movesToAnts.size() == 0) {
Organism::move();
timeTillStarve--;
return;
}
timeStepCount++;
timeTillStarve = DOODLEBUG_STARVE_TIME;
int randomMove = movesToAnts[currGame->generateRandomNumber(0, movesToAnts.size() - 1)];
int antX = x;
int antY = y;
getCoordinate(antX, antY, randomMove);
delete currGame->world[antX][antY];
currGame->world[antX][antY] = this;
currGame->world[x][y] = nullptr;
x = antX;
y = antY;
}
Ant::Ant(GamePtr currGame, int x, int y) : Organism(currGame, x, y) {
timeTillBreed = ANT_BREED_TIME;
}
void Ant::breed() {
if (timeTillBreed > 0) return;
vector<int> validMoves = getMovesToEmptyCells(x, y);
if (validMoves.size() == 0) return;
int randomMove = validMoves[currGame->generateRandomNumber(0, validMoves.size() - 1)];
int newX = x;
int newY = y;
getCoordinate(newX, newY, randomMove);
currGame->world[newX][newY] = new Ant(currGame, newX, newY);
timeTillBreed = ANT_BREED_TIME;
}
int main() {
char userInput;
Game g;
cout << "WELCOME TO DOODLEBUG VS. ANTS GAME" << endl;
cout << "Please enter to start the game, anything else to end" << endl;
cin.get(userInput);
if (userInput != ' ') {
cout << "Ending Program..." << endl;
exit(0);
}
g.startGame();
cout << "This is your randomly generated game board!" << endl << endl;
g.printWorld();
cout << endl;
cout << "Please enter to generate a timestep, anything else to abort: " << endl;
cin.get(userInput);
while (userInput == ' ') {
g.takeTimeStep();
g.printWorld();
cout << endl;
cout << "Please enter to generate a timestep, anything else to abort: " << endl;
cin.get(userInput);
}
cout << "Ending Program..." << endl;
return 0;
}
Explanation / Answer
Includes
#include "stdafx.h"
#include <iostream>
#include <ctime>
#include <vector>
using namespace std;
main
int main()
{
//Create environment object containing
environment antDoodlebugSimulation;
antDoodlebugSimulation.InitializeSimulation();
return 0;
}
Environment
class environment
{
//friends of environment
friend class organism;
friend class doodlebug;
friend class ant;
private:
organism * environmentBoard[20][20];
void CreateStartPopulation();
int GenerateRandomStartingLocations(int min, int max);
void OutputCurrentEnvironment();
void DoodlebugsAct();
void AntsAct();
void ResetCritterTimeStep();
public:
//constructor
environment();
//deconstructor
~environment();
//public member functions
void InitializeSimulation();
};
environment::environment()
{
//Initialize environmentBoard array for entirely empty...all nullptr
for (int yCounter = 0; yCounter < 20; yCounter++)
for (int xCounter = 0; xCounter < 20; xCounter++)
this->environmentBoard[xCounter][yCounter] = nullptr;
//Add ants and doodlebugs into game environment
CreateStartPopulation();
}
void environment::OutputCurrentEnvironment()
{
//Outputs current environment to the screen
for (int yCounter = 0; yCounter < 20; yCounter++)
{
for (int xCounter = 0; xCounter < 20; xCounter++)
if (this->environmentBoard[xCounter][yCounter] == nullptr)
cout << '-';
else
cout << *environmentBoard[xCounter][yCounter];
cout << endl;
}
}
void environment::CreateStartPopulation()
{
//Populates game board for start of simulation
coordinates newLocation;
//Fill 5 doodlebugs
for (int doodleCounter = 0; doodleCounter < 5; doodleCounter++)
{
newLocation.xCoordinate = GenerateRandomStartingLocations(0, 19);
newLocation.yCoordinate = GenerateRandomStartingLocations(0, 19);
//Incase something already placed
while (this->environmentBoard[newLocation.xCoordinate][newLocation.yCoordinate] != nullptr)
{
newLocation.xCoordinate = GenerateRandomStartingLocations(0, 19);
newLocation.yCoordinate = GenerateRandomStartingLocations(0, 19);
}
this->environmentBoard[newLocation.xCoordinate][newLocation.yCoordinate] = new doodlebug(newLocation.xCoordinate, newLocation.yCoordinate, this);
}
for (int antCounter = 0; antCounter < 100; antCounter++)
{
newLocation.xCoordinate = GenerateRandomStartingLocations(0, 19);
newLocation.yCoordinate = GenerateRandomStartingLocations(0, 19);
//Incase something already placed
while (this->environmentBoard[newLocation.xCoordinate][newLocation.yCoordinate] != nullptr)
{
newLocation.xCoordinate = GenerateRandomStartingLocations(0, 19);
newLocation.yCoordinate = GenerateRandomStartingLocations(0, 19);
}
this->environmentBoard[newLocation.xCoordinate][newLocation.yCoordinate] = new ant(newLocation.xCoordinate, newLocation.yCoordinate, this);
}
}
int environment::GenerateRandomStartingLocations(int min, int max)
{
static bool firstIteration = true;
if (firstIteration)
{
//Seed once
srand(time(NULL));
firstIteration = false;
}
//Generate random number between range
return rand() % (max - min + 1) + min;
}
void environment::AntsAct()
{
//Iterate over environment
for(int yCounter = 0; yCounter < 20; yCounter++)
for (int xCounter = 0; xCounter < 20; xCounter++)
{
if (this->environmentBoard[xCounter][yCounter] != nullptr)
{
if (this->environmentBoard[xCounter][yCounter]->GetIdentifierTag() == 'o' && !(this->environmentBoard[xCounter][yCounter]->GetMoveAlternatorStatus()))
{
//Toggle moveAlternator
this->environmentBoard[xCounter][yCounter]->ToggleMoveAlternator();
//Move ant....Move function must return coordinate otherwise we attempt to increment/breed null pointer after movement
this->environmentBoard[xCounter][yCounter]->Move();
}
}
}
//Iterate over Ants again and breed
for(int yCounter = 0; yCounter < 20; yCounter++)
for (int xCounter = 0; xCounter < 20; xCounter++)
{
if (this->environmentBoard[xCounter][yCounter] != nullptr)
{
if (this->environmentBoard[xCounter][yCounter]->GetIdentifierTag() == 'o')
{
//Increment Ant Breeding cycle
this->environmentBoard[xCounter][yCounter]->IncrementBreedingCycle();
//Check if ready to breed
if (this->environmentBoard[xCounter][yCounter]->TimeToBreed())
{
this->environmentBoard[xCounter][yCounter]->Breed();
}
}
}
}
}
void environment::DoodlebugsAct()
{
//Move and eat if ant is found
for (int yCounter = 0; yCounter < 20; yCounter++)
{
for (int xCounter = 0; xCounter < 20; xCounter++)
{
//Not null pointer...a doodlebug....and has not already moved
if (this->environmentBoard[xCounter][yCounter] != nullptr && this->environmentBoard[xCounter][yCounter]->GetIdentifierTag() == 'X' && !this->environmentBoard[xCounter][yCounter]->GetMoveAlternatorStatus())
{
this->environmentBoard[xCounter][yCounter]->ToggleMoveAlternator();
this->environmentBoard[xCounter][yCounter]->Move();
}
}
}
//Breed and then check death cycle
for (int yCounter = 0; yCounter < 20; yCounter++)
{
for (int xCounter = 0; xCounter < 20; xCounter++)
{
if (this->environmentBoard[xCounter][yCounter] != nullptr && this->environmentBoard[xCounter][yCounter]->GetIdentifierTag() == 'X')
{
//Increment breeding step
this->environmentBoard[xCounter][yCounter]->IncrementBreedingCycle();
//Check if ready to breed
if (this->environmentBoard[xCounter][yCounter]->TimeToBreed())
this->environmentBoard[xCounter][yCounter]->Breed();
//Check if dying
if (this->environmentBoard[xCounter][yCounter]->Death())
this->environmentBoard[xCounter][yCounter]->Die();
}
}
}
}
void environment::ResetCritterTimeStep()
{
//Reset all critter time steps at the end of iteration
for (int yCounter = 0; yCounter < 20; yCounter++)
for (int xCounter = 0; xCounter < 20; xCounter++)
if (this->environmentBoard[xCounter][yCounter] != nullptr && (this->environmentBoard[xCounter][yCounter]->GetIdentifierTag() == 'o' || this->environmentBoard[xCounter][yCounter]->GetIdentifierTag() == 'X'))
this->environmentBoard[xCounter][yCounter]->ToggleMoveAlternator();
}
void environment::InitializeSimulation()
{
int numOfants, numOfDoodleBugs;
OutputCurrentEnvironment();
cout << "Simulation initialized starting with 5 doodle bugs and 100 ants. Please begin simulation by pressing the enter key ";
while(cin.get() == ' ')
{
//Manage simulation
numOfants = 0;
numOfDoodleBugs = 0;
//Call doodlebugs to act
DoodlebugsAct();
//Call ants to act
AntsAct();
//Reset all critter time steps
ResetCritterTimeStep();
//output graphical environment status
OutputCurrentEnvironment();
//For easy visual of fluctuations between predator and prey populations
for (int yCounter = 0; yCounter < 20; yCounter++)
for (int xCounter = 0; xCounter < 20; xCounter++)
if (this->environmentBoard[xCounter][yCounter] != nullptr)
if (this->environmentBoard[xCounter][yCounter]->GetIdentifierTag() == 'o')
numOfants++;
else
numOfDoodleBugs++;
cout << "Ants: " << numOfants << endl;
cout << "Doodlebugs: " << numOfDoodleBugs << endl;
cout << "To continue simulation please press enter; otherwise enter -1: ";
}
}
environment::~environment()
{
//Clean up memory for object and repoint all dangling pointers
for(int yCounter = 0; yCounter < 20; yCounter++)
for (int xCounter = 0; xCounter < 20; xCounter++)
{
if (this->environmentBoard[xCounter][yCounter] != nullptr)
{
delete this->environmentBoard[xCounter][yCounter];
this->environmentBoard[xCounter][yCounter] = nullptr;
}
}
}
Organism
class organism
{
//Organism parent class
private:
bool moveAlternator = false;
protected:
environment * environmentPointer;
int breedingCycle = 0;
char identifierTag;
coordinates location;
public:
//Constructor
organism() {location.xCoordinate = 0; location.yCoordinate = 0;};
//deconstructor
virtual ~organism() {};
char GetIdentifierTag() const { return this->identifierTag; };
void IncrementBreedingCycle() { breedingCycle += 1; };
coordinates GetLocation() const { return this->location; };
virtual void Move() = 0;
void RandomDirectionalMovement(int direction);
virtual bool TimeToBreed() = 0;
void Die();
virtual bool Death() =0;
void Breed();
void ResetBreedingCycle() { this->breedingCycle = 0; };
void ToggleMoveAlternator();
bool GetMoveAlternatorStatus() const { return moveAlternator; };
void GoNorth() { this->location.yCoordinate -= 1; };
void GoEast() { this->location.xCoordinate += 1; };
void GoSouth() { this->location.yCoordinate += 1; };
void GoWest() { this->location.xCoordinate -= 1; };
friend ostream &operator<<(ostream &output, const organism ¤tOrg) { output << currentOrg.identifierTag; return output; };
};
void organism::ToggleMoveAlternator()
{
if (this->moveAlternator == false)
this->moveAlternator = true;
else
this->moveAlternator = false;
}
void organism::Breed()
{
//Breeds new
//Generate direction to breed in...if empty..breed
int newDirection = this->environmentPointer->GenerateRandomStartingLocations(1, 4);
bool breedSuccesful = false;
//North
if (newDirection == 1)
{
if (this->location.yCoordinate - 1 > -1 && this->environmentPointer->environmentBoard[this->location.xCoordinate][this->location.yCoordinate - 1] == nullptr)
{
if (this->GetIdentifierTag() == 'o')//Breed new ant
this->environmentPointer->environmentBoard[this->location.xCoordinate][this->location.yCoordinate - 1] = new ant(this->location.xCoordinate, this->location.yCoordinate - 1, this->environmentPointer);
else if (this->GetIdentifierTag() == 'X')//Breed new doodlebug
this->environmentPointer->environmentBoard[this->location.xCoordinate][this->location.yCoordinate - 1] = new doodlebug(this->location.xCoordinate, this->location.yCoordinate - 1, this->environmentPointer);
breedSuccesful = true;
}
}
//East
else if (newDirection == 2)
{
if (this->location.xCoordinate + 1 < 20 && this->environmentPointer->environmentBoard[this->location.xCoordinate + 1][this->location.yCoordinate] == nullptr)
{
if(this->GetIdentifierTag() == 'o')//Breed new ant
this->environmentPointer->environmentBoard[this->location.xCoordinate+1][this->location.yCoordinate] = new ant(this->location.xCoordinate+1, this->location.yCoordinate, this->environmentPointer);
else if (this->GetIdentifierTag() == 'X')//Breed new doodlebug
this->environmentPointer->environmentBoard[this->location.xCoordinate + 1][this->location.yCoordinate] = new doodlebug(this->location.xCoordinate + 1, this->location.yCoordinate, this->environmentPointer);
breedSuccesful = true;
}
}
//South
else if (newDirection == 3)
{
if (this->location.yCoordinate + 1 < 20 && this->environmentPointer->environmentBoard[this->location.xCoordinate][this->location.yCoordinate + 1] == nullptr)
{
if (this->GetIdentifierTag() == 'o')//Breed new ant
this->environmentPointer->environmentBoard[this->location.xCoordinate][this->location.yCoordinate+1] = new ant(this->location.xCoordinate, this->location.yCoordinate+1, this->environmentPointer);
else if (this->GetIdentifierTag() == 'X')//Breed new doodlebug
this->environmentPointer->environmentBoard[this->location.xCoordinate][this->location.yCoordinate+1] = new doodlebug(this->location.xCoordinate, this->location.yCoordinate+1, this->environmentPointer);
breedSuccesful = true;
}
}
//West
else if (newDirection == 4)
{
if (this->location.xCoordinate - 1 > -1 && this->environmentPointer->environmentBoard[this->location.xCoordinate - 1][this->location.yCoordinate] == nullptr)
{
if (this->GetIdentifierTag() == 'o')//Breed new ant
this->environmentPointer->environmentBoard[this->location.xCoordinate-1][this->location.yCoordinate] = new ant(this->location.xCoordinate-1, this->location.yCoordinate, this->environmentPointer);
else if (this->GetIdentifierTag() == 'X')//Breed new doodlebug
this->environmentPointer->environmentBoard[this->location.xCoordinate-1][this->location.yCoordinate] = new doodlebug(this->location.xCoordinate-1, this->location.yCoordinate, this->environmentPointer);
breedSuccesful = true;
}
}
if (breedSuccesful)
this->ResetBreedingCycle();
}
void organism::RandomDirectionalMovement(int direction)
{
//North
if (direction == 1)
{
if (this->location.yCoordinate - 1 > -1 && this->environmentPointer->environmentBoard[this->location.xCoordinate][this->location.yCoordinate - 1] == nullptr)
{
//Empty space found and valid location within stated environment
//Point new location to current organism object
this->environmentPointer->environmentBoard[this->location.xCoordinate][this->location.yCoordinate - 1] = this->environmentPointer->environmentBoard[this->location.xCoordinate][this->location.yCoordinate];
//repoint old location to null
this->environmentPointer->environmentBoard[this->location.xCoordinate][this->location.yCoordinate] = nullptr;
//update ant location
this->GoNorth();
}
}
//East
else if (direction == 2)
{
if (this->location.xCoordinate + 1 < 20 && this->environmentPointer->environmentBoard[this->location.xCoordinate + 1][this->location.yCoordinate] == nullptr)
{
//Empty space found and valid location with stated environment
//Point new location to current ant object
this->environmentPointer->environmentBoard[this->location.xCoordinate + 1][this->location.yCoordinate] = this->environmentPointer->environmentBoard[this->location.xCoordinate][this->location.yCoordinate];
//Repoint old location to null
this->environmentPointer->environmentBoard[this->location.xCoordinate][this->location.yCoordinate] = nullptr;
//update ant location
this->GoEast();
}
}
//South
else if (direction == 3)
{
if (this->location.yCoordinate + 1 < 20 && this->environmentPointer->environmentBoard[this->location.xCoordinate][this->location.yCoordinate + 1] == nullptr)
{
//Empty space found and valid location with stated environment
//Point new location to current ant object
this->environmentPointer->environmentBoard[this->location.xCoordinate][this->location.yCoordinate + 1] = this->environmentPointer->environmentBoard[this->location.xCoordinate][this->location.yCoordinate];
//Repoint old location to null
this->environmentPointer->environmentBoard[this->location.xCoordinate][this->location.yCoordinate] = nullptr;
//update organism location
this->GoSouth();
}
}
//West
else if (direction == 4)
{
if (this->location.xCoordinate - 1 > -1 && this->environmentPointer->environmentBoard[this->location.xCoordinate - 1][this->location.yCoordinate] == nullptr)
{
//Empty space found and valid location with stated environment
//Point new location to current organism object
this->environmentPointer->environmentBoard[this->location.xCoordinate - 1][this->location.yCoordinate] = this->environmentPointer->environmentBoard[this->location.xCoordinate][this->location.yCoordinate];
//Repoint old location to null
this->environmentPointer->environmentBoard[this->location.xCoordinate][this->location.yCoordinate] = nullptr;
//update organism location
this->GoWest();
}
}
}
void organism::Die()
{
//repoint location in environment to null
this->environmentPointer->environmentBoard[this->location.xCoordinate][this->location.yCoordinate] = nullptr;
//delete
delete this;
doodlebug
class doodlebug : public organism
{
//Doodlebug child class
private:
int starveCounter = 0;
public:
//Constructor
doodlebug(int xLocation, int yLocation, environment * worldPointer) { identifierTag = 'X'; location.xCoordinate = xLocation; location.yCoordinate = yLocation; environmentPointer = worldPointer; };
//Deconstructor
~doodlebug() {};
bool TimeToBreed();
void Move();
coordinates SearchForAnts();
void IncrementStarveCounter() { this->starveCounter += 1; };
void ResetStarveCounter() { this->starveCounter = 0; };
bool Death() { if (this->starveCounter == 3) return true; else return false; };
};
coordinates doodlebug::SearchForAnts()
{
coordinates antFound;
antFound.xCoordinate = -1;
antFound.yCoordinate = -1;
//store checked direction
int directionCheck;
vector<int> storeDirections;
directionCheck = this->environmentPointer->GenerateRandomStartingLocations(1, 4);
//randomize first search direction and store already searched directions
while (storeDirections.size() < 4 && antFound.xCoordinate == -1 && antFound.yCoordinate == -1)
{
//Search North for ant...verify not off the board...verify not null pointer...check if ant
if (directionCheck == 1 && this->location.yCoordinate - 1 > -1 && this->environmentPointer->environmentBoard[this->location.xCoordinate][this->location.yCoordinate - 1] != nullptr)
{
if (this->environmentPointer->environmentBoard[this->location.xCoordinate][this->location.yCoordinate - 1]->GetIdentifierTag() == 'o')
{
antFound.xCoordinate = this->location.xCoordinate;
antFound.yCoordinate = this->location.yCoordinate - 1;
}
}
//Search East for Ant....
else if (directionCheck == 2 && this->location.xCoordinate + 1 < 20 && this->environmentPointer->environmentBoard[this->location.xCoordinate + 1][this->location.yCoordinate] != nullptr)
{
if (this->environmentPointer->environmentBoard[this->location.xCoordinate + 1][this->location.yCoordinate]->GetIdentifierTag() == 'o')
{
antFound.xCoordinate = this->location.xCoordinate + 1;
antFound.yCoordinate = this->location.yCoordinate;
}
}
//Search South for Ant....
else if (directionCheck == 3 && this->location.yCoordinate + 1 < 20 && this->environmentPointer->environmentBoard[this->location.xCoordinate][this->location.yCoordinate + 1] != nullptr)
{
if (this->environmentPointer->environmentBoard[this->location.xCoordinate][this->location.yCoordinate + 1]->GetIdentifierTag() == 'o')
{
antFound.xCoordinate = this->location.xCoordinate;
antFound.yCoordinate = this->location.yCoordinate + 1;
}
}
//Search west for Ant...
else if (directionCheck == 4 && this->location.xCoordinate - 1 > -1 && this->environmentPointer->environmentBoard[this->location.xCoordinate - 1][this->location.yCoordinate] != nullptr)
{
if (this->environmentPointer->environmentBoard[this->location.xCoordinate - 1][this->location.yCoordinate]->GetIdentifierTag() == 'o')
{
antFound.xCoordinate = this->location.xCoordinate - 1;
antFound.yCoordinate = this->location.yCoordinate;
}
}
//Store direction check to increase size of stored directions....guarantees 4
storeDirections.push_back(directionCheck);
if (directionCheck < 4)
{
directionCheck += 1;
}
else
directionCheck = 1;
}
return antFound;
}
Ant
class ant : public organism
{
//ant child class
private:
public:
//Constructor
ant(int xLocation, int yLocation, environment * thisWorld) { identifierTag = 'o'; location.xCoordinate = xLocation; location.yCoordinate = yLocation; environmentPointer = thisWorld; };
//Deconstructor
~ant() {};
void Move();
bool TimeToBreed();
bool Death() { return false; };
};
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.