Bonjour,

Je fais les exercices du très bon didacticiel de Eric Berthomier et Daniel Schang : Le C en 20 heures.

Après 200 heures , j'en suis à la page 14 : https://c.developpez.com/cours/20-heures/?page=page_14 sur l'exercice du Jeu de la vie.

J'ai écris un programme, puis j'essaie de comprendre ce qui ne marche pas dans le décompte des voisins. Je n'ai pas écris le programme comme la correction, malgré tout, ce passage suit le même processus, pourtant je trouve toujours 8 voisins ??

Je vous remercie de m'aider, car là, je sèche. Je ne trouve pas ce qui foire.

J'utilise un scanf("%d",&o); pour faire une pause à chaque décompte de voisin. Il suffit d'entrer un nombre pour continuer.

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
/**************************************
            JEU DE LA VIE
****************************************/
#include <stdio.h> 
#include <stdlib.h> 
#include <time.h>
#include <math.h>
 
#define CM 	40   	/* Nombre de Cases Modifiées*/
#define CT 	CM+2 	/* Nombre de Cases total*/
 
/* Taille de la matrice contenant */
/* les cellules + 2 pour la bordure */
/****************************************/
/*******   F O N C T I O N S   **********/
/****************************************/
 
 
void init(int matrice[][CT], int matrice2[][CT]) {
	srand(time(NULL));
	int nbz=0;
	int i,j,h;
	for(i=0;i<CT;i++){
		for(j=0;j<CT;j++){
			if (i==0||i==CT-1||j==0||j==CT-1) {
				matrice[i][j]=8;
				matrice2[i][j]=8;
			}
			else {
				nbz=rand()/10000000;
				if(nbz>150) {
					matrice[i][j]=1;
					matrice2[i][j]=1;
				}
				else {
					matrice[i][j]=0;
					matrice2[i][j]=0;
				};
			};
		};
	};
 
	printf("\n");	
}
 
void affiche(int matrice[][CT],int matrice2[][CT]) {
	int i;
	int j;
	for(i=0;i<CT;i++){
		for(j=0;j<CT;j++){
			printf("%d ",matrice2[i][j]);
			matrice[i][j]=matrice2[i][j];
		};
		printf("\n");
	};
	printf("\n");
}
 
 
void remplissage(int matrice[][CT],int matrice2[][CT],int i,int j) {
	int m,n,a=0,mm=0,nn=0;	
	while (a=0) {
		for(m=1;m<=1;m++) {
			for(n=1;n<=1;n++) {
				/*printf("m: %d %d %d ",i,j,matrice[i+m][j+n]);*/
				if(matrice[i+m][j+n]=0) {
					a=1;
					mm=m;
					nn=n;
				};
			};
		};
	};
	matrice2[i+mm][j+nn]=1;
}	
 
void vidage(int matrice[][CT],int matrice2[][CT],int i,int j) {
	int m,n,a=0,mm=0,nn=0;
	while (a=0) {
		for(m=1;m<=1;m++) {
			for(n=1;n<=1;n++) {
				/*printf("m: %d %d %d ",i,j,matrice[i+m][j+n]);*/
				if(matrice[i+m][j+n]=1) {
					a=1;
					mm=m;
					nn=n;
				};
			};
		};
	};
	matrice2[i+mm][j+nn]=0;
}
 
int nombre_voisins (int matrice[][CT],int i, int j) {
	int m,n,o;
	int voisin=0;
	for(m=i-1;m<=i+1;m++) {
		for(n=j-1;n<=j+1;n++) {
			printf("voisins av : %d\n",voisin);
			if(matrice[m][n]=1) voisin++;	
			printf("mat i j m n : %d %d %d %d %d\n", matrice[m][n],i,j,m,n);
			printf("voisins ap : %d\n",voisin);
		};
	};
	if(matrice[i][j]=1) voisin--;
	printf("voisins fin : %d\n",voisin);
	scanf("%d",&o);
	return voisin;
}
 
void mise_a_jour(int matrice[][CT], int matrice2[][CT]) {
	int i,j;
	int voisin=0;
		for (i=1;i<CM;i++) {
			for (j=1;j<CM;j++) {
				voisin=nombre_voisins(matrice,i,j);
					switch(voisin) {
						case 2:
							remplissage(matrice,matrice2,i,j);
							break;
						case 0: case 4: 
							matrice2[i][j]=0;
							break;
					};
			};
		};
		printf("\n");
 
}
 
int main() {
		int i;
		int matrice[CT][CT];
		int matrice2[CT][CT];
		init(matrice,matrice2);
		affiche(matrice,matrice2);
		for (i=0;i<=2;i++) {
			mise_a_jour(matrice,matrice2);
			affiche(matrice,matrice2);
		};
	return 0;
}
J'ai essayé d'abord d'écrire le sous-programme nombre_voisins comme ceci :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
for(m=-1;m<=1;m++) {
		for(n=-1;n<=1;n++) {
			if(matrice[i+m][j+n]=1) voisin++;	
		};
	};
mais cela ne marchait pas non plus.