Introduction
The Memory Game is a fun and interactive game that challenges your memory and pattern recognition skills. In this game, you will be presented with a series of cards, each hiding a unique number. Your objective is to match pairs of cards by remembering their positions.
Objective
The objective of this game is to test your memory by flipping over cards and matching them with their corresponding pairs. The game ends when all pairs are matched correctly, and the player is notified of their success.
Program Code (C++)
#include #include #include #include #include using namespace std; void shuffleCards(vector& cards) { random_shuffle(cards.begin(), cards.end()); } void printCards(const vector& cards, const vector& revealed) { for (int i = 0; i < cards.size(); ++i) { if (revealed[i]) { cout << cards[i] << " "; } else { cout << "X "; } } cout << endl; } int main() { const int SIZE = 16; // Size of the grid (4x4) vector cards(SIZE); vector revealed(SIZE, false); // Initialize cards for (int i = 0; i < SIZE / 2; ++i) { cards[i] = cards[i + SIZE / 2] = i + 1; } shuffleCards(cards); int matches = 0; int attempts = 0; cout << "Welcome to the Memory Game!" << endl; while (matches < SIZE / 2) { cout << "\nCurrent Board: "; printCards(cards, revealed); int firstChoice, secondChoice; cout << "Enter first card index (0-" << SIZE - 1 << "): "; cin >> firstChoice; cout << "Enter second card index (0-" << SIZE - 1 << "): "; cin >> secondChoice; if (firstChoice < 0 || firstChoice >= SIZE || secondChoice < 0 || secondChoice >= SIZE) { cout << "Invalid indices! Please try again." << endl; continue; } revealed[firstChoice] = true; revealed[secondChoice] = true; attempts++; if (cards[firstChoice] == cards[secondChoice]) { cout << "It's a match!" << endl; matches++; } else { cout << "Not a match!" << endl; revealed[firstChoice] = false; revealed[secondChoice] = false; } } cout << "\nCongratulations! You matched all the pairs in " << attempts << " attempts." << endl; return 0; }
Explanation of the Program Structure
The Memory Game program in C++ follows a simple structure:
- shuffleCards() function: This function shuffles the cards randomly to provide a unique game experience each time.
- printCards() function: This function displays the cards on the screen, showing either the card value or a placeholder (“X”) if the card is not revealed.
- Game Loop: The main game loop continues until all pairs are matched. The user is prompted to select two cards to compare. If the cards match, they are revealed; otherwise, they are hidden again.
- End Condition: The game ends when all pairs have been matched, and the player is informed of the number of attempts made.
How to Run the Program
To run the Memory Game program, follow these steps:
-
- Ensure you have a C++ compiler installed on your computer, such as GCC or MinGW.
- Save the program code into a file, for example, memory_game.cpp.
- Open your terminal or command prompt and navigate to the directory where the file is saved.
- Compile the program using the following command:
g++ memory_game.cpp -o memory_game
-
- Run the compiled program:
./memory_game