Advertisement
Guest User

PathFinder.java

a guest
Aug 28th, 2012
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.92 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Collections;
  3.  
  4. public class PathFinder
  5. {
  6.     private static final int COST_NORM = 10;
  7.     private static final int COST_DIAG = 14;
  8.    
  9.     private int[][] map;
  10.     private Node[][] nodeMap;
  11.    
  12.     private Node endNode;
  13.    
  14.     NodeList openList = new NodeList();
  15.     NodeList closedList = new NodeList();
  16.  
  17.  
  18.     public PathFinder(int[][] map)
  19.     {
  20.         this.map = map;
  21.         nodeMap = getNodes(map.length, map[0].length);
  22.     }
  23.  
  24.     public void findPath(Node start, Node end)
  25.     {
  26.         System.out.println();
  27.         openList.clear();
  28.         closedList.clear();
  29.        
  30.         openList.add(start);
  31.  
  32.         Node currentNode = null;
  33.         endNode = end;
  34.        
  35.         while (!openList.isEmpty())
  36.         {
  37.  
  38.             currentNode = openList.getSmallest();
  39.             //openList.remove(currentNode);
  40.             closedList.add(currentNode);
  41.            
  42.  
  43.             if (closedList.contains(end))
  44.             {
  45.                 System.out.println("Path Found");
  46.                 return;
  47.             }
  48.            
  49.             ArrayList<Movement> movements = getSuccessors(currentNode);
  50.             for (Movement movement : movements)
  51.             {
  52.                 Node node = movement.node;
  53.                
  54.                 if (movement == null)
  55.                     break;
  56.                
  57.                 if (closedList.contains(node))
  58.                     continue;
  59.            
  60.                 if (isWalkable(node))
  61.                 {
  62.                     if (!openList.contains(node))
  63.                     {
  64.                         node.parentNode = currentNode;
  65.                         node.totalCost = movement.getTotalCost();
  66.                         node.movementCost = movement.movementCost;
  67.                         openList.add(node);
  68.                     }
  69.                 }
  70.             }
  71.         }
  72.         System.out.println("Path Not Found");
  73.     }
  74.  
  75.     public ArrayList<Node> getPath()
  76.     {
  77.         ArrayList<Node> path = new ArrayList();
  78.         Node node = closedList.getLast();
  79.         path.add(node);
  80.         while ((node = node.parentNode) != null)
  81.         {
  82.             path.add(node);
  83.         }
  84.         // path.add (startNode);
  85.         Collections.reverse(path);
  86.         return path;
  87.     }
  88.  
  89.     public void displayPath()
  90.     {
  91.         for (Node n : getPath())
  92.         {
  93.             System.out.print("(" + n.xPosition + "," + n.yPosition + ")   ");
  94.         }
  95.     }
  96.  
  97.     /**
  98.      * * Returns true if the Tile represented by this node is walkable * * @param
  99.      * node * @return
  100.      */
  101.     boolean isWalkable(Node node)
  102.     {
  103.         return map[node.yPosition][node.xPosition] == 1;
  104.     }
  105.  
  106.     /**
  107.      * * Gets the Nodes surrounding the current node that can be walked on * @param
  108.      * currentNode * @return
  109.      */
  110.     public ArrayList<Movement> getSuccessors(Node currentNode)
  111.     {
  112.         ArrayList<Movement> movements = new ArrayList();
  113.         int x = currentNode.xPosition;
  114.         int y = currentNode.yPosition;
  115.         Node node = null;
  116.         int heuristicDistance;
  117.         int g = 0;
  118.  
  119.         if (x > 0) // LEFT
  120.         {
  121.             node = nodeMap[y][x - 1];
  122.             g = currentNode.movementCost + COST_NORM;
  123.             heuristicDistance = calculateHeuristic(node);
  124.             movements.add(new Movement(node, g, heuristicDistance));
  125.         }
  126.         if (x < nodeMap[y].length - 1) // RIGHT
  127.         {
  128.             node = nodeMap[y][x + 1];
  129.             g = currentNode.movementCost + COST_NORM;
  130.             heuristicDistance = calculateHeuristic(node);
  131.             movements.add(new Movement(node, g, heuristicDistance));
  132.         }
  133.         if (y > 0)
  134.         {
  135.             if (x > 0) // DIAGONAL Top-left
  136.             {
  137.                 node = nodeMap[y - 1][x - 1];
  138.                 g = currentNode.movementCost + COST_DIAG;
  139.                 heuristicDistance = calculateHeuristic(node);
  140.                 movements.add(new Movement(node, g, heuristicDistance));
  141.             }
  142.             if (x < map[y].length - 1) // DIAGONAL Top-Right
  143.             {
  144.                 node = nodeMap[y - 1][x + 1];
  145.                 g = currentNode.movementCost + COST_DIAG;
  146.                 heuristicDistance = calculateHeuristic(node);
  147.                 movements.add(new Movement(node, g, heuristicDistance));
  148.             }
  149.             node = nodeMap[y - 1][x]; // UP
  150.             g = currentNode.movementCost + COST_NORM;
  151.             heuristicDistance = calculateHeuristic(node);
  152.             movements.add(new Movement(node, g, heuristicDistance));
  153.         }
  154.         if (y < map.length - 1)
  155.         {
  156.             if (x > 0) // DIAGONAL Bottom-left
  157.             {
  158.                 node = nodeMap[y + 1][x - 1];
  159.                 g = currentNode.movementCost + COST_DIAG;
  160.                 heuristicDistance = calculateHeuristic(node);
  161.                 movements.add(new Movement(node, g, heuristicDistance));
  162.             }
  163.             if (x < map[y].length - 1) // DIAGONAL Bottom-Right
  164.             {
  165.                 node = nodeMap[x + 1][y + 1];
  166.                 g = currentNode.movementCost + COST_DIAG;
  167.                 heuristicDistance = calculateHeuristic(node);
  168.                 movements.add(new Movement(node, g, heuristicDistance));
  169.             }
  170.             node = nodeMap[y + 1][x]; // DOWN
  171.             g = currentNode.movementCost + COST_NORM;
  172.             heuristicDistance = calculateHeuristic(node);
  173.             movements.add(new Movement(node, g, heuristicDistance));
  174.         }
  175.         return movements;
  176.     }
  177.  
  178.     public int calculateHeuristic(Node cNode)
  179.     { // MANHATTAN
  180.         return Math.abs((cNode.xPosition - endNode.xPosition)
  181.                 + (cNode.yPosition - endNode.yPosition));
  182.         // EULIDEAN
  183.         // return (int) Math.sqrt(Math.pow((cNode.xPosition -
  184.         // endNode.xPosition), 2) + Math.pow((cNode.yPosition -
  185.         // endNode.yPosition), 2));
  186.     }
  187.  
  188.     private Node[][] getNodes(int height, int width)
  189.     {
  190.         Node[][] nodes = new Node[width][height];
  191.         for (int w = 0; w < nodes.length; w++)
  192.         {
  193.             for (int h = 0; h < nodes[w].length; h++)
  194.             {
  195.                 nodes[w][h] = new Node(w, h);
  196.             }
  197.         }
  198.         return nodes;
  199.     }
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement