19. function getGridContext(grid) {
var p = 0, c, gc = [];
// 判断上方格子是否可通过
p = grid - x;
c = p > 0 && !grids[p];
c ? gc.push(p) : gc.push(-1);
// 判断右方格子是否可通过
p = grid + 1;
c = p % x != 0 && !grids[p];
c ? gc.push(p) : gc.push(-1);
20. // 判断下方格子是否可通过
p = grid + x;
c = p < grids.length && !grids[p];
c ? gc.push(p) : gc.push(-1);
// 判断左方格子是否可通过
p = grid - 1;
c = grid % x != 0 && !grids[p];
c ? gc.push(p) : gc.push(-1);
return gc;
}
21. function hasNextGrid(grid) {
var gc = getGridContext(grid);
if(gc.join(‘,’) !== ‘-1,-1,-1,-1’){
return true;
}
return false;
}
40. /**
* if you try to draw a line from (1, 0) to (1, 3),
* the browser will draw a line covering 0.5 screen pixels
* on either side of x=1.
* The screen can’t display half a pixel,
* so it expands the line to cover a total of two pixels.
*
* Ref: http://diveintohtml5.org/canvas.html
*/
56. if (v & Math.pow(2, d)) { // check grid
switch (d) {
case 0: grid = grid - x; moveTo(grid);
break;
case 1: grid = grid + 1; moveTo(grid);
break;
case 2: grid = grid + x; moveTo(grid);
break;
case 3: grid = grid - 1; moveTo(grid);
break;
}
}
57. function moveTo(grid) {
var l = gridWidth * (grid % x),
t = gridHeight * Math.floor(grid / x);
// set oLostor’s position
oLostor.style.left = l + 'px';
oLostor.style.top = t + 'px';
// gird value is change
v = grids[grid];