# Program To Create a Memory Game in C++ Language Assignment Solution

June 22, 2024
Professor Kevin
🇸🇬 Singapore
C++
Professor Tan is an esteemed academic with a master's degree in computer science from the National University of Singapore. With over 900 completed orders, he is proficient in handling a wide range of C++ tests, from basic syntax to advanced template programming, ensuring students achieve exceptional results.
Key Topics
• Instructions
• Objective
• Requirements and Specifications
Tip of the day
News

## Instructions

### Objective

Write a program to create a memory game in C++ language.

## Requirements and Specifications

Problem 02: Memory Game

The cards are shuffled and placed face down on a table. The players then take turns and select two cards at a time. If both cards match, they are left face up; otherwise, the cards are placed face down at the same positions. Once the players see the selected pair of cards and if the cards do not match, then they can memorize the cards and use their memory to select the next pair of cards.

The game continues until all the cards are face up. Write a program to play the memory game.

Use a two-dimensional array of 4 rows and 4 columns for a deck of 16 cards with 8 matching pairs. You can use numbers 1 to 8 to mark the cards. Use random number generators to randomly store the pairs in the array. Use appropriate functions (see below) in your program, and the main program should be merely a call to function.

Source Code and Solution

```#include using namespace std; /* Method for filling board randomly with number pairs from 1 to 8 */ void fillBoardRandomly(int t[4][4], int o[4][4]) { for (int i = 0; i<4; i++) { for (int j = 0; j<4; j++) { o[i][j] = 0; t[i][j] = 0; } } for (int i = 0; i < 16; i++) { int next = rand() % (16 - i); int counter = 0; for (int j = 0; j<16; j++) { if (t[j / 4][j % 4] == 0) { if (counter == next) { t[j / 4][j % 4] = i / 2 + 1; break; } else { counter++; } } } } } /* Method for asking user to enter location on board to open. Bound validation is made here */ int getCardLocation() { while (1) { cout << "Enter the row (1 to 4) and col (1 to 4) position of the pair:"; int row, col; cin >> row; cin >> col; cout << endl; if (row < 1 || row > 4 || col < 1 || col > 4) { cout << "Invalid position" << endl; } else { return 4*(row-1) + col-1; } } } /* Method for showing board in the console, masking closed cells with '*' */ void showBoard(int t[4][4], int o[4][4]) { for (int i = 0; i<4; i++) { for (int j = 0; j<4; j++) { if (o[i][j] == 1) { cout << " " << t[i][j]; } else { cout << " *"; } } cout << endl; } } /* Method for user to make a move, by asking to open two cell positions */ void makeMove(int t[4][4], int o[4][4]) { int selected1, selected2; showBoard(t, o); // keep asking for 1st position until valid position is entered while (1) { selected1 = getCardLocation(); if (o[selected1 / 4][selected1 % 4] == 1) { cout << "Card at this position already faced up. Select position again." << endl; } else { o[selected1 / 4][selected1 % 4] = 1; break; } } showBoard(t, o); // keep asking for 2nd position until valid position is entered while (1) { selected2 = getCardLocation(); if (o[selected2 / 4][selected2 % 4] == 1) { cout << "Card at this position already faced up. Select position again." << endl; } else { o[selected2 / 4][selected2 % 4] = 1; break; } } // showing board and checking if pair matched showBoard(t, o); if (t[selected1 / 4][selected1 % 4] == t[selected2 / 4][selected2 % 4]) { cout << "Pair match" << endl; } else { // if pair do not match, closing both just opened cells o[selected1 / 4][selected1 % 4] = 0; o[selected2 / 4][selected2 % 4] = 0; cout << "Pair do not match. Select again!" << endl; } } /* Method for checking if game is over. Game is over if all cells are opened */ int isOver(int o[4][4]) { for (int i = 0; i<4; i++) { for (int j = 0; j<4; j++) { if (o[i][j] == 0) { return 0; } } } return 1; } int main() { // board numbers array int board[4][4]; // opened cells array int opened[4][4]; // filling board randomly with numbers fillBoardRandomly(board, opened); // keep asking for move until game is over while (isOver(opened) == 0) { makeMove(board, opened); } } ```

## Similar Samples

Explore our diverse array of programming homework samples at ProgrammingHomeworkHelp.com. Our examples in Java, Python, C++, and more showcase our expertise in solving complex coding challenges with clarity and precision. Each sample is designed to provide insights and guidance, ensuring you excel in your programming assignments. Discover how our samples can empower your learning journey today.