建立一个curr代表当前点初始为null,和currIndex序列号初始为0
let currIndex = 0;
let curr = null;
for(let i = 0; i < openList.length; i++) {
if(openList[i].f < openList[currIndex].f) {
currIndex = i;
}
}
curr = openList[currIndex];
if(curr === endSopt) {
drawPath(curr);
return true;
}
removeFromArray(openList, curr);
closedList.push(curr);
3.遍历curr的neighbors,将合适点的parent设为curr
for(let i = 0; i < curr.neighbors.length; i++) {
let neighbor = curr.neighbors[i];
if(!closedList.includes(neighbor) && !neighbor.wall) {
let tmpF = curr.g + getG(curr, neighbor) + getH(neighbor);
let newPath = false; // 是否是更好的路线
if(openList.includes(neighbor)) {
if(tmpF <= neighbor.f) {
neighbor.f = tmpF;
newPath = true;
}
} else {
neighbor.g = curr.g + getG(curr, neighbor);
neighbor.h = getH(neighbor);
neighbor.f = neighbor.g + neighbor.h;
newPath = true;
openList.push(neighbor);
}
if(newPath) {
neighbor.parent = curr;
}
}
}
4.递归这个函数,当点和终点一致时,返回这个点,然后递归它的parent属性,则能找到路线