j’ai un problème avec des valeurs de variables aleatoires que je n’arrive pas à résoudre.
Je m’explique, j’ai une fonction SaisiePion qui a en argument 4 pointeurs sur des variables de mon main (coordonnées x, coordonnées y, nouvelle coordonnées x, nouvelle coordonnées y).
Généralement à la première saisie cela fonctionne, il me renvoi les bonnes coordonnées mais à la seconde une des quatre variables a une valeur aléatoire.
Je ne sais pas trop d’où ca vient. Sur windows et linux j’ai le même problème, pourtant le compilateur ne m’indique pas d’erreur.
Sur linux j’ai le message final « segmentation fault (core dumped) ».
C'est un jeu de dames, voici le code. si vous avez en plus des suggestions pour optimiser tout ça, je suis preneur car je ne suis pas trop calé.
Merci.
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
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define NB_ELEM_TAB 11
 
/* Déclaration de la structure pion */
struct stpion {
  int ligne;
  int colonne;
  int caseblanche;
  char couleur;
  char type;
};
 
  /*Déclaration de structure joueur */
struct stjoueur {
  char nom [25];
  char couleur;
};
 
void InitDamier (struct stpion tableau[NB_ELEM_TAB][NB_ELEM_TAB]);
void AfficheDamier (struct stpion tableau[NB_ELEM_TAB][NB_ELEM_TAB]);
void SaisiePion (int * ax, int * ay, int * nx, int * ny);
void Deplacer (struct stpion tableau[NB_ELEM_TAB][NB_ELEM_TAB],
				int ax, int ay, int nx, int ny);
void SaisieNom (struct stjoueur * JoueurX);
void SaisieCouleur (struct stjoueur * JoueurX);
/*---------------------------------------------------------------------*/
/*--------------- Prototype d'initialisation du damier ----------------*/
 
/*	caseblanche '0' = case interdite
	couleur 'b' = pion blanc
	couleur 'n' = pion noir
	couleur ' ' = aucune
	type 'p' = pion
	type 'd' = dame
	type 'r' = aucun */
 
void InitDamier (struct stpion tableau[NB_ELEM_TAB][NB_ELEM_TAB])
 {
  int i=0;
  int j=0;
/*------- boucle d'initialisation des pions blancs lignes 1 à 4 ---------*/
  for (i=1;i<5;i=i+1){
		for(j=1;j<NB_ELEM_TAB;j=j+1){
			if ((i%2!=0 && j%2==0) || (i%2==0 && j%2!=0)){
			  tableau[i][j].couleur='b';
			  tableau[i][j].ligne=i;
			  tableau[i][j].colonne=j;
			  tableau[i][j].type='p';}
 
			else 	{tableau[i][j].couleur = ' ';
				 tableau[i][j].caseblanche = 0;
				 tableau[i][j].type='r';
				 tableau[i][j].ligne=i;
	   			 tableau[i][j].colonne=j;
				}
			printf ("\n%d:%d=%c\n",i,j,tableau[i][j].couleur);
					}
				}
/*--------- boucle d'initialisation des pions noires lignes 7 à 10 --------*/
  for (i=7;i<NB_ELEM_TAB;i=i+1){
		for(j=1;j<NB_ELEM_TAB;j=j+1){
			if ((i%2!=0 && j%2==0) || (i%2==0 && j%2!=0)){
			  tableau[i][j].couleur='n';
			  tableau[i][j].ligne=i;
			  tableau[i][j].colonne=j;
			  tableau[i][j].type='p';}
 
			else 	{tableau[i][j].couleur = ' ';
				 tableau[i][j].caseblanche = 0;
				 tableau[i][j].type='r';
				 tableau[i][j].ligne=i;
				 tableau[i][j].colonne=j;}
 
			printf ("\n%d:%d=%c\n",i,j,tableau[i][j].couleur);
					}
				}
/*---------------- boucle d'initialisation des lignes 5 et 6 --------------*/
for (i=5;i<7;i=i+1){
		for(j=1;j<NB_ELEM_TAB;j=j+1){
			if ((i%2!=0 && j%2==0) || (i%2==0 && j%2!=0)){
			  tableau[i][j].couleur=' ';
			  tableau[i][j].ligne=i;
			  tableau[i][j].colonne=j;
			  tableau[i][j].type='r';}
 
			else {tableau[i][j].caseblanche = 0;
			      tableau[i][j].couleur=' ';}
 
			printf ("\n%d:%d=%c\n",i,j,tableau[i][j].couleur);
				}
			}
/*---------------- boucle d'initialisation du tableau[0][j] ---------------*/
for (j=0;j<NB_ELEM_TAB;j=j+1) 
	{
	 i=0;
	 tableau[i][j].couleur =' ';
	 tableau[i][j].caseblanche = 0;
	}
/*---------------- boucle d'initialisation du tableau[i][0] ---------------*/
	 for (i=0;i <NB_ELEM_TAB; i=i+1) 
	{
	 j=0;
	 tableau[i][j].couleur =' ';
	 tableau[i][j].caseblanche = 0;
	}
}
/*------------------- Fin de la fonction InitDamier ----------------------*/
 
