sudoku-api v1.0.0
sudoku-api
A Sudoku Web API that allows you to generate, validate and solve a 9x9 Sudoku grid.
npm i sudoku-api
npm start
What can this API do
- Generate random valid Sudoku puzzles
- Solve a Sudoku puzzle
- Check a Sudoku puzzle to see if it's valid or not
Getting Started
Using the npm repository
npm i sudoku-api
npm start
- Navigate to
http:\\localhost\api\sudoku\generate
to generate a new Sudoku puzzle - For more info, view the API documentation
Clone from the GitHub repository
- Clone this project
git clone https://github.com/wiseindy/sudoku-api
- If not already installed, install Node.js from
https://nodejs.org/en/download/
. - Run
npm install
in the root of the project. - Run
npm run start
- Open your favorite browser and navigate to
http://localhost:3000/api/sudoku/generate
Algorithm:
This application uses the backtracking algorithm to find a solution to the Sudoku puzzle. 1. Find an empty location in the grid 1.1 if no empty location is found, board is solved. 1.2. if empty location is found, continue to step 2 2. pick a number from the array (1 to 9) 3. loop through these numbers one by one 3.1. check if this number is valid in this empty location. 3.2. if yes, store this number in this position in the grid 3.2.1 RECURSION call this function again which repeats steps 1 to 3 until all locations are filled 3.3. if no, continue the loop and repeat steps 3.1 to 3.3 3.4. if no number is valid at this location, it's time to backtrack. 4. Backtrack to the previous position and find the next suitable number for that position. 5. Repeat steps 1 to 5 until board is solved or all combinations are exhausted.
Note:
The default port for the API is 3000
. To change this port, set the PORT
environment variable on your system before running npm run start
.
Mac/Linux: export PORT=8000
Windows:
- CMD:
set PORT=8000
- PowerShell:
$env:PORT=8000