How do I break a do while loop when two 2D arrays become exactly the same?

61 Views Asked by At

I have a project for uni that needs me to create a crossword puzzle using 2 D arrays. I have done some progress, but have stuck on the part where if the user solves the puzzle correctly, it keeps looping. I need help with breaking out of the loop when both of the arrays are completely the same meaning the puzzle has been solved! Thank you in advance.

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        System.out.println("HI! This is a crossword puzzle using 2D arrays in JAVA!");
        System.out.println("There are 5 words in total, that you have to guess!");
        System.out.println("Each letter must be in place of the empty space. Each letter must be wrote in full caps!.");

        char[][] correctPuzzle = {{' ','—','—', '—', '—', '—', '—','—', '—', '—', ' '},
                                  {'|','C','|', 'H', '|', 'A', '|','R', '|', '■', '|'},
                                  {'|','—','—', '—', '—', '—', '—','—', '—', '—', '|'},
                                  {'|','L','|', 'O', '|', 'O', '|','P', '|', '■', '|'},
                                  {'|','—','—', '—', '—', '—', '—','—', '—', '—', '|'},
                                  {'|','A','|', '■', '|', '■', '|','■', '|', 'I', '|'},
                                  {'|','—','—', '—', '—', '—', '—','—', '—', '—', '|'},
                                  {'|','S','|', 'U', '|', 'M', '|','■', '|', 'N', '|'},
                                  {'|','—','—', '—', '—', '—', '—','—', '—', '—', '|'},
                                  {'|','S','|', '■', '|', '■', '|','■', '|', 'T', '|'},
                                  {' ','—','—', '—', '—', '—', '—','—', '—', '—', ' '},};

        char[][] defaultPuzzle = {{' ','—','—', '—', '—', '—', '—','—', '—', '—', ' '},
                {'|',' ','|', ' ', '|', ' ', '|',' ', '|', '■', '|'},
                {'|','—','—', '—', '—', '—', '—','—', '—', '—', '|'},
                {'|',' ','|', ' ', '|', ' ', '|',' ', '|', '■', '|'},
                {'|','—','—', '—', '—', '—', '—','—', '—', '—', '|'},
                {'|',' ','|', '■', '|', '■', '|','■', '|', ' ', '|'},
                {'|','—','—', '—', '—', '—', '—','—', '—', '—', '|'},
                {'|',' ','|', ' ', '|', ' ', '|','■', '|', ' ', '|'},
                {'|','—','—', '—', '—', '—', '—','—', '—', '—', '|'},
                {'|',' ','|', '■', '|', '■', '|','■', '|', ' ', '|'},
                {' ','—','—', '—', '—', '—', '—','—', '—', '—', ' '},};

        int option;
        do{
            for (int i = 0; i < 11; i++) {
                System.out.println();
                for (int j = 0; j < 11; j++) {
                    System.out.print(defaultPuzzle[i][j] + " ");
                }
            }
            System.out.println();
            System.out.println("Question 1 (1st column) What is blueprint for an object?");
            System.out.println("Question 2 (1st row) What data type represents a single character?");
            System.out.println("Question 3 (2nd row) Feature used to execute a particular part of the program repeatedly");
            System.out.println("Question 4 (4th row) Synonym for the word result - used in programming");
            System.out.println("Question 5 (5th column) Abbreviation of the word integer");
            System.out.println();
            System.out.println("Which question would you like to answer? ");
            Scanner sc = new Scanner(System.in);
            int ansNum = sc.nextInt();
            System.out.println("Input your guess! ");
            String guess;
            switch(ansNum) {
                case 1:
                    guess = sc.next();
                    if(guess.length() == 5) {
                        for(int rowNo = 1, charNo = 0; charNo < guess.length(); rowNo+=2, charNo++){
                            defaultPuzzle[rowNo][1] = guess.charAt(charNo);
                        }
                    }
                    else {
                        System.out.println("Incorrect answer length!");
                    }
                    break;

                case 2:
                    guess = sc.next();
                    if(guess.length() == 4) {
                        for(int collNo = 1, charNo = 0; charNo < guess.length(); collNo+=2, charNo++){
                            defaultPuzzle[1][collNo] = guess.charAt(charNo);
                        }
                    }
                    else {
                        System.out.println("Incorrect answer length!");
                    }
                    break;

                case 3:
                    guess = sc.next();
                    if(guess.length() == 4) {
                        for(int collNo = 1, charNo = 0; charNo < guess.length(); collNo+=2, charNo++){
                            defaultPuzzle[3][collNo] = guess.charAt(charNo);
                        }
                    }
                    else {
                        System.out.println("Incorrect answer length!");
                    }
                    break;

                case 4:
                guess = sc.next();
                if(guess.length() == 3) {
                    for(int collNo = 1, charNo = 0; charNo < guess.length(); collNo+=2, charNo++){
                        defaultPuzzle[7][collNo] = guess.charAt(charNo);
                    }
                }
                else {
                    System.out.println("Incorrect answer length!");
                }
                break;

                case 5:
                    guess = sc.next();
                    if(guess.length() == 3) {
                        for(int rowNo = 5, charNo = 0; charNo < guess.length(); rowNo+=2, charNo++){
                            defaultPuzzle[rowNo][9] = guess.charAt(charNo);
                        }
                    }
                    else {
                        System.out.println("Incorrect answer length!");
                    }
                    break;

                default:
                    System.out.println("This answer doesn't exist. Please select 1-5!!!");

            }


            System.out.println("If you want to continue and see your progress, press 1");
            System.out.println("Is you are struggling and want to see the correct answers, press 2");
            option = sc.nextInt();


        }
        while(option == 1);
        if(option == 2) {
        for (int i = 0; i < 11; i++) {
            System.out.println();
            for (int j = 0; j < 11; j++) {
                System.out.print(correctPuzzle[i][j] + " ");
            }
        }
            System.out.println("Better luck next time! These were the correct answers!");
        }
        else if (equals(correctPuzzle, defaultPuzzle)) {
            System.out.print("Congratulations! You have solved the  puzzle");
            for (int i = 0; i < 11; i++) {
                System.out.println();
                for (int j = 0; j < 11; j++) {
                    System.out.print(correctPuzzle[i][j] + " ");
                }
            }
        }

    }
