Comments (10)
Hi there, I just found out about SudokuJS a few days ago and implemented a history to work with it. Let me try to provide some relevant info that I've discovered.
Consider the example from "demo-simple.html":
var board = [
,5, , , ,2,3,8,
, ,1, , , ,4, , ,5
, ,2, , ,5, , , ,
,5, ,7,8, , ,2,1,
,4,6, ,2,3,7, ,5,8
, ,9,8, , ,5,4, ,7
, , , , ,6, , ,4,
,1, , ,9, , , ,6,
, ,7,3,4, , , ,9,undefined
];
Here, we define an array variable called board. There are 81 positions in the array. Each position is either blank or a value from 1-9. However, the final position cannot be blank so if it's not a number between 1-9, then we must set it to undefined.
Now, using the array board, we can construct a sudoku board as follows:
var sudokuBoard = $("#boardDiv").sudokuJS({ board: board });
Next, we may want to play with the board for a while. Later, we may want to get the new board configuration, to do so we say: sudokuBoard.getBoard();
However, this returns an array of objects instead of an array of numbers. If you want to store the board configuration in a convenient format for later use, you may want to strip away the objects to get just an array of numbers (or even just a string). I wrote a function to do this:
/* Code below is from the history project that I mentioned. See here: https://github.com/MichaelWehar/SudokuTree */
function trimBoard(oldBoard){
var newBoard = [];
for(var i=0; i<81; i++){
if(i in oldBoard && oldBoard[i].val != null) newBoard[i] = oldBoard[i].val;
}
return newBoard;
}
If you want to grab this data and construct a board again, you can do so. Just remember that the last position can't be blank. I made a check function to fix this issue before constructing a new board. See below:
var newBoard = trimBoard(sudokuBoard.getBoard());
/* If last position is blank, then assign to undefined */
if(!(80 in newBoard)) newBoard[80] = undefined;
/* Finally, construct new sudoku board */
var newSudokuBoard = $("#newBoardDiv").sudokuJS({ board: newBoard });
Not exactly sure if this is what you are looking for, but I hope it helps a little bit. I'm interested in learning about your project. If you're looking for a collaborator, please let me know and I say that to both @csharsha and @pocketjoso
Hope you have a great week and looking forward to hearing from you!! :)
from sudokujs.
How to auto evaluate the board @MichaelWehar and @pocketjoso after completing the board. I want to show the pop up or any kind of notification to user once they finish the game. Please help if you know.
from sudokujs.
I dont know how to save this in DB so that I can retrieve it back as an array. Can you please help!
@csharsha Use JSON.stringify
before saving to your DB. Then when reading, use JSON.parse
.
You can either stringify the whole array and save it as a string (easier), or if you prefer, you can stringify each object inside the array:
req.body.gameData.map(data => JSON.stringify(data)
from sudokujs.
@MichaelWehar I think I see where the confusion lies. sudokuJS
internally always uses objects for each board position, to store candidate
info as well as the actual val
. All functions (init
, setBoard
) accept/require a board array with Object
s inside. The only reason the init
function
$("#sudoku").sudokuJS({
board: board
});
accepts a board as an array of numbers (in addition to a board with Objects) was to make it easier for people to load their own boards from anywhere (not using my format). If you're getting your boards from sudokuJS
, always use the Object format inside the board
array.
To save and re-use a board exactly where it was, use getBoard
and then setBoard
. Don't manipulate the board that sudokuJs
gives you - it works as is.
I used to have save/load functionality in one of the demos, I don't remember why I took it down. This is the relevant code (saving, loading from localStorage
):
var localStoragePrefix = "my-sudoku-";
/* loadSavedBoard
* -----------------------------------------------------------------*/
var loadSavedBoard = function(id){
var t = localStorage.getItem(localStoragePrefix+id);
if(t){
sudoku.setBoard(JSON.parse(t));
}
}
/* saveBoard
* -----------------------------------------------------------------*/
var saveBoard = function(id){
localStorage.setItem(localStoragePrefix+id, JSON.stringify(board));
}
from sudokujs.
However, this returns an array of objects instead of an array of numbers. If you want to store the board configuration in a convenient format for later use, you may want to strip away the objects to get just an array of numbers (or even just a string)
See my answer to @csharsha. If you just store the val
you lose information about board candidates. Sure, if you don't use them you can ignore them, however for storing the data just use JSON.stringify
and you're good to go.
from sudokujs.
@csharsha:
function trimBoard(oldBoard){
Shorter:
const newBoard = board.map(item => item.val)
from sudokujs.
I should say, it's cool to see some activity on this project! You have to forgive me for not remembering the code very well, as it's been a few years since I wrote it.
As for collaborators - @csharsha if you have good ideas for the project, create new issues for them first, then if I agree it's a good idea we can start talking about how to implement them to the project. I would be happy for you to contribute if there's a need for new features.
from sudokujs.
How to auto evaluate the board @MichaelWehar and @pocketjoso after completing the board
@csharsha https://github.com/pocketjoso/sudokuJS#boardfinishedfn
from sudokujs.
@csharsha @MichaelWehar Any further input? otherwise I will close this for now, but we can always re-open it if there are more questions.
from sudokujs.
Thank you for all of the comments @pocketjoso !! I really appreciate it and I really like your project. :)
I have nothing more to add at the discussion at this time.
from sudokujs.
Related Issues (10)
- multiple Solution
- solveAll() doesnt seem to work after using setBoard() HOT 4
- please freeze cells HOT 1
- Solving/Checking sudoku correctness HOT 3
- analyzeBoard is always returning "finished:True" inspite of errors and incomplete board HOT 1
- Diagonal sudoku? HOT 6
- How to start solving generated sudoku again? HOT 8
- How to set the board from window.location.hash? HOT 8
- multiple solution
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sudokujs.