-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTP3_Game_of_life.c
147 lines (117 loc) · 3.58 KB
/
TP3_Game_of_life.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
#include <stdlib.h>
#include <stdio.h>
#include "couleurs.h"
#define N 17
void recup_nom_fichier(char *nom_fic) {
/*
* Permet de récupérer le nom du fichier demandé. Nécessite un tableau de caractères et ne renvoie rien (car tableau donné en pointeur)
*/
printf("Entrez le nom du fichier : ");
scanf("%s", nom_fic);
}
void initialiser_matrice(int matrice[N+2][N+2]) {
/*
* Initialise une matrice en la remplissant de zéro. Nécessite la dite matrice et ne renvoie rien
*/
int i, j;
for(i = 0; i < N+2; i++) {
for(j = 0; j < N+2; j++) {
matrice[i][j] = 0;
}
}
}
void afficher_matrice(int matrice[N+2][N+2], int type) {
/*
* Affiche la matrice demandée, en fonction de son type (matrice de cellules ou de voisins), avec des couleurs. Nécessite la matrice, son tyoe, et ne renvoie rien
*/
int i, j;
if(type == 1) { //Cellules
for(i = 0; i < N+2; i++) {
for(j = 0; j < N+2; j++) {
if(matrice[i][j]) {
couleur("34");
} else {
couleur("37");
}
printf("%i ", matrice[i][j]);
}
printf("\n");
}
}
if(type == 2) { //Voisins
for(i = 0; i < N+2; i++) {
for(j = 0; j < N+2; j++) {
switch(matrice[i][j]) {
case 0 : couleur("37"); break;
case 2 :
case 3 : couleur("32"); break;
default : couleur("31"); break;
}
printf("%i ", matrice[i][j]);
}
printf("\n");
}
}
couleur("37");
printf("\n");
printf("\n");
}
void calc_voisins(int matrice_cell[N+2][N+2], int matrice_voisins[N+2][N+2]) {
/*
* Permet de calculer les voisins de chaque cellule. Nécessite la matrice de cellules, celle des voisins, et ne renvoie rien
*/
int i, j;
for(i = 1; i < N+1; i++) {
for(j = 1; j < N+1; j++) { // Pour chaque voisins trouvé (case voisine == 1) on incrémente le nombre de voisins
matrice_voisins[i][j] = matrice_cell[i-1][j-1] + matrice_cell[i-1][j] + matrice_cell[i-1][j+1] + matrice_cell[i][j-1] + matrice_cell[i][j+1] + matrice_cell[i+1][j-1] + matrice_cell[i+1][j] + matrice_cell[i+1][j+1];
}
}
}
void calc_gen_suivante(int matrice_cell[N+2][N+2], int matrice_voisins[N+2][N+2]) {
/*
* Permet de calculer la génération suivante, à partir de la génération actuelle. Nécessite la matrice de cellules et celles de voisins, et modifie directement la matrice de cellules.
*/
int i,j;
for(i = 1; i < N+1; i++) {
for(j = 1; j < N+1; j++) {
if(matrice_voisins[i][j] == 2) {
} else if(matrice_voisins[i][j] == 3) {
matrice_cell[i][j] = 1;
} else {
matrice_cell[i][j] = 0;
}
}
}
}
void game_of_life(int tableau_cell[N+2][N+2], int tableau_voisins[N+2][N+2], int nb_generations) {
/*
* Fait les différentes taches du calcul des nb_generations suivantes. Nécessite les deux matrices et le nombre de généraitons
*/
int i;
for(i = 0; i < nb_generations; i++) {
calc_voisins(tableau_cell, tableau_voisins);
calc_gen_suivante(tableau_cell, tableau_voisins);
afficher_matrice(tableau_cell, 1);
}
}
void main() {
int pos_cel_x;
int pos_cel_y;
int tableau_cell[N+2][N+2];
int tableau_voisins[N+2][N+2];
int nb_generations;
char nom_fichier[20];
recup_nom_fichier(nom_fichier);
FILE *fic_placement = fopen(nom_fichier, "r");
initialiser_matrice(tableau_cell);
while(!feof(fic_placement)) {
fscanf(fic_placement, "%i", &pos_cel_x);
fscanf(fic_placement, "%i", &pos_cel_y);
tableau_cell[pos_cel_x][pos_cel_y] = 1;
}
afficher_matrice(tableau_cell, 1);
printf("Nombre de generations : ");
scanf("%i", &nb_generations);
game_of_life(tableau_cell, tableau_voisins, nb_generations);
fclose(fic_placement);
}