if (this.solutions > 1) {
//More than one solution already found: no need to continue.
return;
}
}
//All the numbers have been tried for this cell, so clear the
//cell before returning:
this.setNumber(null, this.empty[n].row, this.empty[n].col);
};
Finally, this is an example of generating a Sudoku puzz le using the ab ove-defined
SudokuGenerator class and the isSolutionUniq ue() method of the Sudoku class:
window.onload = function() {
var s = new Sudoku();
var g = new SudokuGenerator();
do { //Repeats until we have a single-solution Sudoku.
g.reset(); //Loads a completely solved puzzle.
g.shuffle(); //Swaps rows and columns randomly.
g.removeNumbers(30); //Leaves 30 clues in the puzzle.
s.setClues(g.getClues()); //Initializes the Sudoku using
//the obtained clues.
} while (!s.isSolutionUnique()); //Checks whether this is
//a single-solution Sudoku.
s.start("sudoku-puzzle"); //Shows the puzzle to the user.
};
270 Appendix A. Solution to the Last Homework: Sudoku Generator
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.123.190