-
Notifications
You must be signed in to change notification settings - Fork 2
/
PetriDish.cpp
94 lines (87 loc) · 2.12 KB
/
PetriDish.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include "PetriDish.h"
typedef enum
{
OUT_ID = 0, OUT_AGE, OUT_FOOD
} OutputEnum;
const std::string OUTPUT_FIELDS[] = { "Bacteria ID: ", "Age: ", "Food left: " };
PetriDish::PetriDish():_foodLeft(0){}
PetriDish::PetriDish(const PetriDish &original) : _foodLeft(original._foodLeft)
{
for (std::vector<Bacteria*>::size_type i = 0; i < original._bacterias.size(); i++)
{
Bacteria* _newBacteria(original._bacterias[i]);
_bacterias.push_back(_newBacteria);
}
}
PetriDish& PetriDish::operator=(const PetriDish &rightHandSide)
{
if (this == &rightHandSide) return *this; //check for self assignment
_foodLeft = rightHandSide._foodLeft;
for (std::vector<Bacteria*>::size_type i = 0;
i < rightHandSide._bacterias.size(); i++)
{
Bacteria* _newBacteria(rightHandSide._bacterias[i]);
_bacterias.push_back(_newBacteria);
}
return *this;
}
void PetriDish::clean()
{
_foodLeft = 0;
for (std::vector< Bacteria* >::iterator it = _bacterias.begin() ;
it != _bacterias.end(); ++it)
{
delete (*it);
}
_bacterias.clear();
}
void PetriDish::print() const
{
for (std::vector<Bacteria*>::size_type i = 0; i < _bacterias.size(); i++)
{
_bacterias[i]->print();
}
std::cout << OUTPUT_FIELDS[OUT_FOOD] << _foodLeft << std::endl;
}
void PetriDish::updatePopulation(int timeUnits)
{
for (int j = 0; j < timeUnits; j++)
{
std::vector<Bacteria*> temp_bacterias;
for (std::vector<Bacteria*>::size_type i = 0; i < _bacterias.size();
i++)
{
_bacterias[i]->eat(_foodLeft);
Bacteria* temp_bacteria(_bacterias[i]->clone());
if (temp_bacteria != NULL) // add the new bacteria to the temporary vector
{
temp_bacterias.push_back(temp_bacteria);
}
else
{
_bacterias[i]->grow();
}
}
this->removeDeadBacteria();
for (std::vector<Bacteria*>::size_type k = 0; k < temp_bacterias.size();
k++) {
this->addBacteria(temp_bacterias[k]);
}
}
}
void PetriDish::removeDeadBacteria()
{
for (std::vector<Bacteria*>::iterator iter = _bacterias.begin();
iter != _bacterias.end();)
{
if ((*iter)->isDead() == true)
{
delete (*iter);
iter = _bacterias.erase((iter));
}
else
{
iter++;
}
}
}