SudokuSolver is a web application built using Next.js and TypeScript that can solve any given Sudoku puzzle using a backtracking algorithm. The app provides a user-friendly interface where users can input their Sudoku puzzle and let the algorithm solve it.
Explore the live version of the app here: SudokuSolver
Full link: https://shaheenjawadi.github.io/SudokuSolver/
The algorithm solves the Sudoku puzzle using the backtracking approach, which involves:
- Finding the next empty cell: The solver searches for an empty cell (denoted by 0).
- Trying candidates: For each empty cell, it tries numbers from 1 to 9 and checks if the number can be placed based on Sudoku's rules (row, column, and 3x3 block constraints).
- Backtracking: If a number doesn't lead to a valid solution, the algorithm backtracks and tries the next candidate.
- Recursion: The process is repeated recursively for all empty cells until the puzzle is solved or determined unsolvable.
Here is an overview of the main functions used in the algorithm:
-
canPlaceNumber
:Checks if a candidate number can be placed at a given row and column by validating the row, column, and 3x3 block constraints.- Input: Sudoku grid, row, column, candidate number.
- Output: true if the number can be placed, otherwise false.
-
findNextEmptyCell
: Finds the next empty cell in the grid (a cell with value0
).- Input: Sudoku grid.
- Output: Coordinates of the next empty cell or null if no empty cells are found.
-
solvePuzzle
: Solves the Sudoku puzzle using backtracking. It fills the grid by recursively trying numbers and backtracking when necessary.- Input: Sudoku grid.
- Output:
true
if the puzzle is solved, otherwisefalse
.
Follow these steps to run SudokuSolver locally on your machine:
- Clone the repository:
git clone https://github.com/ShaheenJawadi/SudokuSolver.git
- Install dependencies:
cd SudokuSolver
npm install
or
cd SudokuSolver
yarn
- Start the development server:
npm run dev
or
yarn dev
- Open your browser and navigate to http://localhost:3000 to interact with the Sudoku solver.
- Input a Sudoku puzzle in the grid .
- Click the "Solve" button to let the algorithm solve the puzzle.
We welcome contributions to enhance SudokuSolver. Feel free to:
- Fork the repository.
- Create a new branch for your feature or bug fix.
- Make your changes and submit a pull request.
This project is licensed under the MIT License
.