我正在尝试用 JavaScript 为我的寻路机器人实现 A* 算法。唯一的问题是我不明白找到所有相邻的正方形是什么意思。我正在使用曼哈顿距离公式,因为我不能让我的机器人斜着走。这是我的代码(现在):
var open = new Array();
var closed = new Array();
start = [9,18]; //do not take this literally
goal = [1,0]; //again don't
open.push(start);
while (open.length != 0) {
for(var x = 0; x < open.length; x++) {
heuristicValue[x] = computeHeuristicV(maplayout, start[0], start[1], open[x][0], open[x][1], goal[0], goal[1]);
}
minimum = Math.min(100000,heuristicValue[0]);
for(var x = 1; x < open.length; x++) {
minimum = Math.min(minimum, heuristicValue[x]);
}
for(var x = 0; x < open.length; x++) {
if (minimum == heuristicValue[x]) {
current = [open[x][0], open[x][1]];
}
}
closed.push(current);
//INCOMPLETE
}
computeHeuristicV 函数计算上面代码中的启发式值。