-
Notifications
You must be signed in to change notification settings - Fork 0
/
aquarium.cpp
209 lines (180 loc) · 5.22 KB
/
aquarium.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include "Aquarium.h"
#include <random>
#include <iostream>
#include "Display.h"
Aquarium::Aquarium(int capacity, Coordinates borders)
: capacity(capacity), lastId(0), borders(borders), manager(LifeManager(this, creatures)), numberOfCarnivore(0), numberOfHarbivore(0), numberOfPlankton(0)
{
}
Aquarium::~Aquarium()
{
}
void Aquarium::bind()
{
binded = true;
}
void Aquarium::initialize(unsigned int numbP, unsigned int numbHF, unsigned int numbCF)
{
std::random_device rd;
std::mt19937 mt(rd());
std::uniform_int_distribution<int> distx(0, borders.x);
std::uniform_int_distribution<int> disty(0, borders.y);
for (unsigned int i = 0; i < numbCF; i++)
{
addCreature(LifeType::CARNIVOREFISH, Gene(LifeType::CARNIVOREFISH), Coordinates(distx(mt), disty(mt), 0));
};
for (unsigned int i = 0; i < numbHF; i++)
{
addCreature(LifeType::HERBIVOREFISH, Gene(LifeType::HERBIVOREFISH), Coordinates(distx(mt), disty(mt), 0));
};
for (unsigned int i = 0; i < numbP; i++)
{
addCreature(LifeType::PLANKTON, Gene(LifeType::PLANKTON), Coordinates(distx(mt), disty(mt), 0));
};
}
void Aquarium::startGame(bool isForever, int ticks, Display* display_)
{
display = display_;
std::chrono::time_point<std::chrono::system_clock> start, start2, end1, end2;
start = std::chrono::system_clock::now();
while (isForever || ticks)
{
end1 = std::chrono::system_clock::now();
if((std::chrono::duration_cast<std::chrono::milliseconds>(end1 - start) > std::chrono::milliseconds(90)))
{
start = std::chrono::system_clock::now();
display->DrawAquarium();
manager.makeTurn();
if (!(numberOfCarnivore && numberOfHarbivore && numberOfPlankton))
{
end1 = std::chrono::system_clock::now();
std::cout << "STOP!";
manager.printState();
std::cout << "\n\nProgram: " << ((double)std::chrono::duration_cast<std::chrono::milliseconds>(end1 - start).count())/1000 << "seconds\n";
break;
}
}
ticks--;
}
}
bool Aquarium::addCreature(LifeType type, Gene gene, Coordinates coord)
{
if((creatures.size() < capacity) &&
((!(type==LifeType::PLANKTON)) || (numberOfPlankton <= 4*numberOfHarbivore)) &&
((!(type==LifeType::HERBIVOREFISH)) || (numberOfHarbivore <= 4*numberOfCarnivore))
)
{
if (((type == LifeType::CARNIVOREFISH) && (numberOfHarbivore!=0) && (numberOfCarnivore > 0.4*(numberOfHarbivore))) ||
((type == LifeType::HERBIVOREFISH) && (numberOfPlankton!=0) && (numberOfHarbivore > 1*(numberOfPlankton))))
{
return false;
}
Creature* newCreature;
if (type == LifeType::PLANKTON)
{
newCreature = new Plankton(gene, coord, lastId);
numberOfPlankton++;
}
else if (type == LifeType::CARNIVOREFISH)
{
newCreature = new CarnivoreFish(gene, coord, lastId);
numberOfCarnivore++;
}
else
{
numberOfHarbivore++;
newCreature = new HerbivoreFish(gene, coord, lastId);
}
creatures.push_back(newCreature);
throwEvent(newCreature->getPosition(), EventType::BIRTH, newCreature);
getManagerEvent();
lastId++;
return true;
}
else
{
//std::cout << "I can't add fish, because aquarium is full" << std::endl;
return false;
}
}
bool Aquarium::removeCreature(unsigned int id)
{
for (auto iter = creatures.begin(); iter != creatures.end(); iter++)
{
if (id == (*iter)->getId())
{
if ((*iter)->getType() == LifeType::PLANKTON)
{
numberOfPlankton--;
}
else if ((*iter)->getType() == LifeType::CARNIVOREFISH)
{
numberOfCarnivore--;
}
else
{
numberOfHarbivore--;
}
if (!binded)
delete *iter;
creatures.erase(iter);
return true;
}
}
return false;
}
bool Aquarium::removeCreature(std::list<Creature*>::iterator iterator)
{
if ((*iterator)->getType() == LifeType::PLANKTON)
{
numberOfPlankton--;
}
else if ((*iterator)->getType() == LifeType::CARNIVOREFISH)
{
numberOfCarnivore--;
}
else
{
numberOfHarbivore--;
}
if (!binded)
delete *iterator;
creatures.erase(iterator);
return true;
}
void Aquarium::setCapacity(int capacity)
{
this->capacity = capacity;
}
int Aquarium::getCapacity() const
{
return capacity;
}
void Aquarium::setBorders(Coordinates borders)
{
this->borders = borders;
}
Coordinates Aquarium::getBorders() const
{
return borders;
}
int Aquarium::getNumberOfCreatures() const
{
return creatures.size();
}
std::list<Creature*>& Aquarium::getListOfCreatures()
{
return creatures;
}
int Aquarium::getNumberOfP()
{
return numberOfPlankton;
}
int Aquarium::getNumberOfHF()
{
return numberOfHarbivore;
}
int Aquarium::getNumberOfCF()
{
return numberOfCarnivore;
}