GithubHelp home page GithubHelp logo

robatron / sudoku.js Goto Github PK

View Code? Open in Web Editor NEW
401.0 11.0 132.0 174 KB

A Sudoku puzzle generator and solver JavaScript library

License: MIT License

CSS 4.17% JavaScript 95.46% HTML 0.38%
sudoku sudoku-solver sudoku-generator javascript javascript-library

sudoku.js's Introduction

Sudoku.js

A Sudoku puzzle generator and solver JavaScript library.

Check out the online demo to see it in action.

Implementation ideas borrowed from "Solving Every Sudoku Puzzle" by Peter Norvig, and a generator/solver by Michael Anderson.

Intro

Puzzles are represented by a string of digits, 1-9, and '.' as spaces. Each character represents a square, e.g.,

"52...6.........7.13...........4..8..6......5...........418.........3..2...87....."

Represents the following board:

5 2 . | . . 6 | . . .   
. . . | . . . | 7 . 1   
3 . . | . . . | . . .   
------+-------+------
. . . | 4 . . | 8 . .   
6 . . | . . . | . 5 .   
. . . | . . . | . . .   
------+-------+------
. 4 1 | 8 . . | . . .   
. . . | . 3 . | . 2 .   
. . 8 | 7 . . | . . .

(See the included converstion functions to convert between string representations and grids, i.e., two-dimensional arrays.)

Generate a Sudoku puzzle

Generage a Sudoku puzzle of a particular difficulty, e.g,

>>> sudoku.generate("easy")
"672819345193..4862485..3197824137659761945283359...714.38..1426.174.6.38.463...71"

>>> sudoku.generate("medium")
"8.4.71.9.976.3....5.196....3.7495...692183...4.5726..92483591..169847...753612984"

>>> sudoku.generate("hard")
".17..69..356194.2..89..71.6.65...273872563419.43...685521......798..53..634...59."

Valid difficulties are as follows, and represent the number of given squares:

"easy":         62
"medium":       53
"hard":         44
"very-hard":    35
"insane":       26
"inhuman":      17

You may also enter a custom number of squares to give, e.g.,

>>> sudoku.generate(60)
"8941376521532687497269548...72.9158.538.4219..19.852..3874.69.52415793689658.34.."

The number of givens must be a number between 17 and 81 inclusive. If it's outside of that range, the number of givens will be set to the closest bound, e.g., 0 will be treated as 17, and 100 as 81.

By default, the puzzles should have unique solutions, unless you set unique to false, e.g.,

sudoku.generate("easy", false)

Note: Puzzle uniqueness is not yet implemented, so puzzles are not guaranteed to have unique solutions.

Solve a Sudoku puzzle

Solve a Sudoku puzzle given a Sudoku puzzle represented as a string, e.g.,

>>> sudoku.solve(".17..69..356194.2..89..71.6.65...273872563419.43...685521......798..53..634...59.");
"217386954356194728489257136165948273872563419943712685521439867798625341634871592"

Board string ↔ grid

Board string → grid:

>>> sudoku.board_string_to_grid("23.94.67.8..3259149..76.32.1.....7925.321.4864..68.5317..1....96598721433...9...7")
[
    ["2","3",".","9","4",".","6","7","."],
    ["8",".",".","3","2","5","9","1","4"],
    ["9",".",".","7","6",".","3","2","."],
    ["1",".",".",".",".",".","7","9","2"],
    ["5",".","3","2","1",".","4","8","6"],
    ["4",".",".","6","8",".","5","3","1"],
    ["7",".",".","1",".",".",".",".","9"],
    ["6","5","9","8","7","2","1","4","3"],
    ["3",".",".",".","9",".",".",".","7"]
]

Board grid → string:

>>> sudoku.board_grid_to_string([
    ["2","3",".","9","4",".","6","7","."],
    ["8",".",".","3","2","5","9","1","4"],
    ["9",".",".","7","6",".","3","2","."],
    ["1",".",".",".",".",".","7","9","2"],
    ["5",".","3","2","1",".","4","8","6"],
    ["4",".",".","6","8",".","5","3","1"],
    ["7",".",".","1",".",".",".",".","9"],
    ["6","5","9","8","7","2","1","4","3"],
    ["3",".",".",".","9",".",".",".","7"]
])
"23.94.67.8..3259149..76.32.1.....7925.321.4864..68.5317..1....96598721433...9...7"

