A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://TheAlgorithms.github.io/C-Plus-Plus/dd/d92/memory__game_8cpp.html below:

TheAlgorithms/C++: games/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>

Go to the source code of this file.

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.

Definition in file memory_game.cpp.

◆ main()

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

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

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

409 &memory_count);

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

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}


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