数独解算器抛出 ArrayIndexOutOfBoundsException

Sudoku Solver throwing ArrayIndexOutOfBoundsException

我的数独解算器将“-”替换为零,然后解出谜题。它适用于我尝试过的大多数谜题,但对于带有整行破折号的谜题会抛出 ArrayIndexOutOfBoundsException。我试过调整不同的东西来让它工作,但我有点迷路了。

This is what the puzzle looks like.

public static int[][] theArray = new int [9][9];
public static int SIZE = 9;

private static boolean isCompletePuzzle() {
    // checks for 0 in rows/cols
    for (int i = 0; i <= SIZE; i++) {
        for (int j = 0; j <= SIZE; j++) {
            if (theArray[i][j] != 0) {
                return true;
            }
        }
    }
    return false;
}

private static boolean isValidPuzzle(int row, int col, int number) {
    // checks rows
    for (int i = 0; i < SIZE; i++) {
        if (theArray[row][i] == number) {
            return true;
        }
    }

    // checks columns
    for (int i = 0; i < SIZE; i++) {
        if (theArray[i][col] == number) {
            return true;
        }
    }

    // checks 3x3
    int r = row - row % 3;
    int c = col - col % 3;

    for (int i = r; i < r + 3; i++)
        for (int j = c; j < c + 3; j++)
            if (theArray[i][j] == number)
                return true;

    return false;
}

private static boolean isSolvedPuzzle(int row, int col, int number) {

    if (isValidPuzzle(row, col, number) == true && isCompletePuzzle() == true) {
        return true;
    }
    return false;
}

public static boolean solvePuzzle() {
    for (int row = 0; row < SIZE; row++) {
        for (int col = 0; col < SIZE; col++) {

            if (theArray[row][col] == 0) {

                for (int number = 1; number <= SIZE; number++) {
                    if (!isSolvedPuzzle(row, col, number)) {

                        theArray[row][col] = number;
                        if (solvePuzzle()) { 
                            return true;
                        } 

                        else {
                         theArray[row][col] = 0;
                        }
                    }
                }
                return false; 
            }
        }
    }
    return true; 
}

在你的 isCompletePuzzle() 函数你的循环条件 i <= SIZEj <= SIZE 导致 ArrayIndexOutOfBoundsException

当我是 9 时 if (theArray[i][j] != 0) 抛出 ArrayIndexOutOfBoundsException