Note that there are some explanatory texts on larger screens.

plurals
  1. POLogic Solving Algorithm (for Sudoku in Java)
    text
    copied!<p>I'm having issues with my logic solving algorithm. It solves puzzles with a large number of hints very well, it just has issues with puzzles that have less than 45 clues.</p> <p>This is the algorithm for solving. Immutable is a boolean that determines whether or not that value can be changed. cell[row][col].possibleValues is a LinkedList within a class called SudokuCell that stores the values that are possible for that grid element. grid.sGrid is the main int[][] array of the puzzle. removeFromCells() is a method that removes values from the row, column, and quadrant of the grid. That code is provided further down.</p> <p>The second for loop is just for checking for a single solution. I decided to avoid recursion because I really can't get my head around it. This method seems to be working well enough for now.</p> <pre><code>public boolean solve(){ for(int i = 0; i &lt; 81; i++){ for(int row = 0; row &lt; 9; row++){ for(int col = 0; col &lt; 9; col++){ if(!immutable[row][col]){ if(cell[row][col].getSize() == 1){ int value = cell[row][col].possibleValues.get(0); grid.sGrid[row][col] = value; immutable[row][col] = true; removeFromCells(row, col, value); } } } } } int i = 0; for(int row = 0; row &lt; 9; row++){ for(int col = 0; col &lt; 9; col++){ if(grid.sGrid[row][col] == 0){ i++; } } } if(i != 0){ return false; } else{ return true; } } </code></pre> <p>This is the code for removeFromCells()</p> <p>I think most of the code is pretty self-explanatory. The first for loop removes the value from the row and column of (x, y), and the second loop removes the value from the quadrant.</p> <pre><code>public void removeFromCells(int x, int y, int value){ /* * First thing to do, find the quadrant where cell[x][y] belong. */ int topLeftCornerRow = 3 * (x / 3) ; int topLeftCornerCol = 3 * (y / 3) ; /* * Remove the values from each row and column including the one * where the original value to be removed is. */ for(int i = 0; i &lt; 9; i++){ cell[i][y].removeValue(value); cell[x][i].removeValue(value); } for(int row = 0; row &lt; 3; row++){ for(int col = 0; col &lt; 3; col++){ cell[topLeftCornerRow + row][topLeftCornerCol + col].removeValue(value); } } } </code></pre> <p>Another problem spot could be where the possible values are constructed. This is the method I have for that:</p> <p>The first for loop creates new SudokuCells to avoid the dreaded null pointer exception.</p> <p>Any null values in sGrid are represented as 0, so the for loop skips those.</p> <p>The constructor for SudokuBoard calls this method so I know it's being called.</p> <pre><code>public void constructBoard(){ for(int row = 0; row &lt; 9; row++){ for(int col = 0; col &lt; 9; col++){ cell[row][col] = new SudokuCell(); } } immutable = new boolean[9][9]; for(int row = 0; row &lt; 9; row++){ for(int col = 0; col &lt; 9; col++){ immutable[row][col] = false; if(grid.sGrid[row][col] != 0){ removeFromCells(row, col, grid.sGrid[row][col]); immutable[row][col] = true; } } } } </code></pre> <p>I would post the entire file, but there are a lot of unnecessary methods in there. I posted what I think is causing my problems.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload