0
第一行打印,比方說,10,而第二個將打印少1:Javascript:console.log(arr.length)表示長度爲n,console.log(arr)表示長度爲n-1。第二種情況下不會顯示最後一個元素
console.log("ds length: " + ds.length); // Prints, say, 10
console.log(ds); // Prints all but last element and says length is one less.
一些相關的我的部分代碼,所有的單AS:
FindPath: function(oX, oY, dX, dY) { // origin, destination
var heap = AS.Heap();
var g = AS.Grid; // AS.Grid is declared, in AS, as: Grid: new Array(),
var node;
var cn = g[oX][oY]; // current node
var dn = g[dX][dY]; // destination node
heap.push(cn);
cn.CoorType = AS.CoorType.VISITED;
while (heap.length > 0) {
cn = heap.pop();
if (cn === dn) {
var ds = new Array(); // direction set (instructions)
var pn;// parent node;
do {
pn = cn.Parent;
ds.push({
Dir: cn.Y - pn.Y !== 0 ? cn.Y - pn.Y < 0 ? AS.Direction.UP : AS.Direction.DOWN : cn.X - pn.X > 0 ? AS.Direction.RIGHT : AS.Direction.LEFT,
Node: cn});
cn = pn;
} while (pn.Parent);
console.log("ds length: " + ds.length);
console.log(ds);
AS.CleanUp();
return ds;
}
// Bellow, I'm not using functions 'cause occasionally, when FindPath() is called multiple times in a large area within a few milliseconds, it will get laggy. I removed the use of functions to increase performance.
node = g[cn.X+1][cn.Y];
if (node.CoorType === AS.CoorType.NOTHING) {
node.CoorType = AS.CoorType.VISITED;
node.Parent = cn;
node.Score = ((Math.abs(node.X - oX) + Math.abs(node.Y - oY)) * AS.SMALL_ADVANTAGE + Math.abs(node.X - dX) + Math.abs(node.Y - dY)) +
Math.abs((node.X - dX) * (oY - dY) - (node.Y - dY) * (oX - dX)) * 0.0001;
heap.Sink(node);
}
node = g[cn.X][cn.Y+1];
if (node.CoorType === AS.CoorType.NOTHING) {
node.CoorType = AS.CoorType.VISITED;
node.Parent = cn;
node.Score = ((Math.abs(node.X - oX) + Math.abs(node.Y - oY)) * AS.SMALL_ADVANTAGE + Math.abs(node.X - dX) + Math.abs(node.Y - dY)) +
Math.abs((node.X - dX) * (oY - dY) - (node.Y - dY) * (oX - dX)) * 0.0001;
heap.Sink(node);
}
node = g[cn.X-1][cn.Y];
if (node.CoorType === AS.CoorType.NOTHING) {
node.CoorType = AS.CoorType.VISITED;
node.Parent = cn;
node.Score = ((Math.abs(node.X - oX) + Math.abs(node.Y - oY)) * AS.SMALL_ADVANTAGE + Math.abs(node.X - dX) + Math.abs(node.Y - dY)) +
Math.abs((node.X - dX) * (oY - dY) - (node.Y - dY) * (oX - dX)) * 0.0001;
heap.Sink(node);
}
node = g[cn.X][cn.Y-1];
if (node.CoorType === AS.CoorType.NOTHING) {
node.CoorType = AS.CoorType.VISITED;
node.Parent = cn;
node.Score = ((Math.abs(node.X - oX) + Math.abs(node.Y - oY)) * AS.SMALL_ADVANTAGE + Math.abs(node.X - dX) + Math.abs(node.Y - dY)) +
Math.abs((node.X - dX) * (oY - dY) - (node.Y - dY) * (oX - dX)) * 0.0001;
heap.Sink(node);
}
}
AS.CleanUp();
return heap; // No path found
},
Heap: function() {
var heap = new Array();
heap.Sink = function(node) {
var i = this.length-1;
while (i > 0 && this[i].Score < node.Score) i--;
this.splice(i, 0, node);
};
return heap;
},
CleanUp: function() { // Cleans up changes made by any previous path search
var x, y, g = AS.Grid;
var limX = g.length - 3;
var limY = g[0].length - 3;
for (x = 2; x < limX; x++) {
for (y = 2; y < limY; y++) {
if (g[x][y].CoorType === AS.CoorType.VISITED) {
g[x][y].CoorType = AS.CoorType.NOTHING;
delete g[x][y].Parent;
}
}
}
}
我意識到,有時,我應該根據FindPath返回的路徑移動的對象將錯過一步。然後我看到了我上面描述的內容。