sudoku-solver-engine
v1.0.3
Published
Sudoku solver engine
Downloads
4
Maintainers
Readme
Features
- Solve any valid sudoku.
- Detect invalid sudokus.
- 5 preset of sudokus.
- Logs are directly connected to the program status.
How to use this ?
installation:
cd into your project and execute:
npm i sudoku-solver-engine
//or with yarn
yarn add sudoku-solver-engine
Intro:
This is a node_module that can solve a sudoku step by step and produce a full log of how the code solve the problem.
This package is meant yo be use both server-side and client-side. (files are builded with babel)
But if you are using this client-side, make sure to use a front-end framework ( angular / react / vue / etc ) to take advantage of the webpack config.
The best aproach for a "sudoku solver app" would be to put the engine in the general store manager (redux / vueX)
But you can import the diffrent objects in on component and do all the work in the component.
Classes:
sudoku data:
import {grid, gridClear, grid3, grid4, grid5, grid6} from 'sudoku-solver-engine'
This is all the available sudokus. Those grid are 9x9 matices. gridClear is an empty 9x9 matrix. grid2 is not available beacause it's a damned sudoku...
Sudoku class:
This class is meant perform get / set / display / test of a sudoku.
import {Sudoku} from 'sudoku-solver-engine'
Creation:
const mySudoku = new Sudoku(grid) // use any grid that you want
ToolBox class:
This is just a set of helper. (I will explain why in the next section)
import {ToolBox} from 'sudoku-solver-engine'
Engine class:
This class is meant to solve a sudoku
import {Engine} from 'sudoku-solver-engine'
Creation:
myEngine = new Engine(mySudoku, myTooBox)
I know, this: new Engine(mySudoku, myTooBox) is not efficient. But this code was produce to explain to my students the concept of oop
Example:
- create a new directory.
- cd inside
- execute yarn init / npm init
- install the sudoku-solver-engine
- create a testSudoku.js file and write inside:
const {Engine, ToolBox, Sudoku, grid} = require('sudoku-solver-engine')
// or in a front-end framework: import {Engine, ToolBox, Sudoku, grid} from 'sudoku-solver-engine'
const myTooBox = new ToolBox()
const mySudoku = new Sudoku(grid)
mySudoku.displayGrid("Initial sudoku")
const myEngine = new Engine(mySudoku, myTooBox)
myEngine.solveAll()
console.log("testSolveAll")
console.log(myEngine.logs.length)
console.log(myEngine.logs)
mySudoku.displayGrid("Finished sudoku")
- execute: node testSudoku.js
- enjoy :) !
The sudoku grid is the same everywhere. They all share the same data !
Important
Some Sudokus cannot be solved with this algorithm. Those Sudokus need an assumption to continue the resolution.
But the algorithm is able to detect when an assumption needs to be made, so, you could work on the version 2 of this code that would be able to solve those Sudokus ;)!
Technical stack
- JS ES6
- Babel
- Npm
- Git
- A lot of patience :p
About
Npm module made from scratch by me, for you, with <3. I used this code to do this in react.