开发者

Java- Help with writing sudoko

I'm trying to implement a Sudoko in java.

(there a Gui class that prints the matrix).

Here's an example for 2 methods that I wrote regarding the rows of the matrix(9X9).

When there's "-1" in a cell the method puts there a number that does not appear in the spesific row and if there is already a number it is update a boolean array to "true" in the place of the index that equales to the number (number can be between 1-9).

It stops after 4 loops, and I'm having a really hard time to realize why.

Can someone see what is wrong? Maybe a mistake of understanding something?

public static boolean checkRow(int row, int[][] matr) {
    boolean[] ind = new boolean[9];
    Arrays.fill(ind, false);
    for (int j = 0; j <= 8; j++) {
        if (matr[row][j] == -1) {

            int n = whatToputRow(row, matr);

            if (n == 0) {
                return false;
            } else {
                matr[row][j] = n;
                ind[n - 1] = true;
                System.out.print(n+" ");
            }
        } else {
            ind[matr[row][j] - 1] = true;
        }
    }

    return checkBoolean(ind);
}



    public static boolean checkBoolean(boolean[] mat) {
    for (int i = 0; i <= 8; i++) {
        if (!mat[i]) {开发者_如何学C

            return false;
        }
    }
    return true;
}


    public static int whatToputRow(int row, int[][] mat) {

    for (int number = 1; number <= 9; number++) {
        boolean bool = false;
        int i = 0;
        while (!bool && i <= 8) {
            if (mat[row][i] == number) {
                bool = true;
            } else {
                i = i + 1;
            }
        }
        if (bool == false)
            return number;

    }
    return 0;
}


I suspect that whatToPutRow() returns 0 after the 4th iteration. As you are returning false on that condition the loop will stop running.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