package model.world.graphs; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Set; import javafx.geometry.Pos; import org.jgrapht.GraphPath; import org.jgrapht.alg.interfaces.AStarAdmissibleHeuristic; import org.jgrapht.alg.shortestpath.AStarShortestPath; import org.jgrapht.graph.SimpleGraph; import model.world.Direction; import model.world.Position; /* * @TODO : fonction d'interaction avec le graphe * getVertex(POs(x,y)), isLInked(V1, V2) */ public class MappedGraph { protected SimpleGraph graph; private int sizeX; private int sizeY; protected MappedGraph(int sizeX, int sizeY) { this.sizeX = sizeX; this.sizeY = sizeY; this.graph = new SimpleGraph<>(Edge.class); } /* ======================================================================== * Construction methods [mainly for the MappedGraphBuilder] * ======================================================================== */ protected void addVertex(int x, int y) { graph.addVertex(new Vertex(x, y)); } protected void addEdge(int x0, int y0, int x1, int y1) { graph.addEdge(getVertex(x0, y0), getVertex(x1, y1), new Edge()); } protected Position addEdge(Position position, Direction direction) { Position neighbour = getNeighbor(position, direction); if (neighbour == null) { return null; } Position topLeft=position; Position botRight=neighbour; if (direction==Direction.LEFT || direction==Direction.TOP){ topLeft=neighbour; botRight=position; } addEdge(topLeft, botRight); addEdge(neighbour, position); return neighbour; } protected void addEdge(Position p1, Position p2) { Edge edge = new Edge(); graph.addEdge(getVertex(p1), getVertex(p2), edge); } protected void initializeVertex() { for (int i = 0; i < sizeX; i++) {//construction of the grid for (int j = 0; j < sizeY; j++) { Vertex v = new Vertex(i, j); graph.addVertex(v); } } } /* ======================================================================== * Gestion of the grid * ======================================================================== */ public boolean isInMap(Position P) { if (P == null) return false; return (P.x >= 0 && P.x < sizeX) && (P.y >= 0 && P.y < sizeY); } public Position getNeighbor(Position pos, Direction dir) { Position neighbor = new Position(pos.x + dir.dx, pos.y + dir.dy); if(neighbor.x < 0 || neighbor.x >= sizeX) return null; if(neighbor.y < 0 || neighbor.y >= sizeY) return null; return neighbor; } Position getValidAdj(Position pos, Direction dir) { int oldx=pos.x; int oldy=pos.y; int newx=oldx; int newy=oldy; switch(dir) { case LEFT: newx=oldx-1; if (newx<0) { return null; } break; case RIGHT://RIGHT newx=oldx+1; if (newx>=sizeX) { return null; } break; case TOP://TOP newy=oldy-1; if (newy<0) { return null; } break; case BOT://BOT newy=oldy+1; if (newy>=sizeY) { return null; } break; } return new Position(newx, newy); } /* ======================================================================== * Getters and Setters * ======================================================================== */ public int getSizeX() { return sizeX; } public int getSizeY() { return sizeY; } public Vertex getVertex(int x, int y) { Vertex v = new Vertex(x, y); return graph.containsVertex(v) ? v : null; } protected boolean isLinked(Position source, Position target) { return graph.getEdge(getVertex(source), getVertex(target)) != null; } public Vertex getVertex(Position pos) { return getVertex(pos.x, pos.y); } public Set getEdgeSet() { return graph.edgeSet(); } }