我正在尝试实现A *寻路算法并遇到一些麻烦。根据我在互联网上找到的各种伪代码示例,我认为没有理由说它为什么不起作用。当前输出似乎更像是广度优先搜索算法。在尝试绘制结果后我注意到了这一点。如果有人能够发现我遗漏了什么或做错了什么,我将非常感激。相关代码如下:
public void Astar()
{
//priority queues automatically sorted (by f)
Node current = openNodes.peek(); //retrieves node with lowest f
//System.out.println("(" + current.x + ", " + current.y + ")");
//System.out.println(current.f);
//for drawing path (testing)
currents.add(current);
//if goal is reached
if (current.x == goal.x && current.y == goal.y) {
System.out.println("finished");
}
else {
openNodes.remove(current);
closedNodes.add(current);
// neighbor checks
for (int x = current.x - 1; x <= current.x + 1; x++) {
for (int y = current.y - 1; y <= current.y + 1; y++) {
if (x != current.x || y != current.y) {
if ((x == current.x - 1 && y == current.y + 1) ||
(x == current.x + 1 && y == current.y + 1) ||
(x == current.x - 1 && y == current.y - 1) ||
(x == current.x + 1 && y == current.y - 1))
{
Node DiagonalNeighbor = new Node(x, y, current.g + diag, goal); //diagonal node
if (!(closedNodes.contains(DiagonalNeighbor)) && !(openNodes.contains(DiagonalNeighbor)) && map[x][y] == 0) {
openNodes.add(DiagonalNeighbor);
}
}
else
{
Node Neighbor = new Node(x, y, current.g + 20, goal);
if (!(closedNodes.contains(Neighbor)) && !(openNodes.contains(Neighbor)) && map[x][y] == 0) {
openNodes.add(Neighbor);
}
}
}
}
}
}
}
我的节点类:
public class Node implements Comparable<Node>{
int x;
int y;
int g;
float h;
float f;
public Node(int x, int y, int g, Node goal){
this.x = x;
this.y = y;
this.g = g;
this.h = heuristic(goal);
f = this.g+h;
//f = h;
}
public Node(int x, int y){
this.x = x;
this.y = y;
}
// Overrides standard equals method (used to check if a list contains a specific node)
@Override
public boolean equals(Object other) {
if(this == other) {
return true;
}
if(other == null) {
return false;
}
if(getClass() != other.getClass()) {
return false;
}
Node test = (Node)other;
if(this.x == test.x && this.y == test.y) {
return true;
}
return false;
}
//used to sort lists (and sorts priority queue by f) -> [//Collections.sort(openNodes);] - sorts Array List by f values]
@Override
public int compareTo(Node n) {
return (int) (f - n.f);
}
public float heuristic(Node endNode) {
//euclidean distance
/*
float y = Math.abs (this.y - endNode.y); //y distance
float x = Math.abs (this.x - endNode.x); //x distance
float result = (float)Math.sqrt((y)*(y) +(x)*(x));
return result;
*/
//delta max
float x = Math.abs (this.x - endNode.x); //x distance
float y = Math.abs (this.y - endNode.y); //y distance
float result = Math.max(x,y);
return result;
}
}
值得注意的另一件事是,目标节点,正如其名称所暗示的那样,起始节点已经添加到openNode优先级队列中,并且将继续在运行的渲染方法中调用Astar方法无限期(我的最终目标是在游戏中使用它)。 closedNodes和current是ArrayLists,后者存储绘制的路径并且对象应该遍历(我在顶部谈到了这个)。我希望我已经提供了足够的信息。请不要犹豫,向我询问更多信息。
使用的网格是22x15。如果grid [x] [y] = 1则它是一个障碍,它等于0然后它是可遍历的。起始节点位于(20,13),目标节点位于(1,1,)。这是使用的网格。请注意,左下角是(0,0),右上角是(22,15)。我知道它很奇怪只是滚动它。
int[][] map = {
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,1,0,0,0,0,0,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,1,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1,1,1,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1,0,0,1},
{1,0,0,0,1,1,1,0,0,0,0,0,0,0,1},
{1,0,0,0,1,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,1,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,1,0,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,1,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,1,1,1,1,0,0,0,0,1},
{1,0,0,0,0,0,0,0,1,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,1,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, //22
};
当我打印出当前节点路径时,我得到以下内容:
(20,13) (19,13) (20,12) (19,12) (18,13) (20,11) (18,12) (19,11) (18,11) (17,13) (20,10) (17,12) (19,10) (17,11) (18,10) (16,13) (20,9) (17,10) (16,12) (19,9) (16,11) (15,13) (16,10) (17,3) (15,12) (16,9) (15,11) (14,13) (15,10) (17,18) (14,12) (15,9) (16,8) (14,11) (13,13) (15,8) (14,10) (13,12) (17,7) (14,9) (16,8) (13,11) (12,13) (14,18) (15,7) (13,10) (12,12) (17,6) (14,7) (13,9) (16,6) (12,11) (11,13) (13,8) (15,6) (12,10) (11,12) (13,7) (17,5) (14,16) (12,9) (11,11) (16,5) (13,6) (18,5) (12,8) (10,13) (11,10) (15,5) (10,12) (12,7) (17,4) (14,15) (11,9) (10,11) (12,6) (16,4) (13,5) (18,4) (19,5) (11,8) (9,13) (10,10) (15,4) (12,5) (11,7) (19,4) (19,6) (9,12) (17,3) (14,4) (10,9) (9,11) (11,6) (16,3) (13,4) (18,3) (10,8) (20,5) (8,13) (9,10) (11,5) (15,3) (10,7) (19,3) (19,7) (20,6) (8,12) (20,4) (17,2) (9,9) (14,3) (10,6) (8,11) (20,7) (16,2) (20,3) (13,3) (9,8) (18,2) (7,13) (10,5) (8,10) (9,7) (19,2) (7,12) (17,1) (8,9) (14,2) (9,6) (16,1) (8,8) (20,2) (13,2) (18,1) (6,13) (9,5) (7,10) (15,1) (8,7) (19,1) (6,12) (7,9) (14,1) (7,8) (20,1) (13,1) (5,13) (6,10) (7,7) (5,12) (6,9) (7,6) (6,8) (4,13) (6,7) (4,12) (6,6) (7,5) (4,11) (5,8) (6,5) (5,7) (5,6) (7,4) (4,10) (4,8) (5,5) (6,4) (4,7) (5,4) (4,6) (7,3) (3,8) (4,5) (6,3) (8,3) (3,7) (4,4) (5,3) (3,6) (7,2) (4,3) (2,8) (3,5) (6,2) (8,2) (9,3) (2,7) (3,4) (2,9) (5,2) (9,2) (2,6) (7,1) (4,2) (1,8) (2,5) (6,1) (3,2) (8,1) (10,3) (1,7) (2,4) (1,9) (5,1) (2,10) (9,1) (1,6) (10,2) (2,3) (4,1) (1,10) (1,5) (10,1) (2,2) (3,1) (11,3) (1,4) (2,1) (2,11) (11,2) (1,3) (1,11) (1,2) (11,1) (1,1) 完成
我希望我可以告诉你它绘制它时的外观,但我必须能够发送视频来做到这一点。