Ich bin wirklich neu in dieser Art von Sache und this tutorial verwendet, um meinen Code zu schreiben.C++ A * Pathfinding verursacht Endlosschleife
Grundsätzlich ruft der Aufruf meiner Funktion meinen Code zum Absturz, das offensichtliche Problem wäre, dass ich eine Endlosschleife verursache, aber ich kann es nicht sehen.
Werfen Sie einen Blick:
std::vector<Vector2> TileMap::pathFind(Vector2 pathStart, Vector2 pathEnd){
struct Node{
Vector2 pos;
int f;
inline Node operator=(Node a){
pos = a.pos;
f = a.f;
}
};
std::vector<Node> openList;
std::vector<Vector2> closedList;
closedList.push_back(pathStart);
Vector2 currentNode = pathStart;
do{
if(currentNode.x - 1 >= 0){ //NORTH
Node node;
node.pos = Vector2(currentNode.x-1, currentNode.y);
node.f = 1 + (std::abs(pathEnd.x - node.pos.x)+std::abs(pathEnd.y - node.pos.y));
openList.push_back(node);
}
if(currentNode.x + 1 <= MAP_WIDTH){ //SOUTH
Node node;
node.pos = Vector2(currentNode.x+1, currentNode.y);
node.f = 1 + (std::abs(pathEnd.x - node.pos.x)+std::abs(pathEnd.y - node.pos.y));
openList.push_back(node);
}
if(currentNode.y - 1 >= 0){ //WEST
Node node;
node.pos = Vector2(currentNode.x, currentNode.y-1);
node.f = 1 + (std::abs(pathEnd.x - node.pos.x)+std::abs(pathEnd.y - node.pos.y));
openList.push_back(node);
}
if(currentNode.y + 1 <= MAP_HEIGHT){ //EAST
Node node;
node.pos = Vector2(currentNode.x, currentNode.y+1);
node.f = 1 + (std::abs(pathEnd.x - node.pos.x)+std::abs(pathEnd.y - node.pos.y));
openList.push_back(node);
}//step 2 now
if(!(openList.empty())){
Node minimum = openList[0];
int num = 0;
for(auto i : openList){
num++;
if(minimum.f > i.f){
minimum = i;
}
}
currentNode = minimum.pos;
closedList.push_back(minimum.pos);
openList.erase(
std::remove_if(openList.begin(), openList.end(), [&](Node & node) {
return node.pos == minimum.pos;
}), openList.end());
}
if(currentNode == pathEnd){
openList.clear();
}
}while(!(openList.empty()));
return closedList;
}
ich eine einfache Vector2 struct verwende ich hier in einer Header-Datei geschrieben:
#ifndef VECTOR2_H_INCLUDED
#define VECTOR2_H_INCLUDED
struct Vector2{
int x;
int y;
Vector2(int x, int y){
this->x = x;
this->y = y;
}
Vector2(){
this->x = 0;
this->y = 0;
}
inline Vector2 operator=(Vector2 a){
x=a.x;
y=a.y;
}
bool operator==(Vector2 a){
return (x==a.x && y==a.y);
}
};
#endif // VECTOR2_H_INCLUDED
einige Anmerkungen zu Codequalität Hinzufügen wäre schön.
Kann ich 1 gerade zu gehen, nicht ersetzen mit: '(std :: abs (currentNode.x - pathStart.x) + std :: abs (currentNode.y - pathStart.y) + 1)' – genfy
Nein, weil es vielleicht eine Behinderung gibt, die uns davon abhält, direkt von der Beginne mit dem aktuellen Punkt. Zum Beispiel ist es der kürzeste Weg, in den Raum neben dir zu gehen, um die Mauer zu brechen, aber der wahrscheinlichste Weg ist, nach draußen zu gehen, nach links zu gehen und die andere Tür zu öffnen. – aczzdx