From acde8264af930792293afaee4cc608be14ae777d Mon Sep 17 00:00:00 2001 From: Ziver Koc Date: Mon, 16 Apr 2007 19:18:17 +0000 Subject: [PATCH] --- src/ei/game/algo/AStar.java | 111 ---------------------------- src/ei/game/algo/AStarNode.java | 123 -------------------------------- 2 files changed, 234 deletions(-) delete mode 100644 src/ei/game/algo/AStar.java delete mode 100644 src/ei/game/algo/AStarNode.java diff --git a/src/ei/game/algo/AStar.java b/src/ei/game/algo/AStar.java deleted file mode 100644 index 6d13150..0000000 --- a/src/ei/game/algo/AStar.java +++ /dev/null @@ -1,111 +0,0 @@ -package ei.game.algo; -import java.util.*; - -/** - The AStarSearch class, along with the AStarNode class, - implements a generic A* search algorithm. The AStarNode - class should be subclassed to provide searching capability. -*/ -public class AStar { - - - /** - A simple priority list, also called a priority queue. - Objects in the list are ordered by their priority, - determined by the object's Comparable interface. - The highest priority item is first in the list. - */ - public static class PriorityList extends LinkedList { - private static final long serialVersionUID = 1L; - - public void add(Comparable object) { - for (int i=0; i path = new LinkedList(); - while (node.pathParent != null) { - path.addFirst(node); - node = node.pathParent; - } - return path; - } - - private boolean contains(LinkedList list,AStarNode find){ - for(int i=0; i closedList = new LinkedList(); - - startNode.costFromStart = 0; - startNode.estimatedCostToGoal = - startNode.getEstimatedCost(goalNode); - startNode.pathParent = null; - openList.add(startNode); - - while (!openList.isEmpty()) { - AStarNode node = (AStarNode)openList.removeFirst(); - if (node == goalNode) { - // construct the path from start to goal - return constructPath(goalNode); - } - - List neighbors = node.getNeighbors(); - for (int i=0; i0)?1:(v<0)?-1:0; // sign function - } - - public Vector2i getPos(){ - return pos; - } - - /** - Gets the cost between this node and the specified - adjacent (AKA "neighbor" or "child") node. - */ - public float getCost(AStarNode node){ - if(pos.getX() != node.getPos().getX() && - pos.getY() != node.getPos().getY()){ - return 14; - } - return 10; - } - - - /** - Gets the estimated cost between this node and the - specified node. The estimated cost should never exceed - the true cost. The better the estimate, the more - effecient the search. - */ - public float getEstimatedCost(AStarNode node) { - int sum = 0; - if(pos.getX() > node.getPos().getX()) - sum += pos.getX()-node.getPos().getX(); - else - sum += node.getPos().getX()-pos.getX(); - - if(pos.getY() > node.getPos().getY()) - sum += pos.getY()-node.getPos().getY(); - else - sum += node.getPos().getY()-pos.getY(); - return sum; - } - - - /** - Gets the children (AKA "neighbors" or "adjacent nodes") - of this node. - */ - public List getNeighbors() { - LinkedList list = new LinkedList(); - Vector2i tempV; - - //********Vertical and Horisontle - tempV = new Vector2i(pos.getX()+1,pos.getY()); - if(tempV.getX() < InGameState.getMap().getSize().getX() && InGameState.getMap().isPosEmpty(tempV.getX(), tempV.getY())) - list.add(new AStarNode(tempV,this)); - - tempV = new Vector2i(pos.getX()-1,pos.getY()); - if(tempV.getX() >= 0 && InGameState.getMap().isPosEmpty(tempV.getX(), tempV.getY())) - list.add(new AStarNode(tempV,this)); - - tempV = new Vector2i(pos.getX(),pos.getY()+1); - if(tempV.getY() < InGameState.getMap().getSize().getY() && InGameState.getMap().isPosEmpty(tempV.getX(), tempV.getY())) - list.add(new AStarNode(tempV,this)); - - tempV = new Vector2i(pos.getX(),pos.getY()-1); - if(tempV.getX() >= 0 && InGameState.getMap().isPosEmpty(tempV.getX(), tempV.getY())) - list.add(new AStarNode(tempV,this)); - - //*********Diagonal - - tempV = new Vector2i(pos.getX()+1,pos.getY()+1); - if(tempV.getY() < InGameState.getMap().getSize().getY() && - tempV.getX() < InGameState.getMap().getSize().getX() && InGameState.getMap().isPosEmpty(tempV.getX(), tempV.getY())) - list.add(new AStarNode(tempV,this)); - - tempV = new Vector2i(pos.getX()-1,pos.getY()-1); - if(tempV.getY() >= 0 && tempV.getX() >= 0 && InGameState.getMap().isPosEmpty(tempV.getX(), tempV.getY())) - list.add(new AStarNode(tempV,this)); - - tempV = new Vector2i(pos.getX()-1,pos.getY()+1); - if(tempV.getY() < InGameState.getMap().getSize().getY() && - tempV.getX() >= 0 && InGameState.getMap().isPosEmpty(tempV.getX(), tempV.getY())) - list.add(new AStarNode(tempV,this)); - - tempV = new Vector2i(pos.getX()+1,pos.getY()-1); - if(tempV.getY() >= 0 && - tempV.getX() < InGameState.getMap().getSize().getX() && InGameState.getMap().isPosEmpty(tempV.getX(), tempV.getY())) - list.add(new AStarNode(tempV,this)); - - return list; - } -} \ No newline at end of file