Allo, ben ouais je crois progresser dans la correction de mon codes ^_^... voici ce que ça donne jusqu'à maintenant :
Code:
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 //main.cpp #include <iostream> #include <string> #include <windows.h> #include "pendu.h" //code trouvé à http://cpp.enisoc.com/articles/clrscr/ void clear() { HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE); COORD coord = {0, 0}; DWORD count; CONSOLE_SCREEN_BUFFER_INFO csbi; GetConsoleScreenBufferInfo(hStdOut, &csbi); FillConsoleOutputCharacter(hStdOut, ' ', csbi.dwSize.X * csbi.dwSize.Y, coord, &count); SetConsoleCursorPosition(hStdOut, coord); } unsigned int count(const std::string &chaine, const char &caractere) { unsigned int combien = 0; for (unsigned int i = 0; i < chaine.length(); i++) { if (caractere == chaine[i]) combien++; } return combien; } unsigned short int main() { std::string rejouer; do { bool temoin = true; Mot alphabet("ABCDEFGHIJKLMNOPQRSTUVWXYZ"); std::string mot; do { std::cout << "Inscrivez un mot pour faire une partie de pendu : "; std::cin >> mot; for (unsigned int i = 0; i < mot.length(); i++) { if ((temoin && !alphabet.Contient(toupper(mot[i]))) || (mot.length() < 2) || (count(mot, mot[0]) == mot.length())) temoin = false; } } while (!temoin); Pendu pendu(mot); std::string essai; while(pendu.PartieTermine() == ETAT_PENDU::ENCOURS) { clear(); pendu.Affichage(AFFICHAGE::PENDU); pendu.Affichage(AFFICHAGE::MOTTIRETS); pendu.Affichage(AFFICHAGE::ALPHABET); std::cout << "Faire un essai : "; std::cin >> essai; pendu.UnEssai(essai[0]); } clear(); pendu.Affichage(AFFICHAGE::PENDU); if (pendu.PartieTermine() == ETAT_PENDU::GAGNEE) { std::cout << "Bravo! Le mot était bel et bien "; } else { std::cout << "Désolé! Le mot était "; } std::cout << pendu.Get_motADeviner().Get_mot() << "." << std::endl; do { std::cout << "Souhaîtez vous rejouer une partie? (O/N) "; std::cin >> rejouer; rejouer = std::string(1, rejouer[0]); transform(rejouer.begin(), rejouer.end(), rejouer.begin(), toupper); } while ((rejouer != "O") && (rejouer != "N")); clear(); } while (rejouer == "O"); return 0; };
Code:
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 //mot.h #include <string> #include <algorithm> class Mot { private: std::string m_mot; std::string UCase(std::string chaine) { std::transform(chaine.begin(), chaine.end(), chaine.begin(), toupper); return chaine; } public: const std::string Get_mot() { return m_mot; } const unsigned int Get_len() { return m_mot.length(); } void Set_mot(std::string mot) { m_mot = UCase(mot); } Mot() {} Mot(const std::string &mot) { Set_mot(mot); } char Lettre(const unsigned int &position) { if (position < m_mot.length()) return m_mot[position]; else return ' '; } void ChangerLettre(const unsigned int &position, const char &nouvLettre) { if (position < m_mot.length()) m_mot.replace(position, 1, UCase(std::string(1, nouvLettre))); } bool Contient(const char &caractere) { return (m_mot.find(caractere) != m_mot.npos); } unsigned int Cherche(char &occurence) { if (Contient(occurence)) { return m_mot.find(occurence); } else { return 0; } } bool Comparer(Mot &mot) { return (m_mot == mot.Get_mot()); } };
Bref, j'ai remarquer un bug, si lorsque le programme demande à l'utilisateur d'inscrire un mot et qu'il écrit des chiffres mêlés à des lettres. La boucle lui redemande d'inscrire un mot (juske là c'est OK). Mais suite à cela si il entre un mot valide (c-a-d constitué de lettres). La boucle continu. Ah... j'oubliais... on parle de cette boucle :Code:
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 //pendu.h #include <string> #include <algorithm> #include <iostream> #include "mot.h" namespace AFFICHAGE { const enum AFFICHER { PENDU, MOTTIRETS, ALPHABET }; } namespace ETAT_PENDU { const enum STATUS_PARTIE { ENCOURS, GAGNEE, PERDUE }; } class Pendu { private: Mot m_motADeviner; Mot m_motTirets; Mot m_alphabet; unsigned short int m_nbEssaisMax; unsigned short int m_noEssai; public: Mot Get_motADeviner() { return m_motADeviner; } Mot Get_motTirets() { return m_motTirets; } Mot Get_alphabet() { return m_alphabet; } unsigned int Get_nbEssaisMax() { return m_nbEssaisMax; } unsigned int Get_noEssai() { return m_noEssai; } void Set_mot(const std::string &mot) { m_motADeviner.Set_mot(mot); } Pendu () {} Pendu(const std::string &mot) : m_nbEssaisMax(5), m_noEssai(0), m_motADeviner(mot), m_motTirets(std::string(mot.length(), '_')), m_alphabet("ABCDEFGHIJKLMNOPQRSTUVWXYZ") { UnEssai(m_motADeviner.Lettre(0)); } void UnEssai(char lettre) { lettre = toupper(lettre); if ((lettre != ' ') && (m_alphabet.Contient(lettre))) { if (m_motADeviner.Contient(lettre)) { for (unsigned int i = 0; i < m_motADeviner.Get_len(); i++) { if (m_motADeviner.Lettre(i) == lettre) { m_motTirets.ChangerLettre(i, lettre); } } } else { m_noEssai++; } m_alphabet.ChangerLettre(m_alphabet.Cherche(lettre), char(' ')); } } ETAT_PENDU::STATUS_PARTIE PartieTermine() { if (m_motTirets.Comparer(m_motADeviner)) { return ETAT_PENDU::GAGNEE; } else if(m_noEssai == m_nbEssaisMax) { return ETAT_PENDU::PERDUE; } else { return ETAT_PENDU::ENCOURS; } } void Affichage(const AFFICHAGE::AFFICHER &quoi) { switch(quoi) { case AFFICHAGE::PENDU: switch(m_noEssai) { case 0: std::cout << "_______Pendu_______" << std::endl; std::cout << "| |" << std::endl; std::cout << "| |" << std::endl; std::cout << "| |_____ |" << std::endl; std::cout << "| |/ | |" << std::endl; std::cout << "| | | |" << std::endl; std::cout << "| | |" << std::endl; std::cout << "| | |" << std::endl; std::cout << "| | |" << std::endl; std::cout << "| | |" << std::endl; std::cout << "| |________ |" << std::endl; std::cout << "| | | |" << std::endl; std::cout << "| | | |" << std::endl; std::cout << "___________________" << std::endl; break; case 1: std::cout << "_______Pendu_______" << std::endl; std::cout << "| |" << std::endl; std::cout << "| |" << std::endl; std::cout << "| |_____ |" << std::endl; std::cout << "| |/ | |" << std::endl; std::cout << "| | | |" << std::endl; std::cout << "| | ( ) |" << std::endl; std::cout << "| | |" << std::endl; std::cout << "| | |" << std::endl; std::cout << "| | |" << std::endl; std::cout << "| |________ |" << std::endl; std::cout << "| | | |" << std::endl; std::cout << "| | | |" << std::endl; std::cout << "___________________" << std::endl; break; case 2: std::cout << "_______Pendu_______" << std::endl; std::cout << "| |" << std::endl; std::cout << "| |" << std::endl; std::cout << "| |_____ |" << std::endl; std::cout << "| |/ | |" << std::endl; std::cout << "| | | |" << std::endl; std::cout << "| | ( ) |" << std::endl; std::cout << "| | | |" << std::endl; std::cout << "| | | |" << std::endl; std::cout << "| | | |" << std::endl; std::cout << "| |________ |" << std::endl; std::cout << "| | | |" << std::endl; std::cout << "| | | |" << std::endl; std::cout << "___________________" << std::endl; break; case 3: std::cout << "_______Pendu_______" << std::endl; std::cout << "| |" << std::endl; std::cout << "| |" << std::endl; std::cout << "| |_____ |" << std::endl; std::cout << "| |/ | |" << std::endl; std::cout << "| | | |" << std::endl; std::cout << "| | ( ) |" << std::endl; std::cout << "| | _|_ |" << std::endl; std::cout << "| | / | \\ |" << std::endl; std::cout << "| | | |" << std::endl; std::cout << "| |________ |" << std::endl; std::cout << "| | | |" << std::endl; std::cout << "| | | |" << std::endl; std::cout << "___________________" << std::endl; break; case 4: std::cout << "_______Pendu_______" << std::endl; std::cout << "| |" << std::endl; std::cout << "| |" << std::endl; std::cout << "| |_____ |" << std::endl; std::cout << "| |/ | |" << std::endl; std::cout << "| | | |" << std::endl; std::cout << "| | ( ) |" << std::endl; std::cout << "| | _|_ |" << std::endl; std::cout << "| | / | \\ |" << std::endl; std::cout << "| | | |" << std::endl; std::cout << "| |___/_\\__ |" << std::endl; std::cout << "| | | |" << std::endl; std::cout << "| | | |" << std::endl; std::cout << "___________________" << std::endl; break; case 5: std::cout << "_______Pendu_______" << std::endl; std::cout << "| |" << std::endl; std::cout << "| |" << std::endl; std::cout << "| |_____ |" << std::endl; std::cout << "| |/ | |" << std::endl; std::cout << "| | | |" << std::endl; std::cout << "| | | |" << std::endl; std::cout << "| | (x) |" << std::endl; std::cout << "| | /_|_\\ |" << std::endl; std::cout << "| | | |" << std::endl; std::cout << "| |___ | __ |" << std::endl; std::cout << "| | \\/ \\ | |" << std::endl; std::cout << "| | \| | |" << std::endl; std::cout << "___________________" << std::endl; } break; case AFFICHAGE::MOTTIRETS: std::cout << "[Lettres trouvées]" << std::endl; std::cout << " -"; for (unsigned short int i = 0; i < m_motTirets.Get_len(); i++) { std::cout << "--"; } std::cout << " " << std::endl; std::cout << "| "; for (unsigned short int i = 0; i < m_motTirets.Get_len(); i++) { std::cout << m_motTirets.Lettre(i) <<" "; } std::cout << "|" << std::endl; std::cout << " -"; for (unsigned short int i = 0; i < m_motTirets.Get_len(); i++) { std::cout << "--"; } std::cout << " " << std::endl; break; case AFFICHAGE::ALPHABET: std::cout << "[Lettres disponibles]" << std::endl; std::cout << " -------------------------- " << std::endl; std::cout << "|" << m_alphabet.Get_mot() << "|" << std::endl; std::cout << " -------------------------- " << std::endl; } } };
Pour faire encors plus court je crois que le bug se produit dans ces lignes :Code:
1
2
3
4
5
6
7
8 do { std::cout << "Inscrivez un mot pour faire une partie de pendu : "; std::cin >> mot; for (unsigned int i = 0; i < mot.length(); i++) { if ((temoin && !alphabet.Contient(toupper(mot[i]))) || (mot.length() < 2) || (count(mot, mot[0]) == mot.length())) temoin = false; } } while (!temoin);
Donc, je patauge, p-e demain je vais y voir plus clair sur ce nenuit :)Code:
1
2
3
4 for (unsigned int i = 0; i < mot.length(); i++) { if ((temoin && !alphabet.Contient(toupper(mot[i]))) || (mot.length() < 2) || (count(mot, mot[0]) == mot.length())) temoin = false; }