JavaScript Sudoku Backtracking Puzzle Solver

I have discussed using a backtracking algorithm to solve sudoku puzzles multiple times in previous posts. My first sudoku backtracking puzzle solver was programmed in Python with a Tkinter user interface. This worked well except the UI required the user to click a cell and enter a puzzle value into a popup window. I then implemented the same algorithm using an Excel VBA. This time the user entered each puzzle value directly into workbook cells but was about 10 times slower than the Python version. In this version I am using Javascript and a table within a web (this) page.

As a quick review the backtracking algorithm is a method for solving problems recursively by testing incremental solutions. If the solutions fails, then you “backtrack” the solution and attempt another solution. To solve a sudoku puzzle using the backtracking algorithm you place a value in the first empty puzzle square and test if that solution is valid (unique row, column, and 3×3 square value). If the value is not valid, then the next value is tried. If the test solution is valid, then the next empty square is filled with a value and tested until a valid solution is found. If you have tried all values without a valid solution, then you move back one “completed” square and try the next value. This continues until all empty squares have a value that meet the sudoku puzzle requirements.

The conversion from Python and Excel VBA to Javascript was straight forward. The only item I haven’t resolved is showing the solution progress live as with the Python and Excel VBA versions. This slight issue has a positive effect where the puzzle is solved more quickly without any UI updates. Also no fancy web table formatting was done.

Below is a blank sudoku puzzle. Click any table cell and place the known puzzle value. Once you have entered all the know puzzle values click “Solve Puzzle” to see the solution. A solution time is shown with the solved puzzle. You can clear the puzzle and enter new values by clicking “Clear Puzzle.” Enjoy!

Sudoku Puzzle Solver

Enter your puzzle values then click “Solve Puzzle”