forked from alexballet/sr01
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jeu_de_la_vie.c
193 lines (143 loc) · 3.73 KB
/
jeu_de_la_vie.c
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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//Constantes
#define TAILLE 20
#define CYCLE 200
//Fonctions utilisees, préprocesseur
void initialiser(int **matrice);
void afficher(int **matrice);
void modifierMatrice(int **matrice);
int comptNbVoisins(int **sousmatrice);
int AnalyseMatrice(int **matrice, int l, int c);
/*
**************************
Début des fonctions outils
**************************
*/
//Initialisation de la matrice au hasard
void initialiser(int **matrice)
{
int i,j;
for(i=0;i<TAILLE;i++)
{
for(j=0;j<TAILLE;j++)
{
matrice[i][j]=rand()%(2);
}
}
}
//Affichage "graphique"
void afficher(int **matrice)
{
int i, j;
for(i=0;i<TAILLE;i++)
{
for(j=0;j<TAILLE;j++)
{
printf("%d",matrice[i][j]);
}
printf("\n");
}
}
//Modification de la vie en fontion de l'entourage
void modifierMatrice(int **matrice)
{
int i, j,retourAnalMat;
for(i=0;i<TAILLE;i++)
{
for(j=0;j<TAILLE;j++)
{
if(matrice[i][j]==1)
{
retourAnalMat=AnalyseMatrice(matrice,i,j);
if(0<=retourAnalMat && 1>=retourAnalMat)
matrice[i][j]=0;
else if(2<=retourAnalMat && 3>=retourAnalMat)
matrice[i][j]=1;
else if (4<=retourAnalMat && 8>=retourAnalMat)
matrice[i][j]=0;
else
printf("Erreur : nombre de voisins inconnu");
}
else
{
retourAnalMat=AnalyseMatrice(matrice,i,j);
if(retourAnalMat==3)
matrice[i][j]=1;
}
}
}
}
//Compte le nombre de cellule vivante ou morte autour d'une case
int comptNbVoisins(int **sousmatrice)
{
int i, j;
int compteur_Nb_Voisins=0;
for (i=0 ; i<3 ; i++){
for (j=0 ; j<3 ; j++)
{
if(sousmatrice[i][j]==1)
compteur_Nb_Voisins++;
}
}
if (sousmatrice[1][1] == 1)
compteur_Nb_Voisins--;
return compteur_Nb_Voisins;
}
//Analyse case par case la matrice
int AnalyseMatrice(int **matrice, int l, int c)
{
int i, j, k, t;
int nbRetour;
int **sous_Matrice;
sous_Matrice=malloc(3*sizeof(int*));
for (i=0 ; i<3 ; i++)
sous_Matrice[i]=malloc(3*sizeof(int));
for(k=0,i=l-1 ; i<=l+1 ; i++,k++)
{
for(t=0,j=c-1 ; j<=c+1 ; t++,j++)
{
sous_Matrice[k][t]=matrice[(i+TAILLE)%TAILLE][(j+TAILLE)%TAILLE];
}
}
nbRetour=comptNbVoisins(sous_Matrice);
return nbRetour;
}
//Programme principal
int main()
{
int i;
int **matrice;
matrice = malloc(TAILLE*sizeof(int*));
for (i = 0; i < TAILLE; ++i)
matrice[i] = malloc (sizeof(int)*TAILLE);
//Il faudrait vérifier si l'allocation dynamique s'est bien déroulée
srand (time(NULL));
printf("Bienvenue dans le Jeu De La Vie ! Appuyez sur 'Entrer' pour commencer...\n");
getchar();
initialiser(matrice);
printf("La population au départ : \n");
afficher(matrice);
printf("\nPressez sur ENTER pour continuer...\n");
getchar();
for(i=0;i<CYCLE;i++)
{
modifierMatrice(matrice);
printf("La population après %d cycles: \n",i+1);
afficher(matrice);
printf("Pressez sur ENTER pour continuer...\n");
getchar();
}
printf("Nombre de cycle terminé\n");
return 0;
}
/*Tracé des lignes peut être intéressant
void ligne(int largeur)
{
int i;
for(i=0; i<largeur; i++)
printf("+-");
printf("+\n");
}
*/