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
the second value in the first row, for example.
Mike: Since initial is an array of rows, we can select a row simply by using the
array access operator ([]). For examp le , this is the first row:
initial[0] //The first row
Because this is also an arra y, we can apply another array access operator to select a
single eleme nt from it:
initial[0][1] //The second element in the first row
Professor: Sp le ndid! Now you know everything to complete your homework.
Maria: Excuse me, what exactly is it that we have to do?
Professor: Oh, I’m sorry. I re ally sh ould have given you more specific directions. You
first need to obtain a number from one to nine, and then the row and column indexes
from zero to eight of the cell in which the player wants to place that number. Once
you have these three numbers, you try to check whether the number can be placed in
the required cell. You must check whether the same number isn’t already in the same
row or column. That’s the easy part. The tricky part is to check whether the same
number isn’t a lready in the same 3 × 3 box. For example, you cannot put the number
ve in any of the four e mpty cells of the upper left 3 × 3 box because tha t box alr eady
contains that number.
Just a few more keywords to summarize today’s meeting and then I guess we’re done
for this w eek:
In this meeting: comparing dates, value, refer ence, Array object, index, length,
array literal, array elemen t, lookup table, sparse array, for loop, initialize, test,
update, short-circuit evaluation , comma oper ator, break, continue, infinite loop,
array methods, sort(), indexOf(), Str ing object, immutable object, object-to-
primitive conversion, meth od chaining, wrapper object, toString(), two-dimen-
sional arr ay
186 Meeting 9. Understanding Arrays and Strings
..................Content has been hidden....................

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