9.6 Homework
Professor: By the way, do you like Sudoku?
Maria: Isn’t that a num ber puzzle where you have to fill out the missing numbers in
a 9 × 9 grid so tha t each column, each row, and each of the nine 3 × 3 boxes contain
all o f the numbers from one to nine? Yes, I like to solve that puzzle.
Mike: Me too. Are we going to program a Sudoku gene rator?
Professor: Not so fast. First we are going to develop a web page that will assist
a visitor in solving a prede fined Sudoku puzzle, inasmuch as it will warn him /her
about a number that he/sh e has placed in a row, a column, or a 3 × 3 box that already
contains that number. The program will, however, only check the number that has
just been place d in relation to the already-placed numbers, not in relation to the final
solution of the puzzle.
For ho mework, you try your best to implement the part of the algorithm that checks
whether a number can be placed in a certain cell. You can use the following Sudoku
puzzle to work with:
5 7 4 2
3 8 2 7
1
7 3
7 2 8 3
3
4 1
6
1
6
4
7
2 7 1
1
9
2
3
4 1 5 9
Since the puzzle has a two-dimensional layout, the most logical data structure to hold
the numbers is a two-dimensional arra y. JavaScript does no t support two-dimensional
arrays per se but as there’s no lim itation on type of array elements, you can construc t
an arr ay of arrays. For example, you can work with the following two-dimensional
array, which describes the above Sudoku puz zle:
var initial = [
[null, 5, 7, null, 4, null, null, 2, null],
[null, 3, 8, null, null, 2, 7, null, null],
[null, 1, null, 7, 3, null, null, null, null],
[null, 7, null, 2, 8, null, null, null, 3 ],
[3, null, 4, null, null, null, 1, null, 6 ],
[1, null, null, null, 6, 4, null, 7, null],
[null, null, null, null, 2, 7, null, 1, null],
[null, null, 1, 9, null, null, 2, 3, null],
[null, 4, null, null, 1, null, 5, 9, null]
];
Do you perhaps have any ideas as to how to access a value in the above array? Like
9.6. Homework 185