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 diff --git a/src/ABox.java b/src/ABox.java deleted file mode 100644 index 51e1186..0000000 --- a/src/ABox.java +++ /dev/null @@ -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 ; - } - -} diff --git a/src/ASetInterface.java b/src/ASetInterface.java deleted file mode 100644 index 8d4c177..0000000 --- a/src/ASetInterface.java +++ /dev/null @@ -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) ; - - -} diff --git a/src/Box.java b/src/Box.java deleted file mode 100644 index 940bfde..0000000 --- a/src/Box.java +++ /dev/null @@ -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 neighbourList = new ArrayList() ; - - public Box (String label, int x, int y, Maze maze) { - - this.label = label ; - this.x = x ; - this.y = y ; - this.maze = maze ; - - } - - 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 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 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; - } - - - -} diff --git a/src/Dijkstra.java b/src/Dijkstra.java deleted file mode 100644 index 34ab762..0000000 --- a/src/Dijkstra.java +++ /dev/null @@ -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 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 ; - - } - -} diff --git a/src/GraphInterface.java b/src/GraphInterface.java deleted file mode 100644 index 0841a8d..0000000 --- a/src/GraphInterface.java +++ /dev/null @@ -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 getVertexes() ; - -} diff --git a/src/MainTest.java b/src/MainTest.java index e22c50a..599e8fb 100644 --- a/src/MainTest.java +++ b/src/MainTest.java @@ -1,12 +1,21 @@ +import java.util.ArrayList; + +import dijkstra.*; +import 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); + + 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/MazeReadingException.java b/src/MazeReadingException.java deleted file mode 100644 index 62c6335..0000000 --- a/src/MazeReadingException.java +++ /dev/null @@ -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 ; - } - -} diff --git a/src/PiInterface.java b/src/PiInterface.java deleted file mode 100644 index f1a4ac0..0000000 --- a/src/PiInterface.java +++ /dev/null @@ -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) ; - -} diff --git a/src/PreviousInterface.java b/src/PreviousInterface.java deleted file mode 100644 index de81746..0000000 --- a/src/PreviousInterface.java +++ /dev/null @@ -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) ; - -} diff --git a/src/VertexInterface.java b/src/VertexInterface.java deleted file mode 100644 index c78bd3f..0000000 --- a/src/VertexInterface.java +++ /dev/null @@ -1,9 +0,0 @@ - -public interface VertexInterface { - - /** Compare a Vertex to another */ - public boolean compareTo(VertexInterface vertex) ; - - public String getLabel() ; - -} diff --git a/src/dijkstra/ASet.java b/src/dijkstra/ASet.java new file mode 100644 index 0000000..b78bc84 --- /dev/null +++ b/src/dijkstra/ASet.java @@ -0,0 +1,32 @@ +package dijkstra; + +import java.util.HashSet; + +public class ASet implements ASetInterface { + + private HashSet vertexHashSet = new HashSet() ; + + + public ASet(HashSet vertexHashSet) { + + this.vertexHashSet = new HashSet(vertexHashSet); + + } + + public void add(VertexInterface vertex) { + + vertexHashSet.add(vertex) ; + + } + + public boolean contains(VertexInterface vertex) { + + return vertexHashSet.contains(vertex); + + } + + public HashSet getVertexHashSet() { + return vertexHashSet; + } + +} diff --git a/src/dijkstra/ASetInterface.java b/src/dijkstra/ASetInterface.java new file mode 100644 index 0000000..4bc12d0 --- /dev/null +++ b/src/dijkstra/ASetInterface.java @@ -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 true if the element is in the set, false if not. + */ + public boolean contains(VertexInterface vertex) ; + + /** + * 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/Dijkstra.java b/src/dijkstra/Dijkstra.java new file mode 100644 index 0000000..1ed1786 --- /dev/null +++ b/src/dijkstra/Dijkstra.java @@ -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 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 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/GraphInterface.java b/src/dijkstra/GraphInterface.java new file mode 100644 index 0000000..256a211 --- /dev/null +++ b/src/dijkstra/GraphInterface.java @@ -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 VertexInterface in the graph. + * @return An integer giving the number of VertexInterface 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 start. + * @return An integer giving the cost of the link between the given VertexInterface. + */ + public int getCost(VertexInterface start, VertexInterface end) ; + + /** + * Returns all VertexInterface contained in the graph. + * @return An ArrayList of VertexInterface. + */ + public ArrayList getVertexes() ; + + /** + * 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/Pi.java b/src/dijkstra/Pi.java new file mode 100644 index 0000000..2b21078 --- /dev/null +++ b/src/dijkstra/Pi.java @@ -0,0 +1,27 @@ +package dijkstra; + +import java.util.Hashtable; + +public class Pi implements PiInterface { + + private Hashtable hashPi = new Hashtable() ; + + public Pi(Hashtable hashPi) { + + this.hashPi = new Hashtable(hashPi); + + } + + public int getPi(VertexInterface vertex) { + + return hashPi.get(vertex) ; + + } + + public void setPi(int i, VertexInterface vertex) { + + hashPi.put(vertex, i) ; + + } + +} diff --git a/src/dijkstra/PiInterface.java b/src/dijkstra/PiInterface.java new file mode 100644 index 0000000..3c96101 --- /dev/null +++ b/src/dijkstra/PiInterface.java @@ -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 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 + * @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/Previous.java b/src/dijkstra/Previous.java new file mode 100644 index 0000000..9f321e8 --- /dev/null +++ b/src/dijkstra/Previous.java @@ -0,0 +1,40 @@ +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 Previous(Hashtable hashPrevious, ArrayList shortestPath) { + + this.hashPrevious = new Hashtable(hashPrevious); + this.shortestPath = new ArrayList(shortestPath); + } + + 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/dijkstra/PreviousInterface.java b/src/dijkstra/PreviousInterface.java new file mode 100644 index 0000000..c06dd6d --- /dev/null +++ b/src/dijkstra/PreviousInterface.java @@ -0,0 +1,37 @@ +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 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. + * @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 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 new file mode 100644 index 0000000..51d147d --- /dev/null +++ b/src/dijkstra/VertexInterface.java @@ -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() ; + +} diff --git a/src/maze/ABox.java b/src/maze/ABox.java new file mode 100644 index 0000000..94f88f7 --- /dev/null +++ b/src/maze/ABox.java @@ -0,0 +1,19 @@ +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 EBox{ + + public ABox (String label, int x, int y, Maze maze) { + + super(label,x,y,maze) ; + + } + + @Override + public boolean isArrival() { + return true ; + } + +} diff --git a/src/maze/Box.java b/src/maze/Box.java new file mode 100644 index 0000000..00f7694 --- /dev/null +++ b/src/maze/Box.java @@ -0,0 +1,73 @@ +package maze; +import java.util.ArrayList; + +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 { + + private int x ; + private int y ; + private String label = null ; + private Maze maze ; + private ArrayList neighbourList = new ArrayList() ; + + public Box (String label, int x, int y, Maze maze) { + + this.label = label ; + this.x = x ; + this.y = y ; + this.maze = maze ; + + } + + public ArrayList generateNeighbors() { + return maze.generateNeighbours(this) ; + + } + + public boolean isWalkable() { + return true ; + } + + public boolean isArrival() { + return false ; + } + + public boolean isDeparture() { + return false ; + } + + public ArrayList 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; + } + +} diff --git a/src/DBox.java b/src/maze/DBox.java similarity index 50% rename from src/DBox.java rename to src/maze/DBox.java index 757d2e4..cc7b7fb 100644 --- a/src/DBox.java +++ b/src/maze/DBox.java @@ -1,5 +1,10 @@ +package maze; -public class DBox extends Box{ +/** + * A DBox is the Box on which the user is first in the Maze. + */ + +public class DBox extends EBox{ public DBox (String label, int x, int y, Maze maze) { diff --git a/src/EBox.java b/src/maze/EBox.java similarity index 53% rename from src/EBox.java rename to src/maze/EBox.java index 643b4dd..b2d968b 100644 --- a/src/EBox.java +++ b/src/maze/EBox.java @@ -1,3 +1,8 @@ +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.java b/src/maze/Maze.java similarity index 59% rename from src/Maze.java rename to src/maze/Maze.java index 91897a2..6d7ce44 100644 --- a/src/Maze.java +++ b/src/maze/Maze.java @@ -1,9 +1,21 @@ +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; + +/**

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 { @@ -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) { 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() { @@ -40,24 +49,62 @@ 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 ; } - public final void initFromTextFile(String filename) { + public ArrayList generateNeighbours(VertexInterface vertex) { + + Box box = (Box)vertex ; + 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]) ; + } + + 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 ; 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 { @@ -71,9 +118,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 ; @@ -81,10 +126,7 @@ public class Maze while (currentLine != null) { - - //System.out.println(currentLine); currentLineLength = currentLine.length() ; - System.out.println("Current line length :" + currentLineLength); for (int compteurColonne = 0 ; compteurColonne < currentLineLength ; compteurColonne++) { @@ -92,16 +134,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) ; @@ -124,13 +166,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) { @@ -157,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 new file mode 100644 index 0000000..37b0cd3 --- /dev/null +++ b/src/maze/MazeReadingException.java @@ -0,0 +1,21 @@ +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 { + + private static final long serialVersionUID = 1L; + + public MazeReadingException (String filename) { + + super("Invalid maze format : " + filename) ; + + } + +} diff --git a/src/WBox.java b/src/maze/WBox.java similarity index 65% rename from src/WBox.java rename to src/maze/WBox.java index c7759d1..29a1107 100644 --- a/src/WBox.java +++ b/src/maze/WBox.java @@ -1,3 +1,8 @@ +package maze; + +/** + * A WBox is a Box that can't be walked through (a wall). + */ public class WBox extends Box {