From e8896556bc41cc7fd7776f57de7c1ba8d9641be9 Mon Sep 17 00:00:00 2001 From: "Sylvain Rager (regar42)" Date: Mon, 11 Dec 2017 11:46:07 +0100 Subject: [PATCH] Normalize code and fix saveToTextFile * Normalize spaces and blank lines according to Java Code Conventions from Java SE 8 * Wrap code to 80 characters per line (Java Code Conventions for Java SE 8) * Fix saveToTextFile() in Maze : same confusion that was fixed in bc1886ca for initFromTextFile() (x et y coordinates were respectively used as y and x) --- .gitignore | 1 + src/MainTest.java | 18 ++- src/dijkstra/ASet.java | 12 +- src/dijkstra/ASetInterface.java | 21 +-- src/dijkstra/Dijkstra.java | 81 +++++----- src/dijkstra/GraphInterface.java | 33 +++-- src/dijkstra/Pi.java | 19 +-- src/dijkstra/PiInterface.java | 21 +-- src/dijkstra/Previous.java | 36 ++--- src/dijkstra/PreviousInterface.java | 29 ++-- src/dijkstra/VertexInterface.java | 5 +- src/maze/ABox.java | 19 ++- src/maze/Box.java | 54 +++---- src/maze/DBox.java | 12 +- src/maze/EBox.java | 13 +- src/maze/Maze.java | 220 ++++++++++++++-------------- src/maze/MazeReadingException.java | 16 +- src/maze/WBox.java | 19 +-- 18 files changed, 307 insertions(+), 322 deletions(-) diff --git a/.gitignore b/.gitignore index 5e1d7a3..449a8dd 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ *.css src/package-list src/script.js +*.class diff --git a/src/MainTest.java b/src/MainTest.java index 599e8fb..b581dc1 100644 --- a/src/MainTest.java +++ b/src/MainTest.java @@ -6,17 +6,19 @@ import maze.*; public class MainTest { public static void main(String[] args) { - - Maze mazeTest = new Maze("data/labyrinthe.txt") ; - - Previous previous = (Previous) Dijkstra.dijkstra(mazeTest, mazeTest.getMaze()[1][1]); - - ArrayList shortest = previous.getShortestPathTo(mazeTest.getMaze()[5][5]); - + + Maze mazeTest = new Maze("data/labyrinthe.txt"); + + 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 b78bc84..09e41a8 100644 --- a/src/dijkstra/ASet.java +++ b/src/dijkstra/ASet.java @@ -4,29 +4,21 @@ import java.util.HashSet; public class ASet implements ASetInterface { - private HashSet vertexHashSet = new HashSet() ; + private HashSet vertexHashSet; - public ASet(HashSet vertexHashSet) { - this.vertexHashSet = new HashSet(vertexHashSet); - } public void add(VertexInterface vertex) { - - vertexHashSet.add(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 index 4bc12d0..b204293 100644 --- a/src/dijkstra/ASetInterface.java +++ b/src/dijkstra/ASetInterface.java @@ -3,31 +3,32 @@ 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). + * 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) ; - + 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. + * @return true if the element is in the set, + * false if not. */ - public boolean contains(VertexInterface vertex) ; - + 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 index 1ed1786..5368f02 100644 --- a/src/dijkstra/Dijkstra.java +++ b/src/dijkstra/Dijkstra.java @@ -4,62 +4,63 @@ 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() ; - + + private static PreviousInterface dijkstra(GraphInterface g, + VertexInterface r, + ASetInterface a, + PiInterface pi, + PreviousInterface previous) { + + VertexInterface pivot = r; + ArrayList vertexList = g.getVertexes(); + + a.add(r); + + pi.setPi(0, r); 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 (int j = 0; j < g.getVertexNumber(); j++) { for (VertexInterface vertex: g.generateNeighbours(pivot)) { - if (!a.contains(vertex)) { + 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) ; + pi.setPi(pi.getPi(pivot) + g.getCost(pivot, vertex), vertex); + previous.setPrevious(pivot, vertex); } } } - - int piMax = Integer.MAX_VALUE ; + + int piMax = Integer.MAX_VALUE; for (VertexInterface vertex: vertexList) { if (! a.contains(vertex) && pi.getPi(vertex) < piMax) { - pivot = vertex ; - piMax = pi.getPi(vertex) ; + pivot = vertex; + piMax = pi.getPi(vertex); } } - a.add(pivot) ; + 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) ; - - + 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 index 256a211..66aa95a 100644 --- a/src/dijkstra/GraphInterface.java +++ b/src/dijkstra/GraphInterface.java @@ -2,39 +2,44 @@ 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. + * 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. + * @return An integer giving the number of + * VertexInterface in the graph. */ - public int getVertexNumber() ; - + 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. + * 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) ; - + public int getCost(VertexInterface start, VertexInterface end); + /** * Returns all VertexInterface contained in the graph. * @return An ArrayList of VertexInterface. */ - public ArrayList getVertexes() ; - + 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) ; - + public ArrayList generateNeighbours(VertexInterface vertex); } diff --git a/src/dijkstra/Pi.java b/src/dijkstra/Pi.java index 2b21078..6b7a7ca 100644 --- a/src/dijkstra/Pi.java +++ b/src/dijkstra/Pi.java @@ -3,25 +3,18 @@ package dijkstra; import java.util.Hashtable; public class Pi implements PiInterface { - - private Hashtable hashPi = new Hashtable() ; - + + private Hashtable hashPi; + public Pi(Hashtable hashPi) { - this.hashPi = new Hashtable(hashPi); - } - + public int getPi(VertexInterface vertex) { - - return hashPi.get(vertex) ; - + return hashPi.get(vertex); } public void setPi(int i, VertexInterface vertex) { - - hashPi.put(vertex, i) ; - + hashPi.put(vertex, i); } - } diff --git a/src/dijkstra/PiInterface.java b/src/dijkstra/PiInterface.java index 3c96101..9afc7dc 100644 --- a/src/dijkstra/PiInterface.java +++ b/src/dijkstra/PiInterface.java @@ -2,26 +2,29 @@ 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). + * 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. + * The VertexInterface from which we get + * the Pi value (cost) to reach it. + * @return An integer giving the Pi value (cost) + * to reach the VertexInterface. * */ - public int getPi(VertexInterface vertex) ; - + 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. + * The VertexInterface on + * which the Pi value (cost) is to be set. */ - public void setPi(int i, VertexInterface vertex) ; - + public void setPi(int i, VertexInterface vertex); } diff --git a/src/dijkstra/Previous.java b/src/dijkstra/Previous.java index 9f321e8..0a9c5e7 100644 --- a/src/dijkstra/Previous.java +++ b/src/dijkstra/Previous.java @@ -4,37 +4,37 @@ 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) { + private Hashtable hashPrevious; + private ArrayList shortestPath; + + 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) ; - + + return hashPrevious.get(vertex); } public void setPrevious(VertexInterface father, VertexInterface son) { - - hashPrevious.put(son, father) ; + 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) ; - } - } + 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 index c06dd6d..d154d6b 100644 --- a/src/dijkstra/PreviousInterface.java +++ b/src/dijkstra/PreviousInterface.java @@ -3,20 +3,25 @@ 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. + * 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). + * The VertexInterface from which we get + * the father (previous VertexInterface). * @return The direct previous element used to reach vertex. */ - public VertexInterface getPrevious(VertexInterface vertex) ; - + public VertexInterface getPrevious(VertexInterface vertex); + /** * Sets the father of an element to another element. * @param father @@ -24,14 +29,14 @@ public interface PreviousInterface { * @param son * The element that will be assigned a new father. */ - public void setPrevious(VertexInterface father, VertexInterface son) ; - + 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. + * @return An ArrayList of the VertexInterface + * used to reach vertex with the lowest cost. */ - public ArrayList getShortestPathTo(VertexInterface vertex) ; - + public ArrayList getShortestPathTo(VertexInterface vertex); } diff --git a/src/dijkstra/VertexInterface.java b/src/dijkstra/VertexInterface.java index 51d147d..b020bd2 100644 --- a/src/dijkstra/VertexInterface.java +++ b/src/dijkstra/VertexInterface.java @@ -5,10 +5,9 @@ package dijkstra; * */ public interface VertexInterface { - + /** * @return The label of the element. */ - public String getLabel() ; - + public String getLabel(); } diff --git a/src/maze/ABox.java b/src/maze/ABox.java index 94f88f7..9a8b607 100644 --- a/src/maze/ABox.java +++ b/src/maze/ABox.java @@ -1,19 +1,18 @@ package maze; /** - * ABox is an arrival Box. It's the last Box to be walked through and it's an exit to the 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 ; + super(label, x, y, maze); } + @Override + public boolean isArrival() { + return true; + } } diff --git a/src/maze/Box.java b/src/maze/Box.java index 00f7694..91a96a4 100644 --- a/src/maze/Box.java +++ b/src/maze/Box.java @@ -5,11 +5,12 @@ 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. - * + * 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 * @@ -18,40 +19,29 @@ import dijkstra.VertexInterface; public abstract class Box implements VertexInterface { - private int x ; - private int y ; - private String label = null ; - private Maze maze ; - private ArrayList neighbourList = new ArrayList() ; - + private int x; + private int y; + private String label; + private Maze maze; + public Box (String label, int x, int y, Maze maze) { - - this.label = label ; - this.x = x ; - this.y = y ; - this.maze = maze ; - + + this.label = new String(label); + this.x = x; + this.y = y; + this.maze = maze; } - - public ArrayList generateNeighbors() { - return maze.generateNeighbours(this) ; - - } - + public boolean isWalkable() { - return true ; + return true; } - + public boolean isArrival() { - return false ; + return false; } - + public boolean isDeparture() { - return false ; - } - - public ArrayList getNeighbourList() { - return neighbourList; + return false; } public int getX() { @@ -65,9 +55,9 @@ public abstract class Box public String getLabel() { return label; } - + public String toString() { return "Label: " + label + ", x: " + x + ", y: " + y; } - + } diff --git a/src/maze/DBox.java b/src/maze/DBox.java index cc7b7fb..ddc5a08 100644 --- a/src/maze/DBox.java +++ b/src/maze/DBox.java @@ -1,20 +1,18 @@ package maze; /** - * A DBox is the Box on which the user is first in the Maze. + * 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) { - - super(label,x,y,maze) ; - + super(label, x, y, maze); } - + @Override public boolean isDeparture() { - return true ; + return true; } - } diff --git a/src/maze/EBox.java b/src/maze/EBox.java index b2d968b..1648dd8 100644 --- a/src/maze/EBox.java +++ b/src/maze/EBox.java @@ -1,16 +1,13 @@ package maze; /** - * An EBox is a Box that can be walked through (an empty box). + * An EBox is a Box + * that can be walked through (an empty box). */ public class EBox extends Box{ - - - public EBox (String label, int x, int y, Maze maze) { - - super(label,x,y,maze) ; - - } + public EBox (String label, int x, int y, Maze maze) { + super(label, x, y, maze); + } } diff --git a/src/maze/Maze.java b/src/maze/Maze.java index 6d7ce44..0faef52 100644 --- a/src/maze/Maze.java +++ b/src/maze/Maze.java @@ -11,189 +11,196 @@ 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 { +public class Maze + implements GraphInterface { + + private int dimensionX; + private int dimensionY; + private Box[][] maze; - private int dimensionX ; - private int dimensionY ; - private Box[][] maze ; - public Maze(String filename) { - - initFromTextFile(filename) ; - + initFromTextFile(filename); } - + // public Maze(Box[][] maze){ // //Replacement for setMaze() - // //Todo : check if the given array is a proper maze + // //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 ; - return startBox.getNeighbourList().contains(endBox) ? 1 : 0 ; + Box startBox = (Box) start; + Box endBox = (Box) end; + + return generateNeighbours(startBox).contains(endBox) ? 1 : 0; } - + public int getVertexNumber() { - return dimensionX*dimensionY ; + return dimensionX * dimensionY; } public ArrayList getVertexes() { - ArrayList vertexList = new ArrayList() ; - for (int i = 0 ; i < dimensionX ; i++) { - for (int j = 0 ; j < dimensionY ; j++) { + ArrayList vertexList = new ArrayList(); + for (int i = 0; i < dimensionX; i++) { + 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 ArrayList generateNeighbours(VertexInterface vertex) { - - Box box = (Box)vertex ; - int x = box.getX() ; - int y = box.getY() ; - ArrayList neighbourList = new ArrayList() ; + + 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 ; + return neighbourList; } if (x > 0 && maze[x-1][y].isWalkable()) { - neighbourList.add(maze[x-1][y]) ; + neighbourList.add(maze[x-1][y]); } - + if (x < dimensionX-1 && maze[x+1][y].isWalkable()) { - neighbourList.add(maze[x+1][y]) ; + neighbourList.add(maze[x+1][y]); } - + if (y > 0 && maze[x][y-1].isWalkable()) { - neighbourList.add(maze[x][y-1]) ; + neighbourList.add(maze[x][y-1]); } - + if (y < dimensionY-1 && maze[x][y+1].isWalkable()) { - neighbourList.add(maze[x][y+1]) ; + neighbourList.add(maze[x][y+1]); } - - return neighbourList ; - + + return neighbourList; } - - /**Constructs the maze from a text file, according to each single character on each line. - * @param filename + + /** 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 ; - + + FileReader fr = null; + BufferedReader br = null; + try { - fr = new FileReader(filename) ; - br = new BufferedReader(fr) ; + fr = new FileReader(filename); + br = new BufferedReader(fr); while (br.readLine() != null) dimensionY++; } catch (Exception e) { e.printStackTrace(); } finally { - try { br.close() ;} catch (Exception e) {} + try { br.close(); } catch (Exception e) {} } - + try { //generates the maze - fr = new FileReader(filename) ; - br = new BufferedReader(fr) ; - - String currentLine = null ; - - currentLine = br.readLine() ; - dimensionX = currentLine.length() ; //maze's width - int currentLineLength ; - int compteurLigne = 0 ; - - this.maze = new Box[dimensionX][dimensionY] ; - + fr = new FileReader(filename); + br = new BufferedReader(fr); + + String currentLine = null; + + currentLine = br.readLine(); + dimensionX = currentLine.length(); //maze's width + int currentLineLength; + int compteurLigne = 0; + + this.maze = new Box[dimensionX][dimensionY]; + while (currentLine != null) { - currentLineLength = currentLine.length() ; - - for (int compteurColonne = 0 ; compteurColonne < currentLineLength ; compteurColonne++) { - - char currentChar = currentLine.charAt(compteurColonne) ; - + currentLineLength = currentLine.length(); + + for (int compteurColonne = 0; + compteurColonne < currentLineLength; + compteurColonne++) { + + char currentChar = currentLine.charAt(compteurColonne); + switch(currentChar) { - - case 'W' : maze[compteurColonne][compteurLigne] = new WBox("W", compteurColonne, compteurLigne, this) ; + + case 'W': + maze[compteurColonne][compteurLigne] = + new WBox("W", compteurColonne, compteurLigne, this); break; - - case 'E' : maze[compteurColonne][compteurLigne] = new EBox("E", compteurColonne, compteurLigne, this) ; + + case 'E': + maze[compteurColonne][compteurLigne] = + new EBox("E", compteurColonne, compteurLigne, this); break; - - case 'D' : maze[compteurColonne][compteurLigne] = new DBox("D", compteurColonne, compteurLigne, this) ; + + case 'D': + maze[compteurColonne][compteurLigne] = + new DBox("D", compteurColonne, compteurLigne, this); break; - - case 'A' : maze[compteurColonne][compteurLigne] = new ABox("A", compteurColonne, compteurLigne, this) ; + + case 'A': + maze[compteurColonne][compteurLigne] = + new ABox("A", compteurColonne, compteurLigne, this); break; - - default : throw new MazeReadingException(filename) ; - + + default: + throw new MazeReadingException(filename); + } } - + if (currentLineLength != dimensionX) { - throw new MazeReadingException(filename) ; + throw new MazeReadingException(filename); } - - currentLine = br.readLine() ; - compteurLigne++ ; + + currentLine = br.readLine(); + compteurLigne++; } - } catch (MazeReadingException mre) { + } catch (MazeReadingException mre) { mre.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { - try { br.close() ;} catch (Exception e) {} + try { br.close(); } catch (Exception e) {} } - + } - + public final void saveToTextFile(String fileName) { - - FileOutputStream fos = null ; - PrintWriter pw = null ; - + + FileOutputStream fos = null; + PrintWriter pw = null; + try { - - fos = new FileOutputStream(fileName) ; - pw = new PrintWriter(fos) ; - - for (int i = 0; i < dimensionX ; i++) { - for (int j = 0 ; j < dimensionY ; j++) { - - pw.print(maze[i][j].getLabel()); - + + fos = new FileOutputStream(fileName); + pw = new PrintWriter(fos); + + for (int i = 0; i < dimensionY; i++) { + for (int j = 0; j < dimensionX; j++) { + pw.print(maze[j][i].getLabel()); } pw.print("\n"); } - + } catch(Exception e) { e.printStackTrace(); } finally { - try { pw.close();} catch(Exception e) {} + try { pw.close(); } catch(Exception e) {} } } - + public int getDimensionX() { return dimensionX; } @@ -205,5 +212,4 @@ public class Maze public Box[][] getMaze() { return maze; } - } diff --git a/src/maze/MazeReadingException.java b/src/maze/MazeReadingException.java index 37b0cd3..47b5600 100644 --- a/src/maze/MazeReadingException.java +++ b/src/maze/MazeReadingException.java @@ -2,20 +2,18 @@ package maze; /**

MazeReadingException exception

*

- * This class is used to throw exception when reading and initiating the maze from a text file. - * + * This class is used to throw exception when reading and + * initiating the maze from a text file. + * * */ -public class MazeReadingException +public class MazeReadingException extends Exception { - + private static final long serialVersionUID = 1L; - + public MazeReadingException (String filename) { - - super("Invalid maze format : " + filename) ; - + super("Invalid maze format : " + filename); } - } diff --git a/src/maze/WBox.java b/src/maze/WBox.java index 29a1107..8174c5d 100644 --- a/src/maze/WBox.java +++ b/src/maze/WBox.java @@ -1,23 +1,18 @@ package maze; /** - * A WBox is a Box that can't be walked through (a wall). + * A WBox is a Box + * that can't be walked through (a wall). */ public class WBox extends Box { - - - + public WBox (String label, int x, int y, Maze maze) { - - super(label,x,y,maze) ; - - + super(label, x, y, maze); } - + @Override public boolean isWalkable() { - return false ; - } - + return false; + } }