-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
/
memory_game.cpp
414 lines (359 loc) · 12.9 KB
/
memory_game.cpp
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/**
* @file
* @brief A simple [Memory Game](https://en.wikipedia.org/wiki/Matching_game)
* with **3 different sizes** and multiple letters.
* @details
* The game consists on finding **the pair** of all the given letters depending
* on the table size. Once all of the instances are all found, the game will end
* and will ask you if you'd like to play again or not.
*
* It provides **3 different sizes** available that the user can choose (4x2,
* 5x2, 7x2). 7x2 being the biggest table size and hardest mode. The bigger the
* size, **the more letters are available**.
*
* @author [David Leal](https://github.com/Panquesito7)
*/
#include <algorithm> /// for std::shuffle()
#include <cstdlib> /// for std::srand()
#include <ctime> /// for std::time()
#include <iostream> /// for IO operations
#include <random> /// for std::mt19937
#include <vector> /// for std::vector
// `Sleep` is only available in Windows in milliseconds.
// However, on Unix/Linux systems it is `sleep`, in seconds.
#ifdef _WIN32
#include <Windows.h> /// for Sleep()
template <typename T>
constexpr typename std::enable_if<std::is_integral<T>::value, void>::type SLEEP(
T milliseconds) {
Sleep(milliseconds * 1000);
}
#else
#include <unistd.h> /// for sleep()
template <typename T>
constexpr T SLEEP(T seconds) {
return sleep(seconds);
}
#endif
/**
* @namespace
* @brief (Mini)game implementations.
*/
namespace games {
/**
* @namespace
* @brief Functions for the [Memory
* Game](https://en.wikipedia.org/wiki/Matching_game) implementation
*/
namespace memory_game {
/**
* @brief Utility function to verify if the given input is a number or not.
* This is very useful to prevent the program being stuck in a loop.
* @tparam T The type of the input
* @param input The input to check.
* @returns false if the input IS empty or if it contains a non-digit character
* @returns true if the input is NOT empty and if it contains only digit
* characters
*/
template <typename T>
bool is_number(const T &input) {
if (std::cin.fail()) {
std::cin.clear();
std::cin.ignore(256, '\n');
return false;
}
return true;
}
/**
* @brief Initializes the table with the letters.
* @tparam T The type of the table.
* @param table The table to initialize.
* @returns void
*/
template <typename T>
void init(std::vector<T> *table) {
std::vector<char> letters(7);
// Decrease / increase the number of letters depending on the size.
if ((*table).size() == 10) { // 5x2
letters = {'A', 'E', 'Z', 'P', 'D'};
} else if ((*table).size() == 8) { // 4x2
letters = {'A', 'E', 'Z', 'D'};
} else if ((*table).size() == 14) { // 7x2
letters = {'A', 'E', 'Z', 'P', 'D', 'B', 'M'};
}
std::vector<char> pairs;
for (char letter : letters) {
pairs.push_back(letter);
pairs.push_back(letter);
}
std::shuffle(pairs.begin(), pairs.end(),
std::mt19937(std::random_device()()));
for (int i = 0; i < (*table).size(); i++) {
(*table)[i] = pairs[i];
}
std::cout << "All available types are: ";
for (int i = 0; i < letters.size(); i++) {
if (i == letters.size() - 1) {
std::cout << "and " << letters[i] << ".\n\n";
} else {
std::cout << letters[i] << ", ";
}
}
}
/**
* @brief Utility function to print the table.
* @tparam T The type of the table.
* @param table The table to print.
* @returns void
*/
template <typename T>
void print_table(const std::vector<T> &table) {
std::cout << "| ";
std::vector<T> table_print(table.size());
for (int i = 0; i < table.size(); i++) {
table_print[i] = ' ';
if (table[i] != 0) {
table_print[i] = table[i];
}
}
for (int i = 0; i < table.size(); i++) {
if (i % 5 == 0 && i != 0) {
std::cout << "\n| ";
}
std::cout << table_print[i] << " | ";
}
}
// Prototype function. This is needed as `ask_data` calls `reset_data`, and
// `reset_data` calls `ask_data`.
template <typename T>
void reset_data(const std::vector<T> &, int *, int *, int *);
/**
* @brief Function that asks the user for their
* input in the table they previously chose.
* @tparam T The type of the table.
* @param table The table that's used to get the user's input and data.
* @param answer The user's answer.
* @param old_answer The user's previous answer.
* @param memory_count A counter to check if the user has already answered two
* values.
* @returns void
*/
template <typename T>
void ask_data(const std::vector<T> &table, int *answer, int *old_answer,
int *memory_count) {
(*old_answer) = (*answer);
print_table(table);
std::cout << "\n\nType your response here (number index):\n";
std::cin >> (*answer);
if (!is_number((*answer))) {
std::cout << "\nYou must enter a valid number.\n\n";
reset_data(table, answer, old_answer, memory_count);
}
// Increase the memory count, which will be later on used for checking if
// the user has already answered two values.
(*memory_count)++;
if (((*answer) > table.size()) || ((*answer) < 1)) {
std::cout << "\nYou can't check a value that doesn't exist (or an "
"invalid number).\n\n";
reset_data(table, answer, old_answer, memory_count);
}
if ((*old_answer) == (*answer)) {
std::cout << "\nYou can't check the same value twice.\n\n";
reset_data(table, answer, old_answer, memory_count);
}
// If two matches are answered already, but the user checkes a non-answered
// and an answered value, the program will mark it as no match, however, we
// must not allow the user to check the same value twice.
if ((table[(*answer) - 1] != 0) &&
((table[(*old_answer)] == 0) || (table[(*old_answer)] != 0))) {
std::cout << "\nYou can't check the same value twice.\n\n";
reset_data(table, answer, old_answer, memory_count);
}
}
/**
* @brief Utility function that resets the data if the user enters an invalid
* value.
* @tparam T The type of the table.
* @param table The table that will be used to call `ask_data()`.
* @param answer The user's answer.
* @param old_answer The user's previous answer.
* @param memory_count A counter to check if the user has already answered two
* values.
* @returns void
*/
template <typename T>
void reset_data(const std::vector<T> &table, int *answer, int *old_answer,
int *memory_count) {
(*answer) = (*old_answer);
(*memory_count)--;
ask_data(table, answer, old_answer, memory_count);
}
/**
* @brief Checks if the two values given by the user match.
* @tparam T The type of the table.
* @param table_empty The table with no values, slowly assigned from `table`
* depending on the user's input.
* @param table The table with the original values.
* @param answer The user's answer.
* @param first_time A boolean to check if the user has already answered a
* value.
* @param old_answer The user's previous answer.
* @param memory_count A counter to check if the user has already answered two
* values.
* @returns true IF the values given by the user match
* @returns false if the values given by the user do NOT match
*/
template <typename T>
bool match(const std::vector<T> &table, std::vector<T> *table_empty,
const int &answer, bool *first_time, int *old_answer,
int *memory_count) {
if ((*first_time) == true) {
return true;
}
// Search across the whole table and if the two values match, keep results,
// otherwise, hide 'em up.
for (int i = 0; i < table.size() + 1; i++) {
if (i == answer) {
if (table[i - 1] == table[(*old_answer) - 1]) {
(*first_time) = true;
(*memory_count) = 0;
(*old_answer) = 0;
return true;
} else {
std::cout << "\nNo match (value was " << table[i - 1]
<< ", index is " << i << ").\n\n";
(*table_empty)[(*old_answer) - 1] = 0;
(*table_empty)[answer - 1] = 0;
(*first_time) = true;
(*memory_count) = 0;
(*old_answer) = 0;
return false;
}
}
}
return false;
}
/**
* @brief Function to assign the results to the table.
*
* Also checkes if the user has answered all the values already, as well as
* verify if the user made a match or not.
* @tparam T The type of the tables.
* @param table_empty The table with no values, slowly assigned from `table`
* depending on the user's input.
* @param table The table with the original values.
* @param answer The user's answer.
* @param first_time A boolean to check if the user has already answered a
* value.
* @param old_answer The user's previous answer.
* @param memory_count A counter to check if the user has already answered two
* values.
* @returns void
*/
template <typename T>
void assign_results(std::vector<T> *table_empty, std::vector<T> *table,
int *answer, bool *first_time, int *old_answer,
int *memory_count) {
// Search through the entire table and if the answer matches the index, show
// the value. If it doesn't match, hide both the values. Don't forget to
// keep older values already answered.
for (int i = 0; i < (*table).size() + 1; i++) {
if (i == (*answer)) {
if (match((*table), table_empty, (*answer), first_time, old_answer,
memory_count) == true) {
(*table_empty)[i - 1] = (*table)[i - 1];
(*first_time) = true;
}
}
}
if ((*memory_count) == 1) {
(*first_time) = false;
(*memory_count) = 0;
}
char try_again = 'n';
// Has the user finished the game? Use a `for` loop, and if the table is
// full, ask the user if he wants to play again.
for (int i = 0; i < (*table).size() + 1; i++) {
if ((*table_empty)[i] == 0) {
break;
} else if (i == (*table).size() - 1) {
print_table((*table));
std::cout << "\n\nYou won. Congratulations! Do you want to play "
"again? (y/n)\n";
std::cout
<< "Size " << (*table).size()
<< " will be used. This can be changed by re-running the game.";
std::cin >> try_again;
if (try_again == 'y') {
// This is needed when checking if the user has two matches
// already.
for (int i = 0; i < (*table_empty).size(); i++) {
(*table_empty)[i] = 0;
}
init(table);
} else if (try_again == 'n') {
std::cout << "\nThanks for playing the game!\n";
SLEEP(3);
exit(0);
} else {
std::cout << "\nInvalid input (exitting...).\n";
SLEEP(3);
exit(0);
}
}
}
// Ask data again.
ask_data((*table_empty), answer, old_answer, memory_count);
assign_results(table_empty, table, answer, first_time, old_answer,
memory_count);
}
} // namespace memory_game
} // namespace games
/**
* @brief Main function
* @returns 0 on exit
*/
int main() {
// Start randomizer. This changes the values every time.
std::srand(std::time(nullptr));
int size = 0; ///< Size of the table.
int selection = 0; ///< Selection of the size (4x2, 5x2, 7x2).
int response = 0; ///< The answer (number index) that the user chose.
int old_answer = 0; ///< Previous answer (number index).
int memory_count =
0; ///< Counter to check if the user has already answered two values.
bool first_time = true; ///< Whether the user has answered 1 value or not
///< (previous answered values do not count).
std::cout << "\tMEMORY GAME\n";
do {
std::cout << "\n1. 4x2 (1)";
std::cout << "\n2. 5x2 (2)";
std::cout << "\n3. 7x2 (3)\n";
std::cout << "\nChoose table size: ";
std::cin >> selection;
} while ((selection < 1 || selection > 3) &&
(!games::memory_game::is_number(selection)));
switch (selection) {
case 1:
size = 8;
break;
case 2:
size = 10;
break;
case 3:
size = 14;
break;
default:
size = 10;
break;
}
std::vector<char> table(size);
std::vector<char> table_empty(size);
std::cout << "\n";
games::memory_game::init(&table);
games::memory_game::ask_data(table_empty, &response, &old_answer,
&memory_count);
games::memory_game::assign_results(&table_empty, &table, &response,
&first_time, &old_answer, &memory_count);
return 0;
}