Avoid obstacles #1

Merged
thib8956 merged 1 commits from obstacles into master 2018-09-30 14:40:05 +00:00
3 changed files with 30 additions and 5 deletions

View File

@ -1,5 +1,7 @@
package fr.uha.gasser.pathfinding.algorithm;
import fr.uha.gasser.pathfinding.model.Grid;
import java.awt.*;
import java.util.*;
import java.util.List;
@ -8,10 +10,12 @@ class AStarSearch {
private final Node start;
private final Node goal;
private final Grid grid;
AStarSearch(Node start, Node goal) {
AStarSearch(Node start, Node goal, Grid grid) {
this.start = start;
this.goal = goal;
this.grid = grid;
}
@ -57,15 +61,22 @@ class AStarSearch {
private List<Node> getNeighbors(Node current) {
LinkedList<Node> neighbors = new LinkedList<>();
for (Direction direction : Direction.values()) {
neighbors.add(getNeighbor(current, direction));
final Node neighbor = getNeighbor(current, direction);
// If neighbor is a wall or off the grid (null), don't add it to the list
if (neighbor != null) neighbors.add(neighbor);
}
return neighbors;
}
private Node getNeighbor(Node current, Direction direction) {
Node node = new Node(current);
node.setLoc(new Point(current.loc.x + direction.x, current.loc.y + direction.y));
final Dimension gridSize = grid.getSize();
final Node node = new Node(current);
final Point loc = new Point(current.loc.x + direction.x, current.loc.y + direction.y);
if (grid.get(loc).isWall()) return null;
// Don't overflow off the grid
if (loc.x > gridSize.width || loc.y > gridSize.height) return null;
node.setLoc(loc);
return node;
}

View File

@ -1,12 +1,18 @@
package fr.uha.gasser.pathfinding.algorithm;
import fr.uha.gasser.pathfinding.model.Grid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.swing.*;
import java.awt.*;
import java.util.Collections;
import java.util.List;
public class AStarWorker extends SwingWorker<List<Node>, List<Node>> {
private static final Logger LOGGER = LoggerFactory.getLogger(AStarWorker.class);
private final Node start;
private final Node goal;
private final Grid target;
@ -20,7 +26,7 @@ public class AStarWorker extends SwingWorker<List<Node>, List<Node>> {
@Override
protected List<Node> doInBackground() throws Exception {
AStarSearch aStar = new AStarSearch(start, goal);
AStarSearch aStar = new AStarSearch(start, goal, target);
path = aStar.shortestPath();
return path;
}
@ -28,6 +34,10 @@ public class AStarWorker extends SwingWorker<List<Node>, List<Node>> {
@Override
protected void done() {
if (this.isCancelled()) return;
if (path.equals(Collections.EMPTY_LIST)) {
LOGGER.warn("There is no path between the nodes {} and {}.", start, goal);
return;
}
path.forEach(node -> {
if (! node.loc.equals(goal.loc)) target.setColor(node.loc, Color.BLUE);
});

View File

@ -49,6 +49,10 @@ public class Grid implements Iterable<Cell> {
return new Cell(INVALID, null);
}
public final Dimension getSize() {
return size;
}
public void setColor(Point loc, Color color) {
final Cell c = cells.get(loc.x + size.width * loc.y);
final JLabel content = c.getContent();