I'm trying to implement A* search with the following code:
void Map::findPath(Robot robot, Model target, vector<Node> nodeList) {
Node targetNode = target.getCurrentNode();
Node startNode = robot.getCurrentNode();
vector<Node> openList;
vector<Node> closedList;
openList.push_back(startNode);
while (!openList.empty()) {
Node curNode = nodeWithLowestFScore(openList);
if (curNode.equal(targetNode)) {
/*cout << "WTF" << endl;
Node *p = curNode.getParent();
int i = 0;
while (!p->equal(startNode)) {
cout << p->getX() << " " << p->getY() << endl;
p = p->getParent();
cout << i++ << endl;
}*/
break;
}
closedList.push_back(curNode);
removeFromVector(openList, curNode);
vector<Node> adjNodes;
curNode.getWalkableAdjacentNodes(nodeList, adjNodes);
for (int i = 0; i < adjNodes.size(); i++) {
if (inVector(closedList, adjNodes[i])) {
continue;
}
if (!inVector(closedList, adjNodes[i])) {
adjNodes[i].setParent(&curNode);
cout << "Child: " <<adjNodes[i].getX() << " " << adjNodes[i].getY() << endl;
cout << "Parent: " << adjNodes[i].getParent()->getX()
<< " " << adjNodes[i].getParent()->getY() << endl;
adjNodes[i].setG(curNode.getG() + 1);
adjNodes[i].setH(adjNodes[i].getDistance(targetNode, 'm'));
adjNodes[i].setF();
openList.push_back(adjNodes[i]);
}
if (inVector(closedList, adjNodes[i])) {
if (curNode.getG() + 1 < adjNodes[i].getG()) {
adjNodes[i].setParent(&curNode);
adjNodes[i].setG(curNode.getG() + 1);
adjNodes[i].setF();
}
}
}
}
}
Here's the console output of this code(StartP = [x:-7, y:6], EndP = [x:0, y:-2]:
- Child: -1 -2
- Parent: 0 -2
- Child: 1 -2
- Parent: 0 -2
- Child: -2 -2
- Parent: -1 -2
- Child: -1 -3
- Parent: -1 -2
- Child: -3 -2
- Parent: -2 -2
- Child: -3 -1
- Parent: -3 -2
- Child: -3 0
- Parent: -3 -1
- Child: -3 1
- Parent: -3 0
- Child: -4 0
- Parent: -3 0
- Child: -5 0
- Parent: -4 0
- Child: -5 1
- Parent: -5 0
- Child: -5 -1
- Parent: -5 0
- Child: -5 2
- Parent: -5 1
- Child: -5 3
- Parent: -5 2
- Child: -5 4
- Parent: -5 3
- Child: -5 5
- Parent: -5 4
- Child: -6 4
- Parent: -5 4
- Child: -4 4
- Parent: -5 4
- Child: -7 4
- Parent: -6 4
- Child: -8 4
- Parent: -7 4
- Child: -5 6
- Parent: -5 5
- Child: -5 7
- Parent: -5 6
- Child: -4 6
- Parent: -5 6
- Child: -3 2
- Parent: -3 1
- Child: -3 3
- Parent: -3 2
- Child: -2 2
- Parent: -3 2
- Child: -3 4
- Parent: -3 3
- Child: -4 4
- Parent: -3 4
- Child: -2 4
- Parent: -3 4
- Child: -1 4
- Parent: -2 4
- Child: -1 2
- Parent: -2 2
- Child: -3 6
- Parent: -4 6
- Child: -5 8
- Parent: -5 7
- Child: -9 4
- Parent: -8 4
- Child: -5 -2
- Parent: -5 -1
- Child: -1 -4
- Parent: -1 -3
- Child: 2 -2
- Parent: 1 -2
- Child: 3 -2
- Parent: 2 -2
- Child: 2 -3
- Parent: 2 -2
- Child: -2 -4
- Parent: -1 -4
- Child: -3 -4
- Parent: -2 -4
- Child: -3 -5
- Parent: -3 -4
- Child: -5 -3
- Parent: -5 -2
- Child: -9 5
- Parent: -9 4
- Child: -9 6
- Parent: -9 5
- Child: -8 6
- Parent: -9 6
- Child: -7 6
- Parent: -8 6
It's a bit messy but I followed it. It reaches the starting point. (-7, 6)-> (-8, 6) -> (-9, 6) ... (-1, -2)->(0, -2).
However, if I uncomment the line that starts with "WTF" and want to back trace it it just gets into an infinite loop that prints (-7, 6) which is my EndP.
Any help is much appreciated.
Okay problematic line was this: adjNodes[i].setParent(&curNode);
I just had to reallocate a new Node, copy the contents of curNode to it and set that one as parent. Algorithm was fine.