public static boolean equals(char[][] a, char[][] b) {
        if (a.length != b.length)
            return false;
        for(char i = 0; i<a.length; i++) {
            if (a[i] != b[i])
                return false;
        }
        return true;
}
}

I have tried adding a 6th switch statement with a condition if the arrays match it will break, but it doesn't work. I have tried adding a condition to the while(option == 1 && correctPuzzle == defaultPuzzle), but that doesn't seem to work either. And lastly I have created a method to compare the arrays, and I'm not confident with this because it was a last resort effort.

1

There are 1 best solutions below

0
Luke Abruzese On

There are a lot of things I would suggest changing about the code, specifically moving things into lots of different methods outside of main to make your code a lot more readable and easier to debug.

But I believe that your error is in how you're checking array equality. I don't know how you were checking it before adding your own equals method but your equals method doesn't work.

By default doing myArray == myArray2 or myArray.equals(myArray2) will return a reference equals, meaning they check if they're the exact same array by object reference

You can use the Arrays library to match if you don't want write your own method: Arrays.deepEquals(myArray, myArray2) You can use this to check the entire 2D array.

For example: if you have a piece of paper (paper1) the the word "dog" on it and another one (paper2) with the word "dog" on it paper1 == paper2 will tell you they're not the same because they're 2 different pieces of paper but Arrays.deepEquals(paper, paper2) will tell you that the content contained on the paper are the same.

If you want to write your own method you're going to need a nested for loop to check each char in each char array in your 2D array:

    public static boolean equals(char[][] a, char[][] b){
        for(int row = 0; row < a.length; row++){
            for(int col = 0; col < a[row].length; col++){
                if(a[row][col] != b[row][col]) return false;
            }
        }
        return true;
    }