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
|
#include <iostream>
#include <cstdlib>
#include <exception>
#include "population-vivante-v2.h"
using namespace std;
#define CHECK_BOUND(i,j) \
if (i>=N || j>=N){ \
std::cout<<"Accessing a Cell at ("<<i<<","<<j<<") out of range ... aborting"<<std::endl; \
std::terminate(); \
} \
const Cellule* PopulationVivante::at(size_t i, size_t j) const {
for (size_t k = 0 ; k < alive ; k++) {
if ((T[k].getX() == i) && (T[k].getY() == j)) {
return T+k;
}
}
return NULL;
}
Cellule* PopulationVivante::at(size_t i, size_t j) {
for (size_t k = 0 ; k < alive ; k++) {
if ((T[k].getX() == i) && (T[k].getY() == j)) {
return T+k;
}
}
return NULL;
}
size_t PopulationVivante::nb_voisins_vivants(size_t ci, size_t cj) const {
size_t cpt=0;
size_t imin,imax,jmin,jmax;
imin = ci==0?ci:ci-1;
imax = ci==(N-1)?ci:ci+1;
jmin = cj==0?cj:cj-1;
jmax = cj==(N-1)?cj:cj+1;
for (size_t i = imin ; i <= imax ; i++) {
for (size_t j = jmin ; j <= jmax ; j++) {
if (at(i,j) != NULL) {
cpt++;
}
}
}
return cpt - (at(ci,cj) != NULL ? 1 : 0);
}
void PopulationVivante::updateColors() {
//calcule les cellules vivantes qui vont mourir
for (size_t i = 0; i < alive; i++) {
size_t voisin=nb_voisins_vivants(T[i].getX(), T[i].getY());
if ((voisin != 2) && (voisin != 3)) T[i].doitMourir();
}
}
PopulationVivante::PopulationVivante(size_t n, float prob) : alive(0), N(n), probability(prob) {
reset();
}
size_t PopulationVivante::nb_cellules(Cellule::Couleur c) const {
size_t cpt=0;
for (size_t i = 0 ; i < alive ; i++) {
if (CelluleEstDeLaCouleur(T[i],c)) {
cpt++;
}
}
return cpt;
}
size_t PopulationVivante::nb_vivants() const { return N*N-nb_morts();}
size_t PopulationVivante::nb_deces() const { return nb_cellules(Cellule::ROUGE)+nb_cellules(Cellule::JAUNE);}
size_t PopulationVivante::nb_morts() const { return nb_cellules(Cellule::NOIR);}
size_t PopulationVivante::nb_naissances() const { return nb_cellules(Cellule::BLEU);}
Cellule PopulationVivante::getCelluleCopie(size_t i, size_t j) const {
CHECK_BOUND(i,j);
const Cellule* ptr=at(i,j);
if (ptr==NULL) {
return Cellule(false,i,j);
} else {
return *ptr;
}
}
void PopulationVivante::printCell(size_t i, size_t j) const {
CHECK_BOUND(i,j);
getCelluleCopie(i,j).print();
}
void PopulationVivante::kill(size_t i, size_t j) {
CHECK_BOUND(i,j);
const Cellule* ptr=at(i,j);
if (ptr!=NULL) {
size_t k=ptr-T; // retrouve la position dans le tableau
for ( ; k < alive - 1 ; k++) {
T[k]=T[k+1];
}
alive--;
}
}
void PopulationVivante::birth(size_t i, size_t j) {
if (alive+1<NMAX){
CHECK_BOUND(i,j);
Cellule* ptr=at(i,j);
if (ptr==NULL) {
T[alive]=Cellule(true,i,j);
alive++;
}
else{
ptr->setVivante(true);
}
}
else {
std::cerr<<"PopulationVivante: Erreur -> trop de cellule vivante pour NMAX="<<NMAX<<std::endl;
std::cerr<<"aborting...\n";
std::terminate();
}
}
void PopulationVivante::print() const {
for (size_t i = 0; i < N + 2 ; i++) {
cout << "X";
}
cout<<endl;
for (size_t i = 0 ; i < N ; i++) {
cout<<"X";
for (size_t j = 0 ; j < N ; j++) {
cout<<Couleur2String(getCelluleCopie(i,j).getCouleur());
}
cout<<"X"<<endl;
}
for (size_t i = 0 ; i < N + 2 ; i++) {
cout<<"X";
}
cout<<endl;
}
PopulationVivante PopulationVivante::next() const {
PopulationVivante POP(*this);
for (size_t i = 0 ; i < N ; i++) {
for (size_t j = 0 ; j < N ; j++) {
size_t voisin=nb_voisins_vivants(i,j);
if ((voisin == 3) || ((voisin == 2) && (at(i,j) != NULL))) {
POP.birth(i,j);
} else {
POP.kill(i,j);
}
}
}
POP.updateColors();
return POP;
}
/// NEW STUFF TD4
size_t PopulationVivante::getDimension() const {return N;}
float PopulationVivante::getProbability() const {return probability;}
void PopulationVivante::setDimension(size_t n) {N=n; reset();}
void PopulationVivante::setProbability(float p) {probability=p; reset();}
void PopulationVivante::reset() {
alive=0;
srand(time(NULL));
for (size_t i = 0 ; i < N ; i++) {
for (size_t j = 0 ; j < N ; j++) {
if (((rand() % 10000) / 10000.) <= probability) {
birth(i,j);
}
}
}
updateColors();
}
|