SHARE
TWEET

Untitled

a guest Mar 18th, 2019 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class AStarPathingStrategy implements PathingStrategy {
  2.  
  3.  
  4. public int manhattan(Point start, Point end) {
  5.  
  6.         int m = (Math.abs(start.x - end.x) + Math.abs(start.y - end.y));
  7.         return m;
  8.         }
  9.  
  10.     public List<Point> computePath(Point start, Point end,
  11.                                    Predicate<Point> canPassThrough,
  12.                                    BiPredicate<Point, Point> withinReach,
  13.                                    Function<Point, Stream<Point>> potentialNeighbors) {
  14.    
  15.         List<Point> pth = new ArrayList<>();
  16.         HashMap<Point, Point> parent = new HashMap<>();
  17.         HashMap<Point, Integer> length = new HashMap<>();
  18.         PriorityQueue<Cmp> q = new PriorityQueue<>(100000);
  19.         q.add(new Cmp(manhattan(start,end),start));
  20.         parent.put(start, start);
  21.         length.put(start, 0);
  22.         Cmp cur = null;
  23.         //System.out.println(q);
  24.         while (q.peek() != null) {
  25.             cur = q.poll();
  26.             System.out.println(cur);
  27.             if (cur.getP() == end) {
  28.                 break;
  29.             }
  30.             List<Point> neighbors = potentialNeighbors.apply(cur.getP())
  31.                     .filter(canPassThrough)
  32.                     .filter(p -> !p.equals(start) && !p.equals(end) && !parent.containsKey(p)).collect(Collectors.toList());
  33.             //System.out.println(neighbors);
  34.             for (Point p : neighbors) {
  35.                 if ((parent.get(p) == null || (length.get(cur.getP())) < length.get(parent.get(p)))) {
  36.                     parent.put(p, cur.getP());
  37.                     length.put(p, length.get(cur.getP()) + 1);
  38.                     q.add(new Cmp(manhattan(p, end) + length.get(p), p));
  39.                     //System.out.println(q);
  40.                 }
  41.             }
  42.         }
  43.  
  44.         Point p = cur.getP();
  45.         if (p == end){return pth;}
  46.         pth.add(0, p);
  47.             while (parent.get(p) != p) {
  48.                 p = parent.get(p);
  49.                 pth.add(0, p);
  50.             }
  51.  
  52.             return pth;
  53.         }
  54. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top