You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You can generate a sudoku using the getSudoku command, passing an optional difficulty level:
import{getSudoku}from'sudoku-gen';// Get a sudoku of specific difficulty (easy, medium, hard, expert)constsudoku=getSudoku('easy');// Get a sudoku of random difficultyconstsudoku=getSudoku();
puzzle and solution are both 81 character strings representing the 81 cells in a sudoku grid:
puzzle contains placeholder dashes (-) for spaces which need to be filled in by the player. solution contains the entire grid so you can check the player's progress. The example code above would map to the following puzzle/solution:
How it works
Most sudoku generators start with a completed sudoku grid and remove numbers one at a time, using a backtracking algorithm to stop once the puzzle becomes unsolvable. This process is too slow to be performed in real-time, so usually requires a background task and database for generating and storing puzzles as they're created.
SudokuGen works differently. It starts with a known, solvable "seed" puzzle and performs various transformations to turn it into a brand new puzzle. This makes it extremely fast, with no requirement for a back end, whilst maintaining quality.
Each seed gives over 2.4 trillion unique puzzles. To put that in context, if you played sudoku 24/7 and took 3 minutes to solve each puzzle, it would take until your 13,915,534th birthday to exhaust a single seed 🎂
Transformations
The following transformations are used ("!" = factorial):