/*------------------------------------------------------------------------*/
 
/*--------------------Prototype Affichage du damier ----------------------*/
void AfficheDamier (struct stpion tableau[NB_ELEM_TAB][NB_ELEM_TAB])
{
int i = 0;
int j = 0;
 
	printf ("\n");
	printf ("\t    | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10|");
 
	for (i=1;i<NB_ELEM_TAB-2;i=i+1){
	j=1;
	printf ("\n\t");
	printf ("----------------------------------------------\n\t");
	printf ("%d  || %c ",i,tableau[i][j].couleur);
 
		for(j=2;j<NB_ELEM_TAB;j=j+1){
		printf ("| %c ",tableau[i][j].couleur);
		}
		printf ("|");
		}
	 printf ("\n\t----------------------------------------------\n\t");
	 printf ("10 ||");
	 for (j=1;j<NB_ELEM_TAB;j=j+1) {
	 i=10;
	 printf (" %c |",tableau[i][j].couleur);
	 }
	 printf ("\n\t----------------------------------------------\n\t");
}
/*---------------- Fin de la fonction AfficheDamier ----------------------*/
 
/*------------------------------------------------------------------------*/
 
/*--------------- Prototype de la fonction SaisiePion ------------------------*/
void SaisiePion (int * ax, int * ay, int * nx, int * ny)
{
  int i=1;
  int j=1;
  int k=1;
  int l=1;
  int iook=0;
  char ci[1];
  char cj[1];
  char ck[1];
  char cl[1];
 
	do{
		do{
			printf("\n Entrer le numero de ligne du pion a deplacer :\n");
			gets(ci);
			iook=isdigit((int)(ci[0])); /* isdigit renvoi 0 si c'est un caractere */
			if (iook!=0)			
				i=atoi(ci);
			else {printf ("\n SAISIE INCORRECT \n");}
		}while(!iook);
 
		do{
			printf("\n Entrer le numero de colonne du pion a deplacer :\n");
			gets(cj);
			iook=isdigit((int)(cj[0])); /* isdigit renvoi 0 si c'est un caractere */
			if (iook!=0)			
				j=atoi(cj);
			else {printf ("\n SAISIE INCORRECT \n");}
		}while(!iook);
 
		do{
			printf("\n Entrer le numero de la nouvelle ligne :\n");
			gets(ck);
			iook=isdigit((int)(ck[0])); /* isdigit renvoi 0 si c'est un caractere */
			if (iook!=0)			
				k=atoi(ck);
			else {printf ("\n SAISIE INCORRECT \n");}
		}while(!iook);
 
		do{
			printf("\n Entrer le numero de la nouvelle colonne :\n");
			gets(cl);
			iook=isdigit((int)(cl[0])); /* isdigit renvoi 0 si c'est un caractere */
			if (iook!=0)			
				l=atoi(cl);
			else {printf ("\n SAISIE INCORRECT \n");}
		}while(!iook);
 
		if (i<=0 || i>10 || j<=0 || j>10 || k<=0 || k>10 || l<=0 || l>10)
			printf ("\nVOS COORDONNEES SORTENT DU DAMIER.\n");
 
	}while (i<=0 || i>10 || j<=0 || j>10 || k<=0 || k>10 || l<=0 || l>10);
 
   *ax=i;
   *ay=j;
   *nx=k;
   *ny=l;
 
	printf ("\nVous avez saisi [%d,%d] et [%d,%d] %d %d %d %d", i, j, k, l, *ax,*ay,*nx,*ny);
	getchar();
}
/*------------------Fin de la fonction Saisie -------------------------*/
 
/*---------------------------------------------------------------------*/
 
/*----------------Prototype de la fonction Depalcer -------------------*/
void Deplacer (struct stpion tableau[NB_ELEM_TAB][NB_ELEM_TAB],
				int ax, int ay, int nx, int ny)
{
  	tableau[nx][ny].couleur = tableau[ax][ay].couleur;
	tableau[nx][ny].type = tableau[ax][ay].type;
  	tableau[ax][ay].couleur = ' ';
  	tableau[ax][ay].type = 'r';
}
/*----------------Fin de la fonction Deplacer -------------------------*/
 
/*---------------------------------------------------------------------*/
 
 
 
/*---------------------------------------------------------------------*/
 
/*------------------ Prototype de la fonction SaisieNom ---------------*/
void SaisieNom (struct stjoueur * JoueurX)
{
char  nomX [25] ;
 
	printf ("\n Entrer le nom du premier joueur : ");
	fgets (nomX, 25, stdin);
	strncpy (JoueurX->nom, nomX, 25);
}
/*------------------ Fin de la fonction SaisieNom --------------------*/
 
/*-------------------------------------------------------------------*/
 
/*--------------- Prototype de la fonction SaisieCouleur ------------*/
void SaisieCouleur (struct stjoueur * JoueurX)
{
   int couleurX;
 
   printf ("\n\n\t 1 - Vous prennez les pions blancs.");
   printf ("\n\n\t 2 - Vous prennez les pions noirs.\n\n\t");
   scanf ("%d", &couleurX);
   getchar();
 
	if (couleurX == 1) {(JoueurX->couleur = 'b');}
	else 
	{
	if (couleurX == 2) {(JoueurX->couleur = 'n');}
	}
  }
/*---------------- fin de la fonction SaisieCouleur ------------------*/
 
int main(void)
{
struct stpion damier[NB_ELEM_TAB][NB_ELEM_TAB];
struct stjoueur JoueurA;
//struct stjoueur JoueurB;
 
int  xactuel;
//xactuel=(int *)malloc(1*sizeof(int));
int  yactuel;
//yactuel=(int*)malloc(1*sizeof(int));
int  xprochain;
//xprochain=(int*)malloc(1*sizeof(int));
int  yprochain;
//yprochain=(int*)malloc(1*sizeof(int));
//fflush(stdin);
 
  InitDamier ( damier );
  AfficheDamier ( damier );
 
  SaisiePion (&xactuel, &yactuel, &xprochain, &yprochain);
	printf ("\n[%d,%d] [%d,%d]\n", xactuel, yactuel, xprochain, yprochain);
 
  if ((damier[xprochain][yprochain]).caseblanche == 0)
	{SaisiePion (&xactuel, &yactuel, &xprochain, &yprochain);}
  else {Deplacer (damier, xactuel, yactuel, xprochain , yprochain);}
 
  AfficheDamier ( damier );
 
  //printf ("\n[%d,%d] [%d,%d]\n", xactuel, yactuel, xprochain, yprochain);
 
  //AfficheDamier (damier); 
  //SaisieNom (&JoueurA);
  //SaisieCouleur (&JoueurA);
  printf ("\n%s", JoueurA.nom);
  printf ("\n%c", JoueurA.couleur);
 
 getchar();
 return 0;
}