Enhance your memory skills with this interactive Java-based memory game. Match the pairs and test your memory!
Introduction
The Memory Game, also known as the Matching Game, challenges players to match pairs of cards. Players are presented with a grid of cards, each hidden face down. When a card is clicked, it is flipped over to reveal an image or symbol. The player must remember the positions of the cards and match them in pairs. The game ends when all pairs have been matched. This game is not only fun but also improves concentration and memory.
Objective
The objective of the game is simple: match all the pairs of cards before running out of time. The game starts with a shuffled set of cards that are initially face down. Players take turns flipping two cards at a time, trying to find matching pairs. The game continues until all pairs are matched correctly.
Memory Game Code in Java
import java.util.*;
public class MemoryGame {
private static final String[] cards = {"A", "B", "C", "D", "E", "F", "G", "H", "A", "B", "C", "D", "E", "F", "G", "H"};
private static final int BOARD_SIZE = 4;
private static String[][] board = new String[BOARD_SIZE][BOARD_SIZE];
private static boolean[][] revealed = new boolean[BOARD_SIZE][BOARD_SIZE];
private static List shuffledCards = new ArrayList<>();
private static int pairsFound = 0;
public static void main(String[] args) {
shuffleCards();
initializeBoard();
printBoard();
Scanner scanner = new Scanner(System.in);
while (pairsFound < (cards.length / 2)) {
System.out.println("Enter the row and column of the first card (0-3): ");
int row1 = scanner.nextInt();
int col1 = scanner.nextInt();
System.out.println("Enter the row and column of the second card (0-3): ");
int row2 = scanner.nextInt();
int col2 = scanner.nextInt();
if (flipCard(row1, col1, row2, col2)) {
System.out.println("You found a pair!");
pairsFound++;
} else {
System.out.println("No match, try again!");
}
printBoard();
}
System.out.println("Congratulations, you've found all pairs!");
}
private static void shuffleCards() {
for (int i = 0; i < cards.length; i++) {
shuffledCards.add(i);
}
Collections.shuffle(shuffledCards);
}
private static void initializeBoard() {
int index = 0;
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
board[i][j] = cards[shuffledCards.get(index)];
revealed[i][j] = false;
index++;
}
}
}
private static void printBoard() {
System.out.println("Current Board: ");
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
if (revealed[i][j]) {
System.out.print(board[i][j] + " ");
} else {
System.out.print("[ ] ");
}
}
System.out.println();
}
}
private static boolean flipCard(int row1, int col1, int row2, int col2) {
if (revealed[row1][col1] || revealed[row2][col2]) {
System.out.println("One or both cards are already revealed.");
return false;
}
revealed[row1][col1] = true;
revealed[row2][col2] = true;
if (board[row1][col1].equals(board[row2][col2])) {
return true;
} else {
revealed[row1][col1] = false;
revealed[row2][col2] = false;
return false;
}
}
}
Explanation of the Program
The Memory Game program is designed to shuffle cards, display them on a grid, and allow users to flip two cards at a time to find matching pairs. The main components of the game are:
- Shuffle Cards: The cards are shuffled randomly before the game starts.
- Game Board: The board is a 4×4 grid, containing 16 cards with 8 pairs of matching symbols.
- Flip Cards: Players input the row and column coordinates to flip two cards. If the cards match, they are left revealed; otherwise, they are flipped back over.
- Game End Condition: The game ends when all pairs are matched, and the player is congratulated.
How to Run the Program
Follow these steps to run the Memory Game in Java:
- Ensure you have Java Development Kit (JDK) installed on your machine.
- Create a new Java file (e.g.,
MemoryGame.java
) and copy the provided code into the file. - Compile the Java program using the command:
javac MemoryGame.java
- Run the compiled program using the command:
java MemoryGame
- Follow the on-screen instructions to play the game and match the pairs.