我正在尝试用 Java 构建 AI 路径查找应用程序。它在 Eclipse 中正常工作,没有错误,但我在创建 jar 文件时收到编译警告,因此它没有运行。错误来自以下类源代码。请帮我!谢谢
package aStar;
import java.util.ArrayList;
import java.util.Collections;
import aStar.util.ClosestHeuristic;
/**
* The class which uses AStar based algorithm to determine the path.
* @author Dorji Gyaltsen
*/
public class AStarPathFinder{
/** The set of nodes which have been searched(ClosedNode)
* and yet to be searched(OpenNode) */
private ArrayList closedNode = new ArrayList();
private OpenList openNode = new OpenList();
/** The instance of the map being searched */
private Map map;
/** The maximum searchDepth of search */
private int maxSearchDistance;
/** The total set of nodes in the map */
private Node[][] nodes;
/** allowance of diagonal movement */
private boolean DiagonalMovement;
/** The heuristic to determine which node to search first */
private AStarHeuristic heuristic;
/**
* Constructor initialising all the variables
*/
public AStarPathFinder(Map newMap, int newMaxSearchDistance,
boolean newDiagonalMovement, ClosestHeuristic newheuristic) {
map = newMap;
maxSearchDistance = newMaxSearchDistance;
DiagonalMovement = newDiagonalMovement;
heuristic = newheuristic;
/**Node definition and initialisation **/
nodes = new Node[map.getWidth()][map.getHeight()];
for (int x=0;x<nodes.length;x++)
for (int y=0;y<nodes.length;y++)
nodes[x][y] = new Node(x,y);
}
/**
* The main method which find the path based on AStar algorithm **/
public Path searchShortestPath(UnitMover mover, int sx, int sy, int dx, int dy) {
//if the path is blocked
if (map.pathBlocked(mover, dx, dy)){
return null;
}else {
//if the path is not blocked
nodes[sx][sy].pathCost = 0;
nodes[sx][sy].searchDepth = 0;
closedNode.clear();
openNode.clear();
openNode.add(nodes[sx][sy]);
nodes[dx][dy].parent = null;
// When the destination is not reached
int maxSearchDepth = 0;
while ((maxSearchDepth < maxSearchDistance) && (openNode.size() != 0)) {
Node current = getFirstOfOpenNode();
/**if current node is the destination node then stop**/
if (current == nodes[dx][dy]) {
break;
}
else {
clearFromOpenNode(current);
addToClosedNode(current);
// search all curentNode neighbours considering as the next closest node
for (int x=-1;x<2;x++) {
for (int y=-1;y<2;y++) {
// if it is the current node
if ((x == 0) && (y == 0)) {
continue;
}
else if (!DiagonalMovement){
if ((x != 0) && (y != 0))
continue;
}
else {
// determine the location of the neighbour and evaluate it
int xn = x + current.x;
int yn = y + current.y;
if (isValidLocation(mover,sx,sy,xn,yn)) {
float nextNodePathCost = current.pathCost + map.getPathCost(mover, current.x, current.y, xn, yn);
Node neighbour = nodes[xn][yn];
map.pathFinderVisited(xn, yn);
// if the new pathCost we've determined for this node is lower than
// it has been previously makes sure the node hasn't been discarded.
if (nextNodePathCost < neighbour.pathCost) {
if (inopenNode(neighbour))
clearFromOpenNode(neighbour);
else if (inclosedNode(neighbour))
clearFromclosedNode(neighbour);
}
// if the node hasn't already been processed and discarded then
// reset it's pathCost to our current pathCost and add it as a next possible
if (!inopenNode(neighbour) && !(inclosedNode(neighbour))) {
neighbour.pathCost = nextNodePathCost;
neighbour.heuristic = heuristic.getPathCost(map, mover, xn, yn, dx, dy);
maxSearchDepth = Math.max(maxSearchDepth, neighbour.setParentNode(current));
addToopenNode(neighbour);
}
}
}
}
}
}
}
// since we've got an empty openNode openList or we've run out of search
// there was no path. Just return null
if (nodes[dx][dy].parent == null)
return null;
// At this point we've definitely found a path so we can uses the parent
// references of the nodes to find out way from the destination location back
// to the start recording the nodes on the way.
Path path = new Path();
Node destination = nodes[dx][dy];
while (destination != nodes[sx][sy]) {
path.prependStep(destination.x, destination.y);
destination = destination.parent;
}
path.prependStep(sx,sy);
// thats it, we have our path
return path;
}
}
/**
* Get the first element from the openNode openList. This is the next
* one to be searched.
*
* @return The first element in the openNode openList
*/
protected Node getFirstOfOpenNode() {
return (Node) openNode.first();
}
/**
* Add a node to the openNode openList
*
* @param node The node to be added to the openNode openList
*/
protected void addToopenNode(Node node) {
openNode.add(node);
}
/**
* Check if a node is in the openNode openList
*
* @param node The node to check for
* @return True if the node given is in the openNode openList
*/
protected boolean inopenNode(Node node) {
return openNode.contains(node);
}
/**
* clear a node from the openNode openList
*
* @param node The node to clear from the openNode openList
*/
protected void clearFromOpenNode(Node node) {
openNode.clear();
}
/**
* Add a node to the closedNode openList
*
* @param node The node to add to the closedNode openList
*/
protected void addToClosedNode(Node node) {
closedNode.add(node);
}
/**
* Check if the node supplied is in the closedNode openList
*
* @param node The node to search for
* @return True if the node specified is in the closedNode openList
*/
protected boolean inclosedNode(Node node) {
return closedNode.contains(node);
}
/**
* clear a node from the closedNode openList
*
* @param node The node to clear from the closedNode openList
*/
protected void clearFromclosedNode(Node node) {
closedNode.clear();
}
/**
* Check if a given location is valid for the supplied mover
*
* @param mover The mover that would hold a given location
* @param sx The starting x coordinate
* @param sy The starting y coordinate
* @param x The x coordinate of the location to check
* @param y The y coordinate of the location to check
* @return True if the location is valid for the given mover
*/
protected boolean isValidLocation(UnitMover mover, int sx, int sy, int x, int y) {
if((x < 0) || (y < 0) || (x >= map.getWidth()) || (y >= map.getHeight())){
return false;
}
else{
if ((sx != x) || (sy != y))
if(!map.pathBlocked(mover, x, y));
return true;
}
}
/**
* A simple sorted openList
* @author Dorji Gyaltsen
*/
private class OpenList {
/** The openList of elements */
private ArrayList openList = new ArrayList();
/**
* @return The first element from the openList
*/
public Object first() {
return openList.get(0);
}
/**
* Empty the openList
*/
public void clear() {
openList.clear();
}
/**
* Adding an element to open ist and sorting
*/
@SuppressWarnings("unchecked")
public void add(Object o) {
openList.add(o);
Collections.sort(openList);
}
/**
* @return The total number of element in the openList
*/
public int size() {
return openList.size();
}
/**
* @return True if the element is in the openList
*/
public boolean contains(Object o) {
return openList.contains(o);
}
}
/**
* A single node in the search graph
*/
private class Node implements Comparable {
/** The x and y coordinates of the node */
private int x;
private int y;
/** The path pathCost for this node */
private float pathCost;
/** The parent of this node*/
private Node parent;
/** The heuristic pathCost of this node */
private float heuristic;
/** The search searchDepth of this node */
private int searchDepth;
/**
* The creation of a new node
* @param x and y The x and y coordinates of the node
*/
public Node(int x1, int y1) {
x = x1;
y = y1;
}
/**
* Set the parent for this node
* @param parent The parent node which lead to this node
* @return The searchDepth for searching
*/
public int setParentNode(Node parent) {
searchDepth = parent.searchDepth + 1;
this.parent = parent;
return searchDepth;
}
/**
* compareTo(Object)
*/
public int compareTo(Object other) {
Node o = (Node) other;
float f = heuristic + pathCost;
float of = o.heuristic + o.pathCost;
if (f < of) return -1;
else if (f > of) return 1;
else return 0;
}
}
}