Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
261 lines (241 loc) · 8.37 KB
/
Copy pathMain.java
File metadata and controls
261 lines (241 loc) · 8.37 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
class Main {
enum Statut {Naissante1, Vivante1, Mourrante1, Naissante2, Vivante2, Mourrante2, Morte};
//enum Statut {Naissante, Vivante, Mourrante, Morte};
static Statut[][] grille;
static Statut[][] grilleTemp;
// Paramètres
static int tailleX;
static int tailleY;
static int nombreDeGenerations;
static double tauxDeRemplissage;
static boolean modeCouleur;
static boolean etapesIntermediaires;
static int taillePop1;
static int taillePop2;
public static void main(String[] args) {
System.out.println("Saisir nombre de lignes :");
tailleX = Clavier.lireEntier();
System.out.println("Saisir nombre de colonnes :");
tailleY = Clavier.lireEntier();
System.out.println("Saisir nombre de Générations :");
nombreDeGenerations = Clavier.lireEntier();
System.out.println("Saisir taux de remplissage(en %) :");
tauxDeRemplissage = Clavier.lireReel();
System.out.println("Voulez-vous le mode couleur :");
modeCouleur = Clavier.lireBooleen();
System.out.println("Voulez-vous visualiser les étapes intermédiares :");
etapesIntermediaires = Clavier.lireBooleen();
grille = new Statut[tailleX][tailleY];
grilleTemp = new Statut[tailleX][tailleY];
for (int g = 0; g <= nombreDeGenerations; g++) {
if (g == 0) {
System.out.println("Génération initiale");
for (int x = 0; x < tailleX; x++) {
for (int y = 0; y < tailleY; y++) {
int nbrAleatoire = Outils.entierAleatoire(0,100);
if (nbrAleatoire <= tauxDeRemplissage) {
//grille[x][y] = Statut.Vivante;
int choixDePop = Outils.entierAleatoire(0,2);
if (choixDePop < 1) {
grille[x][y] = Statut.Vivante1;
taillePop1 += 1;
} else {
grille[x][y] = Statut.Vivante2;
taillePop2 += 1;
}
} else {
grille[x][y] = Statut.Morte;
}
afficherCellule(grille[x][y]);
}
System.out.println("");
}
} else {
Ecran.annulerAffichageCouleur();
System.out.println("Génération n°" + g + " intermédiaire");
evaluerNouvelleGen();
afficherGrille(grilleTemp);
copierGrille();
if (etapesIntermediaires) {
Ecran.annulerAffichageCouleur();
System.out.println("Génération n°" + g);
afficherGrille(grille);
}
}
}
}
static void evaluerNouvelleGen() {
for (int x = 0; x < tailleX; x++) {
for (int y = 0; y < tailleY; y++) {
/*int nombreDeVoisins = evaluerNombreDeCellules(x,y,1,1);
if ((nombreDeVoisins < 2) || (nombreDeVoisins > 3)) {
if (etapesIntermediaires) {
if (grille[x][y] == Statut.Vivante) {
grilleTemp[x][y] = Statut.Mourrante;
continue;
}
}
grilleTemp[x][y] = Statut.Morte;
} else if (nombreDeVoisins == 3) {
if (etapesIntermediaires) {
if (grille[x][y] == Statut.Morte) {
grilleTemp[x][y] = Statut.Naissante;
continue;
}
}
grilleTemp[x][y] = Statut.Vivante;
} else {
grilleTemp[x][y] = grille[x][y];
}*/
Statut statutPrecedent = grille[x][y];
if (statutPrecedent == Statut.Vivante1) {
int nombreDeVoisins = evaluerNombreDeCellules(x, y, 1, Statut.Vivante1);
if ((nombreDeVoisins < 2) || (nombreDeVoisins > 3)) {
if (etapesIntermediaires) {
grilleTemp[x][y] = Statut.Mourrante1;
continue;
}
grilleTemp[x][y] = Statut.Morte;
taillePop1 -= 1;
} else {
grilleTemp[x][y] = Statut.Vivante1;
}
} else if (statutPrecedent == Statut.Vivante2) {
int nombreDeVoisins = evaluerNombreDeCellules(x, y, 1, Statut.Vivante2);
if ((nombreDeVoisins < 2) || (nombreDeVoisins > 3)) {
if (etapesIntermediaires) {
grilleTemp[x][y] = Statut.Mourrante2;
continue;
}
grilleTemp[x][y] = Statut.Morte;
taillePop2 -= 1;
} else {
grilleTemp[x][y] = Statut.Vivante2;
}
}
if (statutPrecedent == Statut.Morte) {
int voisinsPop1 = evaluerNombreDeCellules(x,y,1,Statut.Vivante1);
int voisinsPop2 = evaluerNombreDeCellules(x,y,1,Statut.Vivante2);
if (voisinsPop1 == 3 && voisinsPop2 == 3) {
// TODO : evaluer en cas de conflits (systeme de rescencement de pop)
int voisins2Pop1 = evaluerNombreDeCellules(x,y,2,Statut.Vivante1);
int voisins2Pop2 = evaluerNombreDeCellules(x,y,2,Statut.Vivante2);
if (voisins2Pop1 > voisins2Pop2) {
grilleTemp[x][y] = Statut.Naissante1;
} else if (voisins2Pop2 > voisins2Pop1) {
grilleTemp[x][y] = Statut.Naissante2;
} else {
if (taillePop1 > taillePop2) {
grilleTemp[x][y] = Statut.Naissante1;
} else if (taillePop2 > taillePop2) {
grilleTemp[x][y] = Statut.Naissante2;
} else {
grilleTemp[x][y] = Statut.Morte;
}
}
} else if (voisinsPop1 == 3) {
if (etapesIntermediaires) {
grilleTemp[x][y] = Statut.Naissante1;
continue;
}
grilleTemp[x][y] = Statut.Vivante1;
} else if (voisinsPop2 == 3) {
if (etapesIntermediaires) {
grilleTemp[x][y] = Statut.Naissante2;
continue;
}
grilleTemp[x][y] = Statut.Vivante2;
} else {
grilleTemp[x][y] = Statut.Morte;
}
}
}
}
}
static int evaluerNombreDeCellules(int indexX, int indexY, int rang, Statut population) {
int nombreDeCellules = 0;
for (int x = indexX - rang; x <= indexX + rang; x++) {
for (int y = indexY - rang; y <= indexY + rang; y++) {
if ((x >= 0) && (y >= 0) && (x < tailleX) && (y < tailleY)) {
if (((x == indexX) && (y == indexY)) != true) {
if (grille[x][y] == population) {
nombreDeCellules += 1;
}
}
}
}
}
return nombreDeCellules;
}
static void afficherGrille(Statut[][] grilleAfficher) {
for (int x = 0; x < tailleX; x++) {
for (int y = 0; y < tailleY; y++) {
afficherCellule(grilleAfficher[x][y]);
}
Ecran.annulerAffichageCouleur();
System.out.println("");
}
}
static void afficherCellule(Statut statut) {
if (modeCouleur) {
switch(statut) {
case Morte:
Ecran.changerCouleurFond(Ecran.Couleur.BLANC);
System.out.print(" ");
break;
case Vivante1:
Ecran.changerCouleurFond(Ecran.Couleur.NOIR);
System.out.print(" ");
break;
case Vivante2:
Ecran.changerCouleurFond(Ecran.Couleur.BLEU);
System.out.print(" ");
break;
case Naissante1:
case Naissante2:
Ecran.changerCouleurFond(Ecran.Couleur.VERT);
System.out.print(" ");
break;
case Mourrante1:
case Mourrante2:
Ecran.changerCouleurFond(Ecran.Couleur.ROUGE);
System.out.print(" ");
break;
}
} else {
switch(statut) {
case Morte:
System.out.print(".");
break;
case Vivante1:
System.out.print("#");
break;
case Vivante2:
System.out.print("@");
break;
case Naissante1:
case Naissante2:
System.out.print("+");
break;
case Mourrante1:
case Mourrante2:
System.out.print("-");
break;
}
}
}
static void copierGrille() {
for (int x = 0; x < tailleX; x++) {
for (int y = 0; y < tailleY; y++) {
grille[x][y] = grilleTemp[x][y];
if (grilleTemp[x][y] == Statut.Morte || grilleTemp[x][y] == Statut.Mourrante1|| grilleTemp[x][y] == Statut.Mourrante2) {
grille[x][y] = Statut.Morte;
} else if (grilleTemp[x][y] == Statut.Vivante1 || grilleTemp[x][y] == Statut.Naissante1) {
grille[x][y] = Statut.Vivante1;
} else if (grilleTemp[x][y] == Statut.Vivante2 || grilleTemp[x][y] == Statut.Naissante2) {
grille[x][y] = Statut.Vivante2;
}
}
}
}
}