Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
|
A simple Memory Game with 3 different sizes and multiple letters. More...
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <random>
#include <vector>
#include <unistd.h>
Functions | |
template<typename T > | |
constexpr T | SLEEP (T seconds) |
for sleep() | |
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<typename T > | |
void | games::memory_game::init (std::vector< T > *table) |
Initializes the table with the letters. | |
template<typename T > | |
void | games::memory_game::print_table (const std::vector< T > &table) |
Utility function to print the table. | |
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<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<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<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. | |
int | main () |
Main function. | |
A simple Memory Game with 3 different sizes and multiple letters.
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.
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.
T | The type of the table. |
table | The table that's used to get the user's input and data. |
answer | The user's answer. |
old_answer | The user's previous answer. |
memory_count | A counter to check if the user has already answered two values. |
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.
T | The type of the tables. |
table_empty | The table with no values, slowly assigned from table depending on the user's input. |
table | The table with the original values. |
answer | The user's answer. |
first_time | A boolean to check if the user has already answered a value. |
old_answer | The user's previous answer. |
memory_count | A counter to check if the user has already answered two values. |
void games::memory_game::init | ( | std::vector< T > * | table | ) |
Initializes the table with the letters.
T | The type of the table. |
table | The table to initialize. |
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.
T | The type of the input |
input | The input to check. |
int main | ( | void | ) |
Main function.
< Size of the table.
< Selection of the size (4x2, 5x2, 7x2).
< The answer (number index) that the user chose.
< Previous answer (number index).
< Counter to check if the user has already answered two values.
< Whether the user has answered 1 value or not (previous answered values do not count).
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.
T | The type of the table. |
table_empty | The table with no values, slowly assigned from table depending on the user's input. |
table | The table with the original values. |
answer | The user's answer. |
first_time | A boolean to check if the user has already answered a value. |
old_answer | The user's previous answer. |
memory_count | A counter to check if the user has already answered two values. |
void games::memory_game::print_table | ( | const std::vector< T > & | table | ) |
Utility function to print the table.
T | The type of the table. |
table | The table to print. |
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.
T | The type of the table. |
table | The table that will be used to call ask_data() . |
answer | The user's answer. |
old_answer | The user's previous answer. |
memory_count | A counter to check if the user has already answered two values. |
|
constexpr |
for sleep()
for std::shuffle() for std::srand() for std::time() for IO operations for std::mt19937 for std::vector