Get candidates

Get a grid of squares and their candidate values, propagating constraints, i.e., candidates restrict their peer candidates.

>>> sudoku.get_candidates("4.25..389....4.265..523.147..1652.7.6..1945322543876915....3.1....4..9.....8....3")
[
    ["4",     "167",    "2",    "5",  "167",  "16",   "3",   "8",  "9"  ],
    ["13789", "13789",  "3789", "79", "4",    "189",  "2",   "6",  "5"  ],
    ["89",    "689",    "5",    "2",  "3",    "689",  "1",   "4",  "7"  ],
    ["389",   "389",    "1",    "6",  "5",    "2",    "48",  "7",  "48" ],
    ["6",     "78",     "78",   "1",  "9",    "4",    "5",   "3",  "2"  ],
    ["2",     "5",      "4",    "3",  "8",    "7",    "6",   "9",  "1"  ],
    ["5",     "246789", "6789", "79", "267",  "3",    "478", "1",  "468"],
    ["1378",  "123678", "3678", "4",  "1267", "156",  "9",   "25", "68" ],
    ["179",   "124679", "679",  "8",  "1267", "1569", "47",  "25", "3"  ]
]

Print a board to the console

>>> sudoku.print_board(".17..69..356194.2..89..71.6.65...273872563419.43...685521......798..53..634...59.");
. 1 7   . . 6   9 . .   
3 5 6   1 9 4   . 2 .   
. 8 9   . . 7   1 . 6   

. 6 5   . . .   2 7 3   
8 7 2   5 6 3   4 1 9   
. 4 3   . . .   6 8 5   

5 2 1   . . .   . . .   
7 9 8   . . 5   3 . .   
6 3 4   . . .   5 9 .  

References:

License:

The MIT License (MIT)

Copyright (c) 2014 Rob McGuire-Dale

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

sudoku.js's People

Contributors

laurens-vrh avatar robatron avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

sudoku.js's Issues

This is broken. Every puzzle has 20+ solutions.

Am I missing something here?

A proper Sudoku puzzle only has one solution. If it has more, the player will reach a point where there are more than one possibility and is forced to make a choice/guess. This is basic stuff being done wrong here.

In fact, this basically renders this generator completely useless.

  • Easy and Medium are way too easy (62 and 53 is WAY too many clues), and yet often there are 2 or 3 solutions.
  • Hard and above have WAY too many solutions. More solutions means even less of the board is solvable without guessing. There are so many solutions that these difficulties are basically useless.

So sudoku.js offers:

  1. Ridiculously easy puzzles that are completely boring to anyone but struggling young children. And yet still manage to have multiple solutions.
  2. Puzzles that are supposed to be hard but the player quickly realizes that they are forced make a guess.

It is alarming that people just accept this library without questioning it. I'm not even sure this would be accepted as a homework assignment. I can't even determine the true difficulty of any of these puzzles because of how many solutions there are.


Here is an example puzzle generated with the "Insane" preset of sudoku.js. It happens to have over 600 possible solutions; likely significantly more. I will take just 3 solutions (which are unique to 3 different solvers, that is to say, each solver comes up with a different solution). All of the solutions are fully valid.

Original Puzzle:

74..1.2.8.......4.39........7.945.1.9.3.....45.4.38972657........................

Solutions:

745316298186592743392874651278945316963721584514638972657183429829467135431259867
745316298126589347398274156872945613913627584564138972657892431439761825281453769
745316298162589347398427156276945813983271564514638972657893421829164735431752689

Now lets overlay them and compare:

  • Light blue background: Our givens/clues.
  • Dark blue background: Common solved grids in all 3 solutions.
  • Dark red background: Differences between solutions, indicating solver had to choose a branching path.

image

Self-explanatory really.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.