Question

So im having a bit of problem with my code.. It's suppose to cross check rows and columns for same integers.

this is what i have so far.. but when i run it, it only seems to check the first integer only. (for example the first line of the sudoku board reads. 1 2 2 2 2 2 2 2 2 2) it wont detect the obvious multiple 2's but if i change the input to 1 1 2 2 2 2 2 2 2 the error will come up of multiple 1's in this case. the multiple any suggestions to tweak my loops to make it go through the columns?

public static void validate(final int[][] sudokuBoard) {
    int width = sudokuBoard[0].length;
    int depth = sudokuBoard.length;

    for (int i = 0; i < width; i++) {
            int j = i;
            int reference = sudokuBoard[i][j];

            while (true) {
                if ((j >= width) || (j >= depth)) {
                    break;
                } 
                else if (i == j){
                    // do nothing
                }
                else if (j < width) {
                    int current = sudokuBoard[i][j];

                    if (current == reference) {
                        System.out.print("Invalid entry found (width)" + "\n");
                        System.out.print(current + "\n");


                        // invalid entry found do something
                    }
                } else if (j < depth) {
                    // note reversed indexes
                    int current = sudokuBoard[j][i];

                    if (current == reference) {
                        System.out.print("Invalid entry found (depth)" + "\n");
                        System.out.print(current + "\n");

                        // invalid entry found do something
                    }
                }
                j++;
            }
Was it helpful?

Solution

Your code is more complex than it should be. Why put everything in one single function when you could split in several different functions?

public static void Validate(final int[][] sudokuBoard)
{
    int width = sudokuBoard[0].length;
    int depth = sudokuBoard.length;

    for(int i = 0; i < width; i++)
        if(!IsValidRow(sudokuBoard, i, width))
        {
          //Do something - The row has repetitions
        }
    for(int j = 0; j < height; j++)
        if(!IsValidColumn(sudokuBoard, j, width))
        {
          //Do something - The columns has repetitions
        }
}

static bool 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;
}

static bool IsValidColumn(int[][] sudokuBoard, int referenceColumn, int height)
{
    //Compare each value in the column to each other
    for(int i = 0; i < height; i++)
    {
        for(int j = i + 1; j < height; j++)
        {
            if(sudokuBoard[i][referenceColumn] == sudokuBoard[j][referenceColumn])
                return false
        }
    }
    return true;
}

That way, your code is much more easily maintainable/readable. This code above hasn't been tested, but it should be about right.

I suggest debugging this code step by step to really understand what's going on, if that's not clear for you.

OTHER TIPS

Given the constraints of sudoku (a row of n cells must contain the numbers 1-n only) you don't need an order n^2 search (per row or column), you can do it order n by keeping a bit array indicating which numbers you've seen. Here's the pseudo-code for checking rows, do the same for columns:

for i in 0 to depth-1 // rows
  boolean seen[] = new seen[width];
  for j in 0 to width-1 // columns
    if seen[board[i][j]-1] == true
      duplicate number
    else
      seen[board[i][j]-1] = true

I would break the functionality into smaller boolean checks. This way, you can validate row by row, column by column, and square by square. For instance

private boolean isValidRow(int[] row) {
    // Code here to check for valid row (ie, check for duplicate numbers)
}

private boolean isValidColumn(int[] column) {
    // Code here to check for valid column
}

private boolean isValidSquare(int[][] square) {
    // Code here to check for valid square
}

Note that rows and columns only need to be passed a 1 dimensional array. Squares are a 2 dimensional array as you need to check a 3x3 area. You can also treat these methods as static as their functionality is independent of the Sudoku board instance.

Edit: A suggestion on row/column/square validation is to use a HashSet. Sets can only have 1 element of a certain value, so you can add elements and look for a failure. For example:

HashSet<Integer> hs = new HashSet<Integer>();
for(int i = 0; i < 9; i++) {
    if(!hs.add(integerArray[i])) // HashSet.add returns 'false' if the add fails 
                                 // (ie, if the element exists)
        return false;
}
return true;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top