TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
memory_game.cpp File Reference

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>
Include dependency graph for memory_game.cpp:

Go to the source code of this file.

Namespaces

namespace  games
 (Mini)game implementations.
 
namespace  games::memory_game
 Functions for the Memory Game implementation.
 

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.
 

Detailed Description

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.

Author
David Leal

Definition in file memory_game.cpp.

Function Documentation

◆ main()

int main ( void )

Main function.

Returns
0 on exit

< 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).

Definition at line 360 of file memory_game.cpp.

360 {
361 // Start randomizer. This changes the values every time.
362 std::srand(std::time(nullptr));
363
364 int size = 0;
365 int selection = 0;
366
367 int response = 0;
368 int old_answer = 0;
369
370 int memory_count =
371 0;
372 bool first_time = true;
374
375 std::cout << "\tMEMORY GAME\n";
376
377 do {
378 std::cout << "\n1. 4x2 (1)";
379 std::cout << "\n2. 5x2 (2)";
380 std::cout << "\n3. 7x2 (3)\n";
381
382 std::cout << "\nChoose table size: ";
383 std::cin >> selection;
384 } while ((selection < 1 || selection > 3) &&
385 (!games::memory_game::is_number(selection)));
386
387 switch (selection) {
388 case 1:
389 size = 8;
390 break;
391 case 2:
392 size = 10;
393 break;
394 case 3:
395 size = 14;
396 break;
397 default:
398 size = 10;
399 break;
400 }
401
402 std::vector<char> table(size);
403 std::vector<char> table_empty(size);
404
405 std::cout << "\n";
406
408 games::memory_game::ask_data(table_empty, &response, &old_answer,
409 &memory_count);
410 games::memory_game::assign_results(&table_empty, &table, &response,
411 &first_time, &old_answer, &memory_count);
412
413 return 0;
414}
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 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.

◆ SLEEP()

template<typename T >
T SLEEP ( T seconds)
constexpr

for sleep()

for std::shuffle() for std::srand() for std::time() for IO operations for std::mt19937 for std::vector

Definition at line 36 of file memory_game.cpp.

36 {
37 return sleep(seconds);
38}