29constexpr typename std::enable_if<std::is_integral<T>::value,
void>::type
SLEEP(
31 Sleep(milliseconds * 1000);
37 return sleep(seconds);
51namespace memory_game {
63 if (std::cin.fail()) {
65 std::cin.ignore(256,
'\n');
80void init(std::vector<T> *table) {
81 std::vector<char> letters(7);
84 if ((*table).size() == 10) {
85 letters = {
'A',
'E',
'Z',
'P',
'D'};
86 }
else if ((*table).size() == 8) {
87 letters = {
'A',
'E',
'Z',
'D'};
88 }
else if ((*table).size() == 14) {
89 letters = {
'A',
'E',
'Z',
'P',
'D',
'B',
'M'};
92 std::vector<char> pairs;
93 for (
char letter : letters) {
94 pairs.push_back(letter);
95 pairs.push_back(letter);
98 std::shuffle(pairs.begin(), pairs.end(),
99 std::mt19937(std::random_device()()));
101 for (
int i = 0; i < (*table).size(); i++) {
102 (*table)[i] = pairs[i];
105 std::cout <<
"All available types are: ";
107 for (
int i = 0; i < letters.size(); i++) {
108 if (i == letters.size() - 1) {
109 std::cout <<
"and " << letters[i] <<
".\n\n";
111 std::cout << letters[i] <<
", ";
125 std::vector<T> table_print(table.size());
127 for (
int i = 0; i < table.size(); i++) {
128 table_print[i] =
' ';
131 table_print[i] = table[i];
135 for (
int i = 0; i < table.size(); i++) {
136 if (i % 5 == 0 && i != 0) {
140 std::cout << table_print[i] <<
" | ";
147void reset_data(
const std::vector<T> &,
int *,
int *,
int *);
161void ask_data(
const std::vector<T> &table,
int *answer,
int *old_answer,
163 (*old_answer) = (*answer);
166 std::cout <<
"\n\nType your response here (number index):\n";
167 std::cin >> (*answer);
170 std::cout <<
"\nYou must enter a valid number.\n\n";
171 reset_data(table, answer, old_answer, memory_count);
178 if (((*answer) > table.size()) || ((*answer) < 1)) {
179 std::cout <<
"\nYou can't check a value that doesn't exist (or an "
180 "invalid number).\n\n";
181 reset_data(table, answer, old_answer, memory_count);
184 if ((*old_answer) == (*answer)) {
185 std::cout <<
"\nYou can't check the same value twice.\n\n";
186 reset_data(table, answer, old_answer, memory_count);
192 if ((table[(*answer) - 1] != 0) &&
193 ((table[(*old_answer)] == 0) || (table[(*old_answer)] != 0))) {
194 std::cout <<
"\nYou can't check the same value twice.\n\n";
195 reset_data(table, answer, old_answer, memory_count);
211void reset_data(
const std::vector<T> &table,
int *answer,
int *old_answer,
213 (*answer) = (*old_answer);
216 ask_data(table, answer, old_answer, memory_count);
235bool match(
const std::vector<T> &table, std::vector<T> *table_empty,
236 const int &answer,
bool *first_time,
int *old_answer,
238 if ((*first_time) ==
true) {
244 for (
int i = 0; i < table.size() + 1; i++) {
246 if (table[i - 1] == table[(*old_answer) - 1]) {
247 (*first_time) =
true;
253 std::cout <<
"\nNo match (value was " << table[i - 1]
254 <<
", index is " << i <<
").\n\n";
256 (*table_empty)[(*old_answer) - 1] = 0;
257 (*table_empty)[answer - 1] = 0;
259 (*first_time) =
true;
290 int *answer,
bool *first_time,
int *old_answer,
295 for (
int i = 0; i < (*table).size() + 1; i++) {
296 if (i == (*answer)) {
297 if (
match((*table), table_empty, (*answer), first_time, old_answer,
298 memory_count) ==
true) {
299 (*table_empty)[i - 1] = (*table)[i - 1];
300 (*first_time) =
true;
305 if ((*memory_count) == 1) {
306 (*first_time) =
false;
310 char try_again =
'n';
314 for (
int i = 0; i < (*table).size() + 1; i++) {
315 if ((*table_empty)[i] == 0) {
317 }
else if (i == (*table).size() - 1) {
320 std::cout <<
"\n\nYou won. Congratulations! Do you want to play "
323 <<
"Size " << (*table).size()
324 <<
" will be used. This can be changed by re-running the game.";
325 std::cin >> try_again;
326 if (try_again ==
'y') {
329 for (
int i = 0; i < (*table_empty).size(); i++) {
330 (*table_empty)[i] = 0;
334 }
else if (try_again ==
'n') {
335 std::cout <<
"\nThanks for playing the game!\n";
340 std::cout <<
"\nInvalid input (exitting...).\n";
349 ask_data((*table_empty), answer, old_answer, memory_count);
350 assign_results(table_empty, table, answer, first_time, old_answer,
362 std::srand(std::time(
nullptr));
372 bool first_time =
true;
375 std::cout <<
"\tMEMORY GAME\n";
378 std::cout <<
"\n1. 4x2 (1)";
379 std::cout <<
"\n2. 5x2 (2)";
380 std::cout <<
"\n3. 7x2 (3)\n";
382 std::cout <<
"\nChoose table size: ";
383 std::cin >> selection;
384 }
while ((selection < 1 || selection > 3) &&
402 std::vector<char> table(size);
403 std::vector<char> table_empty(size);
411 &first_time, &old_answer, &memory_count);
constexpr T SLEEP(T seconds)
for sleep()
bool is_number(const T &input)
Utility function to verify if the given input is a number or not. This is very useful to prevent the ...
bool match(const std::vector< T > &table, std::vector< T > *table_empty, const int &answer, bool *first_time, int *old_answer, int *memory_count)
Checks if the two values given by the user match.
void assign_results(std::vector< T > *table_empty, std::vector< T > *table, int *answer, bool *first_time, int *old_answer, int *memory_count)
Function to assign the results to the table.
void ask_data(const std::vector< T > &table, int *answer, int *old_answer, int *memory_count)
Function that asks the user for their input in the table they previously chose.
void print_table(const std::vector< T > &table)
Utility function to print the table.
void init(std::vector< T > *table)
Initializes the table with the letters.
void reset_data(const std::vector< T > &, int *, int *, int *)
Utility function that resets the data if the user enters an invalid value.
(Mini)game implementations.