Enter the Sudoku you want to solve into the form on the left. Press "set puzzle" to submit the form and register your data. When you submit the form the boxes you enter will appear as bold black text.

When the Sudoku is filled in press "solve" and the algorithm will attempt to solve the puzzle. The algorithm will fill in the boxes it can, anything else will be left blank. The boxes filled in by the algorithm will appear as blue text.

If you need to correct an entry you made use the browsers back button to go back until the form appears and change it there. If you click "set puzzle" after you have solved the solved boxes will be treated as boxes you entered and will turn to bold black.

This solver keeps a 3-dimensional array with an entry for each box in the puzzle. Dimensions 1 and 2 correspond to the row and column of the grid. Dimension 3 uses key 0 for the solution for that box, and keys 1-9 for each of the 9 possible values. Keys 1-9 start off as 1, meaning that every value is possible. When a value is ruled out for a box that value is set to 0.

When the form is set the array is updated to make the values entered the solutions for the corresponding boxes, and those values are removed from the appropriate other boxes.

From here a simple loop is entered. The loop is repeated for each individual box, row, column and quadrant of nine:

  • Any values that are set are removed from all other boxes in which they could appear.
  • For each object we check if there is one and only one value which is available.
  • If there is a solution we fill it in, and remove that value from the other places where it can no longer be used.
  • The loop is repeated until all the boxes are either solved, or a maximum of 20 iterations. I have not encountered a puzzle that has taken more than 3 iterations to solve.

While this has correctly solved every Sudoku I've given it, I can not guarantee it will work for every solvable Sudoku.

This is implemented in a PHP class I call, appropriately, "Sudoku." The class takes in the two dimensional array of form data when it is constructed. To solve the puzzle you call public method solve().

After being solved, the object has two public variables - solutionArray contains the solved puzzle and initArray contains the initial form data, which I use to differentiate between the initial form entries and the solved squares for the purposes of display.

The code is up on my GitHub page, although it could definitely use some cleaning up.