a star java_无法在java中实现A Star

我一直在努力让这个算法运行起来,但是我不能为我的生活做准备.我在网上阅读了很多教程,并在AS3,

javascript和C中阅读了源代码;但我无法适应我所看到的自己的代码.

我创建了一个AStar类,它有一个名为Node的嵌套类.该贴图是名为MAP的2D阵列.

我遇到的最大问题是在pathfind函数中拉出F值.

我已经实现了F = G H,我的问题是实际的AStar算法.有人可以帮忙,这是我到目前为止还有多远:

import java.util.ArrayList;

public class AStar

{

int MAP[][];

Node startNode, endNode;

public AStar(int MAP[][], int startXNode, int startYNode,

int endXNode, int endYNode)

{

this.MAP = MAP;

startNode = new Node(startXNode, startYNode);

endNode = new Node(endXNode, endYNode);

}

public void pathfinder()

{

ArrayList openList = new ArrayList();

ArrayList closedList = new ArrayList();

}

public int F(Node startNode, Node endNode)

{

return (H(startNode, endNode) + G(startNode));

}

//H or Heuristic part of A* algorithm

public int H(Node startNode, Node endNode)

{

int WEIGHT = 10;

int distance = (Math.abs(startNode.getX() - endNode.getX()) + Math.abs(startNode.getY() - endNode.getY()));

return (distance * WEIGHT);

}

public int G(Node startNode)

{

if(MAP[startNode.getX() - 1][startNode.getY()] != 1)

{

return 10;

}

if(MAP[startNode.getX() + 1][startNode.getY()] != 1)

{

return 10;

}

if(MAP[startNode.getX()][startNode.getY() -1] != 1)

{

return 10;

}

if(MAP[startNode.getX()][startNode.getY() + 1] != 1)

{

return 0;

}

return 0;

}

public class Node

{

private int NodeX;

private int NodeY;

private int gScore;

private int hScore;

private int fScore;

public Node(int NodeX, int NodeY)

{

this.NodeX = NodeX;

this.NodeY = NodeY;

}

public int getX()

{

return NodeX;

}

public int getY()

{

return NodeY;

}

public int getG()

{

return gScore;

}

public void setG(int gScore)

{

this.gScore = gScore;

}

public int getH()

{

return hScore;

}

public void setH(int hScore)

{

this.hScore = hScore;

}

public int getF()

{

return fScore;

}

public void setF(int fScore)

{

this.fScore = fScore;

}

}

}

这是我使用探路者功能所能达到的最远:

public void pathfinder()

{

LinkedList openList = new LinkedList();

LinkedList closedList = new LinkedList();

Node currentNode;

openList.add(startNode);

while(openList.size() > 0)

{

currentNode = (Node) openList.get(0);

closedList.add(currentNode);

for(int i = 0; i < openList.size(); i++)

{

int cost = F(currentNode, endNode);

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值