GithubHelp home page GithubHelp logo

pocketjoso / sudokujs Goto Github PK

View Code? Open in Web Editor NEW
201.0 13.0 81.0 47 KB

JavaScript step-by-step Sudoku solver and board generator

Home Page: https://jonassebastianohlsson.com/sudoku/

CSS 4.09% JavaScript 72.96% HTML 22.96%
sudoku javascript board-generation sudoku-board sudoku-puzzle

sudokujs's People

Contributors

pocketjoso 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  avatar  avatar

sudokujs's Issues

solveAll() doesnt seem to work after using setBoard()

HI

I'm having a problem with solveAll() function. It doesnt seem to work after updating board with a different board. This is how my code looks like:

view:

<a href="#" data-sudoku="4, ,,,9,,,,5,5,3,,,8,,,9,1,,,7,5,,4, 3,,,7,8,,9,,2,,5,6,,,5,7,,8,9,,,2,9,,1,,5,,7,4,,,1,4,,9,6,,,3,7,,,2,,,1,9,6,,9,,7,,,,8">test</a>

javascript:

var board = [
    , , ,4, ,8, ,2,9
    , , , , , , , , ,4
    ,8,5, , ,2, , , ,7
    , , ,8,3,7,4,2, ,
    , ,2, , , , , , ,
    , , ,3,2,6,1,7, ,
    , , , , ,9,3,6,1,2
    ,2, , , , , ,4, ,3
    ,1,3, ,6,4,2, ,7,undefined
]
//NOTE: if last cell of board is empty, 'undefined' has to be used as value!
var mySudokuJS = $("#sudoku").sudokuJS({
        board: board
})


