Advertisement
Guest User

Untitled

a guest
Jun 30th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. public void findPath(Node startNode, Node targetNode)
  2. throws HeapFullException, HeapEmptyException {
  3. Heap<Node> openSet = new Heap<>(width * height); // this where we make use of our heaps
  4. Heap<Node> closedSet = new Heap<>(width * height);
  5. startNode.gCost = 0;
  6. startNode.hCost = getDistance(startNode, targetNode);
  7. openSet.add(startNode);
  8.  
  9. while (!openSet.isEmpty()) {
  10.  
  11. Node current = openSet.removeFirst();
  12. ArrayList<Node> neighbours = getNeighbours(current);
  13.  
  14. for (Node node : neighbours) {
  15. node.parent = current;
  16. }
  17.  
  18. for (Node node : neighbours) {
  19. if (node.equals(targetNode)) {
  20. return;
  21. }
  22. node.gCost = current.gCost + getDistance(current, node);
  23. node.hCost = getDistance(node, targetNode);
  24.  
  25. if (!openSet.contains(node) && openSet.peekTop().getFCost() < node.getFCost()) {
  26. openSet.add(node);
  27. }
  28. }
  29. closedSet.add(current);
  30. }
  31.  
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement