Bonjour, j'en suis rendus à créer une simulation du jeu du pendu en C. Lorsque j'implemente dans une seul et même fichier "main.c" le jeu marche parfaitement, mais lorsque j'essaye de le faire en plusieur fichier avec de la compilation modulaire ca pinaille mais je ne comprends pas pourquoi..
Voici le code qui marche :
Et voici celui qui ne marche pas
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 #include <stdio.h> #include <stdlib.h> #include <time.h> #include <ctype.h> #include "c_string.h" #include "Read.h" void Check(char *Word, char *Crypto, char Character, int Size_String) { int j; for (j=0; j<Size_String; j++) { if (Word[j]==Character) { Crypto[j]=Word[j]; } } } int main(int argc, char *argv[]) { char Char = 0; char *Word = NULL; char Character = 0; char *Crypt = NULL; char *Crypto = NULL; char String[100] = {0}; int k; int i; int MAX = 0; int Rand = 0; int Size = 0; int Size2 = 0; int Round = 10; const int MIN = 0; int Nombre_Mot = 0; int Size_String = 0; FILE *Dico = NULL; Dico = fopen("Dico.txt", "r"); if(Dico != NULL) { while(Character != EOF) { Character = fgetc(Dico); if(Character == '\n') { Nombre_Mot ++; } } MAX = Nombre_Mot; srand(time(NULL)); Rand = (rand() % (MAX - MIN + 1)) + MIN; k = Rand - 1; if(k < 0) { k = 0; } printf("Nombre total de mot : %d.\n", MAX); printf("Nombre aleatoire dans Dico.txt : %d.\n", Rand); rewind(Dico); while(k > 0) { Character = fgetc(Dico); if (Character == '\n') k --; } fgets(String, 100, Dico); String[c_strlen(String) - 1] = '\0'; Size_String = c_strlen(String); Crypto = malloc(Size_String * sizeof(char)); printf("Mot aléatoire : %s\n", String); Size = c_strlen(String); printf("TAILLE MOT ALEATOIRE : %d.\n", Size); fclose(Dico); } else { printf("Impossible d'ouvrir le fichier : %s.\n", __FILE__); } Word = String; printf("Nouvelle chaine : %s.\n", Word); for(i=0; i<Size_String; i++) { Crypto[i] = '*'; } Crypt = Crypto; Size2 = c_strlen(Crypt); printf("Cryptographie : %s.\n\n", Crypt); printf("Taille de Crypto : %d.\n", Size2); printf("\n\n\n"); printf("\t\t\t\tJeu du Pendu.\n\n\n\n"); printf("\t\tBienvenue sur le jeu du pendu.\n"); while( (Round > 0) && (c_strcmp(Word, Crypto) !=0)) { printf("Il vous reste %d coups a jouer.\n", Round); printf("Touvez le mot mot cache : %s.\n", Crypto); printf("Entrez une lettre : "); Char = Read_Character(); Round --; Check(Word, Crypto, Char, Size_String); printf("\n\n"); }; if(c_strcmp(Word, Crypto) == 0) { printf("Bravo, vous avez trouve le mot secret : %s.\n", Crypto); } if(Round == 0) { printf("Perdu, vous avez ete pendu.\n"); } printf("\n\n\n"); free(Crypto); return 0; }
main.c
Pendu.c
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 #include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <time.h> #include "Read.h" #include "Dico.h" #include "Pendu.h" #include "c_string.h" #define MAX_SIZE 20 int main(int argc, char *argv []) { char Character = 0; char *Word = NULL; char *Crypto = NULL; int Round = 10; int Size_String = 0; Word = Dico(); Size_String = c_strlen(Word); Crypto = Crypt(Size_String); printf("\t\t\t\tJeu du Pendu.\n"); printf("\t\tBienvenue sur le jeu du pendu.\n"); while( (Round > 0) && (c_strcmp(Word, Crypto) !=0)) { printf("Il vous reste %d coups a jouer.\n", Round); printf("Touvez le mot mot cache : %s.\n", Crypto); printf("Entrez une lettre : "); Character = Read_Character(); Round --; Check(Word, Crypto, Character, Size_String); }; if(c_strcmp(Word, Crypto) == 0) { printf("Bravo, vous avez trouve le mot secret : %s.\n", Crypto); } if(Round == 0) { printf("Perdu, vous avez ete pendu.\n"); } free(Crypto); return 0; }
Dico.c
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 char *Crypt(int Size_String) { int i; char *Crypto = NULL; Crypto = malloc(Size_String * sizeof(char)); for(i=0; i<Size_String; i++) { Crypto[i] = '*'; } return Crypto; } void Check(char *Word, char *Crypto, char Character, int Size_String) { int i; for (i = 0; i < Size_String; i ++) { if (Word[i]==Character) { Crypto[i]=Word[i]; } } }
Merci d'avance.
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 char *Dico() { char Character = 0; char *Crypto = NULL; char String[100] = {0}; int k; int MAX = 0; int Rand = 0; int Size = 0; const int MIN = 0; int Nombre_Mot = 0; int Size_String = 0; FILE *Dico = NULL; Dico = fopen("Dico.txt", "r"); if(Dico != NULL) { while(Character != EOF) { Character = fgetc(Dico); if(Character == '\n') { Nombre_Mot ++; } } MAX = Nombre_Mot; srand(time(NULL)); Rand = (rand() % (MAX - MIN + 1)) + MIN; k = Rand - 1; if(k < 0) { k = 0; } printf("Nombre total de mot : %d.\n", MAX); printf("Nombre aleatoire dans Dico.txt : %d.\n", Rand); rewind(Dico); while(k > 0) { Character = fgetc(Dico); if (Character == '\n') k --; } fgets(String, 100, Dico); String[c_strlen(String) - 1] = '\0'; Size_String = c_strlen(String); Crypto = malloc(Size_String * sizeof(char)); printf("Mot aléatoire : %s\n", String); Size = c_strlen(String); printf("TAILLE MOT ALEATOIRE : %d.\n", Size); fclose(Dico); } else { printf("Impossible d'ouvrir le fichier : %s.\n", __FILE__); } return String; }
Partager