Skip to content

Commit

Permalink
Separate availability & cost into Grid.Node
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas ADAM <[email protected]>
  • Loading branch information
tadam50 committed Jan 26, 2024
1 parent 65dee64 commit 01af579
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public final class DijkstraPathFinder implements PathFinder {
public List<Point> findShortestPath(Grid grid, Point start, Point goal) {
Set<Point> visited = new HashSet<>();
PriorityQueue<Grid.Node> queue = new PriorityQueue<>(Comparator.comparingDouble(node -> node.getCost() + node.getDistance()));
queue.add(new Grid.Node(start, 0, start.distance(goal)));
queue.add(new Grid.Node(start, true, start.distance(goal)));

while (!queue.isEmpty()) {
Grid.Node current = queue.poll();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ public class Grid {

static class Node {
private final Point point;
private boolean available;
private int cost;
private double distance;
private Node parent;

public Node(Point p, int cost, double distance) {
public Node(Point p, boolean available, double distance) {
this.point = p;
this.cost = cost;
this.available = available;
this.distance = distance;
this.parent = null;
}
Expand All @@ -46,9 +47,6 @@ public Node getParent() {
}
}

private static final int NOT_WALKABLE = -1;
private static final int WALKABLE = 0;

private final Node[][] nodes;
private final int width;
private final int height;
Expand All @@ -59,7 +57,7 @@ public Grid(int width, int height) {
this.nodes = new Node[width][height];
for (int x = 0; x < nodes.length; ++x) {
for (int y = 0; y < nodes[0].length; ++y) {
nodes[x][y] = new Node(new Point(x, y), NOT_WALKABLE, 0);
nodes[x][y] = new Node(new Point(x, y), false, 0);
}
}
}
Expand All @@ -83,7 +81,7 @@ private Node getNode(double x, double y) {
}

public void setAvailability(double x, double y, boolean available) {
getNode(x, y).cost = available ? WALKABLE : NOT_WALKABLE;
getNode(x, y).available = available;
}

public void setAvailability(Point point, boolean available) {
Expand All @@ -99,7 +97,7 @@ public boolean isAvailable(Node n) {
}

public boolean isAvailable(Point point) {
return point.getX() >= 0 && point.getX() < width && point.getY() >= 0 && point.getY() < height && nodes[(int) point.getX()][(int) point.getY()].cost != NOT_WALKABLE;
return point.getX() >= 0 && point.getX() < width && point.getY() >= 0 && point.getY() < height && nodes[(int) point.getX()][(int) point.getY()].available;
}

protected List<Node> getNeighbors(Point point) {
Expand Down

0 comments on commit 01af579

Please sign in to comment.