Merge branch 'lastDijsktraClasses' of antograb/TpMazeJava into master
This commit was merged in pull request #7.
This commit is contained in:
commit
c3e74a20f3
27 files changed
+526
-288
No files matched your search
@@ -0,0 +1,4 @@
|
|||||||
|
*.html
|
||||||
|
*.css
|
||||||
|
src/package-list
|
||||||
|
src/script.js
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
|
|
||||||
public class ABox extends Box{
|
|
||||||
|
|
||||||
public ABox (String label, int x, int y, Maze maze) {
|
|
||||||
|
|
||||||
super(label,x,y,maze) ;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isArrival() {
|
|
||||||
return true ;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
|
|
||||||
|
|
||||||
public interface ASetInterface {
|
|
||||||
|
|
||||||
/** Add a vertex to the ASet */
|
|
||||||
public void add(VertexInterface vertex) ;
|
|
||||||
|
|
||||||
/** Check if a vertex belongs to ASet */
|
|
||||||
public boolean contains(VertexInterface vertex) ;
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
-107
@@ -1,107 +0,0 @@
|
|||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public abstract class Box
|
|
||||||
implements VertexInterface {
|
|
||||||
|
|
||||||
private int x ;
|
|
||||||
private int y ;
|
|
||||||
private String label ;
|
|
||||||
private Maze maze ;
|
|
||||||
private List<Box> neighbourList = new ArrayList<Box>() ;
|
|
||||||
|
|
||||||
public Box (String label, int x, int y, Maze maze) {
|
|
||||||
|
|
||||||
this.label = label ;
|
|
||||||
this.x = x ;
|
|
||||||
this.y = y ;
|
|
||||||
this.maze = maze ;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<Box> generateNeighbors() {
|
|
||||||
|
|
||||||
Box[][] vMaze = maze.getMaze() ;
|
|
||||||
|
|
||||||
if (x > 0 && vMaze[x-1][y].isWalkable()) {
|
|
||||||
neighbourList.add(vMaze[x-1][y]) ;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (x < maze.getDimensionX()-1 && vMaze[x+1][y].isWalkable()) {
|
|
||||||
neighbourList.add(vMaze[x+1][y]) ;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (y > 0 && vMaze[x][y-1].isWalkable()) {
|
|
||||||
neighbourList.add(vMaze[x][y-1]) ;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (y < maze.getDimensionY()-1 && vMaze[x][y+1].isWalkable()) {
|
|
||||||
neighbourList.add(vMaze[x][y+1]) ;
|
|
||||||
}
|
|
||||||
|
|
||||||
return neighbourList ;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean compareTo(VertexInterface vertex) {
|
|
||||||
if (this.getLabel() == vertex.getLabel()) {
|
|
||||||
return true ;
|
|
||||||
}
|
|
||||||
return false ;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isWalkable() {
|
|
||||||
return true ;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isArrival() {
|
|
||||||
return false ;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isDeparture() {
|
|
||||||
return false ;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<Box> getNeighbourList() {
|
|
||||||
return neighbourList;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setNeighbourList(List<Box> neighbourList) {
|
|
||||||
this.neighbourList = neighbourList;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getX() {
|
|
||||||
return x;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setX(int x) {
|
|
||||||
this.x = x;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getY() {
|
|
||||||
return y;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setY(int y) {
|
|
||||||
this.y = y;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getLabel() {
|
|
||||||
return label;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setLabel(String label) {
|
|
||||||
this.label = label;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Maze getMaze() {
|
|
||||||
return maze;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setMaze(Maze maze) {
|
|
||||||
this.maze = maze;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,44 +0,0 @@
|
|||||||
import java.util.ArrayList;
|
|
||||||
|
|
||||||
public class Dijkstra {
|
|
||||||
|
|
||||||
private PreviousInterface dijkstra(GraphInterface g, VertexInterface r, ASetInterface a, PiInterface pi, PreviousInterface previous) {
|
|
||||||
|
|
||||||
a.add(r) ; ;
|
|
||||||
VertexInterface pivot = r ;
|
|
||||||
pi.setPi(0, r) ;
|
|
||||||
|
|
||||||
ArrayList<VertexInterface> vertexList = g.getVertexes() ;
|
|
||||||
|
|
||||||
for (VertexInterface vertex: vertexList) {
|
|
||||||
if (vertex != r) {
|
|
||||||
pi.setPi(Integer.MAX_VALUE, vertex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int n = g.getVertexNumber();
|
|
||||||
for (int j = 0; j < n; j++) {
|
|
||||||
for (VertexInterface vertex: vertexList) {
|
|
||||||
if (! a.contains(vertex) && previous.getPrevious(vertex).compareTo(pivot)) {
|
|
||||||
if (pi.getPi(pivot) + g.getCost(pivot, vertex) < pi.getPi(vertex)) {
|
|
||||||
pi.setPi(pi.getPi(pivot) + g.getCost(pivot, vertex), vertex) ;
|
|
||||||
previous.setPrevious(pivot, vertex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int piMax = Integer.MAX_VALUE ;
|
|
||||||
for (VertexInterface vertex: vertexList) {
|
|
||||||
if (! a.contains(vertex) && pi.getPi(vertex) < piMax) {
|
|
||||||
pivot = vertex ;
|
|
||||||
piMax = pi.getPi(vertex) ;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
a.add(pivot);
|
|
||||||
}
|
|
||||||
|
|
||||||
return previous ;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
import java.util.ArrayList;
|
|
||||||
|
|
||||||
public interface GraphInterface {
|
|
||||||
|
|
||||||
/** Returns the number of Vertexes */
|
|
||||||
public int getVertexNumber() ;
|
|
||||||
|
|
||||||
/** Return the cost between two vertexes */
|
|
||||||
public int getCost(VertexInterface start, VertexInterface end) ;
|
|
||||||
|
|
||||||
/** Returns an list of Vertexes */
|
|
||||||
public ArrayList<VertexInterface> getVertexes() ;
|
|
||||||
|
|
||||||
}
|
|
||||||
+12
-3
@@ -1,12 +1,21 @@
|
|||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
import dijkstra.*;
|
||||||
|
import maze.*;
|
||||||
|
|
||||||
public class MainTest {
|
public class MainTest {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
// TODO Auto-generated method stub
|
|
||||||
|
|
||||||
Maze mazeTest = new Maze("data/labyrinthe.txt") ;
|
Maze mazeTest = new Maze("data/labyrinthe.txt") ;
|
||||||
//mazeTest.saveToTextFile("data/out.txt");
|
|
||||||
System.out.println("\n" + mazeTest);
|
Previous previous = (Previous) Dijkstra.dijkstra(mazeTest, mazeTest.getMaze()[1][1]);
|
||||||
|
|
||||||
|
ArrayList<VertexInterface> shortest = previous.getShortestPathTo(mazeTest.getMaze()[5][5]);
|
||||||
|
|
||||||
|
for (VertexInterface vertex : shortest) {
|
||||||
|
System.out.println(vertex);
|
||||||
|
}
|
||||||
|
|
||||||
mazeTest.saveToTextFile("data/labyOutput.txt");
|
mazeTest.saveToTextFile("data/labyOutput.txt");
|
||||||
|
|
||||||
|
|||||||
@@ -1,18 +0,0 @@
|
|||||||
|
|
||||||
public class MazeReadingException
|
|
||||||
extends Exception {
|
|
||||||
|
|
||||||
private String filename ;
|
|
||||||
|
|
||||||
public MazeReadingException (String filename) {
|
|
||||||
|
|
||||||
super("Invalid maze format : " + filename) ;
|
|
||||||
this.filename = filename ;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getFilename() {
|
|
||||||
return filename ;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
|
|
||||||
public interface PiInterface {
|
|
||||||
|
|
||||||
/** Returns the value of pi(v) */
|
|
||||||
public int getPi(VertexInterface vertex) ;
|
|
||||||
|
|
||||||
/** Sets the Pi value of an element */
|
|
||||||
public void setPi(int i, VertexInterface vertex) ;
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
|
|
||||||
public interface PreviousInterface {
|
|
||||||
|
|
||||||
/** Returns the father of an element */
|
|
||||||
public VertexInterface getPrevious(VertexInterface vertex) ;
|
|
||||||
|
|
||||||
/** Sets the father of an element to another element */
|
|
||||||
public void setPrevious(VertexInterface father, VertexInterface son) ;
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
|
|
||||||
public interface VertexInterface {
|
|
||||||
|
|
||||||
/** Compare a Vertex to another */
|
|
||||||
public boolean compareTo(VertexInterface vertex) ;
|
|
||||||
|
|
||||||
public String getLabel() ;
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
package dijkstra;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
|
||||||
|
public class ASet implements ASetInterface {
|
||||||
|
|
||||||
|
private HashSet<VertexInterface> vertexHashSet = new HashSet<VertexInterface>() ;
|
||||||
|
|
||||||
|
|
||||||
|
public ASet(HashSet<VertexInterface> vertexHashSet) {
|
||||||
|
|
||||||
|
this.vertexHashSet = new HashSet<VertexInterface>(vertexHashSet);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void add(VertexInterface vertex) {
|
||||||
|
|
||||||
|
vertexHashSet.add(vertex) ;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean contains(VertexInterface vertex) {
|
||||||
|
|
||||||
|
return vertexHashSet.contains(vertex);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public HashSet<VertexInterface> getVertexHashSet() {
|
||||||
|
return vertexHashSet;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
package dijkstra;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This is the ASet class as described in the INF101 course of Telecom Paristech.
|
||||||
|
* The ASet contains vertexes for which the shortest path has already been found (vertexes already examined by the dijkstra algorithm).
|
||||||
|
*/
|
||||||
|
public interface ASetInterface {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a vertex to the set.
|
||||||
|
* @param vertex
|
||||||
|
* The element to be added to the set.
|
||||||
|
*/
|
||||||
|
public void add(VertexInterface vertex) ;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if a vertex belongs to the set.
|
||||||
|
* @param vertex
|
||||||
|
* The element we must check if it belongs to the set.
|
||||||
|
* @return <code>true</code> if the element is in the set, <code>false</code> if not.
|
||||||
|
*/
|
||||||
|
public boolean contains(VertexInterface vertex) ;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the HashSet which represents the ASet.
|
||||||
|
* @return A <code>HashSet</code> containing the same elements as in the set.
|
||||||
|
*/
|
||||||
|
public HashSet<VertexInterface> getVertexHashSet();
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,65 @@
|
|||||||
|
package dijkstra;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Hashtable;
|
||||||
|
|
||||||
|
public class Dijkstra {
|
||||||
|
|
||||||
|
private static PreviousInterface dijkstra(GraphInterface g, VertexInterface r, ASetInterface a, PiInterface pi, PreviousInterface previous) {
|
||||||
|
|
||||||
|
a.add(r) ;
|
||||||
|
VertexInterface pivot = r ;
|
||||||
|
pi.setPi(0, r) ;
|
||||||
|
|
||||||
|
ArrayList<VertexInterface> vertexList = g.getVertexes() ;
|
||||||
|
|
||||||
|
for (VertexInterface vertex: vertexList) {
|
||||||
|
if (vertex != r) {
|
||||||
|
pi.setPi(Integer.MAX_VALUE, vertex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int n = g.getVertexNumber();
|
||||||
|
|
||||||
|
for (int j = 0; j < n; j++) {
|
||||||
|
for (VertexInterface vertex: g.generateNeighbours(pivot)) {
|
||||||
|
if (!a.contains(vertex)) {
|
||||||
|
if (pi.getPi(pivot) + g.getCost(pivot, vertex) < pi.getPi(vertex)) {
|
||||||
|
pi.setPi(pi.getPi(pivot) + g.getCost(pivot, vertex), vertex) ;
|
||||||
|
previous.setPrevious(pivot, vertex) ;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int piMax = Integer.MAX_VALUE ;
|
||||||
|
for (VertexInterface vertex: vertexList) {
|
||||||
|
if (! a.contains(vertex) && pi.getPi(vertex) < piMax) {
|
||||||
|
pivot = vertex ;
|
||||||
|
piMax = pi.getPi(vertex) ;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
a.add(pivot) ;
|
||||||
|
}
|
||||||
|
|
||||||
|
return previous ;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static PreviousInterface dijkstra(GraphInterface g, VertexInterface r) {
|
||||||
|
|
||||||
|
HashSet<VertexInterface> vertexHashSet = new HashSet<VertexInterface>() ;
|
||||||
|
ASet aSet = new ASet(vertexHashSet) ;
|
||||||
|
|
||||||
|
Hashtable<VertexInterface, Integer> hashPi = new Hashtable<VertexInterface, Integer>() ;
|
||||||
|
Pi pi = new Pi(hashPi) ;
|
||||||
|
|
||||||
|
Hashtable<VertexInterface, VertexInterface> hashPrevious = new Hashtable<VertexInterface, VertexInterface>() ;
|
||||||
|
ArrayList<VertexInterface> shortestPath = new ArrayList<VertexInterface>() ;
|
||||||
|
Previous previous = new Previous(hashPrevious, shortestPath) ;
|
||||||
|
|
||||||
|
return dijkstra(g, r, aSet, pi, previous) ;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
package dijkstra;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This is the Graph class as described in the INF101 course of Telecom Paristech.
|
||||||
|
* Graph contains the representation of all the vertexes, with the information on which vertex(es) a vertex is linked to and at what cost.
|
||||||
|
*/
|
||||||
|
public interface GraphInterface {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the number of <code>VertexInterface</code> in the graph.
|
||||||
|
* @return An integer giving the number of <code>VertexInterface</code> in the graph.
|
||||||
|
*/
|
||||||
|
public int getVertexNumber() ;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the cost of the link (if it exists) between two vertexes.
|
||||||
|
* @param start
|
||||||
|
* The departure vertex.
|
||||||
|
* @param end
|
||||||
|
* The vertex from which we must see the cost of the link to <code>start</code>.
|
||||||
|
* @return An integer giving the cost of the link between the given <code>VertexInterface</code>.
|
||||||
|
*/
|
||||||
|
public int getCost(VertexInterface start, VertexInterface end) ;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns all <code>VertexInterface</code> contained in the graph.
|
||||||
|
* @return An <code>ArrayList</code> of <code>VertexInterface</code>.
|
||||||
|
*/
|
||||||
|
public ArrayList<VertexInterface> getVertexes() ;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the neighbour list for a given <code>VertexInterface</code>.
|
||||||
|
* @param vertex
|
||||||
|
* The <code>VertexInterface</code> from which we will seek for neighbours.
|
||||||
|
* @return An <code>ArrayList</code> of <code>VertexInterface</code>.
|
||||||
|
*/
|
||||||
|
public ArrayList<VertexInterface> generateNeighbours(VertexInterface vertex) ;
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package dijkstra;
|
||||||
|
|
||||||
|
import java.util.Hashtable;
|
||||||
|
|
||||||
|
public class Pi implements PiInterface {
|
||||||
|
|
||||||
|
private Hashtable<VertexInterface, Integer> hashPi = new Hashtable<VertexInterface, Integer>() ;
|
||||||
|
|
||||||
|
public Pi(Hashtable<VertexInterface, Integer> hashPi) {
|
||||||
|
|
||||||
|
this.hashPi = new Hashtable<VertexInterface, Integer>(hashPi);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getPi(VertexInterface vertex) {
|
||||||
|
|
||||||
|
return hashPi.get(vertex) ;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPi(int i, VertexInterface vertex) {
|
||||||
|
|
||||||
|
hashPi.put(vertex, i) ;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package dijkstra;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This is the Pi class as described in the INF101 course of Telecom Paristech.
|
||||||
|
* It contains the path's cost to reach vertexes (typically, from the root of the graph).
|
||||||
|
*/
|
||||||
|
public interface PiInterface {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the cost to reach the given <code>VertexInterface</code>
|
||||||
|
* @param vertex
|
||||||
|
* The <code>VertexInterface</code> from which we get the Pi value (cost) to reach it.
|
||||||
|
* @return An <code>integer</code> giving the Pi value (cost) the reach the <code>VertexInterface</code>.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public int getPi(VertexInterface vertex) ;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the Pi value of an element
|
||||||
|
* @param i
|
||||||
|
* The Pi value (cost) to set.
|
||||||
|
* @param vertex
|
||||||
|
* The <code>VertexInterface</code> on which the Pi value (cost) is to be set.
|
||||||
|
*/
|
||||||
|
public void setPi(int i, VertexInterface vertex) ;
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
package dijkstra;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Hashtable;
|
||||||
|
|
||||||
|
public class Previous implements PreviousInterface {
|
||||||
|
|
||||||
|
private Hashtable<VertexInterface, VertexInterface> hashPrevious = new Hashtable<VertexInterface, VertexInterface>() ;
|
||||||
|
private ArrayList<VertexInterface> shortestPath = new ArrayList<VertexInterface>() ;
|
||||||
|
|
||||||
|
public Previous(Hashtable<VertexInterface, VertexInterface> hashPrevious, ArrayList<VertexInterface> shortestPath) {
|
||||||
|
|
||||||
|
this.hashPrevious = new Hashtable<VertexInterface, VertexInterface>(hashPrevious);
|
||||||
|
this.shortestPath = new ArrayList<VertexInterface>(shortestPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
public VertexInterface getPrevious(VertexInterface vertex) {
|
||||||
|
|
||||||
|
return hashPrevious.get(vertex) ;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPrevious(VertexInterface father, VertexInterface son) {
|
||||||
|
|
||||||
|
hashPrevious.put(son, father) ;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public ArrayList<VertexInterface> getShortestPathTo(VertexInterface vertex) {
|
||||||
|
|
||||||
|
VertexInterface father = getPrevious(vertex) ;
|
||||||
|
shortestPath.add(father) ;
|
||||||
|
|
||||||
|
if (getPrevious(father) == null) return shortestPath ;
|
||||||
|
else {
|
||||||
|
return getShortestPathTo(father) ;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
package dijkstra;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The <code>PreviousInterface</code> contains information about the father (as in the graph's theory) of some <code>VertexInterface</code>.
|
||||||
|
* The father of a <code>VertexInterface</code> is the <code>VertexInterface</code> used to reach it.
|
||||||
|
* Typically in Dijkstra, it's the <code>VertexInterface</code> used to reach <code>vertex</code> with the lowest cost.
|
||||||
|
*/
|
||||||
|
public interface PreviousInterface {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the father of a <code>VertexInterface</code>.
|
||||||
|
* @param vertex
|
||||||
|
* The <code>VertexInterface</code> from which we get the father (previous <code>VertexInterface</code>).
|
||||||
|
* @return The direct previous element used to reach <code>vertex</code>.
|
||||||
|
*/
|
||||||
|
public VertexInterface getPrevious(VertexInterface vertex) ;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the father of an element to another element.
|
||||||
|
* @param father
|
||||||
|
* The element that will become the father of the other element.
|
||||||
|
* @param son
|
||||||
|
* The element that will be assigned a new father.
|
||||||
|
*/
|
||||||
|
public void setPrevious(VertexInterface father, VertexInterface son) ;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Shortest path from root to a vertex.
|
||||||
|
* @param vertex
|
||||||
|
* The vertex to which the shortest path will be computed.
|
||||||
|
* @return An <code>ArrayList</code> of the <code>VertexInterface</code> used to reach vertex with the lowest cost.
|
||||||
|
*/
|
||||||
|
public ArrayList<VertexInterface> getShortestPathTo(VertexInterface vertex) ;
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package dijkstra;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This describes a vertex as seen in graphs (see INF101 course).
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public interface VertexInterface {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return The label of the element.
|
||||||
|
*/
|
||||||
|
public String getLabel() ;
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package maze;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <code>ABox</code> is an arrival <code>Box</code>. It's the last <code>Box</code> to be walked through and it's an exit to the <code>Maze</code>.
|
||||||
|
*/
|
||||||
|
public class ABox extends EBox{
|
||||||
|
|
||||||
|
public ABox (String label, int x, int y, Maze maze) {
|
||||||
|
|
||||||
|
super(label,x,y,maze) ;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isArrival() {
|
||||||
|
return true ;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,73 @@
|
|||||||
|
package maze;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
import dijkstra.VertexInterface;
|
||||||
|
|
||||||
|
/** <h1> Box class </h1>
|
||||||
|
* <p>
|
||||||
|
*
|
||||||
|
* This class describes a Box and implements {@link dijkstra.VertexInterface}.
|
||||||
|
* <p>
|
||||||
|
* A Box is represented with its coordinates, its label, and its neighbourList. It also has a reference on the Maze.
|
||||||
|
*
|
||||||
|
* @author Antonin Godard
|
||||||
|
* @author Sylvain Rager
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
public abstract class Box
|
||||||
|
implements VertexInterface {
|
||||||
|
|
||||||
|
private int x ;
|
||||||
|
private int y ;
|
||||||
|
private String label = null ;
|
||||||
|
private Maze maze ;
|
||||||
|
private ArrayList<Box> neighbourList = new ArrayList<Box>() ;
|
||||||
|
|
||||||
|
public Box (String label, int x, int y, Maze maze) {
|
||||||
|
|
||||||
|
this.label = label ;
|
||||||
|
this.x = x ;
|
||||||
|
this.y = y ;
|
||||||
|
this.maze = maze ;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public ArrayList<VertexInterface> generateNeighbors() {
|
||||||
|
return maze.generateNeighbours(this) ;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isWalkable() {
|
||||||
|
return true ;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isArrival() {
|
||||||
|
return false ;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isDeparture() {
|
||||||
|
return false ;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ArrayList<Box> getNeighbourList() {
|
||||||
|
return neighbourList;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getX() {
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getY() {
|
||||||
|
return y;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getLabel() {
|
||||||
|
return label;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toString() {
|
||||||
|
return "Label: " + label + ", x: " + x + ", y: " + y;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,5 +1,10 @@
|
|||||||
|
package maze;
|
||||||
|
|
||||||
public class DBox extends Box{
|
/**
|
||||||
|
* A <code>DBox</code> is the <code>Box</code> on which the user is first in the <code>Maze</code>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class DBox extends EBox{
|
||||||
|
|
||||||
public DBox (String label, int x, int y, Maze maze) {
|
public DBox (String label, int x, int y, Maze maze) {
|
||||||
|
|
||||||
@@ -1,3 +1,8 @@
|
|||||||
|
package maze;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An <code>EBox</code> is a <code>Box</code> that can be walked through (an empty box).
|
||||||
|
*/
|
||||||
|
|
||||||
public class EBox extends Box{
|
public class EBox extends Box{
|
||||||
|
|
||||||
@@ -1,9 +1,21 @@
|
|||||||
|
package maze;
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.FileReader;
|
import java.io.FileReader;
|
||||||
import java.io.PrintWriter;
|
import java.io.PrintWriter;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
import dijkstra.GraphInterface;
|
||||||
|
import dijkstra.VertexInterface;
|
||||||
|
|
||||||
|
/** <h1> Maze class </h1>
|
||||||
|
* <p>
|
||||||
|
* This class describes the maze as a matrix of Boxes ({@link maze.Box}).
|
||||||
|
*
|
||||||
|
* @author Antonin Godard
|
||||||
|
* @author Sylvain Rager
|
||||||
|
*
|
||||||
|
*/
|
||||||
public class Maze
|
public class Maze
|
||||||
implements GraphInterface {
|
implements GraphInterface {
|
||||||
|
|
||||||
@@ -17,21 +29,18 @@ public class Maze
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// public Maze(Box[][] maze){
|
||||||
|
// //Replacement for setMaze()
|
||||||
|
// //Todo : check if the given array is a proper maze
|
||||||
|
// //and then set dimensionX and dimensionX
|
||||||
|
// }
|
||||||
|
|
||||||
public int getCost(VertexInterface start, VertexInterface end) {
|
public int getCost(VertexInterface start, VertexInterface end) {
|
||||||
|
|
||||||
Box startBox = (Box) start ;
|
Box startBox = (Box) start ;
|
||||||
Box endBox = (Box) end ;
|
Box endBox = (Box) end ;
|
||||||
|
|
||||||
if (startBox.getNeighbourList().contains(endBox)) {
|
return startBox.getNeighbourList().contains(endBox) ? 1 : 0 ;
|
||||||
if (startBox.isWalkable() & endBox.isWalkable()) {
|
|
||||||
return 1 ;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return 0 ;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0 ;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getVertexNumber() {
|
public int getVertexNumber() {
|
||||||
@@ -40,24 +49,62 @@ public class Maze
|
|||||||
|
|
||||||
public ArrayList<VertexInterface> getVertexes() {
|
public ArrayList<VertexInterface> getVertexes() {
|
||||||
ArrayList<VertexInterface> vertexList = new ArrayList<VertexInterface>() ;
|
ArrayList<VertexInterface> vertexList = new ArrayList<VertexInterface>() ;
|
||||||
for (int i = 0 ; i <= dimensionX ; i++) {
|
for (int i = 0 ; i < dimensionX ; i++) {
|
||||||
for (int j = 0 ; j <= dimensionY ; j++) {
|
for (int j = 0 ; j < dimensionY ; j++) {
|
||||||
|
if (maze[i][j].isWalkable()) {
|
||||||
vertexList.add(maze[i][j]) ;
|
vertexList.add(maze[i][j]) ;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return vertexList ;
|
return vertexList ;
|
||||||
}
|
}
|
||||||
|
|
||||||
public final void initFromTextFile(String filename) {
|
public ArrayList<VertexInterface> generateNeighbours(VertexInterface vertex) {
|
||||||
|
|
||||||
|
Box box = (Box)vertex ;
|
||||||
|
int x = box.getX() ;
|
||||||
|
int y = box.getY() ;
|
||||||
|
ArrayList<VertexInterface> neighbourList = new ArrayList<VertexInterface>() ;
|
||||||
|
|
||||||
|
// Always returns an empty list if the departure box is a wall
|
||||||
|
if (! box.isWalkable()) {
|
||||||
|
return neighbourList ;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (x > 0 && maze[x-1][y].isWalkable()) {
|
||||||
|
neighbourList.add(maze[x-1][y]) ;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (x < dimensionX-1 && maze[x+1][y].isWalkable()) {
|
||||||
|
neighbourList.add(maze[x+1][y]) ;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (y > 0 && maze[x][y-1].isWalkable()) {
|
||||||
|
neighbourList.add(maze[x][y-1]) ;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (y < dimensionY-1 && maze[x][y+1].isWalkable()) {
|
||||||
|
neighbourList.add(maze[x][y+1]) ;
|
||||||
|
}
|
||||||
|
|
||||||
|
return neighbourList ;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**Constructs the maze from a text file, according to each single character on each line.
|
||||||
|
* @param filename
|
||||||
|
* The path to the file containing the representation of the maze.
|
||||||
|
*/
|
||||||
|
|
||||||
|
private final void initFromTextFile(String filename) {
|
||||||
|
|
||||||
FileReader fr = null ;
|
FileReader fr = null ;
|
||||||
BufferedReader br = null ;
|
BufferedReader br = null ;
|
||||||
|
|
||||||
try { //Determines the height of the maze and stores it into lineCount
|
try {
|
||||||
fr = new FileReader(filename) ;
|
fr = new FileReader(filename) ;
|
||||||
br = new BufferedReader(fr) ;
|
br = new BufferedReader(fr) ;
|
||||||
while (br.readLine() != null) dimensionY++;
|
while (br.readLine() != null) dimensionY++;
|
||||||
//System.out.println("Line count :" + lineCount);
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
} finally {
|
} finally {
|
||||||
@@ -71,9 +118,7 @@ public class Maze
|
|||||||
String currentLine = null ;
|
String currentLine = null ;
|
||||||
|
|
||||||
currentLine = br.readLine() ;
|
currentLine = br.readLine() ;
|
||||||
System.out.println(currentLine);
|
|
||||||
dimensionX = currentLine.length() ; //maze's width
|
dimensionX = currentLine.length() ; //maze's width
|
||||||
System.out.println("Line length :" + dimensionX);
|
|
||||||
int currentLineLength ;
|
int currentLineLength ;
|
||||||
int compteurLigne = 0 ;
|
int compteurLigne = 0 ;
|
||||||
|
|
||||||
@@ -81,10 +126,7 @@ public class Maze
|
|||||||
|
|
||||||
while (currentLine != null)
|
while (currentLine != null)
|
||||||
{
|
{
|
||||||
|
|
||||||
//System.out.println(currentLine);
|
|
||||||
currentLineLength = currentLine.length() ;
|
currentLineLength = currentLine.length() ;
|
||||||
System.out.println("Current line length :" + currentLineLength);
|
|
||||||
|
|
||||||
for (int compteurColonne = 0 ; compteurColonne < currentLineLength ; compteurColonne++) {
|
for (int compteurColonne = 0 ; compteurColonne < currentLineLength ; compteurColonne++) {
|
||||||
|
|
||||||
@@ -92,16 +134,16 @@ public class Maze
|
|||||||
|
|
||||||
switch(currentChar) {
|
switch(currentChar) {
|
||||||
|
|
||||||
case 'W' : maze[compteurLigne][compteurColonne] = new WBox("W", compteurLigne, compteurColonne, this) ;
|
case 'W' : maze[compteurColonne][compteurLigne] = new WBox("W", compteurColonne, compteurLigne, this) ;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'E' : maze[compteurLigne][compteurColonne] = new EBox("E", compteurLigne, compteurColonne, this) ;
|
case 'E' : maze[compteurColonne][compteurLigne] = new EBox("E", compteurColonne, compteurLigne, this) ;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'D' : maze[compteurLigne][compteurColonne] = new DBox("D", compteurLigne, compteurColonne, this) ;
|
case 'D' : maze[compteurColonne][compteurLigne] = new DBox("D", compteurColonne, compteurLigne, this) ;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'A' : maze[compteurLigne][compteurColonne] = new ABox("A", compteurLigne, compteurColonne, this) ;
|
case 'A' : maze[compteurColonne][compteurLigne] = new ABox("A", compteurColonne, compteurLigne, this) ;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default : throw new MazeReadingException(filename) ;
|
default : throw new MazeReadingException(filename) ;
|
||||||
@@ -124,13 +166,6 @@ public class Maze
|
|||||||
try { br.close() ;} catch (Exception e) {}
|
try { br.close() ;} catch (Exception e) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
//neighbour generation for each box in the maze
|
|
||||||
for (int i = 0 ; i < dimensionX ; i++) {
|
|
||||||
for (int j = 0 ; j < dimensionY ; j++) {
|
|
||||||
maze[i][j].generateNeighbors() ;
|
|
||||||
}
|
|
||||||
System.out.println();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public final void saveToTextFile(String fileName) {
|
public final void saveToTextFile(String fileName) {
|
||||||
@@ -157,32 +192,18 @@ public class Maze
|
|||||||
} finally {
|
} finally {
|
||||||
try { pw.close();} catch(Exception e) {}
|
try { pw.close();} catch(Exception e) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getDimensionX() {
|
public int getDimensionX() {
|
||||||
return dimensionX;
|
return dimensionX;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setDimensionX(int dimensionX) {
|
|
||||||
this.dimensionX = dimensionX;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getDimensionY() {
|
public int getDimensionY() {
|
||||||
return dimensionY;
|
return dimensionY;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setDimensionY(int dimensionY) {
|
|
||||||
this.dimensionY = dimensionY;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Box[][] getMaze() {
|
public Box[][] getMaze() {
|
||||||
return maze;
|
return maze;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setMaze(Box[][] maze) {
|
|
||||||
this.maze = maze;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
package maze;
|
||||||
|
|
||||||
|
/**<h1> MazeReadingException exception </h1>
|
||||||
|
* <p>
|
||||||
|
* This class is used to throw exception when reading and initiating the maze from a text file.
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class MazeReadingException
|
||||||
|
extends Exception {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
public MazeReadingException (String filename) {
|
||||||
|
|
||||||
|
super("Invalid maze format : " + filename) ;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,3 +1,8 @@
|
|||||||
|
package maze;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A <code>WBox</code> is a <code>Box</code> that can't be walked through (a wall).
|
||||||
|
*/
|
||||||
|
|
||||||
public class WBox extends Box {
|
public class WBox extends Box {
|
||||||
|
|
||||||
Reference in new issue
Block a user