TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
games::memory_game Namespace Reference

Functions for the Memory Game implementation. More...

Functions

template<typename T >
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 program being stuck in a loop.
 
template<typename T >
void init (std::vector< T > *table)
 Initializes the table with the letters.
 
template<typename T >
void print_table (const std::vector< T > &table)
 Utility function to print the table.
 
template<typename T >
void reset_data (const std::vector< T > &table, int *answer, int *old_answer, int *memory_count)
 Utility function that resets the data if the user enters an invalid value.
 
template<typename T >
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.
 
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)
 Checks if the two values given by the user match.
 
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)
 Function to assign the results to the table.
 

Detailed Description

Functions for the Memory Game implementation.

Function Documentation

◆ ask_data()

template<typename T >
void games::memory_game::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.

Template Parameters
TThe type of the table.
Parameters
tableThe table that's used to get the user's input and data.
answerThe user's answer.
old_answerThe user's previous answer.
memory_countA counter to check if the user has already answered two values.
Returns
void

Definition at line 161 of file memory_game.cpp.

162 {
163 (*old_answer) = (*answer);
164 print_table(table);
165
166 std::cout << "\n\nType your response here (number index):\n";
167 std::cin >> (*answer);
168
169 if (!is_number((*answer))) {
170 std::cout << "\nYou must enter a valid number.\n\n";
171 reset_data(table, answer, old_answer, memory_count);
172 }
173
174 // Increase the memory count, which will be later on used for checking if
175 // the user has already answered two values.
176 (*memory_count)++;
177
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);
182 }
183
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);
187 }
188
189 // If two matches are answered already, but the user checkes a non-answered
190 // and an answered value, the program will mark it as no match, however, we
191 // must not allow the user to check the same value twice.
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);
196 }
197}
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 ...
void print_table(const std::vector< T > &table)
Utility function to print the table.
void reset_data(const std::vector< T > &, int *, int *, int *)
Utility function that resets the data if the user enters an invalid value.

◆ assign_results()

template<typename T >
void games::memory_game::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.

Also checkes if the user has answered all the values already, as well as verify if the user made a match or not.

Template Parameters
TThe type of the tables.
Parameters
table_emptyThe table with no values, slowly assigned from table depending on the user's input.
tableThe table with the original values.
answerThe user's answer.
first_timeA boolean to check if the user has already answered a value.
old_answerThe user's previous answer.
memory_countA counter to check if the user has already answered two values.
Returns
void

Definition at line 289 of file memory_game.cpp.

291 {
292 // Search through the entire table and if the answer matches the index, show
293 // the value. If it doesn't match, hide both the values. Don't forget to
294 // keep older values already answered.
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;
301 }
302 }
303 }
304
305 if ((*memory_count) == 1) {
306 (*first_time) = false;
307 (*memory_count) = 0;
308 }
309
310 char try_again = 'n';
311
312 // Has the user finished the game? Use a `for` loop, and if the table is
313 // full, ask the user if he wants to play again.
314 for (int i = 0; i < (*table).size() + 1; i++) {
315 if ((*table_empty)[i] == 0) {
316 break;
317 } else if (i == (*table).size() - 1) {
318 print_table((*table));
319
320 std::cout << "\n\nYou won. Congratulations! Do you want to play "
321 "again? (y/n)\n";
322 std::cout
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') {
327 // This is needed when checking if the user has two matches
328 // already.
329 for (int i = 0; i < (*table_empty).size(); i++) {
330 (*table_empty)[i] = 0;
331 }
332
333 init(table);
334 } else if (try_again == 'n') {
335 std::cout << "\nThanks for playing the game!\n";
336 SLEEP(3);
337
338 exit(0);
339 } else {
340 std::cout << "\nInvalid input (exitting...).\n";
341 SLEEP(3);
342
343 exit(0);
344 }
345 }
346 }
347
348 // Ask data again.
349 ask_data((*table_empty), answer, old_answer, memory_count);
350 assign_results(table_empty, table, answer, first_time, old_answer,
351 memory_count);
352}
constexpr T SLEEP(T seconds)
for sleep()
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 init(std::vector< T > *table)
Initializes the table with the letters.

◆ init()

template<typename T >
void games::memory_game::init ( std::vector< T > * table)

Initializes the table with the letters.

Template Parameters
TThe type of the table.
Parameters
tableThe table to initialize.
Returns
void

Definition at line 80 of file memory_game.cpp.

