Open In App

Memory Game in Java

The Memory Game is a game where the player has to flip over pairs of cards with the same symbol.

Implementation




import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
  
// class name : memorygame
public class MemoryGame {
  
    public static void main(String[] args)
    {
  
        Scanner scanner = new Scanner(System.in);
        ArrayList<String> cards = new ArrayList<>();
        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()];
        boolean[] flipped = new boolean[cards.size()];
        int pairsFound = 0;
  
        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;
  
            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;
            }
        }
        // win
        System.out.println("Congratulations, you won!");
    }
  
    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 (int i = 0; i < board.length; i++) {
            System.out.print("| " + board[i] + " ");
        }
        System.out.println("|");
    }
}

Code Explanation

In this example, the MemoryGame class represents the game logic. The constructor takes an argument number which specifies the number of pairs of cards in the game. The play method runs the game loop, prompting the user to choose two cards and reveal them, checking if they match, and hiding them again if they don’t. The complete method checks if all the cards have been shown. The primary method creates a new MemoryGame object with 6 pairs of cards and starts the game by calling the play method.

Output

Output


Article Tags :