diff --git a/src/MainTest.java b/src/MainTest.java index 3de2320..599e8fb 100644 --- a/src/MainTest.java +++ b/src/MainTest.java @@ -1,5 +1,7 @@ -import dijkstra.Dijkstra; -import maze.Maze; +import java.util.ArrayList; + +import dijkstra.*; +import maze.*; public class MainTest { @@ -7,7 +9,13 @@ public class MainTest { Maze mazeTest = new Maze("data/labyrinthe.txt") ; - Dijkstra.dijkstra(mazeTest, mazeTest.getMaze()[1][1]) ; + Previous previous = (Previous) Dijkstra.dijkstra(mazeTest, mazeTest.getMaze()[1][1]); + + ArrayList shortest = previous.getShortestPathTo(mazeTest.getMaze()[5][5]); + + for (VertexInterface vertex : shortest) { + System.out.println(vertex); + } mazeTest.saveToTextFile("data/labyOutput.txt"); diff --git a/src/dijkstra/ASet.java b/src/dijkstra/ASet.java index 5dc12b5..b78bc84 100644 --- a/src/dijkstra/ASet.java +++ b/src/dijkstra/ASet.java @@ -2,8 +2,6 @@ package dijkstra; import java.util.HashSet; -import maze.Box; - public class ASet implements ASetInterface { private HashSet vertexHashSet = new HashSet() ; @@ -11,20 +9,19 @@ public class ASet implements ASetInterface { public ASet(HashSet vertexHashSet) { - this.vertexHashSet = vertexHashSet; + this.vertexHashSet = new HashSet(vertexHashSet); } public void add(VertexInterface vertex) { - vertexHashSet.add((Box)vertex) ; + vertexHashSet.add(vertex) ; } public boolean contains(VertexInterface vertex) { - if (vertexHashSet.contains(vertex)) return true ; - return false ; + return vertexHashSet.contains(vertex); } diff --git a/src/dijkstra/Pi.java b/src/dijkstra/Pi.java index 6cd3c7d..2b21078 100644 --- a/src/dijkstra/Pi.java +++ b/src/dijkstra/Pi.java @@ -8,7 +8,7 @@ public class Pi implements PiInterface { public Pi(Hashtable hashPi) { - this.hashPi = hashPi ; + this.hashPi = new Hashtable(hashPi); } diff --git a/src/dijkstra/Previous.java b/src/dijkstra/Previous.java index 527b8af..9f321e8 100644 --- a/src/dijkstra/Previous.java +++ b/src/dijkstra/Previous.java @@ -7,13 +7,11 @@ public class Previous implements PreviousInterface { private Hashtable hashPrevious = new Hashtable() ; private ArrayList shortestPath = new ArrayList() ; - - public Previous(Hashtable hashPrevious, ArrayList shortestPath) { - this.hashPrevious = hashPrevious; - this.shortestPath = shortestPath; + this.hashPrevious = new Hashtable(hashPrevious); + this.shortestPath = new ArrayList(shortestPath); } public VertexInterface getPrevious(VertexInterface vertex) { @@ -36,8 +34,7 @@ public class Previous implements PreviousInterface { if (getPrevious(father) == null) return shortestPath ; else { return getShortestPathTo(father) ; - } - + } } } diff --git a/src/dijkstra/VertexInterface.java b/src/dijkstra/VertexInterface.java index e047a9a..51d147d 100644 --- a/src/dijkstra/VertexInterface.java +++ b/src/dijkstra/VertexInterface.java @@ -6,16 +6,9 @@ package dijkstra; */ public interface VertexInterface { - /** - * Compares a VertexInterface to another. - * @param vertex - * The vertex to which the equality will be tested. - * @return true is the two VertexInterface are the same, false if not. - */ - public boolean compareTo(VertexInterface vertex) ; - /** * @return The label of the element. */ public String getLabel() ; + } diff --git a/src/maze/ABox.java b/src/maze/ABox.java index ef72aa2..94f88f7 100644 --- a/src/maze/ABox.java +++ b/src/maze/ABox.java @@ -3,7 +3,7 @@ package maze; /** * ABox is an arrival Box. It's the last Box to be walked through and it's an exit to the Maze. */ -public class ABox extends Box{ +public class ABox extends EBox{ public ABox (String label, int x, int y, Maze maze) { diff --git a/src/maze/Box.java b/src/maze/Box.java index 9219de9..00f7694 100644 --- a/src/maze/Box.java +++ b/src/maze/Box.java @@ -1,6 +1,5 @@ package maze; import java.util.ArrayList; -import java.util.List; import dijkstra.VertexInterface; @@ -23,7 +22,7 @@ public abstract class Box private int y ; private String label = null ; private Maze maze ; - private List neighbourList = new ArrayList() ; + private ArrayList neighbourList = new ArrayList() ; public Box (String label, int x, int y, Maze maze) { @@ -34,18 +33,11 @@ public abstract class Box } - public List generateNeighbors() { + public ArrayList generateNeighbors() { return maze.generateNeighbours(this) ; } - public boolean compareTo(VertexInterface vertex) { - if (this.getLabel() == vertex.getLabel()) { - return true ; - } - return false ; - } - public boolean isWalkable() { return true ; } @@ -58,48 +50,24 @@ public abstract class Box return false ; } - public List getNeighbourList() { + public ArrayList getNeighbourList() { return neighbourList; } - public void setNeighbourList(List 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; - } public String toString() { - return label; + return "Label: " + label + ", x: " + x + ", y: " + y; } } diff --git a/src/maze/DBox.java b/src/maze/DBox.java index 3b62e7c..cc7b7fb 100644 --- a/src/maze/DBox.java +++ b/src/maze/DBox.java @@ -4,7 +4,7 @@ package maze; * A DBox is the Box on which the user is first in the Maze. */ -public class DBox extends Box{ +public class DBox extends EBox{ public DBox (String label, int x, int y, Maze maze) { diff --git a/src/maze/Maze.java b/src/maze/Maze.java index 47d5748..6d7ce44 100644 --- a/src/maze/Maze.java +++ b/src/maze/Maze.java @@ -29,24 +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) { Box startBox = (Box) start ; Box endBox = (Box) end ; - - if (startBox.getNeighbourList().contains(endBox)) { - if (startBox.isWalkable() && endBox.isWalkable()) { - return 1 ; - } - else { - return 0 ; - } - } - - return 0 ; + + return startBox.getNeighbourList().contains(endBox) ? 1 : 0 ; } public int getVertexNumber() { @@ -71,7 +65,12 @@ public class Maze int x = box.getX() ; int y = box.getY() ; ArrayList neighbourList = new ArrayList() ; - + + // 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]) ; } @@ -97,7 +96,7 @@ public class Maze * The path to the file containing the representation of the maze. */ - public final void initFromTextFile(String filename) { + private final void initFromTextFile(String filename) { FileReader fr = null ; BufferedReader br = null ; @@ -193,32 +192,18 @@ public class Maze } finally { try { pw.close();} catch(Exception e) {} } - } public int getDimensionX() { return dimensionX; } - public void setDimensionX(int dimensionX) { - this.dimensionX = dimensionX; - } - public int getDimensionY() { return dimensionY; } - public void setDimensionY(int dimensionY) { - this.dimensionY = dimensionY; - } - public Box[][] getMaze() { return maze; } - public void setMaze(Box[][] maze) { - this.maze = maze; - } - - } diff --git a/src/maze/MazeReadingException.java b/src/maze/MazeReadingException.java index 4cc5822..37b0cd3 100644 --- a/src/maze/MazeReadingException.java +++ b/src/maze/MazeReadingException.java @@ -10,17 +10,12 @@ package maze; public class MazeReadingException extends Exception { - private String filename ; + private static final long serialVersionUID = 1L; public MazeReadingException (String filename) { super("Invalid maze format : " + filename) ; - this.filename = filename ; } - public String getFilename() { - return filename ; - } - }