pocketjoso / sudokujs Goto Github PK
View Code? Open in Web Editor NEWJavaScript step-by-step Sudoku solver and board generator
Home Page: https://jonassebastianohlsson.com/sudoku/
JavaScript step-by-step Sudoku solver and board generator
Home Page: https://jonassebastianohlsson.com/sudoku/
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);
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!
Hi,
your code is amazing, thank you for this! But is also possible to solve diagonal sudoku?
it has multiple solution and its not fair !
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
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?
like this
$('.sudoku-board-cell').each(function(){
var cell = $(this).find('input');
if (parseInt(cell.attr('value'))>0) cell.attr('disabled','disabled');
});
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 :)
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?
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
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.