Click here to Skip to main content
16,017,351 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
import java.util.Scanner;
import java.util.Arrays;
import java.util.Random;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // Set up the board
        int[] board = setupBoard();
        int[] playingBoard = new int[board.length]; // initialize to all zeros
        int[] pairs = new int[board.length/2];
        Arrays.fill(pairs, 2); // initialize to all twos
        placePairs(board, pairs);

        // Display the main menu
        int attempts = pairs.length + 2;
        boolean playUnlimited = false;
        while (true) {
            displayMainMenu(attempts, playUnlimited);
            int choice = scanner.nextInt();
            if (choice == 1) {
                playUnlimited = false;
                break;
            } else if (choice == 2) {
                playUnlimited = true;
                break;
            } else if (choice == 3) {
                System.exit(0);
            } else {
                System.out.println("Invalid choice. Please enter 1, 2, or 3.");
            }
        }

        // Play the game
        int foundPairs = 0;
        while (playUnlimited || foundPairs < pairs.length) {
            // Display the playing board
            displayBoard(playingBoard);

            // Ask the user for two undiscovered cells
            int cell1, cell2;
            do {
                System.out.print("Enter the indexes of two undiscovered cells (separated by a space): ");
                cell1 = scanner.nextInt();
                cell2 = scanner.nextInt();
                if (!isWithinBounds(board.length, cell1) || !isWithinBounds(board.length, cell2)) {
                    System.out.println("Invalid cell indexes. Please try again.");
                    continue;
                }
                if (isCellSelected(playingBoard, cell1) || isCellSelected(playingBoard, cell2)) {
                    System.out.println("One or both of the cells you selected has already been cleared. Please try again.");
                    continue;
                }
                break;
            } while (true);

            // Check if the selected cells contain a pair
            if (board[cell1] == board[cell2]) {
                clearCell(playingBoard, cell1, board[cell1]);
                clearCell(playingBoard, cell2, board[cell2]);
                foundPairs++;
                System.out.println("You found a pair!");
            } else {
                System.out.println("Sorry, those cells don't contain a pair.");
                attempts--;
            }

            // Check if the game is over
            if (attempts == 5) {
                System.out.println("Game over. You ran out of attempts.");
                break;
            } else if (foundPairs == pairs.length) {
                System.out.println("Congratulations! You found all the pairs.");
                break;
            }
        }

        scanner.close();
    }

    // Set up the board
    public static int[] setupBoard() {
        Scanner scanner = new Scanner(System.in);
        int size;
        do {
            System.out.print("Enter the size of the board (even number): ");
            size = scanner.nextInt();
            if (size % 2 != 0) {
                System.out.println("Invalid board size. Please enter an even number.");
                continue;
            }
            break;
        } while (true);

        int[] board = new int[size];
        return board;
          }
}


What I have tried:

I have tried to putting a symbol for the method but nothing

Here are the Error codes I get
Main.java:14: error: cannot find symbol
        placePairs(board, pairs);
        ^
  symbol:   method placePairs(int[],int[])
  location: class Main
./Main.java:20: error: cannot find symbol
            displayMainMenu(attempts, playUnlimited);
            ^
  symbol:   method displayMainMenu(int,boolean)
  location: class Main
./Main.java:39: error: cannot find symbol
 sh -c javac -classpath .:target/dependency/* -d . $(find . -type f -name '*.java')
./Main.java:14: error: cannot find symbol
        placePairs(board, pairs);
        ^
  symbol:   method placePairs(int[],int[])
  location: class Main
./Main.java:20: error: cannot find symbol
            displayMainMenu(attempts, playUnlimited);
            ^
  symbol:   method displayMainMenu(int,boolean)
  location: class Main
./Main.java:39: error: cannot find symbol
 sh -c javac -classpath .:target/dependency/* -d . $(find . -type f -name '*.java')
./Main.java:13: error: cannot find symbol
        Arrays.fill(pairs, 2); // initialize to all twos
                    ^
  symbol:   variable pairs
  location: class Main
./Main.java:14: error: cannot find symbol
        placePairs(board, pairs);
                          ^
  symbol:   variable pairs
  location: class Main
./Main.java:17: error: cannot find symbol
        int attempts = pairs.length + 2;
                       ^
  symbol:   variable pairs
  location: class Main
./Main.java:20: error: cannot find symbol
            displayMainMenu(attempts, playUnlimited);
            ^
  symbol:   method displayMainMenu(int,boolean)
  location: class Main
./Main.java:37: error: cannot find symbol
        while (playUnlimited || foundPairs < pairs.length) {
                                             ^
  symbol:   variable pairs
  location: class Main
./Main.java:39: error: cannot find symbol
            displayBoard(playingBoard);
            ^
  symbol:   method displayBoard(int[])
  location: class Main
./Main.java:47: error: cannot find symbol
 sh -c javac -classpath .:target/dependency/* -d . $(find . -type f -name '*.java')
./Main.java:14: error: cannot find symbol
        placePairs(board, pairs);
        ^
  symbol:   method placePairs(int[],int[])
  location: class Main
./Main.java:20: error: cannot find symbol
            displayMainMenu(attempts, playUnlimited);
            ^
  symbol:   method displayMainMenu(int,boolean)
  location: class Main
./Main.java:39: error: cannot find symbol
            displayBoard(playingBoard);
            ^
  symbol:   method displayBoard(int[])
  location: class Main
./Main.java:47: error: cannot find symbol
                if (!isWithinBounds(board.length, cell1) || !isWithinBounds(board.length, cell2)) {
                     ^
  symbol:   method isWithinBounds(int,int)
  location: class Main
./Main.java:47: error: cannot find symbol
                if (!isWithinBounds(board.length, cell1) || !isWithinBounds(board.length, cell2)) {
                                                             ^
  symbol:   method isWithinBounds(int,int)
  location: class Main
./Main.java:51: error: cannot find symbol
                if (isCellSelected(playingBoard, cell1) || isCellSelected(playingBoard, cell2)) {
                    ^
  symbol:   method isCellSelected(int[],int)
  location: class Main
./Main.java:51: error: cannot find symbol
                if (isCellSelected(playingBoard, cell1) || isCellSelected(playingBoard, cell2)) {
                                                           ^
  symbol:   method isCellSelected(int[],int)
  location: class Main
./Main.java:60: error: cannot find symbol
                clearCell(playingBoard, cell1, board[cell1]);
                ^
  symbol:   method clearCell(int[],int,int)
  location: class Main
./Main.java:61: error: cannot find symbol
                clearCell(playingBoard, cell2, board[cell2]);
                ^
  symbol:   method clearCell(int[],int,int)
  location: class Main
9 errors
exit status 1
 
Posted
Updated 7-Apr-23 19:13pm
v2
Comments
Patrice T 7-Apr-23 22:32pm    
Give full error message !
ExZoTic_ Thunder 7-Apr-23 23:04pm    
error: cannot find symbol
placePairs(board, pairs);
M Imran Ansari 8-Apr-23 0:44am    
placePairs() method that is being called in the main() method is missing in your above code. Without this method, the code will not compile, I hope that would be there.

1 solution

You are calling a method called placePairs and the system cannot find it. Since it isn't a standard Java method, you will need to go back to wherever you copied that code from and look there - we can't help you as we have no idea what it is meant to do ...
 
Share this answer
 
Comments
Dave Kreskowiak 8-Apr-23 10:54am    
Countered the 1-vote
OriginalGriff 8-Apr-23 11:33am    
Some people just don't like being told ... :D

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900