Introduction

A memory matching game is a fun and educational activity that requires players to match pairs of cards with identical symbols. The game enhances memory skills by challenging players to remember the locations of cards as they are flipped over. In this project, we’ll learn how to create a memory matching game using C programming. The objective of this game is to match all pairs of cards while keeping track of the number of moves made.

Objective

The goal of this game is to match pairs of cards with the same value. The cards are randomly shuffled at the beginning of the game. The player will select two cards at a time, and if the cards match, they remain face-up. If not, they are flipped back face-down. The game ends when all pairs are matched. The player should try to complete the game in the fewest number of moves possible.

Memory Game Code in C


#include 
#include 
#include 

#define SIZE 4  // Grid size (4x4)

void shuffle(int grid[SIZE][SIZE]);
void printGrid(int grid[SIZE][SIZE], int revealed[SIZE][SIZE]);
int checkMatch(int grid[SIZE][SIZE], int revealed[SIZE][SIZE]);

int main() {
    int grid[SIZE][SIZE] = {
        {1, 2, 3, 4},
        {1, 2, 3, 4},
        {5, 6, 7, 8},
        {5, 6, 7, 8}
    };
    int revealed[SIZE][SIZE] = {{0}};
    int row1, col1, row2, col2, moves = 0;

    srand(time(NULL));
    shuffle(grid);

    printf("Welcome to the Memory Matching Game!\n");
    while (checkMatch(grid, revealed) == 0) {
        printGrid(grid, revealed);
        printf("\nEnter the coordinates of the first card (row col): ");
        scanf("%d %d", &row1, &col1);
        printf("Enter the coordinates of the second card (row col): ");
        scanf("%d %d", &row2, &col2);

        if (grid[row1][col1] == grid[row2][col2]) {
            revealed[row1][col1] = 1;
            revealed[row2][col2] = 1;
        }

        moves++;
    }

    printf("\nYou completed the game in %d moves!\n", moves);
    return 0;
}

void shuffle(int grid[SIZE][SIZE]) {
    for (int i = 0; i < SIZE; i++) {
        for (int j = 0; j < SIZE; j++) {
            int randRow = rand() % SIZE;
            int randCol = rand() % SIZE;
            int temp = grid[i][j];
            grid[i][j] = grid[randRow][randCol];
            grid[randRow][randCol] = temp;
        }
    }
}

void printGrid(int grid[SIZE][SIZE], int revealed[SIZE][SIZE]) {
    printf("\nMemory Game Board:\n");
    for (int i = 0; i < SIZE; i++) {
        for (int j = 0; j < SIZE; j++) {
            if (revealed[i][j] == 1)
                printf("%d ", grid[i][j]);
            else
                printf("X ");
        }
        printf("\n");
    }
}

int checkMatch(int grid[SIZE][SIZE], int revealed[SIZE][SIZE]) {
    for (int i = 0; i < SIZE; i++) {
        for (int j = 0; j < SIZE; j++) {
            if (revealed[i][j] == 0)
                return 0;
        }
    }
    return 1;
}
        

Program Structure and How It Works

The program defines a 4×4 grid of numbers, which represents the cards in the game. The numbers are initially paired, and the cards are shuffled using the shuffle() function. The player is asked to input the coordinates of two cards they want to flip. The game continues until all pairs are matched.

Key Functions:

  • shuffle(): Shuffles the cards in the grid randomly.
  • printGrid(): Displays the current state of the board, showing face-up cards and face-down cards as “X”.
  • checkMatch(): Checks if all pairs of cards have been revealed.

How to Run the Program

To run the memory matching game, follow these steps:

  1. Write the code in a C editor (like Code::Blocks, Dev-C++, or an online C compiler).
  2. Compile the code using a C compiler (e.g., GCC).
  3. Run the program from the terminal or IDE.
  4. Follow the on-screen instructions to play the game by entering the coordinates of the cards you want to flip.
© 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 :)