Logic Solving Algorithm for Sudoku (Java)
18:10 25 Feb 2011

I decided to write a logic solving algorithm for my Sudoku application. What I wrote works for a limited amount of grid values, but then the recursion stops way too soon.

What my methods do: addToThirdDimension(): A three dimensional array stores any possible values that can be put into the grid value at logicGrid[x][y]. This method refreshes the three dimensional array. It does this by testing values 1-9 in every grid index, and if it's valid, it adds that number to the array. If not, it sets that value to zero.

checkValues(): Checks how many possibilities are left in the three dimensional grid. It goes through the logicGrid and returns the number of non-zero values are in the grid.

checkSingleValue(int row, int col): Checks logicGrid[row][col] to see if there is one and only one value left in there (If there is one value left, it is the only possibility for the grid element at [row, col]). It returns the amount of non-zero values that are in that grid location.

getSingleValue(int row, int col): Returns the single number that's left in logicGrid[row][col]

immutableValues: A two dimensional boolean array that stores whether or not a specific grid element is immutable or not. If it is immutable, the solve method should not touch it.

public boolean solveWithLogic(){

    addToThirdDimension();
    if(checkValues() == 0){
        return true;
    }

    for(int row = 0; row < 9; row++){
        for(int col = 0; col < 9; col++){
            if(!immutableValues[row][col]){
                if(checkSingleValue(row, col) == 1){
                    sGrid[row][col] = getSingleValue(row, col);
                    setValues[row][col] = true;
                    addToThirdDimension();
                }
            }
        }
    }

    if(checkValues() != 0){
        solveWithLogic();
    } else{
        return true;
    }

    return false;
}

I cannot see where I am going wrong. After a certain number of tries, checkValues returns 0 even though there should be more possibilities. Here is the code for addToThirdDimension() as I am sure that if something is wrong, it is here.

sGrid is the main two-dimensional integer array that stores the values for the puzzle.

public void addToThirdDimension(){

    logicGrid = new int[9][9][9];

    for(int x = 0; x < 9; x++){
        for(int y = 0; y < 9; y++){
            for(int z = 0; z < 9; z++){
                logicGrid[x][y][z] = z + 1;
            }
        }
    }

    int[][] temp1 = sGrid;

    for(int row = 0; row < 9; row++){
        for(int col = 0; col < 9; col++){
            if(setValues[row][col]){
                for(int i = 0; i < 9; i++){
                    logicGrid[row][col][i] = 0;
                }
            } else{
                for(int i = 1; i <= 9; i++){
                    temp1[row][col] = i;
                    if(!isColumnValid(col, temp1) && !isRowValid(row, temp1) &&
                            !isQuadrantValid(row, col, temp1){
                        logicGrid[row][col][i-1] = 0;
                    }
                }
            }
            temp1[row][col] = sGrid[row][col];
        }
    }
}

The code isn't too efficient at the moment. I want to get it working before I start minimizing solve times.

java algorithm recursion