-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
289 lines (265 loc) · 8.73 KB
/
main.cpp
File metadata and controls
289 lines (265 loc) · 8.73 KB
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
// Fichier d'include
#include <iostream>
#include <iomanip>
#include <limits>
#include <array>
#include <map>
#include <fstream>
#include <vector>
#include <string>
#include <random>
#include <algorithm>
#include <cctype>
// Déclaration des prototypes
static void DisplayMainMenu();
static char InputLetter(const std::string& msg);
static bool LetterIsPresent(const char letter, const std::string& word);
static std::vector<std::string> LoadWordlist(const std::string& path);
static std::map<std::string, std::string> PeekWordFromDictionnary(const std::vector<std::string>& wordlist);
static void DiscoverLetterFromWordMap(std::map<std::string, std::string>&wordmap, const char letter);
static int ConsoleInputInteger(const std::string& msg, const int minInterval, const int maxInterval);
static void DrawHangingMan(const std::string& word, const int state);
// Constantes
static constexpr std::string FILEPATH {"frwordlist"};
int main()
{
bool bGameRun = true;
while(bGameRun)
{
DisplayMainMenu();
auto choice = ConsoleInputInteger("Select >", 1, 2);
if(choice == 2)
{
std::cout << "...Bey..." << std::endl;
bGameRun = false;
}
else
{
auto wordlist = LoadWordlist(FILEPATH);
auto wordMap = PeekWordFromDictionnary(wordlist);
bool gameOver = false;
int hangingManState = 0;
while(!gameOver)
{
char letter = InputLetter("Letter>");
if(LetterIsPresent(letter, wordMap["word"]))
{
DiscoverLetterFromWordMap(wordMap, letter);
}
else
{
hangingManState++;
}
DrawHangingMan(wordMap["hidden"], hangingManState);
if(hangingManState == 7 || std::count(std::begin(wordMap["hidden"]), std::end(wordMap["hidden"]), '-') == 0)
gameOver = true;
}
}
}
return 0;
}
/**
* Affiche le menu principale
*/
static void DisplayMainMenu()
{
using std::cout;
using std::endl;
using std::setw;
cout << setw(50) << "==========================" << endl;
cout << setw(50) << "= HANGING MAN =" << endl;
cout << setw(50) << "==========================" << endl;
cout << " ___________.._______" << endl \
<< "| .__________))______|" << endl \
<< "| | / / ||" << endl \
<< "| |/ / ||" << endl \
<< "| | / ||.-\'\'." << endl \
<< "| |/ |/ _ \\" << endl \
<< "| | || `/,|" << endl \
<< "| | (\\`_.' " << endl \
<< "| | .-`--'." << endl \
<< "| | /Y . . Y\\\\" << endl \
<< "| | // | | \\\\" << endl \
<< "| | // | . | \\\\" << endl \
<< "| | ') | | (`" << endl \
<< "| | || ||" << endl \
<< "| | || ||" << endl \
<< "| | || ||" << endl \
<< "| | || ||" << endl \
<< "| | / | | \\" << endl \
<< "__________| `- `- |_____|" << endl \
<< "| | \\ \\ | |" << endl \
<< "| | \\ \\ | |" << endl \
<< ": : \\ \\ : : " << endl; \
cout << endl;
cout << "(1) Start a new game" << endl;
cout << "(2) Leave application" << endl;
}
/**
* Demande a l'utilisateur une lettre au clavier
*/
static char InputLetter(const std::string& msg)
{
std::string buffer;
bool isChar = false;
do
{
isChar = true;
std::cout << msg;
std::cin >> buffer;
if(!std::isalpha(buffer[0]))
{
std::cout << "Please enter a letter !" << std::endl;
isChar = false;
}
}while(!isChar);
return std::tolower(buffer[0]);
}
/**
* Vérifie si la lettre est présente dans le mot
*/
static bool LetterIsPresent(const char letter, const std::string& word)
{
for(char c : word)
{
if(c == letter)
return true;
}
return false;
}
/**
* Lit le fichier wordlist passez en paramètre
*/
static std::vector<std::string> LoadWordlist(const std::string& path)
{
std::fstream file{path, std::ios::in};
std::vector<std::string> words;
if(file.is_open())
{
while(!file.eof())
{
std::string word;
std::getline(file, word);
words.push_back(word);
}
}
return words;
}
/**
* Récupère un mot aléatoirement depuis la wordlist
*/
static std::map<std::string, std::string> PeekWordFromDictionnary(const std::vector<std::string>& wordlist)
{
// Génère un nombre aléatoire entre 0 et le nombre d'éléments du wordlist
std::random_device rd;
auto wordIndex = std::uniform_int_distribution<int>(0, wordlist.size())(rd);
auto word = wordlist[wordIndex];
std::for_each(std::begin(word), std::end(word), [](char& c) -> void { c = std::tolower(c);}); // mettre le mot en miniscule
// Créer un map avec le mot cacher et le mot en clair
std::map<std::string, std::string> wordMap;
wordMap["word"] = word;
wordMap["hidden"] = std::string(word.size(), '-');
return wordMap;
}
static int ConsoleInputInteger(const std::string& msg, const int minInterval, const int maxInterval)
{
int data = 0;
bool bInputOk = false;
while(!bInputOk)
{
bInputOk = true;
std::cout << msg;
std::cin >> data;
if(!std::cin.good() || data < minInterval || data > maxInterval)
{
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "You need to input a number between " << minInterval << " and " << maxInterval << std::endl;
bInputOk = false;
}
}
return data;
}
// Affiche a l'écran l'état du pendu
static void DrawHangingMan(const std::string& word, const int state)
{
using std::cout;
using std::endl;
cout << "The hidden word: " << word << std::endl;
switch(state)
{
case 1:
cout << "+---+" << endl \
<< "| |" << endl \
<< " |" << endl \
<< " |" << endl \
<< " |" << endl \
<< " |" << endl \
<< "=========" << endl;
break;
case 2:
cout << "+---+" << endl \
<< "| |" << endl \
<< "o |" << endl \
<< " |" << endl \
<< " |" << endl \
<< " |" << endl \
<< "=========" << endl;
break;
case 3:
cout << "+---+" << endl \
<< "| |" << endl \
<< "o |" << endl \
<< "| |" << endl \
<< " |" << endl \
<< " |" << endl \
<< "=========" << endl;
break;
case 4:
cout << " +---+" << endl \
<< " | |" << endl \
<< " o |" << endl \
<< "/| |" << endl \
<< " |" << endl \
<< " |" << endl \
<< " =========" << endl;
break;
case 5:
cout << " +---+" << endl \
<< " | |" << endl \
<< " o |" << endl \
<< "/|\\ |" << endl \
<< " |" << endl \
<< " |" << endl \
<< " =========" << endl;
break;
case 6:
cout << " +---+" << endl \
<< " | |" << endl \
<< " o |" << endl \
<< "/|\\ |" << endl \
<< "/ |" << endl \
<< " |" << endl \
<< " =========" << endl;
break;
case 7:
cout << " +---+" << endl \
<< " | |" << endl \
<< " o |" << endl \
<< "/|\\ |" << endl \
<< "/ \\ |" << endl \
<< " |" << endl \
<< " =========" << endl;
break;
}
}
static void DiscoverLetterFromWordMap(std::map<std::string, std::string>&wordmap, const char letter)
{
for(auto i = 0; i < wordmap["word"].size(); i++)
{
if(wordmap["word"][i] == letter)
{
wordmap["hidden"][i] = letter;
}
}
}