0

I'm knew with multi-dimensional arrays and I kind of know the basics but I'm stuck on combining them into a boolean method. In this case, I'm trying to find out how I can make this method simplified which will function as:

  • Comparing one array with a location (row:column) to another array (with the same location).
  • If all locations from both arrays match, it should return true.

The method below works with what I have for my code but I want to know the proper way on how to not "hard code" the numbers single handedly. Would I need to use a nested for loop for comparing both arrays?

Thanks for the assistance~

public static boolean gameIsWon(int[][] workingPuzzle, int[][] solvedPuzzle)
{
    if (workingPuzzle[0][0] == solvedPuzzle[0][0] &&
            workingPuzzle[0][1] == solvedPuzzle[0][1] &&
            workingPuzzle[0][2] == solvedPuzzle[0][2] &&
            workingPuzzle[0][3] == solvedPuzzle[0][3] &&
            workingPuzzle[0][4] == solvedPuzzle[0][4] &&
            workingPuzzle[0][5] == solvedPuzzle[0][5] &&
            workingPuzzle[0][6] == solvedPuzzle[0][6] &&
            workingPuzzle[0][7] == solvedPuzzle[0][7] &&
            workingPuzzle[0][8] == solvedPuzzle[0][8] &&
            workingPuzzle[1][0] == solvedPuzzle[1][0] &&
            workingPuzzle[1][1] == solvedPuzzle[1][1] &&
            workingPuzzle[1][2] == solvedPuzzle[1][2] &&
            workingPuzzle[1][3] == solvedPuzzle[1][3] &&
            workingPuzzle[1][4] == solvedPuzzle[1][4] &&
            workingPuzzle[1][5] == solvedPuzzle[1][5] &&
            workingPuzzle[1][6] == solvedPuzzle[1][6] &&
            workingPuzzle[1][7] == solvedPuzzle[1][7] &&
            workingPuzzle[1][8] == solvedPuzzle[1][8] &&
            workingPuzzle[2][0] == solvedPuzzle[2][0] &&
            workingPuzzle[2][1] == solvedPuzzle[2][1] &&
            workingPuzzle[2][2] == solvedPuzzle[2][2] &&
            workingPuzzle[2][3] == solvedPuzzle[2][3] &&
            workingPuzzle[2][4] == solvedPuzzle[2][4] &&
            workingPuzzle[2][5] == solvedPuzzle[2][5] &&
            workingPuzzle[2][6] == solvedPuzzle[2][6] &&
            workingPuzzle[2][7] == solvedPuzzle[2][7] &&
            workingPuzzle[2][8] == solvedPuzzle[2][8] &&
            workingPuzzle[3][0] == solvedPuzzle[3][0] &&
            workingPuzzle[3][1] == solvedPuzzle[3][1] &&
            workingPuzzle[3][2] == solvedPuzzle[3][2] &&
            workingPuzzle[3][3] == solvedPuzzle[3][3] &&
            workingPuzzle[3][4] == solvedPuzzle[3][4] &&
            workingPuzzle[3][5] == solvedPuzzle[3][5] &&
            workingPuzzle[3][6] == solvedPuzzle[3][6] &&
            workingPuzzle[3][7] == solvedPuzzle[3][7] &&
            workingPuzzle[3][8] == solvedPuzzle[3][8] &&
            workingPuzzle[4][0] == solvedPuzzle[4][0] &&
            workingPuzzle[4][1] == solvedPuzzle[4][1] &&
            workingPuzzle[4][2] == solvedPuzzle[4][2] &&
            workingPuzzle[4][3] == solvedPuzzle[4][3] &&
            workingPuzzle[4][4] == solvedPuzzle[4][4] &&
            workingPuzzle[4][5] == solvedPuzzle[4][5] &&
            workingPuzzle[4][6] == solvedPuzzle[4][6] &&
            workingPuzzle[4][7] == solvedPuzzle[4][7] &&
            workingPuzzle[4][8] == solvedPuzzle[4][8] &&
            workingPuzzle[5][0] == solvedPuzzle[5][0] &&
            workingPuzzle[5][1] == solvedPuzzle[5][1] &&
            workingPuzzle[5][2] == solvedPuzzle[5][2] &&
            workingPuzzle[5][3] == solvedPuzzle[5][3] &&
            workingPuzzle[5][4] == solvedPuzzle[5][4] &&
            workingPuzzle[5][5] == solvedPuzzle[5][5] &&
            workingPuzzle[5][6] == solvedPuzzle[5][6] &&
            workingPuzzle[5][7] == solvedPuzzle[5][7] &&
            workingPuzzle[5][8] == solvedPuzzle[5][8] &&
            workingPuzzle[6][0] == solvedPuzzle[6][0] &&
            workingPuzzle[6][1] == solvedPuzzle[6][1] &&
            workingPuzzle[6][2] == solvedPuzzle[6][2] &&
            workingPuzzle[6][3] == solvedPuzzle[6][3] &&
            workingPuzzle[6][4] == solvedPuzzle[6][4] &&
            workingPuzzle[6][5] == solvedPuzzle[6][5] &&
            workingPuzzle[6][6] == solvedPuzzle[6][6] &&
            workingPuzzle[6][7] == solvedPuzzle[6][7] &&
            workingPuzzle[6][8] == solvedPuzzle[6][8] &&
            workingPuzzle[7][0] == solvedPuzzle[7][0] &&
            workingPuzzle[7][1] == solvedPuzzle[7][1] &&
            workingPuzzle[7][2] == solvedPuzzle[7][2] &&
            workingPuzzle[7][3] == solvedPuzzle[7][3] &&
            workingPuzzle[7][4] == solvedPuzzle[7][4] &&
            workingPuzzle[7][5] == solvedPuzzle[7][5] &&
            workingPuzzle[7][6] == solvedPuzzle[7][6] &&
            workingPuzzle[7][7] == solvedPuzzle[7][7] &&
            workingPuzzle[7][8] == solvedPuzzle[7][8] &&
            workingPuzzle[8][0] == solvedPuzzle[8][0] &&
            workingPuzzle[8][1] == solvedPuzzle[8][1] &&
            workingPuzzle[8][2] == solvedPuzzle[8][2] &&
            workingPuzzle[8][3] == solvedPuzzle[8][3] &&
            workingPuzzle[8][4] == solvedPuzzle[8][4] &&
            workingPuzzle[8][5] == solvedPuzzle[8][5] &&
            workingPuzzle[8][6] == solvedPuzzle[8][6] &&
            workingPuzzle[8][7] == solvedPuzzle[8][7] &&
            workingPuzzle[8][8] == solvedPuzzle[8][8]
        )
        return true;
    else
        return false;
}
1
  • 1
    Nested for loops... Commented Nov 17, 2016 at 3:13

2 Answers 2

3

You need to learn about loops.

And about storing a two-dimensional matrix in a one-dimensional array using index mapping (which is easier to handle than a two-dimensional array).

But in this particular case:

return java.util.Arrays.deepEquals(workingPuzzle, solvedPuzzle);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this worked, although I have yet covered on this. I will look into this further.
1

Use nested for loops based on the array lengths.

 public static boolean gameIsWon(int[][] workingPuzzle, int[][] solvedPuzzle) {
  if(workingPuzzle.length != solvedPuzzle.length) {
      return false; 
  }
  for(int i = 0; i < workingPuzzle.length; i++) {
    if(workingPuzzle[i].length != solvedPuzzle[i].length) {
      return false;
    }
    for(int j = 0; j < workingPuzzle[i].length; j++) {
      if(workingPuzzle[i][j] != solvedPuzzle[i][j]) {
          return false; 
      }
    }
  }
  return true;
}

4 Comments

Isn't that what the first line of the for loop is checking?
The rows are checked by the if statement outside the for loop and each "row" is check inside the for loop (the first statement inside the for loop). Am I missing something?
Thanks, this worked. It helps to see what I need to structure.
Thanks both for the help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.