私は初心者です。私はそのような厄介な問題に直面しています。スターの経路探索|六角形のグリップ
です。
アルゴリズムが余分な16進数を飲み込んだようです。 誰かが私の間違いを指摘できますか?
だからここでは、コードです:
1.Pathfinding funcを(1に表示され、2 '障害' が設定されている 'OB'):
private static Node pathFinding(Vector2i startHex, Vector2i goalHex, HashSet<Vector2i> ob, int movesLeft) {
start = new Node(startHex);
goal = new Node(goalHex);
if (distance(start, goal) > movesLeft) return null;
TreeSet<Node> openSet = new TreeSet<>(new NodeComparator());
HashSet<Node> closedSet = new HashSet<>();
start.cameFrom = null;
start.g = 0;
start.h = distance(start, goal);
start.f = start.g + start.h;
openSet.add(start);
float tentativeScore;
while (!openSet.isEmpty()) {
current = openSet.pollFirst();
if (current.coords.equals(goal.coords)) {
return current;
}
closedSet.add(current);
for (Node node : getNeighbors(current, ob)) {
node.g = distance(start, node);
tentativeScore = current.g + distance(current, node);
if (!closedSet.contains(node) || tentativeScore < node.g) {
node.g = tentativeScore;
node.h = distance(node, goal);
node.f = node.g + node.h;
node.cameFrom = current;
openSet.add(node);
}
}
}
return null;
}
2.Neighbors :
private static final Vector2i[] directions2i = {
new Vector2i(0,-1), new Vector2i(1,-1), new Vector2i(1,0),
new Vector2i(0,1), new Vector2i(-1,1), new Vector2i(-1,0)
};
private static List<Node> getNeighbors(Node origin, HashSet<Vector2i> ob) {
List<Node> list = new ArrayList<>();
for (int i = 0; i < 6; i++) {
Vector2i dir = new Vector2i(origin.coords.x + directions2i[i].x, origin.coords.y + directions2i[i].y);
if (!ob.contains(dir))
list.add(new Node(dir));
}
return list;
}
3.Heuristic:
static float distance(Node a, Node b) {
return (Math.abs(a.coords.x - b.coords.x) + Math.abs(a.coords.y - b.coords.y) + Math.abs(a.coords.x +a.coords.y -b.coords.x -b.coords.y))/2;
}
4.And念のために、ここでノードと比較するクラスです:
public class Node {
public Node cameFrom;
public Vector2i coords;
float g, h, f;
@Override
public int hashCode() {
return coords.x * 100000 + coords.y;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (getClass() != o.getClass()) return false;
Node oth = (Node) o;
return this.coords.equals(oth.coords);
}
Node(Vector2i coords) {
this.coords = new Vector2i(coords);
}
}
private static class NodeComparator implements Comparator<Node> {
@Override
public int compare(Node o1, Node o2) {
return Float.compare(o1.f, o2.f);
}
}
答えはありませんが、コードは読んで理解しにくく、特に見るだけでは「デバッグ」するのが難しいです。しかし、自分で見つけられなかった場合に備えて、http://www.redblobgames.com/grids/hexagonsには、六角形のグリッドに関する優れたリソースがあります(隣人探索、距離計算、障害物を含む経路探索を含む) – Marco13
'openSet.add'は' false'を返しましたか? – harold
@ Marco13有益な情報ありがとう! – GrastaSsS