-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopulation.cpp
120 lines (106 loc) · 2.12 KB
/
population.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
#include "population.hpp"
#include <algorithm>
#include <stdlib.h>
Population::Population()
{
//pop = Animal[400];
for(int i = 0; i < TAILLEGRILLE*TAILLEGRILLE; i++)
{
freeId.push_back(i);
}
}
Animal Population::get(int id) const
{
for(int i = 0; i < TAILLEGRILLE*TAILLEGRILLE; i++)
{
if(pop[i].getId() == id)
{
return pop[i];
}
}
return *(new Animal{});
}
Animal Population::getIndex(int i) const {
return pop[i];
}
vector<int> Population::getIds(Espece type) const
{
vector<int> helper;
int pop_size = sizeof(pop) / sizeof(pop[0]);
for(int i = 0; i < pop_size; i++)
{
if(pop[i].getEspece() == type)
{
helper.push_back(pop[i].getId());
}
}
return helper;
}
int Population::reserve()
{
if(freeId.size() == 0)
{
return -1;
}
else
{
int helper = freeId[0];
freeId.erase(freeId.begin());
return helper;
}
}
int Population::set(Espece type, Coord c)
{
int id = reserve();
if(id == -1)
{
return -1;
}
pop[id] = *(new Animal{id, type, c});
return id;
}
void Population::supprime(int id)
{
for(int i = 0; i < TAILLEGRILLE*TAILLEGRILLE; i++)
{
if(pop[i].getId() == id)
{
pop[i] = {};
freeId.push_back(id);
sort(freeId.begin(), freeId.end());
}
}
}
void Population::changeCoord(int id, Coord c) {
for(int i = 0; i < TAILLEGRILLE*TAILLEGRILLE; i++)
{
if(pop[i].getId() == id)
{
pop[i].setCoord(c);
}
}
}
void Population::setFoodInit(int id, int f) {
int y;
for(int i = 0; i < TAILLEGRILLE*TAILLEGRILLE; i++)
{
if(pop[i].getId() == id)
{
y = i;
}
}
if(f > pop[y].getMaxFood()) {
pop[y].setFoodInit(pop[y].getMaxFood());
}else {
pop[y].setFoodInit(f);
}
}
void Population::aged(int id) {
for(int i = 0; i < TAILLEGRILLE*TAILLEGRILLE; i++)
{
if(pop[i].getId() == id)
{
pop[i].aged();
}
}
}