Skip to content

Commit 52c3dcf

Browse files
committed
added cell and organism
0 parents  commit 52c3dcf

17 files changed

+175
-0
lines changed

cell.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include "cell.hpp"
2+
/**
3+
* Brief: Default Constructor
4+
* Description: empty constructor to prevent default compiler generated constructur
5+
* */
6+
Cell::Cell()
7+
{
8+
}
9+
10+
/**
11+
* Brief: Constructor with x and y co-ordinates provided
12+
* Description : Initializes a cell with x and y co-ordinates
13+
* @param: int xVal - x co-ordinate
14+
* @param: int yVal - y co-ordinate
15+
* */
16+
Cell::Cell(int xVal, int yVal) : x(xVal), y(yVal)
17+
{ }
18+
19+
/**
20+
* Brief: Overladed equality operator
21+
* Description : Utility overload to compare to cells
22+
* @param: const Cell& a - first cell to be compared
23+
* @param: const Cell& b - second cell to be compared
24+
* @return : true if equal
25+
* */
26+
bool operator ==(const Cell& a, const Cell& b)
27+
{
28+
return ((a.x == b.x) && (a.y == b.y));
29+
}
30+
31+
/**
32+
* Brief: Utility function to return string notation of cell
33+
* Description : Returns a string representation of the cell with its x and y co-ordinates
34+
* @return : string representation of cell
35+
* */
36+
std::string Cell::str() const
37+
{
38+
return '(' + std::to_string(x) + ',' + std::to_string(y) + ')';
39+
}

cell.hpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
///implementation of cell class
2+
#ifndef CELL_H
3+
#define CELL_H
4+
5+
#include <string>
6+
#include <vector>
7+
8+
class Cell
9+
{
10+
public:
11+
///default constructor
12+
Cell();
13+
//initialize cell with x and y chords
14+
Cell(int xVal, int yVal);
15+
//overladed operator to check for equality
16+
friend bool operator ==(const Cell& cell1, const Cell& cell2);
17+
int x;
18+
int y;
19+
std::string str() const;
20+
};
21+
///vector to hold cells
22+
typedef std::vector<Cell> CellVector;
23+
24+
#endif /* CELL_H */

controller.cpp

Whitespace-only changes.

controller.hpp

Whitespace-only changes.

coyote.cpp

Whitespace-only changes.

coyotee.hpp

Whitespace-only changes.

grid.cpp

Whitespace-only changes.

grid.hpp

Whitespace-only changes.

main.cpp

Whitespace-only changes.

organism.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#include "organism.hpp"
2+
3+
/**
4+
* Description : Constructor to initialize an organism
5+
* @param : int requiredSurvivalTime - time before breed
6+
* @param : char organismSymbol - type of organism
7+
* @param : const Cell& cell - cell location
8+
* */
9+
Organism::Organism(
10+
int requiredSurvivalTime,
11+
char organismSymbol,
12+
const Cell& cell
13+
) :
14+
myCell(cell),
15+
required_survival_time_for_breeding(requiredSurvivalTime),
16+
symbol(organismSymbol),
17+
timeSurvived(0)
18+
{ }
19+
20+
/**
21+
* Default Destructor
22+
* */
23+
Organism::~Organism() {}
24+
25+
/**
26+
* Description : Get what organism is residing in cell
27+
* @param : Organism* organism - organism pointer to cell
28+
* @return : Symbol of organism if present else empty space symbol
29+
* */
30+
31+
char getSymbolOrEmptyChar(Organism* organism)
32+
{
33+
if (organism == NULL)
34+
return EMPTY_SPACE_CHAR;
35+
else
36+
return organism->symbol;
37+
}
38+
39+
/**
40+
* Brief : compare cells to see what organism resides there
41+
* @param : char value - value to be compared
42+
* */
43+
OrganismComparator::OrganismComparator(char value) :
44+
compareValue(value)
45+
{ }
46+
/**
47+
* Brief : compare cells to see what organism resides there
48+
* Description : Uses the organism object to compare value
49+
* @param : Organism* organism - pointer to organism in cell
50+
*
51+
* */
52+
OrganismComparator::OrganismComparator(Organism* organism) :
53+
compareValue(getSymbolOrEmptyChar(organism))
54+
{ }
55+
56+
/**
57+
* Description : Overloaded operator to compare organism values
58+
* @return : true if the match else false
59+
* */
60+
bool OrganismComparator::compare(Organism* organism)
61+
{
62+
return (compareValue == getSymbolOrEmptyChar(organism));
63+
}

organism.hpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#ifndef ORGANISM_H
2+
#define ORGANISM_H
3+
4+
#include "cell.hpp"
5+
#include <vector>
6+
7+
const char ROADRUNNER_CHAR = 'o';
8+
const char COYOTE_CHAR = 'X';
9+
const char EMPTY_SPACE_CHAR = '-';
10+
11+
class Organism
12+
{
13+
public:
14+
Organism(int requiredSurvivalTime, char organismSymbol, const Cell& cell);
15+
///destructor
16+
virtual ~Organism() = 0;
17+
//virtual functions to be implemented in base classes
18+
virtual void move(const Cell& newCell) = 0;
19+
virtual Cell getCell() const = 0;
20+
virtual void setCell(const Cell& cell) = 0;
21+
virtual int getTimeSurvived() const = 0;
22+
virtual void incrementTimeSurvived() = 0;
23+
virtual bool readyToBreed() const = 0;
24+
const char symbol;
25+
//gets type of organism
26+
friend char getSymbolOrEmptyChar(Organism* organism);
27+
protected:
28+
const int required_survival_time_for_breeding;
29+
int timeSurvived;
30+
Cell myCell;
31+
};
32+
33+
///vector of organism pointers
34+
typedef std::vector<Organism*> OrganismVector;
35+
36+
/**
37+
* Utility class to compare organisms
38+
* */
39+
class OrganismComparator
40+
{
41+
public:
42+
OrganismComparator(char value);
43+
OrganismComparator(Organism* organism);
44+
bool compare(Organism* organism);
45+
private:
46+
const char compareValue;
47+
};
48+
49+
#endif /* ORGANISM_H */

organismcreator.cpp

Whitespace-only changes.

organismcreator.hpp

Whitespace-only changes.

organismimplementation.cpp

Whitespace-only changes.

organismimplementation.hpp

Whitespace-only changes.

roadrunner.cpp

Whitespace-only changes.

roadrunner.hpp

Whitespace-only changes.

0 commit comments

Comments
 (0)