std::enable_if<std::is_integral<T>::value,
void>::type
SLEEP(
31Sleep(milliseconds * 1000);
37 returnsleep(seconds);
63 if(std::cin.fail()) {
65std::cin.ignore(256,
'\n');
80void init(std::vector<T> *table) {
81std::vector<char> letters(7);
84 if((*table).size() == 10) {
85letters = {
'A',
'E',
'Z',
'P',
'D'};
86}
else if((*table).size() == 8) {
87letters = {
'A',
'E',
'Z',
'D'};
88}
else if((*table).size() == 14) {
89letters = {
'A',
'E',
'Z',
'P',
'D',
'B',
'M'};
92std::vector<char> pairs;
93 for(
charletter : letters) {
94pairs.push_back(letter);
95pairs.push_back(letter);
98std::shuffle(pairs.begin(), pairs.end(),
99std::mt19937(std::random_device()()));
101 for(
inti = 0; i < (*table).size(); i++) {
102(*table)[i] = pairs[i];
105std::cout <<
"All available types are: ";
107 for(
inti = 0; i < letters.size(); i++) {
108 if(i == letters.size() - 1) {
109std::cout <<
"and "<< letters[i] <<
".\n\n";
111std::cout << letters[i] <<
", ";
122template<
typenameT>
125std::vector<T> table_print(table.size());
127 for(
inti = 0; i < table.size(); i++) {
128table_print[i] =
' ';
131table_print[i] = table[i];
135 for(
inti = 0; i < table.size(); i++) {
136 if(i % 5 == 0 && i != 0) {
137std::cout <<
"\n| ";
140std::cout << table_print[i] <<
" | ";
146template<
typenameT>
147void reset_data(
conststd::vector<T> &,
int*,
int*,
int*);
160template<
typenameT>
161void ask_data(
conststd::vector<T> &table,
int*answer,
int*old_answer,
163(*old_answer) = (*answer);
166std::cout <<
"\n\nType your response here (number index):\n";
167std::cin >> (*answer);
170std::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)) {
179std::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)) {
185std::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))) {
194std::cout <<
"\nYou can't check the same value twice.\n\n";
195 reset_data(table, answer, old_answer, memory_count);
210template<
typenameT>
211void reset_data(
conststd::vector<T> &table,
int*answer,
int*old_answer,
213(*answer) = (*old_answer);
216 ask_data(table, answer, old_answer, memory_count);
234template<
typenameT>
235bool match(
conststd::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(
inti = 0; i < table.size() + 1; i++) {
246 if(table[i - 1] == table[(*old_answer) - 1]) {
247(*first_time) =
true;
253std::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;
288template<
typenameT>
290 int*answer,
bool*first_time,
int*old_answer,
295 for(
inti = 0; i < (*table).size() + 1; i++) {
296 if(i == (*answer)) {
297 if(
match((*table), table_empty, (*answer), first_time, old_answer,
298memory_count) ==
true) {
299(*table_empty)[i - 1] = (*table)[i - 1];
300(*first_time) =
true;
305 if((*memory_count) == 1) {
306(*first_time) =
false;
310 chartry_again =
'n';
314 for(
inti = 0; i < (*table).size() + 1; i++) {
315 if((*table_empty)[i] == 0) {
317}
else if(i == (*table).size() - 1) {
320std::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.";
325std::cin >> try_again;
326 if(try_again ==
'y') {
329 for(
inti = 0; i < (*table_empty).size(); i++) {
330(*table_empty)[i] = 0;
334}
else if(try_again ==
'n') {
335std::cout <<
"\nThanks for playing the game!\n";
340std::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,
362std::srand(std::time(
nullptr));
372 boolfirst_time =
true;
375std::cout <<
"\tMEMORY GAME\n";
378std::cout <<
"\n1. 4x2 (1)";
379std::cout <<
"\n2. 5x2 (2)";
380std::cout <<
"\n3. 7x2 (3)\n";
382std::cout <<
"\nChoose table size: ";
383std::cin >> selection;
384}
while((selection < 1 || selection > 3) &&
402std::vector<char> table(size);
403std::vector<char> table_empty(size);
411&first_time, &old_answer, &memory_count);
constexpr T SLEEP(T seconds)
for sleep()
int main()
Main function.
Functions for the Memory Game implementation.
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.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4