Skip to content

letItCurl/sudoku-solver-engine

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

45 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Look what we can do !

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
method usage
displayGrid(title: sting) console.log the sudoku
displayRow(x: number) console.log the row at x
displayCol(y: number) console.log the col at y
displayCross(x: number, y: number) console.log the column at y and the row at x
getCol(y: number) return the row at y
getRow(x: number) return the row at x
getArea(x: number, y: number) return the area at x,y
isMoreThanOne(arr: number[]) return true if in the array there is no double numbers
checkInitialArea(x: number, y: number) return true if area is ok
checkInitialRow(x: number) return true if row is ok
checkInitialCol(y: number) return true if col is ok
checkInput(x: number, y: number) return true if at x,y the area/col/area are okay. If false it will return an object: {erros: []}

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)
method usage
solveN(n: number) Detects all possiblities of n in a sudoku and set the number in the right position. It also feed the log data.
solveAll() Solve all the sudoku and feed the log data. return false if the sudoku is a "multiple choice problem"

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.

Hey there !
I'm letItCurl, fullstack developer engineer in freelance a.k.a Roland in real life :p
If you have any question you can contact me if you wish !

I'm always ready to help !

Email me

About

This engine can solve basics sudoku 🤘

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published