$('#different-sudokus a[href]').on('click', function (e) {

var elemstring = $(this).attr('data-sudoku').split(",");

    var empty = [

         ,   ,   ,   ,   ,   ,   ,   ,   ,
         ,   ,   ,   ,   ,   ,   ,   ,   ,
         ,   ,   ,   ,   ,   ,   ,   ,   ,
         ,   ,   ,   ,   ,   ,   ,   ,   ,
         ,   ,   ,   ,   ,   ,   ,   ,   ,
         ,   ,   ,   ,   ,   ,   ,   ,   ,
         ,   ,   ,   ,   ,   ,   ,   ,   ,
         ,   ,   ,   ,   ,   ,   ,   ,   ,
         ,   ,   ,   ,   ,   ,   ,   ,undefined
    ];

    for (i = 0; i < empty.length; i++) {
        var numberOfscotss = parseInt(elemstring[i]);
        if(!(isNaN(numberOfscotss))) {
            empty.splice(i,1,numberOfscotss);
        } 
    }

      mySudokuJS .setBoard(empty );

    $(".solve-my-sudoku").on("click", mySudokuJS .solveAll);

this is how it looks in view:
image

mySudokuJS.getBoard() Retrieve Array of Objects

Hi,

As you already know mySudokuJS.getBoard() returns Array of Objects with size of 81. It is as below

Array.length = 81
[1] Object {val: 1, candidates: Array[9]}
..........
......
......
[81] Object {val: 1, candidates: Array[9]}

Like this I have 81 Objects. I am trying to pass this Array from HTML file hidden field to mongoose schema, but when I am trying to save this array it is getting saved as String in MongoDB, something like this

"[object Object],[object Object],[object Object],[object Object]............"

So I cannot evaluate this while I want to retrieve this data and use it in mySudokuJS,setBoard().

Mongoose Schema is as below

 var tbl_GameData = new mongoose.Schema({
    LevelID : {type: Number},
    Data : {type: Array} 
  });

and saving from HTML file is like as below

 app.post('/saveGame', function(req, res) {
        var gameData = new database.GameData({
      LevelID : 1,
      Data : req.body.gameData
 });

 gameData.save(function(err) {
if(err){
	console.log(err);
}

req.login(gameData, function(err){
	if(err){
		req.redirect('/login');
	}
	else {
		res.render('startGame',{ user: req.user ,title:"Sudoku Online Match"});
	}
      });	
 });
 });

I dont know how to save this in DB so that I can retrieve it back as an array. Can you please help!

Diagonal sudoku?

Hi,
your code is amazing, thank you for this! But is also possible to solve diagonal sudoku?

analyzeBoard is always returning "finished:True" inspite of errors and incomplete board

I'm trying to use your code for one of my project and after intializing the board, and clicking on analyze function, it is returning "finished:true" every time. If the board is incomplete or have errors, then also it is returning the same message.

`var mySudokuJS = $("#sudoku").sudokuJS({
difficulty: "easy"
});

function analyze()
{
var data = mySudokuJS.analyzeBoard();
console.log(data);
}
`

can u please help

How to set the board from window.location.hash?

I wanna create url with generated sudoku and use only Javascript. I can create url like this: https://.../#3,,,8,,,1,9,7,,,,,,,,,,,8,,,,6,3,4,2,,3,,5,2,8,,,6,1,2,,,,,,,,6,,9,4,,,,8,,,,6,,8,1,7,,,,,,,,,4,,,7,,,,6,,8,,1, but Javascript thinks that it's string, not array. I tried for example this hash = Array.from(window.location.hash); mySudokuJS.setBoard(hash);. Also I tried create empty array, then insert values by push.

It works in PHP like this: https://.../?3,,,8,,,1,9,7,,,,,,,,,,,8,,,,6,3,4,2,,3,,5,2,8,,,6,1,2,,,,,,,,6,,9,4,,,,8,,,,6,,8,1,7,,,,,,,,,4,,,7,,,,6,,8,,1, and then var hash = ['.$_SERVER['QUERY_STRING'].']; mySudokuJS.setBoard(hash);. Is possible to do it only in Javascript?

please freeze cells

like this

$('.sudoku-board-cell').each(function(){
   var cell = $(this).find('input');
   if (parseInt(cell.attr('value'))>0) cell.attr('disabled','disabled');
});

Solving/Checking sudoku correctness

Hi,
I was wondering how come you are solving the sudoku while you already generated all results at the start? I am trying to implement a check function, which If i understand correctly just compares input values with $board values, or am I missing something? Is it maybe for users who are passing the board on their own?

PS: Dunno how to mark this "issue" as a question :)

How to start solving generated sudoku again?

I've got one little question. I want start solving again some sudoku. I'm triying to add button "Again" with delete all values in "highlight-val" classes - that's ok but script still thinks that there are values in these cells.
So how to delete "highlight-val" values and tell the script that these cells are empty?

multiple solution

Hello, I am using the version v0.4.4
I was able to generate a board with multiple solutions :
[{"val":3},{"val":4},{"val":9},{"val":6},{"val":1},{"val":8},{"val":5},{"val":7},{"val":2},{"val":7},{"val":8},{"val":2},{"val":3},{"val":5},{"val":9},{"val":null,"candidates":[null,null,null,4,null,6,null,null,null,null]},{"val":null,"candidates":[1,null,null,4,null,null,null,null,null,null]},{"val":null,"candidates":[1,null,null,null,null,6,null,null,null,null]},{"val":6},{"val":5},{"val":1},{"val":7},{"val":4},{"val":2},{"val":3},{"val":8},{"val":9},{"val":2},{"val":7},{"val":3},{"val":8},{"val":9},{"val":5},{"val":null,"candidates":[null,null,null,4,null,6,null,null,null,null]},{"val":null,"candidates":[1,null,null,4,null,null,null,null,null,null]},{"val":null,"candidates":[1,null,null,null,null,6,null,null,null,null]},{"val":1},{"val":9},{"val":5},{"val":4},{"val":7},{"val":6},{"val":2},{"val":3},{"val":8},{"val":8},{"val":6},{"val":4},{"val":2},{"val":3},{"val":1},{"val":9},{"val":5},{"val":7},{"val":5},{"val":2},{"val":7},{"val":1},{"val":6},{"val":4},{"val":8},{"val":9},{"val":3},{"val":9},{"val":1},{"val":8},{"val":5},{"val":2},{"val":3},{"val":7},{"val":6},{"val":4},{"val":4},{"val":3},{"val":6},{"val":9},{"val":8},{"val":7},{"val":1},{"val":2},{"val":5}]

Is there a way to avoid this?
Thank you

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.