cplusplus
cplusplus

 

 

 

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:

    1. Ensure you have a C++ compiler installed on your computer, such as GCC or MinGW.
    2. Save the program code into a file, for example, memory_game.cpp.
    3. Open your terminal or command prompt and navigate to the directory where the file is saved.
    4. Compile the program using the following command:
g++ memory_game.cpp -o memory_game
    1. Run the compiled program:
./memory_game

 

© 2025 Learn Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)