Question

Hello Im working on a Sudoku Checker which verifies the completed board's solution is correct. I'm currently stumped on how to check within the blocks. right now I have a boolean as follows where Im checking the upper left block (Block1). What I'm unsure about is what parameters to set it to and how to run the two for loops successfully.

The problem is that I want to check a section of a 2d array condensed too a 3x3 square and see if the integers in that area are not repeated that only 1-9 appear once. I have similar code in which i made this code that checks to see if a row has repeating integers and a column.

static boolean isBlock1Valid(int[][] sudokuBoard, int referenceRow, int referenceColumn)
{

    for(int i =0; i<2;i++){
        for(int j=0; j<2; j++){
            if(sudokuBoard[i][j]==sudokuBoard[i][j])
                return false;
        }
    }
    return true;
}//end of isBlock1Valid

here is the row checker I used as reference to make the block checker

       static boolean IsValidRow(int[][] sudokuBoard, int referenceRow, int width)
{
    //Compare each value in the row to each other
    for(int i = 0; i < width; i++)
    {
        for(int j = i+1; j < width; j++)
        {

            if(sudokuBoard[referenceRow][i] == sudokuBoard[referenceRow][j])
                return false;

        }
    }

    return true;
}
Was it helpful?

Solution

I'm not that quite sure what do you want you given code to do. But this method will always return false.

If you want make sure that there is only one instance of each element in one block. Then one soltuion would be to have a kind of checklist:

boolean[] seen = new boolean[9];

for (int i = 0; i < 3; i++)
    for (int j = 0; j < 3; j++)
        if (seen(sudokuBoard[referenceColumn+i][referenceRow+j])) return false;
        else seen[sudokuBoard[referenceColumn+i][referenceRow+j]) = true;
return true;

OTHER TIPS

As this is homework, just a hint. Modulo 3 (i % 3) and integer division by 3 (i / 3) can be used to split coords 0 .. 8 into 3 parts and index in that part.

In your IsValidRow method you have used two nested loops. The first one loops over the whole row and the second one loops over the rest of the row to check if the value repeats. In your isBlock1Valid method you also use two nested loops. But now you use the outer loop for the y coordinate and the inner loop for the x coordinate. Together they do what your first loop in IsValidRow does. Looping over the whole group of cells. You still have to implement the second loop which loops over the rest of the box. Unfortunately this isn't easy with nested loops. But just like Joop Eggen said, you can use modulo and integer division to get a different view on your coords. For example you could say j % 3 + (i % 3) * 3 is the index of (j, i) within the box. This way you would only need two loops, just as in IsValidRow:

static boolean isBlock1Valid(int[][] sudokuBoard)
{
    for(int i = 0; i < 9; i++)
    {
        for(int j = i+1; j < 9; j++)
        {

            if(sudokuBoard[i/3][i%3] == sudokuBoard[j/3][j%3])
                return false;

        }
    }
}

Just sum them; if 45 then good, else false — assuming you already checked the rows and columns, of course. Also, it is better to keep a separate grid where you denote each region by a unique digit, then you can do lots of other nice stuff, looping over it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top