Appendix A
Solution to the Last Homework:
Sudoku Generator
The following code implements the Sud okuGenerator class used for generating a
Sudoku puzzle, which is not guaran te ed to have a unique solution. At the end of the
code there is a deļ¬nition of the ra nd() function used for ge nerating integer random
numbers.
/*
* Constructs a SudokuGenerator object instance. After constructing
* an object instance, you should call the methods reset(),
* shuffle(), removeNumbers(), and getClues().
*
* Example:
* var clues, g = new SudokuGenerator();
* g.reset();
* g.shuffle();
* g.removeNumbers(30);
* clues = g.getClues();
*/
var SudokuGenerator = function() {
this.clues = null;
};
/*
* Resets a SudokuGenerator object. Whenever you want to re-create
* a puzzle, this method should be called first.
*/
SudokuGenerator.prototype.reset = function() {
this.clues = [
[8, 2, 7, 1, 5, 4, 3, 9, 6],
[9, 6, 5, 3, 2, 7, 1, 4, 8],
[3, 4, 1, 6, 8, 9, 7, 5, 2],
[5, 9, 3, 4, 6, 8, 2, 7, 1],
[4, 7, 2, 5, 1, 3, 6, 8, 9],
[6, 1, 8, 9, 7, 2, 4, 3, 5],
265