Open In App

Memory Game in Java

Last Updated : 30 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The Memory Game is a fun and simple two-player game that tests your memory. In this game, a set of cards is laid face down. Players take turns flipping two cards to find matching pairs. If the two cards match, they remain face up. If not, they are flipped back down. The game continues until all pairs are found.

In this article, we will build a Memory Game in Java that runs in the console. Players will select card positions using numbers, and the board will update after every move.

Game Board Layout

The game has 8 cards in total, which is made up of 4 matching pairs. Each card is numbered from 0 to 7. When a player flips over two cards, their values are shown. If the cards match, then they stay face up. If not, then they’re turned back down again.

The Game board layout will look something like this:

|   |   |   |   |   |   |   |   |

Note: When the player flips cards, the board updates accordingly.

How to Play the Game

The steps to play the game are listed below:

  • The game board has 4 pairs of cards (8 total), which are randomly shuffled.
  • The board is shown as a row of numbered slots from 0 to 7.
  • On each turn:
    • A player selects two different indices to flip.
    • If the values match, the pair remains visible.
    • If the value does not match, then they are hidden again.
  • The game will end when all the pairs are found.

Key Concepts Used

We are using an array to manage the game state:

  • Cards: We are using an ArrayList<String> to store and shuffle the card values.
  • Board: We are using a String[] array to show the current state.
  • Flipped: We are using a boolean[] array to track which cards have been flipped.

Project Implementation

Java
// A simple Java program to demonstrate 
// Memory Game
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;

public class Geeks {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        ArrayList<String> cards = new ArrayList<>();

        // Add 4 pairs of cards
        cards.add("A"); cards.add("A");
        cards.add("B"); cards.add("B");
        cards.add("C"); cards.add("C");
        cards.add("D"); cards.add("D");

        Collections.shuffle(cards);

        String[] board = new String[cards.size()];
        Arrays.fill(board, " ");
        boolean[] flipped = new boolean[cards.size()];
        int pairsFound = 0;

        System.out.println("Welcome to the Memory Game!");
        while (pairsFound < 4) {
            printBoard(board);

            int firstIndex = getCardIndex(scanner, board, flipped, 
            "Enter index of first card to flip:");
            board[firstIndex] = cards.get(firstIndex);
            flipped[firstIndex] = true;
            printBoard(board);

            int secondIndex = getCardIndex(scanner, board, flipped, 
            "Enter index of second card to flip:");
            board[secondIndex] = cards.get(secondIndex);
            flipped[secondIndex] = true;
            printBoard(board);

            if (cards.get(firstIndex).equals(cards.get(secondIndex))) {
                System.out.println("You found a pair!");
                pairsFound++;
            } else {
                System.out.println("Sorry, those cards don't match.");
                board[firstIndex] = " ";
                board[secondIndex] = " ";
                flipped[firstIndex] = false;
                flipped[secondIndex] = false;
            }
        }

        System.out.println("Congratulations, you won!");
        scanner.close();
    }

    public static int getCardIndex(Scanner scanner, String[] board, 
    boolean[] flipped, String prompt) {
        int index;
        while (true) {
            System.out.println(prompt);
            index = scanner.nextInt();
            if (index < 0 || index >= board.length) {
                System.out.println("Invalid index, try again.");
            } else if (flipped[index]) {
                System.out.println("Card already flipped, try again.");
            } else {
                break;
            }
        }
        return index;
    }

    public static void printBoard(String[] board) {
        for (String value : board) {
            System.out.print("| " + value + " ");
        }
        System.out.println("|");
    }
}


Output:

Welcome to the Memory Game!
| | | | | | | | |
Enter index of first card to flip:
0
| B | | | | | | | |
Enter index of second card to flip:
1
| B | A | | | | | | |
Sorry, those cards don't match.
| | | | | | | | |
Enter index of first card to flip:
0
| B | | | | | | | |
Enter index of second card to flip:
3
| B | | | C | | | | |
Sorry, those cards don't match.
| | | | | | | | |
Enter index of first card to flip:
3
| | | | C | | | | |
Enter index of second card to flip:
5
| | | | C | | D | | |
Sorry, those cards don't match.
| | | | | | | | |
Enter index of first card to flip:
6
| | | | | | | D | |
Enter index of second card to flip:
5
| | | | | | D | D | |
You found a pair!
| | | | | | D | D | |
Enter index of first card to flip:
7
| | | | | | D | D | A |
Enter index of second card to flip:
1
| | A | | | | D | D | A |
You found a pair!
| | A | | | | D | D | A |
Enter index of first card to flip:
2
| | A | C | | | D | D | A |
Enter index of second card to flip:
3
| | A | C | C | | D | D | A |
You found a pair!
| | A | C | C | | D | D | A |
Enter index of first card to flip:
0
| B | A | C | C | | D | D | A |
Enter index of second card to flip:
4
| B | A | C | C | B | D | D | A |
You found a pair!
Congratulations, you won!


Steps to Run on IntelliJ IDE

  • Open IntelliJ IDE.
  • Create a New Java project.
  • Right-click on the src folder and create a new class like a class named Geeks.
  • Now, write your source code and ctrl+s to save it.
  • Now, to execute the program right-click the src folder and click on run as Java application.

You can check below screenshot for your reference.

Output

Article Tags :
Practice Tags :

Similar Reads