13.4 Completing Sudoku Puzzle Helper
Professor: I hope you still h ave energy to finish Sudoku, which is our last project in
this course. Before doing it, let’s briefly refre sh our memory of what we produced so
far. We wrote two objects, Cell and Sudoku, and this is the list of things we need to
know to be able to continue the work:
• The Sudoku() constructor creates a Sudoku object instance.
• The setClues() me thod of the Sudoku object takes as an argument a 9 × 9
array of initial clues for the cells. Each clue can be either a numeric value
between on e and nine, or the null value if the cell is empty.
• The setNumber() method of the Sudoku object accepts three arguments: a
number that is to be written into the puzzle, and the row and column index of
the cell into which the numb er is to be written. The method returns false if the
same number already exists in the same row, column, or 3 × 3 box. Otherwise,
it inserts the number into the puzzle grid and returns t rue.
• The board property of the Sudoku object is a 9 × 9 array of references to Cell
object instances.
• The getNumber() method of the Cel l object returns the number that is cur-
rently written in the cell. If the cell is empty, then the method re turns null.
• The isWritable( ) method of the Cell obje ct returns true if you ca n write
numbers into the cell, and false otherwise.
Now, let’s get the job do ne. We are g oing to generate the HTML part of the p uzzle
dynamic ally, of course. Nevertheless, here is static HTML code for the puzzle grid to
get an impression of how the final document should look:
<form>
<table class="sudoku-table">
<tr>
<td><input type="text"></td>
<td><input type="text"></td>
<!-- ... -->
</tr>
<tr>
<td><input type="text"></td>
<td><input type="text"></td>
<!-- ... -->
</tr>
<!-- ... -->
</table>
</form>
The comments containing ellipses hint that parts of the code are o mitted. Everyth ing in
fact repeats verbatim nine times and the whole puzzle is simply a 9×9 table containing
text boxes inside its cells.
This is the actua l HTML that we’re going to use for our Sudoku:
13.4. Completing Sudoku Puzzle H elper 255