80 {
81 std::vector<char> letters(7);
82
83 // Decrease / increase the number of letters depending on the size.
84 if ((*table).size() == 10) { // 5x2
85 letters = {'A', 'E', 'Z', 'P', 'D'};
86 } else if ((*table).size() == 8) { // 4x2
87 letters = {'A', 'E', 'Z', 'D'};
88 } else if ((*table).size() == 14) { // 7x2
89 letters = {'A', 'E', 'Z', 'P', 'D', 'B', 'M'};
90 }
91
92 std::vector<char> pairs;
93 for (char letter : letters) {
94 pairs.push_back(letter);
95 pairs.push_back(letter);
96 }
97
98 std::shuffle(pairs.begin(), pairs.end(),
99 std::mt19937(std::random_device()()));
100
101 for (int i = 0; i < (*table).size(); i++) {
102 (*table)[i] = pairs[i];
103 }
104
105 std::cout << "All available types are: ";
106
107 for (int i = 0; i < letters.size(); i++) {
108 if (i == letters.size() - 1) {
109 std::cout << "and " << letters[i] << ".\n\n";
110 } else {
111 std::cout << letters[i] << ", ";
112 }
113 }
114}

◆ is_number()

template<typename T >
bool games::memory_game::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 program being stuck in a loop.

Template Parameters
TThe type of the input
Parameters
inputThe input to check.
Returns
false if the input IS empty or if it contains a non-digit character
true if the input is NOT empty and if it contains only digit characters

Definition at line 62 of file memory_game.cpp.

62 {
63 if (std::cin.fail()) {
64 std::cin.clear();
65 std::cin.ignore(256, '\n');
66
67 return false;
68 }
69
70 return true;
71}

◆ match()

template<typename T >
bool games::memory_game::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.

Template Parameters
TThe type of the table.
Parameters
table_emptyThe table with no values, slowly assigned from table depending on the user's input.
tableThe table with the original values.
answerThe user's answer.
first_timeA boolean to check if the user has already answered a value.
old_answerThe user's previous answer.
memory_countA counter to check if the user has already answered two values.
Returns
true IF the values given by the user match
false if the values given by the user do NOT match

Definition at line 235 of file memory_game.cpp.

237 {
238 if ((*first_time) == true) {
239 return true;
240 }
241
242 // Search across the whole table and if the two values match, keep results,
243 // otherwise, hide 'em up.
244 for (int i = 0; i < table.size() + 1; i++) {
245 if (i == answer) {
246 if (table[i - 1] == table[(*old_answer) - 1]) {
247 (*first_time) = true;
248 (*memory_count) = 0;
249
250 (*old_answer) = 0;
251 return true;
252 } else {
253 std::cout << "\nNo match (value was " << table[i - 1]
254 << ", index is " << i << ").\n\n";
255
256 (*table_empty)[(*old_answer) - 1] = 0;
257 (*table_empty)[answer - 1] = 0;
258
259 (*first_time) = true;
260 (*memory_count) = 0;
261
262 (*old_answer) = 0;
263 return false;
264 }
265 }
266 }
267
268 return false;
269}

◆ print_table()

template<typename T >
void games::memory_game::print_table ( const std::vector< T > & table)

Utility function to print the table.

Template Parameters
TThe type of the table.
Parameters
tableThe table to print.
Returns
void

Definition at line 123 of file memory_game.cpp.

123 {
124 std::cout << "| ";
125 std::vector<T> table_print(table.size());
126
127 for (int i = 0; i < table.size(); i++) {
128 table_print[i] = ' ';
129
130 if (table[i] != 0) {
131 table_print[i] = table[i];
132 }
133 }
134
135 for (int i = 0; i < table.size(); i++) {
136 if (i % 5 == 0 && i != 0) {
137 std::cout << "\n| ";
138 }
139
140 std::cout << table_print[i] << " | ";
141 }
142}

◆ reset_data()

template<typename T >
void games::memory_game::reset_data ( const std::vector< T > & table,
int * answer,
int * old_answer,
int * memory_count )

Utility function that resets the data if the user enters an invalid value.

Template Parameters
TThe type of the table.
Parameters
tableThe table that will be used to call ask_data().
answerThe user's answer.
old_answerThe user's previous answer.
memory_countA counter to check if the user has already answered two values.
Returns
void

Definition at line 211 of file memory_game.cpp.

212 {
213 (*answer) = (*old_answer);
214 (*memory_count)--;
215
216 ask_data(table, answer, old_answer, memory_count);
217}