From acab098ea1e873c88357d00a6bfdc725272edde8 Mon Sep 17 00:00:00 2001 From: Antonin Godard Date: Sun, 3 Dec 2017 19:48:40 +0100 Subject: [PATCH 01/15] Added packages, Previous class and ASet class. --- src/MainTest.java | 4 +-- src/dijkstra/ASet.java | 33 +++++++++++++++++++++ src/{ => dijkstra}/ASetInterface.java | 1 + src/{ => dijkstra}/Dijkstra.java | 1 + src/{ => dijkstra}/GraphInterface.java | 1 + src/{ => dijkstra}/PiInterface.java | 1 + src/dijkstra/Previous.java | 35 +++++++++++++++++++++++ src/{ => dijkstra}/PreviousInterface.java | 7 +++++ src/{ => dijkstra}/VertexInterface.java | 1 + src/{ => maze}/ABox.java | 1 + src/{ => maze}/Box.java | 3 ++ src/{ => maze}/DBox.java | 1 + src/{ => maze}/EBox.java | 1 + src/{ => maze}/Maze.java | 12 ++++++-- src/{ => maze}/MazeReadingException.java | 1 + src/{ => maze}/WBox.java | 1 + 16 files changed, 99 insertions(+), 5 deletions(-) create mode 100644 src/dijkstra/ASet.java rename src/{ => dijkstra}/ASetInterface.java (92%) rename src/{ => dijkstra}/Dijkstra.java (98%) rename src/{ => dijkstra}/GraphInterface.java (94%) rename src/{ => dijkstra}/PiInterface.java (92%) create mode 100644 src/dijkstra/Previous.java rename src/{ => dijkstra}/PreviousInterface.java (61%) rename src/{ => dijkstra}/VertexInterface.java (89%) rename src/{ => maze}/ABox.java (93%) rename src/{ => maze}/Box.java (97%) rename src/{ => maze}/DBox.java (93%) rename src/{ => maze}/EBox.java (90%) rename src/{ => maze}/Maze.java (94%) rename src/{ => maze}/MazeReadingException.java (95%) rename src/{ => maze}/WBox.java (93%) diff --git a/src/MainTest.java b/src/MainTest.java index e22c50a..e25518d 100644 --- a/src/MainTest.java +++ b/src/MainTest.java @@ -1,11 +1,11 @@ +import maze.Maze; public class MainTest { public static void main(String[] args) { - // TODO Auto-generated method stub Maze mazeTest = new Maze("data/labyrinthe.txt") ; - //mazeTest.saveToTextFile("data/out.txt"); + System.out.println("\n" + mazeTest); mazeTest.saveToTextFile("data/labyOutput.txt"); diff --git a/src/dijkstra/ASet.java b/src/dijkstra/ASet.java new file mode 100644 index 0000000..fb55d18 --- /dev/null +++ b/src/dijkstra/ASet.java @@ -0,0 +1,33 @@ +package dijkstra; + +import java.util.HashSet; + +import maze.Box; + +public class ASet implements ASetInterface { + + private HashSet vertexHashSet = new HashSet() ; + + + public ASet(HashSet vertexHashSet) { + + this.vertexHashSet = vertexHashSet; + + } + + public void add(VertexInterface vertex) { + + vertexHashSet.add((Box)vertex) ; + + } + + public boolean contains(VertexInterface vertex) { + + for (VertexInterface v: vertexHashSet) { + if (v == vertex) return true ; + } + return false ; + + } + +} diff --git a/src/ASetInterface.java b/src/dijkstra/ASetInterface.java similarity index 92% rename from src/ASetInterface.java rename to src/dijkstra/ASetInterface.java index 8d4c177..27eb66c 100644 --- a/src/ASetInterface.java +++ b/src/dijkstra/ASetInterface.java @@ -1,3 +1,4 @@ +package dijkstra; public interface ASetInterface { diff --git a/src/Dijkstra.java b/src/dijkstra/Dijkstra.java similarity index 98% rename from src/Dijkstra.java rename to src/dijkstra/Dijkstra.java index 34ab762..ffa1b63 100644 --- a/src/Dijkstra.java +++ b/src/dijkstra/Dijkstra.java @@ -1,3 +1,4 @@ +package dijkstra; import java.util.ArrayList; public class Dijkstra { diff --git a/src/GraphInterface.java b/src/dijkstra/GraphInterface.java similarity index 94% rename from src/GraphInterface.java rename to src/dijkstra/GraphInterface.java index 0841a8d..55cfd7e 100644 --- a/src/GraphInterface.java +++ b/src/dijkstra/GraphInterface.java @@ -1,3 +1,4 @@ +package dijkstra; import java.util.ArrayList; public interface GraphInterface { diff --git a/src/PiInterface.java b/src/dijkstra/PiInterface.java similarity index 92% rename from src/PiInterface.java rename to src/dijkstra/PiInterface.java index f1a4ac0..87fce98 100644 --- a/src/PiInterface.java +++ b/src/dijkstra/PiInterface.java @@ -1,3 +1,4 @@ +package dijkstra; public interface PiInterface { diff --git a/src/dijkstra/Previous.java b/src/dijkstra/Previous.java new file mode 100644 index 0000000..6526365 --- /dev/null +++ b/src/dijkstra/Previous.java @@ -0,0 +1,35 @@ +package dijkstra; + +import java.util.ArrayList; +import java.util.Hashtable; + +public class Previous implements PreviousInterface { + + private Hashtable hashPrevious = new Hashtable() ; + private ArrayList shortestPath = new ArrayList() ; + + public VertexInterface getPrevious(VertexInterface vertex) { + + return hashPrevious.get(vertex) ; + + } + + public void setPrevious(VertexInterface father, VertexInterface son) { + + hashPrevious.put(son, father) ; + + } + + public ArrayList getShortestPathTo(VertexInterface vertex) { + + VertexInterface father = getPrevious(vertex) ; + shortestPath.add(father) ; + + if (getPrevious(father) == null) return shortestPath ; + else { + return getShortestPathTo(father) ; + } + + } + +} diff --git a/src/PreviousInterface.java b/src/dijkstra/PreviousInterface.java similarity index 61% rename from src/PreviousInterface.java rename to src/dijkstra/PreviousInterface.java index de81746..9d1f8c1 100644 --- a/src/PreviousInterface.java +++ b/src/dijkstra/PreviousInterface.java @@ -1,3 +1,6 @@ +package dijkstra; + +import java.util.ArrayList; public interface PreviousInterface { @@ -6,5 +9,9 @@ public interface PreviousInterface { /** Sets the father of an element to another element */ public void setPrevious(VertexInterface father, VertexInterface son) ; + + /**Shortest path from root to a vertex */ + public ArrayList getShortestPathTo(VertexInterface vertex) ; + } diff --git a/src/VertexInterface.java b/src/dijkstra/VertexInterface.java similarity index 89% rename from src/VertexInterface.java rename to src/dijkstra/VertexInterface.java index c78bd3f..84c7b11 100644 --- a/src/VertexInterface.java +++ b/src/dijkstra/VertexInterface.java @@ -1,3 +1,4 @@ +package dijkstra; public interface VertexInterface { diff --git a/src/ABox.java b/src/maze/ABox.java similarity index 93% rename from src/ABox.java rename to src/maze/ABox.java index 51e1186..1b875d0 100644 --- a/src/ABox.java +++ b/src/maze/ABox.java @@ -1,3 +1,4 @@ +package maze; public class ABox extends Box{ diff --git a/src/Box.java b/src/maze/Box.java similarity index 97% rename from src/Box.java rename to src/maze/Box.java index 940bfde..b4a6abf 100644 --- a/src/Box.java +++ b/src/maze/Box.java @@ -1,6 +1,9 @@ +package maze; import java.util.ArrayList; import java.util.List; +import dijkstra.VertexInterface; + public abstract class Box implements VertexInterface { diff --git a/src/DBox.java b/src/maze/DBox.java similarity index 93% rename from src/DBox.java rename to src/maze/DBox.java index 757d2e4..6bcdc8e 100644 --- a/src/DBox.java +++ b/src/maze/DBox.java @@ -1,3 +1,4 @@ +package maze; public class DBox extends Box{ diff --git a/src/EBox.java b/src/maze/EBox.java similarity index 90% rename from src/EBox.java rename to src/maze/EBox.java index 643b4dd..88c63d5 100644 --- a/src/EBox.java +++ b/src/maze/EBox.java @@ -1,3 +1,4 @@ +package maze; public class EBox extends Box{ diff --git a/src/Maze.java b/src/maze/Maze.java similarity index 94% rename from src/Maze.java rename to src/maze/Maze.java index 91897a2..291ecdb 100644 --- a/src/Maze.java +++ b/src/maze/Maze.java @@ -1,9 +1,13 @@ +package maze; import java.io.BufferedReader; import java.io.FileOutputStream; import java.io.FileReader; import java.io.PrintWriter; import java.util.ArrayList; +import dijkstra.GraphInterface; +import dijkstra.VertexInterface; + public class Maze implements GraphInterface { @@ -40,9 +44,11 @@ public class Maze public ArrayList getVertexes() { ArrayList vertexList = new ArrayList() ; - for (int i = 0 ; i <= dimensionX ; i++) { - for (int j = 0 ; j <= dimensionY ; j++) { - vertexList.add(maze[i][j]) ; + for (int i = 0 ; i < dimensionX ; i++) { + for (int j = 0 ; j < dimensionY ; j++) { + if (maze[i][j].isWalkable()) { + vertexList.add(maze[i][j]) ; + } } } return vertexList ; diff --git a/src/MazeReadingException.java b/src/maze/MazeReadingException.java similarity index 95% rename from src/MazeReadingException.java rename to src/maze/MazeReadingException.java index 62c6335..0bfd24c 100644 --- a/src/MazeReadingException.java +++ b/src/maze/MazeReadingException.java @@ -1,3 +1,4 @@ +package maze; public class MazeReadingException extends Exception { diff --git a/src/WBox.java b/src/maze/WBox.java similarity index 93% rename from src/WBox.java rename to src/maze/WBox.java index c7759d1..d0adc4b 100644 --- a/src/WBox.java +++ b/src/maze/WBox.java @@ -1,3 +1,4 @@ +package maze; public class WBox extends Box { -- 2.56.1 From 3b674f6049498672c9b78caf0d78828e8a376b65 Mon Sep 17 00:00:00 2001 From: Antonin Godard Date: Sun, 3 Dec 2017 20:57:01 +0100 Subject: [PATCH 02/15] Added Pi class. Modified Dijsktra and fixed Maze class. --- src/dijkstra/Dijkstra.java | 1 + src/dijkstra/Pi.java | 21 +++++++++++++++++++++ src/maze/Maze.java | 2 +- 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 src/dijkstra/Pi.java diff --git a/src/dijkstra/Dijkstra.java b/src/dijkstra/Dijkstra.java index ffa1b63..7bf41f5 100644 --- a/src/dijkstra/Dijkstra.java +++ b/src/dijkstra/Dijkstra.java @@ -42,4 +42,5 @@ public class Dijkstra { } + } diff --git a/src/dijkstra/Pi.java b/src/dijkstra/Pi.java new file mode 100644 index 0000000..b36c4d2 --- /dev/null +++ b/src/dijkstra/Pi.java @@ -0,0 +1,21 @@ +package dijkstra; + +import java.util.Hashtable; + +public class Pi implements PiInterface { + + private Hashtable hashPi = new Hashtable() ; + + public int getPi(VertexInterface vertex) { + + return hashPi.get(vertex) ; + + } + + public void setPi(int i, VertexInterface vertex) { + + hashPi.put(vertex, i) ; + + } + +} diff --git a/src/maze/Maze.java b/src/maze/Maze.java index 291ecdb..2c2bdbe 100644 --- a/src/maze/Maze.java +++ b/src/maze/Maze.java @@ -27,7 +27,7 @@ public class Maze Box endBox = (Box) end ; if (startBox.getNeighbourList().contains(endBox)) { - if (startBox.isWalkable() & endBox.isWalkable()) { + if (startBox.isWalkable() && endBox.isWalkable()) { return 1 ; } else { -- 2.56.1 From 5d7c970e1b223b95846251626d4485ddc8338a27 Mon Sep 17 00:00:00 2001 From: Antonin Godard Date: Mon, 4 Dec 2017 13:36:14 +0100 Subject: [PATCH 03/15] Added signature method for Dijkstra class. Fixed Previous et Pi class. --- src/dijkstra/Dijkstra.java | 22 +++++++++++++++++++++- src/dijkstra/Pi.java | 6 ++++++ src/dijkstra/Previous.java | 8 ++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/dijkstra/Dijkstra.java b/src/dijkstra/Dijkstra.java index 7bf41f5..e69f0a6 100644 --- a/src/dijkstra/Dijkstra.java +++ b/src/dijkstra/Dijkstra.java @@ -1,9 +1,13 @@ package dijkstra; import java.util.ArrayList; +import java.util.HashSet; +import java.util.Hashtable; + +import maze.Box; public class Dijkstra { - private PreviousInterface dijkstra(GraphInterface g, VertexInterface r, ASetInterface a, PiInterface pi, PreviousInterface previous) { + private static PreviousInterface dijkstra(GraphInterface g, VertexInterface r, ASetInterface a, PiInterface pi, PreviousInterface previous) { a.add(r) ; ; VertexInterface pivot = r ; @@ -42,5 +46,21 @@ public class Dijkstra { } + public static PreviousInterface dijkstra(GraphInterface g, VertexInterface r) { + + HashSet vertexHashSet = new HashSet() ; + ASet aSet = new ASet(vertexHashSet) ; + + Hashtable hashPi = new Hashtable() ; + Pi pi = new Pi(hashPi) ; + + Hashtable hashPrevious = new Hashtable() ; + ArrayList shortestPath = new ArrayList() ; + Previous previous = new Previous(hashPrevious, shortestPath) ; + + return dijkstra(g, r, aSet, pi, previous) ; + + + } } diff --git a/src/dijkstra/Pi.java b/src/dijkstra/Pi.java index b36c4d2..6cd3c7d 100644 --- a/src/dijkstra/Pi.java +++ b/src/dijkstra/Pi.java @@ -6,6 +6,12 @@ public class Pi implements PiInterface { private Hashtable hashPi = new Hashtable() ; + public Pi(Hashtable hashPi) { + + this.hashPi = hashPi ; + + } + public int getPi(VertexInterface vertex) { return hashPi.get(vertex) ; diff --git a/src/dijkstra/Previous.java b/src/dijkstra/Previous.java index 6526365..cc1637a 100644 --- a/src/dijkstra/Previous.java +++ b/src/dijkstra/Previous.java @@ -7,6 +7,14 @@ public class Previous implements PreviousInterface { private Hashtable hashPrevious = new Hashtable() ; private ArrayList shortestPath = new ArrayList() ; + + + + public Previous(Hashtable hashPrevious, ArrayList shortestPath) { + super(); + this.hashPrevious = hashPrevious; + this.shortestPath = shortestPath; + } public VertexInterface getPrevious(VertexInterface vertex) { -- 2.56.1 From d7368f7cda56f75ac50ab51f2a1c2dada6c0e363 Mon Sep 17 00:00:00 2001 From: Antonin Godard Date: Wed, 6 Dec 2017 22:30:58 +0100 Subject: [PATCH 04/15] Fixed last dijkstra classes. Algorithm working for squared maze but bugs for non square mazes. --- src/MainTest.java | 5 ++-- src/dijkstra/ASet.java | 12 ++++++---- src/dijkstra/ASetInterface.java | 3 +++ src/dijkstra/Dijkstra.java | 13 +++++----- src/dijkstra/GraphInterface.java | 9 ++++--- src/dijkstra/Previous.java | 5 +++- src/dijkstra/PreviousInterface.java | 3 +-- src/dijkstra/VertexInterface.java | 2 +- src/maze/Box.java | 29 +++++----------------- src/maze/Maze.java | 37 +++++++++++++++++++++-------- 10 files changed, 64 insertions(+), 54 deletions(-) diff --git a/src/MainTest.java b/src/MainTest.java index e25518d..3de2320 100644 --- a/src/MainTest.java +++ b/src/MainTest.java @@ -1,3 +1,4 @@ +import dijkstra.Dijkstra; import maze.Maze; public class MainTest { @@ -5,8 +6,8 @@ public class MainTest { public static void main(String[] args) { Maze mazeTest = new Maze("data/labyrinthe.txt") ; - - System.out.println("\n" + mazeTest); + + Dijkstra.dijkstra(mazeTest, mazeTest.getMaze()[1][1]) ; mazeTest.saveToTextFile("data/labyOutput.txt"); diff --git a/src/dijkstra/ASet.java b/src/dijkstra/ASet.java index fb55d18..5dc12b5 100644 --- a/src/dijkstra/ASet.java +++ b/src/dijkstra/ASet.java @@ -6,10 +6,10 @@ import maze.Box; public class ASet implements ASetInterface { - private HashSet vertexHashSet = new HashSet() ; + private HashSet vertexHashSet = new HashSet() ; - public ASet(HashSet vertexHashSet) { + public ASet(HashSet vertexHashSet) { this.vertexHashSet = vertexHashSet; @@ -23,11 +23,13 @@ public class ASet implements ASetInterface { public boolean contains(VertexInterface vertex) { - for (VertexInterface v: vertexHashSet) { - if (v == vertex) return true ; - } + if (vertexHashSet.contains(vertex)) return true ; return false ; } + public HashSet getVertexHashSet() { + return vertexHashSet; + } + } diff --git a/src/dijkstra/ASetInterface.java b/src/dijkstra/ASetInterface.java index 27eb66c..c09024c 100644 --- a/src/dijkstra/ASetInterface.java +++ b/src/dijkstra/ASetInterface.java @@ -1,5 +1,6 @@ package dijkstra; +import java.util.HashSet; public interface ASetInterface { @@ -8,6 +9,8 @@ public interface ASetInterface { /** Check if a vertex belongs to ASet */ public boolean contains(VertexInterface vertex) ; + + public HashSet getVertexHashSet(); } diff --git a/src/dijkstra/Dijkstra.java b/src/dijkstra/Dijkstra.java index e69f0a6..e91af31 100644 --- a/src/dijkstra/Dijkstra.java +++ b/src/dijkstra/Dijkstra.java @@ -3,13 +3,11 @@ import java.util.ArrayList; import java.util.HashSet; import java.util.Hashtable; -import maze.Box; - public class Dijkstra { private static PreviousInterface dijkstra(GraphInterface g, VertexInterface r, ASetInterface a, PiInterface pi, PreviousInterface previous) { - a.add(r) ; ; + a.add(r) ; VertexInterface pivot = r ; pi.setPi(0, r) ; @@ -20,11 +18,12 @@ public class Dijkstra { 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)) { + 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); @@ -48,7 +47,7 @@ public class Dijkstra { public static PreviousInterface dijkstra(GraphInterface g, VertexInterface r) { - HashSet vertexHashSet = new HashSet() ; + HashSet vertexHashSet = new HashSet() ; ASet aSet = new ASet(vertexHashSet) ; Hashtable hashPi = new Hashtable() ; diff --git a/src/dijkstra/GraphInterface.java b/src/dijkstra/GraphInterface.java index 55cfd7e..5c8c8e6 100644 --- a/src/dijkstra/GraphInterface.java +++ b/src/dijkstra/GraphInterface.java @@ -3,13 +3,16 @@ import java.util.ArrayList; public interface GraphInterface { - /** Returns the number of Vertexes */ + /** Returns the number of Vertexes **/ public int getVertexNumber() ; - /** Return the cost between two vertexes */ + /** Return the cost between two vertexes **/ public int getCost(VertexInterface start, VertexInterface end) ; - /** Returns an list of Vertexes */ + /** Returns an list of Vertexes **/ public ArrayList getVertexes() ; + + /** Returns the neighbour list for a given box */ + public ArrayList generateNeighbours(VertexInterface vertex) ; } diff --git a/src/dijkstra/Previous.java b/src/dijkstra/Previous.java index cc1637a..b6acaeb 100644 --- a/src/dijkstra/Previous.java +++ b/src/dijkstra/Previous.java @@ -2,6 +2,9 @@ package dijkstra; import java.util.ArrayList; import java.util.Hashtable; +import java.util.List; + +import maze.Box; public class Previous implements PreviousInterface { @@ -11,7 +14,7 @@ public class Previous implements PreviousInterface { public Previous(Hashtable hashPrevious, ArrayList shortestPath) { - super(); + this.hashPrevious = hashPrevious; this.shortestPath = shortestPath; } diff --git a/src/dijkstra/PreviousInterface.java b/src/dijkstra/PreviousInterface.java index 9d1f8c1..02c08b9 100644 --- a/src/dijkstra/PreviousInterface.java +++ b/src/dijkstra/PreviousInterface.java @@ -11,7 +11,6 @@ public interface PreviousInterface { public void setPrevious(VertexInterface father, VertexInterface son) ; /**Shortest path from root to a vertex */ - public ArrayList getShortestPathTo(VertexInterface vertex) ; - + public ArrayList getShortestPathTo(VertexInterface vertex) ; } diff --git a/src/dijkstra/VertexInterface.java b/src/dijkstra/VertexInterface.java index 84c7b11..e62827c 100644 --- a/src/dijkstra/VertexInterface.java +++ b/src/dijkstra/VertexInterface.java @@ -1,4 +1,5 @@ package dijkstra; +import java.util.List; public interface VertexInterface { @@ -6,5 +7,4 @@ public interface VertexInterface { public boolean compareTo(VertexInterface vertex) ; public String getLabel() ; - } diff --git a/src/maze/Box.java b/src/maze/Box.java index b4a6abf..2326e21 100644 --- a/src/maze/Box.java +++ b/src/maze/Box.java @@ -9,7 +9,7 @@ public abstract class Box private int x ; private int y ; - private String label ; + private String label = null ; private Maze maze ; private List neighbourList = new ArrayList() ; @@ -22,27 +22,8 @@ public abstract class Box } - public List 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 List generateNeighbors() { + return maze.generateNeighbours(this) ; } @@ -105,6 +86,8 @@ public abstract class Box this.maze = maze; } - + public String toString() { + return label; + } } diff --git a/src/maze/Maze.java b/src/maze/Maze.java index 2c2bdbe..93ce736 100644 --- a/src/maze/Maze.java +++ b/src/maze/Maze.java @@ -54,6 +54,33 @@ public class Maze return vertexList ; } + public ArrayList generateNeighbours(VertexInterface vertex) { + + Box box = (Box)vertex ; + int x = box.getX() ; + int y = box.getY() ; + ArrayList neighbourList = new ArrayList() ; + + 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 ; + + } + public final void initFromTextFile(String filename) { FileReader fr = null ; @@ -77,9 +104,7 @@ public class Maze String currentLine = null ; currentLine = br.readLine() ; - System.out.println(currentLine); dimensionX = currentLine.length() ; //maze's width - System.out.println("Line length :" + dimensionX); int currentLineLength ; int compteurLigne = 0 ; @@ -90,7 +115,6 @@ public class Maze //System.out.println(currentLine); currentLineLength = currentLine.length() ; - System.out.println("Current line length :" + currentLineLength); for (int compteurColonne = 0 ; compteurColonne < currentLineLength ; compteurColonne++) { @@ -130,13 +154,6 @@ public class Maze 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) { -- 2.56.1 From bc1886ca946609da28c573b329d166751fc1f191 Mon Sep 17 00:00:00 2001 From: "Sylvain Rager (regar42)" Date: Thu, 7 Dec 2017 10:49:04 +0100 Subject: [PATCH 05/15] Fix rectangle maze generation Switch compteurColonne and compteurLigne Despite their names, compteurColonne and compteurLigne respectively represent the x and y coordinates --- src/maze/Maze.java | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/maze/Maze.java b/src/maze/Maze.java index 93ce736..7b31882 100644 --- a/src/maze/Maze.java +++ b/src/maze/Maze.java @@ -90,7 +90,6 @@ public class Maze fr = new FileReader(filename) ; br = new BufferedReader(fr) ; while (br.readLine() != null) dimensionY++; - //System.out.println("Line count :" + lineCount); } catch (Exception e) { e.printStackTrace(); } finally { @@ -112,8 +111,6 @@ public class Maze while (currentLine != null) { - - //System.out.println(currentLine); currentLineLength = currentLine.length() ; for (int compteurColonne = 0 ; compteurColonne < currentLineLength ; compteurColonne++) { @@ -122,16 +119,16 @@ public class Maze 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; - case 'E' : maze[compteurLigne][compteurColonne] = new EBox("E", compteurLigne, compteurColonne, this) ; + case 'E' : maze[compteurColonne][compteurLigne] = new EBox("E", compteurColonne, compteurLigne, this) ; break; - case 'D' : maze[compteurLigne][compteurColonne] = new DBox("D", compteurLigne, compteurColonne, this) ; + case 'D' : maze[compteurColonne][compteurLigne] = new DBox("D", compteurColonne, compteurLigne, this) ; break; - case 'A' : maze[compteurLigne][compteurColonne] = new ABox("A", compteurLigne, compteurColonne, this) ; + case 'A' : maze[compteurColonne][compteurLigne] = new ABox("A", compteurColonne, compteurLigne, this) ; break; default : throw new MazeReadingException(filename) ; -- 2.56.1 From 9d341349432dae54cff71989703bd7c5fab09f2c Mon Sep 17 00:00:00 2001 From: Antonin GODARD Date: Thu, 7 Dec 2017 11:51:14 +0100 Subject: [PATCH 06/15] Added documentation for modified interfaces. --- src/dijkstra/ASetInterface.java | 6 +++++- src/dijkstra/Dijkstra.java | 4 ++-- src/dijkstra/GraphInterface.java | 12 ++++++++---- src/dijkstra/PiInterface.java | 4 ++++ src/dijkstra/Previous.java | 3 --- src/dijkstra/VertexInterface.java | 8 ++++++-- src/maze/Maze.java | 16 ++++++++++++++-- 7 files changed, 39 insertions(+), 14 deletions(-) diff --git a/src/dijkstra/ASetInterface.java b/src/dijkstra/ASetInterface.java index c09024c..3f5c8a4 100644 --- a/src/dijkstra/ASetInterface.java +++ b/src/dijkstra/ASetInterface.java @@ -2,6 +2,9 @@ package dijkstra; import java.util.HashSet; +/** + * This is the ASet class as described in the INF101 course of Telecom Paristech. + */ public interface ASetInterface { /** Add a vertex to the ASet */ @@ -9,7 +12,8 @@ public interface ASetInterface { /** Check if a vertex belongs to ASet */ public boolean contains(VertexInterface vertex) ; - + + /** Returns the HashSet which represents the ASet. */ public HashSet getVertexHashSet(); diff --git a/src/dijkstra/Dijkstra.java b/src/dijkstra/Dijkstra.java index e91af31..1ed1786 100644 --- a/src/dijkstra/Dijkstra.java +++ b/src/dijkstra/Dijkstra.java @@ -26,7 +26,7 @@ public class Dijkstra { 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); + previous.setPrevious(pivot, vertex) ; } } } @@ -38,7 +38,7 @@ public class Dijkstra { piMax = pi.getPi(vertex) ; } } - a.add(pivot); + a.add(pivot) ; } return previous ; diff --git a/src/dijkstra/GraphInterface.java b/src/dijkstra/GraphInterface.java index 5c8c8e6..97abce9 100644 --- a/src/dijkstra/GraphInterface.java +++ b/src/dijkstra/GraphInterface.java @@ -1,18 +1,22 @@ package dijkstra; import java.util.ArrayList; +/** + * This is the Graph class as described in the INF101 course of Telecom Paristech. + * + */ public interface GraphInterface { - /** Returns the number of Vertexes **/ + /** Returns the number of Vertexes. **/ public int getVertexNumber() ; - /** Return the cost between two vertexes **/ + /** Return the cost between two vertexes. **/ public int getCost(VertexInterface start, VertexInterface end) ; - /** Returns an list of Vertexes **/ + /** Returns an list of Vertexes. **/ public ArrayList getVertexes() ; - /** Returns the neighbour list for a given box */ + /** Returns the neighbour list for a given box. */ public ArrayList generateNeighbours(VertexInterface vertex) ; } diff --git a/src/dijkstra/PiInterface.java b/src/dijkstra/PiInterface.java index 87fce98..4e74e9f 100644 --- a/src/dijkstra/PiInterface.java +++ b/src/dijkstra/PiInterface.java @@ -1,5 +1,9 @@ package dijkstra; +/** + * This is the Pi class as described in the INF101 course of Telecom Paristech. + * + */ public interface PiInterface { /** Returns the value of pi(v) */ diff --git a/src/dijkstra/Previous.java b/src/dijkstra/Previous.java index b6acaeb..527b8af 100644 --- a/src/dijkstra/Previous.java +++ b/src/dijkstra/Previous.java @@ -2,9 +2,6 @@ package dijkstra; import java.util.ArrayList; import java.util.Hashtable; -import java.util.List; - -import maze.Box; public class Previous implements PreviousInterface { diff --git a/src/dijkstra/VertexInterface.java b/src/dijkstra/VertexInterface.java index e62827c..b0ffe0d 100644 --- a/src/dijkstra/VertexInterface.java +++ b/src/dijkstra/VertexInterface.java @@ -1,10 +1,14 @@ package dijkstra; -import java.util.List; +/** + * This describes a vertex as seen in graphs (see INF101 course). + * + */ public interface VertexInterface { - /** Compare a Vertex to another */ + /** Compare a Vertex to another. */ public boolean compareTo(VertexInterface vertex) ; + /** Returns the Label of the vertex. */ public String getLabel() ; } diff --git a/src/maze/Maze.java b/src/maze/Maze.java index 93ce736..f616912 100644 --- a/src/maze/Maze.java +++ b/src/maze/Maze.java @@ -8,6 +8,15 @@ import java.util.ArrayList; import dijkstra.GraphInterface; import dijkstra.VertexInterface; +/**

Maze class

+ *

+ * This class describes the maze as a matrix of Boxes ({@link maze.Box}). + * + * + * @author Antonin Godard + * @author Sylvain Rager + * + */ public class Maze implements GraphInterface { @@ -81,16 +90,19 @@ public class Maze } + /**Constructs the maze from a text file, according to each single character on each line. + * @param filename + */ + public final void initFromTextFile(String filename) { FileReader fr = null ; BufferedReader br = null ; - try { //Determines the height of the maze and stores it into lineCount + try { fr = new FileReader(filename) ; br = new BufferedReader(fr) ; while (br.readLine() != null) dimensionY++; - //System.out.println("Line count :" + lineCount); } catch (Exception e) { e.printStackTrace(); } finally { -- 2.56.1 From 8a8eac7ac11883f154398d5a097af1151ef007d5 Mon Sep 17 00:00:00 2001 From: Antonin Godard Date: Thu, 7 Dec 2017 14:29:04 +0100 Subject: [PATCH 07/15] Added some documentation for classes and methods of modified files. --- src/maze/Box.java | 12 ++++++++++++ src/maze/Maze.java | 12 ++++++++++-- src/maze/MazeReadingException.java | 7 +++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/maze/Box.java b/src/maze/Box.java index 2326e21..9219de9 100644 --- a/src/maze/Box.java +++ b/src/maze/Box.java @@ -4,6 +4,18 @@ import java.util.List; import dijkstra.VertexInterface; +/**

Box class

+ *

+ * + * This class describes a Box and implements {@link dijkstra.VertexInterface}. + *

+ * 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 { diff --git a/src/maze/Maze.java b/src/maze/Maze.java index f616912..a5a8dde 100644 --- a/src/maze/Maze.java +++ b/src/maze/Maze.java @@ -10,8 +10,7 @@ import dijkstra.VertexInterface; /**

Maze class

*

- * This class describes the maze as a matrix of Boxes ({@link maze.Box}). - * + * This class describes the maze as a matrix of Boxes ({@link maze.Box}). * * @author Antonin Godard * @author Sylvain Rager @@ -30,6 +29,9 @@ public class Maze } + /** + * + */ public int getCost(VertexInterface start, VertexInterface end) { Box startBox = (Box) start ; @@ -51,6 +53,9 @@ public class Maze return dimensionX*dimensionY ; } + /** + * @param ArrayList + */ public ArrayList getVertexes() { ArrayList vertexList = new ArrayList() ; for (int i = 0 ; i < dimensionX ; i++) { @@ -63,6 +68,9 @@ public class Maze return vertexList ; } + /** + * + */ public ArrayList generateNeighbours(VertexInterface vertex) { Box box = (Box)vertex ; diff --git a/src/maze/MazeReadingException.java b/src/maze/MazeReadingException.java index 0bfd24c..4cc5822 100644 --- a/src/maze/MazeReadingException.java +++ b/src/maze/MazeReadingException.java @@ -1,5 +1,12 @@ package maze; +/**

MazeReadingException exception

+ *

+ * This class is used to throw exception when reading and initiating the maze from a text file. + * + * + */ + public class MazeReadingException extends Exception { -- 2.56.1 From ec845a0da2fcb86ce7ab885d8595270644132605 Mon Sep 17 00:00:00 2001 From: Antonin Godard Date: Thu, 7 Dec 2017 14:35:17 +0100 Subject: [PATCH 08/15] Added .gitignore --- .gitignore | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5e1d7a3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +*.html +*.css +src/package-list +src/script.js -- 2.56.1 From eb1e7a8bc0a76f6337680a42342bb928cd8adea2 Mon Sep 17 00:00:00 2001 From: "Sylvain Rager (regar42)" Date: Thu, 7 Dec 2017 12:24:04 +0100 Subject: [PATCH 09/15] Sharpen getCost and improve wallbox handling * Rewrite getCost() from Maze to remove unneeded isWalkable checks on boxes * Return an empty neighbor list for wall boxes (in generateNeighbours() from Maze) --- src/maze/Maze.java | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/src/maze/Maze.java b/src/maze/Maze.java index 93ce736..34d5cf0 100644 --- a/src/maze/Maze.java +++ b/src/maze/Maze.java @@ -25,17 +25,8 @@ public class Maze 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() { @@ -60,7 +51,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]) ; } @@ -180,7 +176,6 @@ public class Maze } finally { try { pw.close();} catch(Exception e) {} } - } public int getDimensionX() { -- 2.56.1 From ebb91b01b388f6c07f29f2d5fcb3a48402011faf Mon Sep 17 00:00:00 2001 From: "Sylvain Rager (regar42)" Date: Sat, 9 Dec 2017 23:48:50 +0100 Subject: [PATCH 10/15] Code cleanup * Remove unwanted getters and setters * Properly handle object passed to constructors * Remove redundant condition check on ASet.contains()'s return * Make Maze.initFromTextFile() private --- src/dijkstra/ASet.java | 7 +++---- src/dijkstra/Pi.java | 2 +- src/dijkstra/Previous.java | 4 ++-- src/maze/Maze.java | 21 +++++++-------------- src/maze/MazeReadingException.java | 4 ---- 5 files changed, 13 insertions(+), 25 deletions(-) diff --git a/src/dijkstra/ASet.java b/src/dijkstra/ASet.java index 5dc12b5..76c569b 100644 --- a/src/dijkstra/ASet.java +++ b/src/dijkstra/ASet.java @@ -11,20 +11,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 b6acaeb..bde8d5f 100644 --- a/src/dijkstra/Previous.java +++ b/src/dijkstra/Previous.java @@ -15,8 +15,8 @@ public class Previous implements PreviousInterface { 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) { diff --git a/src/maze/Maze.java b/src/maze/Maze.java index 34d5cf0..652a87b 100644 --- a/src/maze/Maze.java +++ b/src/maze/Maze.java @@ -21,6 +21,12 @@ 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 ; @@ -77,7 +83,7 @@ public class Maze } - public final void initFromTextFile(String filename) { + private final void initFromTextFile(String filename) { FileReader fr = null ; BufferedReader br = null ; @@ -182,25 +188,12 @@ public class Maze 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 0bfd24c..432f077 100644 --- a/src/maze/MazeReadingException.java +++ b/src/maze/MazeReadingException.java @@ -12,8 +12,4 @@ public class MazeReadingException } - public String getFilename() { - return filename ; - } - } -- 2.56.1 From fda73a202535dc8fd902fdd1eb0df3bc70f639b7 Mon Sep 17 00:00:00 2001 From: Antonin Godard Date: Sun, 10 Dec 2017 11:38:33 +0100 Subject: [PATCH 11/15] Fixed mistake were I used List instead of ArrayList --- src/maze/Box.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/maze/Box.java b/src/maze/Box.java index 2326e21..4778b48 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; @@ -11,7 +10,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) { -- 2.56.1 From 8db8b3faad5eca628a6c4b2daa844296004ff572 Mon Sep 17 00:00:00 2001 From: Antonin Godard Date: Sun, 10 Dec 2017 11:57:52 +0100 Subject: [PATCH 12/15] Remove unneeded javadocs in Maze.java --- src/maze/Maze.java | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/maze/Maze.java b/src/maze/Maze.java index a5a8dde..55bb3c4 100644 --- a/src/maze/Maze.java +++ b/src/maze/Maze.java @@ -53,9 +53,6 @@ public class Maze return dimensionX*dimensionY ; } - /** - * @param ArrayList - */ public ArrayList getVertexes() { ArrayList vertexList = new ArrayList() ; for (int i = 0 ; i < dimensionX ; i++) { @@ -68,9 +65,6 @@ public class Maze return vertexList ; } - /** - * - */ public ArrayList generateNeighbours(VertexInterface vertex) { Box box = (Box)vertex ; -- 2.56.1 From 4d1d3b39a2c55cf0762809e6af749471c5bf4e99 Mon Sep 17 00:00:00 2001 From: "Sylvain Rager (regar42)" Date: Sun, 10 Dec 2017 15:27:57 +0100 Subject: [PATCH 13/15] Add documentation and fix Javadoc warnings * Describe the aim of the interfaces * Explain what are the box inherited from Box * Fix Javadoc warnings by specifying the parameters and returns of the methods in the interfaces --- src/dijkstra/ASetInterface.java | 19 ++++++++++++++++--- src/dijkstra/GraphInterface.java | 28 +++++++++++++++++++++++----- src/dijkstra/PiInterface.java | 18 +++++++++++++++--- src/dijkstra/PreviousInterface.java | 27 ++++++++++++++++++++++++--- src/dijkstra/VertexInterface.java | 11 +++++++++-- src/maze/ABox.java | 3 +++ src/maze/DBox.java | 4 ++++ src/maze/EBox.java | 4 ++++ src/maze/Maze.java | 1 + src/maze/WBox.java | 4 ++++ 10 files changed, 103 insertions(+), 16 deletions(-) diff --git a/src/dijkstra/ASetInterface.java b/src/dijkstra/ASetInterface.java index 3f5c8a4..4bc12d0 100644 --- a/src/dijkstra/ASetInterface.java +++ b/src/dijkstra/ASetInterface.java @@ -4,16 +4,29 @@ 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 { - /** Add a vertex to the ASet */ + /** + * Adds a vertex to the set. + * @param vertex + * The element to be added to the set. + */ public void add(VertexInterface vertex) ; - /** Check if a vertex belongs to ASet */ + /** + * Checks if a vertex belongs to the set. + * @param vertex + * The element we must check if it belongs to the set. + * @return true if the element is in the set, false if not. + */ public boolean contains(VertexInterface vertex) ; - /** Returns the HashSet which represents the ASet. */ + /** + * Returns the HashSet which represents the ASet. + * @return A HashSet containing the same elements as in the set. + */ public HashSet getVertexHashSet(); diff --git a/src/dijkstra/GraphInterface.java b/src/dijkstra/GraphInterface.java index 97abce9..256a211 100644 --- a/src/dijkstra/GraphInterface.java +++ b/src/dijkstra/GraphInterface.java @@ -3,20 +3,38 @@ 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 { - /** Returns the number of Vertexes. **/ + /** + * Gets the number of VertexInterface in the graph. + * @return An integer giving the number of VertexInterface in the graph. + */ public int getVertexNumber() ; - /** Return the cost between two vertexes. **/ + /** + * 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 start. + * @return An integer giving the cost of the link between the given VertexInterface. + */ public int getCost(VertexInterface start, VertexInterface end) ; - /** Returns an list of Vertexes. **/ + /** + * Returns all VertexInterface contained in the graph. + * @return An ArrayList of VertexInterface. + */ public ArrayList getVertexes() ; - /** Returns the neighbour list for a given box. */ + /** + * Returns the neighbour list for a given VertexInterface. + * @param vertex + * The VertexInterface from which we will seek for neighbours. + * @return An ArrayList of VertexInterface. + */ public ArrayList generateNeighbours(VertexInterface vertex) ; } diff --git a/src/dijkstra/PiInterface.java b/src/dijkstra/PiInterface.java index 4e74e9f..3c96101 100644 --- a/src/dijkstra/PiInterface.java +++ b/src/dijkstra/PiInterface.java @@ -2,14 +2,26 @@ 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 { - /** Returns the value of pi(v) */ + /** + * Gets the cost to reach the given VertexInterface + * @param vertex + * The VertexInterface from which we get the Pi value (cost) to reach it. + * @return An integer giving the Pi value (cost) the reach the VertexInterface. + * + */ public int getPi(VertexInterface vertex) ; - /** Sets the Pi value of an element */ + /** + * Sets the Pi value of an element + * @param i + * The Pi value (cost) to set. + * @param vertex + * The VertexInterface on which the Pi value (cost) is to be set. + */ public void setPi(int i, VertexInterface vertex) ; } diff --git a/src/dijkstra/PreviousInterface.java b/src/dijkstra/PreviousInterface.java index 02c08b9..c06dd6d 100644 --- a/src/dijkstra/PreviousInterface.java +++ b/src/dijkstra/PreviousInterface.java @@ -2,15 +2,36 @@ package dijkstra; import java.util.ArrayList; +/** + * The PreviousInterface contains information about the father (as in the graph's theory) of some VertexInterface. + * The father of a VertexInterface is the VertexInterface used to reach it. + * Typically in Dijkstra, it's the VertexInterface used to reach vertex with the lowest cost. + */ public interface PreviousInterface { - /** Returns the father of an element */ + /** + * Returns the father of a VertexInterface. + * @param vertex + * The VertexInterface from which we get the father (previous VertexInterface). + * @return The direct previous element used to reach vertex. + */ public VertexInterface getPrevious(VertexInterface vertex) ; - /** Sets the father of an element to another element */ + /** + * 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 */ + /** + * Shortest path from root to a vertex. + * @param vertex + * The vertex to which the shortest path will be computed. + * @return An ArrayList of the VertexInterface used to reach vertex with the lowest cost. + */ public ArrayList getShortestPathTo(VertexInterface vertex) ; } diff --git a/src/dijkstra/VertexInterface.java b/src/dijkstra/VertexInterface.java index b0ffe0d..e047a9a 100644 --- a/src/dijkstra/VertexInterface.java +++ b/src/dijkstra/VertexInterface.java @@ -6,9 +6,16 @@ package dijkstra; */ public interface VertexInterface { - /** Compare a Vertex to another. */ + /** + * 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) ; - /** Returns the Label of the vertex. */ + /** + * @return The label of the element. + */ public String getLabel() ; } diff --git a/src/maze/ABox.java b/src/maze/ABox.java index 1b875d0..ef72aa2 100644 --- a/src/maze/ABox.java +++ b/src/maze/ABox.java @@ -1,5 +1,8 @@ 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 ABox (String label, int x, int y, Maze maze) { diff --git a/src/maze/DBox.java b/src/maze/DBox.java index 6bcdc8e..3b62e7c 100644 --- a/src/maze/DBox.java +++ b/src/maze/DBox.java @@ -1,5 +1,9 @@ package maze; +/** + * A DBox is the Box on which the user is first in the Maze. + */ + public class DBox extends Box{ public DBox (String label, int x, int y, Maze maze) { diff --git a/src/maze/EBox.java b/src/maze/EBox.java index 88c63d5..b2d968b 100644 --- a/src/maze/EBox.java +++ b/src/maze/EBox.java @@ -1,5 +1,9 @@ package maze; +/** + * An EBox is a Box that can be walked through (an empty box). + */ + public class EBox extends Box{ diff --git a/src/maze/Maze.java b/src/maze/Maze.java index 55bb3c4..6d1c32a 100644 --- a/src/maze/Maze.java +++ b/src/maze/Maze.java @@ -94,6 +94,7 @@ public class Maze /**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. */ public final void initFromTextFile(String filename) { diff --git a/src/maze/WBox.java b/src/maze/WBox.java index d0adc4b..29a1107 100644 --- a/src/maze/WBox.java +++ b/src/maze/WBox.java @@ -1,5 +1,9 @@ package maze; +/** + * A WBox is a Box that can't be walked through (a wall). + */ + public class WBox extends Box { -- 2.56.1 From d46b3c3b99f5819064dffed58108b9e97a03b9d9 Mon Sep 17 00:00:00 2001 From: Antonin Godard Date: Sun, 10 Dec 2017 17:08:54 +0100 Subject: [PATCH 14/15] Fix List to ArrayList Add toString method for Box Add code to print the shortest path for this particular case. --- src/MainTest.java | 14 +++++++++++--- src/dijkstra/ASet.java | 2 -- src/dijkstra/Previous.java | 8 +------- src/dijkstra/VertexInterface.java | 1 - src/maze/ABox.java | 2 +- src/maze/Box.java | 8 ++++---- src/maze/DBox.java | 2 +- src/maze/MazeReadingException.java | 3 +-- 8 files changed, 19 insertions(+), 21 deletions(-) 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 76c569b..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() ; diff --git a/src/dijkstra/Previous.java b/src/dijkstra/Previous.java index bde8d5f..9f321e8 100644 --- a/src/dijkstra/Previous.java +++ b/src/dijkstra/Previous.java @@ -2,16 +2,11 @@ package dijkstra; import java.util.ArrayList; import java.util.Hashtable; -import java.util.List; - -import maze.Box; public class Previous implements PreviousInterface { private Hashtable hashPrevious = new Hashtable() ; private ArrayList shortestPath = new ArrayList() ; - - public Previous(Hashtable hashPrevious, ArrayList shortestPath) { @@ -39,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 e62827c..fcff284 100644 --- a/src/dijkstra/VertexInterface.java +++ b/src/dijkstra/VertexInterface.java @@ -1,5 +1,4 @@ package dijkstra; -import java.util.List; public interface VertexInterface { diff --git a/src/maze/ABox.java b/src/maze/ABox.java index 1b875d0..44601f0 100644 --- a/src/maze/ABox.java +++ b/src/maze/ABox.java @@ -1,6 +1,6 @@ package 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 4778b48..63b34cf 100644 --- a/src/maze/Box.java +++ b/src/maze/Box.java @@ -21,7 +21,7 @@ public abstract class Box } - public List generateNeighbors() { + public ArrayList generateNeighbors() { return maze.generateNeighbours(this) ; } @@ -45,11 +45,11 @@ public abstract class Box return false ; } - public List getNeighbourList() { + public ArrayList getNeighbourList() { return neighbourList; } - public void setNeighbourList(List neighbourList) { + public void setNeighbourList(ArrayList neighbourList) { this.neighbourList = neighbourList; } @@ -86,7 +86,7 @@ public abstract class Box } 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 6bcdc8e..a5dd14f 100644 --- a/src/maze/DBox.java +++ b/src/maze/DBox.java @@ -1,6 +1,6 @@ package 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/MazeReadingException.java b/src/maze/MazeReadingException.java index 432f077..d96b1ca 100644 --- a/src/maze/MazeReadingException.java +++ b/src/maze/MazeReadingException.java @@ -3,12 +3,11 @@ 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 ; } -- 2.56.1 From d836c2c9f02208dd5ee5f06fd324d8d6197f756f Mon Sep 17 00:00:00 2001 From: Antonin Godard Date: Sun, 10 Dec 2017 17:21:26 +0100 Subject: [PATCH 15/15] Remove unneeded setters in Box.java Remove compareTo in Box.java and VertexInterface --- src/dijkstra/VertexInterface.java | 4 +--- src/maze/Box.java | 31 ------------------------------- 2 files changed, 1 insertion(+), 34 deletions(-) diff --git a/src/dijkstra/VertexInterface.java b/src/dijkstra/VertexInterface.java index fcff284..4c20442 100644 --- a/src/dijkstra/VertexInterface.java +++ b/src/dijkstra/VertexInterface.java @@ -2,8 +2,6 @@ package dijkstra; public interface VertexInterface { - /** Compare a Vertex to another */ - public boolean compareTo(VertexInterface vertex) ; - public String getLabel() ; + } diff --git a/src/maze/Box.java b/src/maze/Box.java index 63b34cf..0845613 100644 --- a/src/maze/Box.java +++ b/src/maze/Box.java @@ -26,13 +26,6 @@ public abstract class Box } - public boolean compareTo(VertexInterface vertex) { - if (this.getLabel() == vertex.getLabel()) { - return true ; - } - return false ; - } - public boolean isWalkable() { return true ; } @@ -49,41 +42,17 @@ public abstract class Box return neighbourList; } - public void setNeighbourList(ArrayList 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: " + label + ", x: " + x + ", y: " + y; -- 2.56.1