Memory game in Java

The Memory game is a game where a number of cards are laid face down on a table, and the player must flip over pairs of cards to find matching pairs. The game is won when all pairs have been found.

Here's how the game works in more detail:

  1. The game begins by asking the player how many cards they would like to play with. The number of cards must be even.
  2. The cards are laid out on the table face down. Each card has a value on it that is hidden from the player.
  3. The player selects two cards to flip over. If the values on the cards match, the player has found a pair and the cards are removed from the table. If the values do not match, the cards are flipped back over and remain on the table.
  4. The player continues to flip over pairs of cards until all pairs have been found and the game is won.
  5. The game can also be lost if the player runs out of time or guesses.

The game is implemented using an array of integers to represent the cards. The game tracks the state of each card (whether it is face up or face down) using a second array of booleans. The player's input is handled using the Scanner class, which allows the player to input the index of the card they would like to flip over.

Memory game in Java:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class MemoryGame {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Welcome to the Memory Game!");
        System.out.print("Enter the number of cards (must be an even number): ");
        int numCards = input.nextInt();
        
        if (numCards % 2 != 0) {
            System.out.println("Error: Number of cards must be even.");
            return;
        }
        
        List cards = new ArrayList<>();
        for (int i = 1; i <= numCards / 2; i++) {
            cards.add(Integer.toString(i));
            cards.add(Integer.toString(i));
        }
        Collections.shuffle(cards);
        
        String[] board = new String[numCards];
        boolean[] revealed = new boolean[numCards];
        for (int i = 0; i < numCards; i++) {
            board[i] = "X";
            revealed[i] = false;
        }
        
        int numPairs = 0;
        while (numPairs < numCards / 2) {
            printBoard(board);
            System.out.print("Enter the index of a card to reveal (0-" + (numCards - 1) + "): ");
            int index = input.nextInt();
            
            if (index < 0 || index >= numCards) {
                System.out.println("Error: Invalid index.");
                continue;
            }
            if (revealed[index]) {
                System.out.println("Error: Card already revealed.");
                continue;
            }
            
            board[index] = cards.get(index);
            revealed[index] = true;
            printBoard(board);
            
            if (countTrue(revealed) % 2 == 0) {
                int firstIndex = -1;
                for (int i = 0; i < numCards; i++) {
                    if (revealed[i]) {
                        if (firstIndex == -1) {
                            firstIndex = i;
                        } else if (cards.get(i).equals(cards.get(firstIndex))) {
                            System.out.println("You found a pair!");
                            numPairs++;
                            firstIndex = -1;
                        } else {
                            System.out.println("Sorry, not a match.");
                            board[i] = "X";
                            board[firstIndex] = "X";
                            revealed[i] = false;
                            revealed[firstIndex] = false;
                            firstIndex = -1;
                        }
                    }
                }
            }
        }
        System.out.println("Congratulations, you won!");
    }
    
    public static void printBoard(String[] board) {
        for (String card : board) {
            System.out.print(card + " ");
        }
        System.out.println();
    }
    
    public static int countTrue(boolean[] arr) {
        int count = 0;
        for (boolean val : arr) {
            if (val) {
                count++;
            }
        }
        return count;
    }
}

This code prompts the user to enter the number of cards they would like to play with, creates a shuffled list of cards with pairs of numbers, and initializes the game board with all cards facing down. It then allows the player to reveal cards by inputting the index of the card they would like to flip over. If two revealed cards match, they remain to face up and the player earns a point. If they do not match, they are flipped back over and the player must try again. The game continues until all pairs have been found, at which point the player wins the game.

Here's an example of how you could run the game and input values:

Welcome to the Memory Game!
Enter the number of cards (must be an even number): 6
X X X X X X 
Enter the index of a card to reveal (0-5): 2
X X 3 X X X 
Enter the index of a card to reveal (0-5): 4
X X 3 X 5 X 
Sorry, not a match.
X X X X X X 
Enter the index of a card to reveal (0-5): 1
X 2 X X X X 
Enter the index of a card to reveal (0-5): 3
X 2 X 4 X X 
You found a pair!
2 2 X 4 X X 
Enter the index of a card to reveal (0-5): 0
1 2 X 4 X X 
Enter the index of a card to reveal (0-5): 5
1 2 X 4 X 6 
You found a pair!
1 2 X 4 X X 
Enter the index of a card to reveal (0-5): 2
1 2 3 4 X X 
You found a pair!
1 2 3 4 X X 
Enter the index of a card to reveal (0-5): 5
1 2 3 4 X 6 
You found a pair!
1 2 3 4 X X 
Enter the index of a card to reveal (0-5): 0
1 2 3 4 X X 
Enter the index of a card to reveal (0-5): 1
1 2 3 4 X X 
Enter the index of a card to reveal (0-5): 3
1 2 3 4 X X 
Enter the index of a card to reveal (0-5): 4
1 2 3 4 5 X 
You found a pair!
1 2 3 4 5 X 
Enter the index of a card to reveal (0-5): 0
1 2 3 4 5 X 
Enter the index of a card to reveal (0-5): 1
1 2 3 4 5 X 
Enter the index of a card to reveal (0-5): 5
1 2 3 4 5 6 
You found a pair!
1 2 3 4 5 6 
Congratulations, you won!

Next Post Previous Post
No Comment
Add Comment
comment url