Ich brauche Hilfe mit meinem A * -Algorithmus. Es findet überhaupt keinen Weg. Ich dachte, es wäre mein Haufen, aber ich habe überprüft und es ist nicht. Könnte es mein Algorithmus sein? Ich verwende ein 2d-Array, um alle Knoten zu speichern. Das Array ist im Grunde eine Karte und einige Knoten sind Inseln (d. H. Nicht erreichbar). Wenn ich also die Nachfolger eines Knotens bekomme, bekomme ich alle Knoten, die keine Hindernisse sind.Ein * Algorithmus funktioniert nicht
public void findPath(Node startNode, Node targetNode) throws HeapFullException, HeapEmptyException {
Heap<Node> openSet = new Heap<Node>(this.width*this.height);
ArrayList<Node> closeSet = new ArrayList<Node>();
int gScore;
startNode.gCost = 0;
startNode.hCost = getDistance(startNode,targetNode);
openSet.add(startNode);
while(!openSet.isEmpty()){
Node current = openSet.removeFirst();
if(current == targetNode){
break;
}
closeSet.add(current);
ArrayList<Node> successors = getNeighbours(current);
for (int i=0;i<successors.size();i++){
Node successor = successors.get(i);
if (closeSet.contains(successor)){
continue;
}
gScore = current.gCost + getDistance(current,successor);
if (!openSet.contains(successor)){
openSet.add(successor);
}
else if (gScore >= current.gCost){
continue;
}
successor.parent = current;
successor.gCost = gScore;
successor.hCost = getDistance(successor,targetNode);
}
}
}
private ArrayList<Node> getNeighbours(Node node) {
ArrayList<Node> neighbours = new ArrayList<Node>();
int x = node.gridX;
int y = node.gridY;
int[][] positions = {{x-1,y+1}, {x-1,y},{x-1,y-1},{x,y+1},{x,y-1},{x+1,y+1}, {x+1,y},{x+1,y-1}};
for (int i=0 ; i < 8; i ++){
if(positionExists(positions[i][0],positions[i][1])){
if (map[positions[i][1]][positions[i][0]].walkable){
neighbours.add(map[positions[i][1]][positions[i][0]]);
}
}
}
return neighbours;
}
getDistance() verwendet nur die Entfernungsformel.
zuerst würde ich die Eingaben posten. Stellen Sie sicher, dass es einen Pfad gibt – efekctive
Ich habe eine Karte, die gedruckt wird, und es gibt immer einen Pfad – user1995933
wo ist getNeighbours (...)? – efekctive