Javascript A*算法在intellij中运行缓慢
我正在开发一款类似于《氏族冲突》的游戏(只是一个练习)。我用一个*算法让士兵找到目标的路径。地图的大小是120 x 120。 但是当一名士兵找到他的目标时,需要4到5秒才能找到他的路线。(结果大约是120个步骤,开放列表中的5xxx节点)。 当我在浏览器上运行这些代码时,只需要几毫秒Javascript A*算法在intellij中运行缓慢,javascript,algorithm,intellij-idea,Javascript,Algorithm,Intellij Idea,我正在开发一款类似于《氏族冲突》的游戏(只是一个练习)。我用一个*算法让士兵找到目标的路径。地图的大小是120 x 120。 但是当一名士兵找到他的目标时,需要4到5秒才能找到他的路线。(结果大约是120个步骤,开放列表中的5xxx节点)。 当我在浏览器上运行这些代码时,只需要几毫秒 findPath: function (startTile, endTile, id, isIgnoreWall) { if (startTile.x === endTile.x &&
findPath: function (startTile, endTile, id, isIgnoreWall) {
if (startTile.x === endTile.x && startTile.y === endTile.y)
return [];
if (!id)
id = 0;
var openPath = [], closePath = [];
openPath[0] = {
pos: startTile,
parent: null,
g: 0,
f: this.calculateH(startTile, endTile)
};
var matrix = SceneMgr.getCurrentScene().getMapView().matrix;
var isFound = false, k = 0;
var _path = [];
while (openPath.length) {
var minIndex = this.findMin(openPath);
var minNode = openPath[minIndex];
openPath.splice(minIndex, 1);
closePath.push(minNode);
var aroundNode = this.findAroundNode(minNode.pos);
for (var i = 0; i < aroundNode.length; i++) {
var pos = aroundNode[i];
var indexClose = this.getNodeIndex(pos, closePath);
if (indexClose != -1 || !this.canMove(pos, isIgnoreWall, id, matrix))
continue;
if (pos.x === endTile.x && pos.y === endTile.y) {
var node = {
pos: pos,
parent: minNode
};
var path = [];
cc.log("tim thay: ", closePath.length);
while (node.parent) {
path.unshift(node);
node = node.parent;
}
return path;
break;
}
var cost = this.calculateH(minNode.pos, pos);
var g = minNode.g + cost;
var h = this.calculateH(pos, endTile);
var f = g + h;
var newNode = {
pos: pos,
g: g,
f: f,
parent: minNode
};
var indexOpen = this.getNodeIndex(pos, openPath);
//if node with the same pos is already in open list
if (indexOpen != -1) {
//if node in open list has f less than f, then re-assign, else skip it
if (openPath[indexOpen].g > g) {
openPath[indexOpen] = newNode;
}
} else {
openPath.push(newNode);
//_path.push({pos});
//this.draw([{pos}]);
}
}
}
return [];
}
findPath:function(startTile、endTile、id、isIgnoreWall){
if(startTile.x==endTile.x&&startTile.y==endTile.y)
返回[];
如果(!id)
id=0;
变量openPath=[],closePath=[];
openPath[0]={
位置:startTile,
父项:null,
g:0,
f:这个。计算器(开始,结束)
};
var matrix=SceneMgr.getCurrentScene().getMapView().matrix;
var isFound=false,k=0;
var_path=[];
while(openPath.length){
var minIndex=this.findMin(openPath);
var minNode=openPath[minIndex];
开放路径拼接(minIndex,1);
closePath.push(minNode);
var aroundNode=this.findAroundNode(minNode.pos);
for(var i=0;ig){
openPath[indexOpen]=newNode;
}
}否则{
push(newNode);
//_push({pos});
//这个.draw([{pos}]);
}
}
}
返回[];
}
请不要关心id和isIgnoreWall变量:D您能澄清在IDEA中运行代码的意思吗?IDE本身不运行您的代码,它运行在浏览器中或使用Node.js解释器,我不认为问题与IDE有关。我不知道原因。但当我复制这段代码并在chrome浏览器中运行它时,需要几毫秒才能完成。但是当我用intellij跑步的时候,大概需要4到5秒。你怎么跑呢?