Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Validate the puzzle like this:</p> <ol> <li>Create a boolean array of 9 elements.</li> <li>Loop through every row, column and 9x9 box. <ul> <li>If you read a number, set the corresponding value in the array to true.</li> <li>If it is already true throw an error (impossible puzzle).</li> <li>After reading a row, column or 9x9 box reset the boolean array.</li> </ul></li> <li>Then, if the validation succeeded call the solving method.</li> </ol> <p>EDIT: Source code</p> <pre><code>public boolean checkPuzzle() { boolean[] nums = new boolean[9]; for (int row = 0; row &lt; panel.puzzleSize; row++) { for (int cell = 0; cell &lt; panel.puzzleSize; cell++) { if (nums[puzzle[row][cell]]) return false; nums[puzzle[row][cell]] = true; } nums = new boolean[9]; } for (int col = 0; col &lt; panel.puzzleSize; col++) { for (int cell = 0; cell &lt; panel.puzzleSize; cell++) { if (nums[puzzle[cell][col]]) return false; nums[puzzle[cell][col]] = true; } nums = new boolean[9]; } for (int square = 0; square &lt; panel.puzzleSize; square++) { int squareCol = panel.squareSize * (square % panel.squareSize); int squareRow = panel.squareSize * Math.floor(square / panel.squareSize); for (int cell = 0; cell &lt; panel.puzzleSize; cell++) { int col = cell % panel.squareSize; int row = Math.floor(cell / panel.squareSize); if (nums[puzzle[squareCol + col][squareRow + row]]) return false; nums[puzzle[squareCol + col][squareRow + row]] = true; } nums = new boolean[9]; } return true; } </code></pre> <p>Didn't have too much time to test out, but it might work (?). The row/col variable namings might be incorrect, because I didn't have time to find that in your code, but it shouldn't matter for it to work or not.</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