" How I Built a Sudoku Solver Using Backtracking from Scratch (Complete with UI) "

Solving Sudoku manually is fun โ€” but building a Sudoku solver from scratch in code? Thatโ€™s where the real magic begins. In this blog, Iโ€™ll walk you through how I built an interactive 9x9 Sudoku game with a built-in solver using JavaScript, HTML/CSS, and the classic backtracking algorithm. You can try it, play with it, and learn how the logic works behind the scenes!


๐Ÿ” What is Backtracking?

Backtracking is a problem-solving algorithm that incrementally builds candidates to the solutions and abandons a candidate ("backtracks") as soon as it determines that the candidate cannot possibly be completed to a valid solution.

In Sudoku, this means:

  • Try placing a number.

  • If it leads to a conflict, undo it and try the next possibility.

  • Repeat until the entire board is filled correctly.

Think of it like trying keys in a lock until one fits โ€” but efficiently skipping keys that are clearly wrong.


๐Ÿงฉ Why Backtracking is Perfect for Sudoku

Sudoku is a constraint satisfaction problem, where:

  • Each number 1โ€“9 must appear once in every row, column, and 3x3 subgrid.

Backtracking helps explore the possible number placements for each empty cell while ensuring constraints are satisfied. If a dead-end is reached, the algorithm backtracks and tries another path โ€” perfect!


๐Ÿ’ป Technologies Used

  • HTML & CSS: For layout and styling.

  • JavaScript (jQuery): For logic, board rendering, input handling, validation, and the backtracking solver.


๐Ÿง  The Logic Behind My Sudoku Solver

Hereโ€™s a simplified view of the key functionality:

๐Ÿ” 1. Finding the Next Empty Cell

findClosestEmptySquare: function (row, col) {
    for (let i = col + 9 * row; i < 81; i++) {
        let walkingRow = Math.floor(i / 9);
        let walkingCol = i % 9;
        if (this.matrix.row[walkingRow][walkingCol] === '') {
            return this.$cellMatrix[walkingRow][walkingCol];
        }
    }
    return null;
}
findLegalValuesForSquare: function (row, col) {
    let legalNums = [1, 2, 3, 4, 5, 6, 7, 8, 9];
    // Eliminate numbers already present in row, col, or 3x3 box
}

๐Ÿงฎ 3. Recursive Solver with Backtracking

solveGame: function (row, col) {
    this.recursionCounter++;
    let next = this.findClosestEmptySquare(row, col);
    if (!next) return true; // Solved!

    let legalValues = this.findLegalValuesForSquare(...);
    for (let val of legalValues) {
        // Try value
        if (this.solveGame(...)) return true;
        // Backtrack if it doesnโ€™t work
    }
    return false;
}

๐ŸŽฎ The Interface: Intuitive and Clean

Users can:

  • Input values manually.

  • Click "Validate Board" to check correctness.

  • Click "Solve" to auto-complete the puzzle.

  • Click "Reset" to clear the board.

The UI dynamically highlights invalid entries and disables input after solving. It even logs the number of recursive calls and time taken!

Screenshot of the Game


โœจ Styling Highlights (CSS)

  • Dark gradient theme with neon accents.

  • Responsive layout โ€” mobile-friendly.

  • Clean separation of 3x3 subgrids using color contrast.

Example:

.sudoku-menu button {
    background: linear-gradient(to right, #ff416c, #ff4b2b);
    border-radius: 8px;
    font-weight: bold;
}

โš ๏ธ Challenges I Faced

  • Managing and updating 3 different validation matrices (rows, columns, subgrids).

  • Handling user inputs and real-time validation.

  • Avoiding global state issues with multiple solver calls.

  • Making the UI intuitive and interactive while keeping it responsive.


๐Ÿ“š What I Learned

  • Deepened my understanding of recursion and constraint-based search.

  • Gained hands-on experience in modular JavaScript and UI interaction.

  • Realized how powerful simple algorithms can be when combined with good UX.


๐Ÿ”— Live Demo + Source Code

๐Ÿ”— GitHub: https://github.com/Sushil2k4

Play Game ๐ŸŽฎ

Feel free to fork it, customize it, and build on top of it!


๐Ÿ™Œ Final Thoughts

This project started as a DSA exercise but turned into a fully functioning game with UI polish and robust logic. If you're learning algorithms, I highly recommend building a Sudoku solver. Itโ€™s a great way to reinforce:

  • Backtracking

  • Constraint handling

  • DOM manipulation

And the best part? Itโ€™s really fun.

Want to try the Sudoku Solver? Drop me a star โญ on GitHub and feel free to reach out on LinkedIn if you have any questions !


"Stay curious, keep learning, and let your code make a difference!"

~ Sushil Kumar Mishra

1
Subscribe to my newsletter

Read articles from Sushil Kumar Mishra directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Sushil Kumar Mishra
Sushil Kumar Mishra