Vector.prototype.setY = function(y) {
var x = this.getX();
this.d = Math.sqrt(x * x + y * y);
this.alpha = Math.atan2(y, x);
};
You can use these modified methods in exactly the same way as the original ones, and
the two lines of co de from the above example still work just the same:
v4.setX(42);
console.log(v4.getX()); //Writes 42
Note that w hat I have just showed you is not the only advantage of having special
methods for setting and quer ying the object’s properties. Using methods instead of
directly setting the values of properties also enables y ou to check th e integrity of the
values b efore setting the m. Imagine that you want to have a class representing the
natural numbers and prevent the programme r from erro neously using a negative or
non-integer numbers with that class. You can easily add this kind of checking to a
method that the programmer will use for setting values of members of the natural
number class.
I think we can now return to our Sudoku puzzle helper.
11.8 Sudoku Puzzle Helper
Professor: You already did some work o n the Sudoku helper when you implemented
a check ing of whether a numb er can be placed on th e playing grid. Our final goa l,
however, is more ambitious than that. We are going to design a web page that will
assist the visitor in solving a Sudoku pu zzle. Our product will consist of a grid of edit
boxes, into which the visitor will enter the missing numbers. Each time the entered
number conflicts with the numbers already on the puzzle board, the number will be
colored red. Apart from that, our Sudoku helper will allow the visitor to enter more
than a single number into a cell, thus creating a list of solution candidates for that cell.
We will display solution candidates sm aller and will not check whether or no t they are
placed w rongly.
Note that our program won’t attem pt to actually solve a puzzle. It will only check
whether the most recently placed number is in conflict with the nu mbers a lready on
the board, regardless of whether those numbers have been placed correctly or n ot.
Let’s now begin programming our puzzle. Because you already know enough about
objects, we can opt for object-oriented desig n. Basically, we need to identify dierent
objects that will help us to construct the program and then define the interactions of
these objects. Can you perhaps th ink of any par t of a Sudoku puzzle that we can
implement as an independent object?
Maria: A c ell could be an object.
Professor: Definitely. So let’s start out with a cell.
11.8. Sudoku P uzzle Help er 219
However, before we begin to program a cell object, we should be able to answer two
very important questions about it:
What information should the object store?
How should the object interact with its environment? By environment I mean
the rest of the program.
Mike: Those are properties and methods, aren’t they?
Professor: Precisely. Infor mation about the objec t is stored in its prope rties, while
methods enable the object to respond to actions from its environment. You can per-
form a simple test to see whether you’ve p ic ked the right ca ndidate to serve as an
object in your application. If you cannot think of any obvious piece of infor mation
that the object could store, or you cannot think of any obvious action that an object
could respond to, th en you probably haven’t picked the right candida te .
What kind of information do you think our cell should store?
Maria: Obviously a nu mber. And perhaps a Boolean property spe cifying whethe r
or not the number is permitted to change. Namely, the original numbers that define
a particular Sudoku pu zzle—I think they are called givens or clues—should not be
changed.
Professor: This is no doubt essential information. Let’s write it down in a list:
number: a number that is written in the cell or null if the cell is empty.
writable: a Boolean property controlling the permission to change the num-
ber.
Apart from these, a cell will of course have methods to set and query its properties,
except the pr operty writabl e won’t need a special set method as there’s no need
for this property ever to change. Its value will be set in the cell’s c onstructor. N ote
that names o f methods used for querying Boolean pr operties sometimes start with “is”
rather than “get” in orde r to suggest that the property is o f the Boolean type. Namely,
if the method is named isWritable(), then the name can be interpreted as a question
whether or not the object is writable.
I gue ss that everything is q uite straightforward and we can produce code for the Cell
class right away:
var Cell = function(num) { //The Cell() constructor
this.number = num;
if (num == null) {
this.writable = true;
}
else { //If the initial value is not null, then this
//is a given and is not allowed to change.
this.writable = false;
}
};
220 Meeting 11. Building Your Own Objects
Cell.prototype.setNumber = function(num) {
if (this.writable) {
//Sets the value only if this cell is writable.
this.number = num;
}
};
Cell.prototype.getNumber = function() {
return this.number;
};
Cell.prototype.isWritable = function() {
return this.writable;
};
The Cell() c onstructor sets both properties of Cell to their initial values. Recall
that you once used the null value to denote an empty cell in your homework and we
will stick to this co nvention through out the project. All the values tha t are initially
not null are loc ked for writing in the c onstructor by setting their wr itable p roperty
to false. These cells represent clues for a puzzle. Notice how I used the property
writable inside the setNumber() method to prevent writing a value to a cell that is
not writable.
The Cell class is thus c ompleted and we’re ready to move on. The nature of Sudoku
is such that cells do not interact with each other directly, so it comes naturally to build
an object named Sudoku and make cells pro perties of that object. The Sudoku object
will be responsible for ordering the cells and controlling their behavior.
In Sudoku, there ar e 91 cells altogether, arrange d in nine rows and nine c olumns,
which ca n be represented in a matrix form as follows:
*
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
,
C
0, 0
C
0, 1
C
0, 2
C
0, 3
C
0, 4
C
0, 5
C
0, 6
C
0, 7
C
0, 8
C
1, 0
C
1, 1
C
1, 2
C
1, 3
C
1, 4
C
1, 5
C
1, 6
C
1, 7
C
1, 8
C
2, 0
C
2, 1
C
2, 2
C
2, 3
C
2, 4
C
2, 5
C
2, 6
C
2, 7
C
2, 8
C
3, 0
C
3, 1
C
3, 2
C
3, 3
C
3, 4
C
3, 5
C
3, 6
C
3, 7
C
3, 8
C
4, 0
C
4, 1
C
4, 2
C
4, 3
C
4, 4
C
4, 5
C
4, 6
C
4, 7
C
4, 8
C
5, 0
C
5, 1
C
5, 2
C
5, 3
C
5, 4
C
5, 5
C
5, 6
C
5, 7
C
5, 8
C
6, 0
C
6, 1
C
6, 2
C
6, 3
C
6, 4
C
6, 5
C
6, 6
C
6, 7
C
6, 8
C
7, 0
C
7, 1
C
7, 2
C
7, 3
C
7, 4
C
7, 5
C
7, 6
C
7, 7
C
7, 8
C
8, 0
C
8, 1
C
8, 2
C
8, 3
C
8, 4
C
8, 5
C
8, 6
C
8, 7
C
8, 8
+
/
/
/
/
/
/
/
/
/
/
/
/
/
/
/
/
/
/
/
/
/
-
Apart from rows and columns, the re are also nine 3 × 3 boxes, delimited by the two
horizontal and two vertical lines in the matrix. Notice that each cell C
i, j
has two
indexes that stand for the row (i) and column (j) in which the cell is placed. It is very
useful that we have a scheme like that so we kn ow exactly which cells we ar e referring
to.
11.8. Sudoku P uzzle Help er 221
There’s no t much work for the Sudoku() constructor. It only initializes the board
property of the Sudoku class:
var Sudoku = function() {
this.board = [];
};
The board property represents the two-dimensio nal grid of Cell objects and is for
now set to an empty array.
Next, we n eed to write a method to initialize the playing bo ard. The following method
calls the Cell() constructor f or each o f the 91 cells and fills th e board property of
the puzzle with th em:
Sudoku.prototype.setClues = function(initial) {
for (i = 0; i < 9; i++) {
this.board[i] = [];
for (j = 0; j < 9; j++) {
this.board[i][j] = new Cell(initial[i][j]);
}
}
};
The param eter initial is expected to be a two-dimensional array of primitive values
like the one on page 185, which you used for your homework. Because the problem
is two-dimensional, there are two nested for lo ops inside the method definition. The
outer loop iterates rows, and for each r ow it first creates an empty array representing
that row. With the help of the inner for loop it then creates and inserts nine cells into
that row. Notice how a corresponding value is passed as an argument to the Cell ()
constructo r.
Maria: I’m no t sur e if I quite follow you. Can you please explain how you created
the two-dimensional array board?
Professor: No problem . In the constructor I already created an empty ar ray object:
this.board = [];
At the beginning of the outer for loop in the setClues() method there’s this state-
ment:
this.board[i] = [];
The statement creates another empty arra y object, a reference to which is assigned
to the ith element of the board array. Elements of board are there fore also arrays.
By the way, because the ith element does not yet exist a t the time of the stateme nt
execution, it is automatically appended to the board array.
222 Meeting 11. Building Your Own Objects
It is clear now that the expression board[i] is an ar ray representing the ith row of the
board. Inside the in ner for loop the jth element of this array is assigned a reference
to a cell object created at the right side of the assignment op erator. That leads us to
the fact that the expression boa rd[i][j] is from now on a reference to the cell object
representin g the cell C
i, j
.
For clarity, here is a drawing of the board with its nine rows and all the cells they refer
to.
C
0,0
C
0,1
C
0,2
C
0,3
C
0,4
C
0,5
C
0,6
C
0,7
C
0,8
C
1,0
C
1,1
C
1,2
C
1,3
C
1,4
C
1,5
C
1,6
C
1,7
C
1,8
C
2,0
C
2,1
C
2,2
C
2,3
C
2,4
C
2,5
C
2,6
C
2,7
C
2,8
C
8,0
C
8,1
C
8,2
C
8,3
C
8,4
C
8,5
C
8,6
C
8,7
C
8,8
...
board[0]
board[1]
board[2]
board[8]
...
The last of the Sudoku methods that we’re going to write today will take care of
entering n umbers into the cells, and we will name it setNumber(). As it happens,
you already did most of the job of checking whether a number can be placed into the
grid. We will only need to modify your c ode a bit to suit our current needs.
The setNumber( ) method will take as arguments the number that we want to enter
and a reference to the cell that should accept the n umber.
Mike: We already implemen te d the same method on the Cell class. Why do we need
another m ethod that does exactly the sam e thing?
Professor: We need it because the setNumber() method of the S udoku object will do
a lot more than just ente r a number to a cell. Since the Sudoku object has an overview
of the complete gr id of cells, it can also check whethe r the entere d number already
exists in a particular row, column, or 3 × 3 box .
Here is the implementation of the method:
Sudoku.prototype.setNumber = function(number, row, col) {
var i, j;
var iStart, jStart;
//Removes the current number
this.board[row][col].setNumber(null);
//Checks a row
for (i = 0; i < 9; i++) {
if (this.board[row][i].getNumber() == number) {
return false;
}
}
11.8. Sudoku Puzzle Helper 223
..................Content has been hidden....................

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