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)
This commit is contained in:
regar42 committed 2017-12-14 21:53:49 +01:00
1 parent c3e74a20f3
commit e8896556bc
18 files changed
+307 -322

No files matched your search

+1
View File
@@ -2,3 +2,4 @@
*.css *.css
src/package-list src/package-list
src/script.js src/script.js
*.class
+10 -8
View File
@@ -6,17 +6,19 @@ import maze.*;
public class MainTest { public class MainTest {
public static void main(String[] args) { public static void main(String[] args) {
Maze mazeTest = new Maze("data/labyrinthe.txt") ; Maze mazeTest = new Maze("data/labyrinthe.txt");
Previous previous = (Previous) Dijkstra.dijkstra(mazeTest, mazeTest.getMaze()[1][1]); Previous previous =
(Previous) Dijkstra.dijkstra(mazeTest, mazeTest.getMaze()[1][1]);
ArrayList<VertexInterface> shortest = previous.getShortestPathTo(mazeTest.getMaze()[5][5]);
ArrayList<VertexInterface> shortest =
previous.getShortestPathTo(mazeTest.getMaze()[5][5]);
for (VertexInterface vertex : shortest) { for (VertexInterface vertex : shortest) {
System.out.println(vertex); System.out.println(vertex);
} }
mazeTest.saveToTextFile("data/labyOutput.txt"); mazeTest.saveToTextFile("data/labyOutput.txt");
} }
+2 -10
View File
@@ -4,29 +4,21 @@ import java.util.HashSet;
public class ASet implements ASetInterface { public class ASet implements ASetInterface {
private HashSet<VertexInterface> vertexHashSet = new HashSet<VertexInterface>() ; private HashSet<VertexInterface> vertexHashSet;
public ASet(HashSet<VertexInterface> vertexHashSet) { public ASet(HashSet<VertexInterface> vertexHashSet) {
this.vertexHashSet = new HashSet<VertexInterface>(vertexHashSet); this.vertexHashSet = new HashSet<VertexInterface>(vertexHashSet);
} }
public void add(VertexInterface vertex) { public void add(VertexInterface vertex) {
vertexHashSet.add(vertex);
vertexHashSet.add(vertex) ;
} }
public boolean contains(VertexInterface vertex) { public boolean contains(VertexInterface vertex) {
return vertexHashSet.contains(vertex); return vertexHashSet.contains(vertex);
} }
public HashSet<VertexInterface> getVertexHashSet() { public HashSet<VertexInterface> getVertexHashSet() {
return vertexHashSet; return vertexHashSet;
} }
} }
+11 -10
View File
@@ -3,31 +3,32 @@ package dijkstra;
import java.util.HashSet; import java.util.HashSet;
/** /**
* This is the ASet class as described in the INF101 course of Telecom Paristech. * This is the ASet class as described in the
* The ASet contains vertexes for which the shortest path has already been found (vertexes already examined by the dijkstra algorithm). * 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 { public interface ASetInterface {
/** /**
* Adds a vertex to the set. * Adds a vertex to the set.
* @param vertex * @param vertex
* The element to be added to the set. * 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. * Checks if a vertex belongs to the set.
* @param vertex * @param vertex
* The element we must check if it belongs to the set. * The element we must check if it belongs to the set.
* @return <code>true</code> if the element is in the set, <code>false</code> if not. * @return <code>true</code> if the element is in the set,
* <code>false</code> if not.
*/ */
public boolean contains(VertexInterface vertex) ; public boolean contains(VertexInterface vertex);
/** /**
* Returns the HashSet which represents the ASet. * Returns the HashSet which represents the ASet.
* @return A <code>HashSet</code> containing the same elements as in the set. * @return A <code>HashSet</code> containing the same elements as in the set.
*/ */
public HashSet<VertexInterface> getVertexHashSet(); public HashSet<VertexInterface> getVertexHashSet();
} }
+41 -40
View File
@@ -4,62 +4,63 @@ import java.util.HashSet;
import java.util.Hashtable; import java.util.Hashtable;
public class Dijkstra { public class Dijkstra {
private static PreviousInterface dijkstra(GraphInterface g, VertexInterface r, ASetInterface a, PiInterface pi, PreviousInterface previous) { private static PreviousInterface dijkstra(GraphInterface g,
VertexInterface r,
a.add(r) ; ASetInterface a,
VertexInterface pivot = r ; PiInterface pi,
pi.setPi(0, r) ; PreviousInterface previous) {
ArrayList<VertexInterface> vertexList = g.getVertexes() ; VertexInterface pivot = r;
ArrayList<VertexInterface> vertexList = g.getVertexes();
a.add(r);
pi.setPi(0, r);
for (VertexInterface vertex: vertexList) { for (VertexInterface vertex: vertexList) {
if (vertex != r) { if (vertex != r) {
pi.setPi(Integer.MAX_VALUE, vertex); 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)) { 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)) { if (pi.getPi(pivot) + g.getCost(pivot, vertex) < pi.getPi(vertex)) {
pi.setPi(pi.getPi(pivot) + g.getCost(pivot, vertex), vertex) ; pi.setPi(pi.getPi(pivot) + g.getCost(pivot, vertex), vertex);
previous.setPrevious(pivot, vertex) ; previous.setPrevious(pivot, vertex);
} }
} }
} }
int piMax = Integer.MAX_VALUE ; int piMax = Integer.MAX_VALUE;
for (VertexInterface vertex: vertexList) { for (VertexInterface vertex: vertexList) {
if (! a.contains(vertex) && pi.getPi(vertex) < piMax) { if (! a.contains(vertex) && pi.getPi(vertex) < piMax) {
pivot = vertex ; pivot = vertex;
piMax = pi.getPi(vertex) ; piMax = pi.getPi(vertex);
} }
} }
a.add(pivot) ; a.add(pivot);
} }
return previous ;
}
public static PreviousInterface dijkstra(GraphInterface g, VertexInterface r) {
HashSet<VertexInterface> vertexHashSet = new HashSet<VertexInterface>() ;
ASet aSet = new ASet(vertexHashSet) ;
Hashtable<VertexInterface, Integer> hashPi = new Hashtable<VertexInterface, Integer>() ;
Pi pi = new Pi(hashPi) ;
Hashtable<VertexInterface, VertexInterface> hashPrevious = new Hashtable<VertexInterface, VertexInterface>() ; return previous;
ArrayList<VertexInterface> shortestPath = new ArrayList<VertexInterface>() ; }
Previous previous = new Previous(hashPrevious, shortestPath) ;
public static PreviousInterface dijkstra(GraphInterface g,
return dijkstra(g, r, aSet, pi, previous) ; VertexInterface r) {
HashSet<VertexInterface> vertexHashSet = new HashSet<VertexInterface>();
ASet aSet = new ASet(vertexHashSet);
Hashtable<VertexInterface, Integer> hashPi =
new Hashtable<VertexInterface, Integer>();
Pi pi = new Pi(hashPi);
Hashtable<VertexInterface, VertexInterface> hashPrevious =
new Hashtable<VertexInterface, VertexInterface>();
ArrayList<VertexInterface> shortestPath = new ArrayList<VertexInterface>();
Previous previous = new Previous(hashPrevious, shortestPath);
return dijkstra(g, r, aSet, pi, previous);
} }
} }
+19 -14
View File
@@ -2,39 +2,44 @@ package dijkstra;
import java.util.ArrayList; import java.util.ArrayList;
/** /**
* This is the Graph class as described in the INF101 course of Telecom Paristech. * This is the Graph class as described in
* Graph contains the representation of all the vertexes, with the information on which vertex(es) a vertex is linked to and at what cost. * 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 { public interface GraphInterface {
/** /**
* Gets the number of <code>VertexInterface</code> in the graph. * Gets the number of <code>VertexInterface</code> in the graph.
* @return An integer giving the number of <code>VertexInterface</code> in the graph. * @return An integer giving the number of
* <code>VertexInterface</code> in the graph.
*/ */
public int getVertexNumber() ; public int getVertexNumber();
/** /**
* Gets the cost of the link (if it exists) between two vertexes. * Gets the cost of the link (if it exists) between two vertexes.
* @param start * @param start
* The departure vertex. * The departure vertex.
* @param end * @param end
* The vertex from which we must see the cost of the link to <code>start</code>. * The vertex from which we must see the cost
* @return An integer giving the cost of the link between the given <code>VertexInterface</code>. * of the link to <code>start</code>.
* @return An integer giving the cost of the link between
* the given <code>VertexInterface</code>.
*/ */
public int getCost(VertexInterface start, VertexInterface end) ; public int getCost(VertexInterface start, VertexInterface end);
/** /**
* Returns all <code>VertexInterface</code> contained in the graph. * Returns all <code>VertexInterface</code> contained in the graph.
* @return An <code>ArrayList</code> of <code>VertexInterface</code>. * @return An <code>ArrayList</code> of <code>VertexInterface</code>.
*/ */
public ArrayList<VertexInterface> getVertexes() ; public ArrayList<VertexInterface> getVertexes();
/** /**
* Returns the neighbour list for a given <code>VertexInterface</code>. * Returns the neighbour list for a given <code>VertexInterface</code>.
* @param vertex * @param vertex
* The <code>VertexInterface</code> from which we will seek for neighbours. * The <code>VertexInterface</code> from which we will seek for neighbours.
* @return An <code>ArrayList</code> of <code>VertexInterface</code>. * @return An <code>ArrayList</code> of <code>VertexInterface</code>.
*/ */
public ArrayList<VertexInterface> generateNeighbours(VertexInterface vertex) ; public ArrayList<VertexInterface> generateNeighbours(VertexInterface vertex);
} }
+6 -13
View File
@@ -3,25 +3,18 @@ package dijkstra;
import java.util.Hashtable; import java.util.Hashtable;
public class Pi implements PiInterface { public class Pi implements PiInterface {
private Hashtable<VertexInterface, Integer> hashPi = new Hashtable<VertexInterface, Integer>() ; private Hashtable<VertexInterface, Integer> hashPi;
public Pi(Hashtable<VertexInterface, Integer> hashPi) { public Pi(Hashtable<VertexInterface, Integer> hashPi) {
this.hashPi = new Hashtable<VertexInterface, Integer>(hashPi); this.hashPi = new Hashtable<VertexInterface, Integer>(hashPi);
} }
public int getPi(VertexInterface vertex) { public int getPi(VertexInterface vertex) {
return hashPi.get(vertex);
return hashPi.get(vertex) ;
} }
public void setPi(int i, VertexInterface vertex) { public void setPi(int i, VertexInterface vertex) {
hashPi.put(vertex, i);
hashPi.put(vertex, i) ;
} }
} }
+12 -9
View File
@@ -2,26 +2,29 @@ package dijkstra;
/** /**
* This is the Pi class as described in the INF101 course of Telecom Paristech. * 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 { public interface PiInterface {
/** /**
* Gets the cost to reach the given <code>VertexInterface</code> * Gets the cost to reach the given <code>VertexInterface</code>
* @param vertex * @param vertex
* The <code>VertexInterface</code> from which we get the Pi value (cost) to reach it. * The <code>VertexInterface</code> from which we get
* @return An <code>integer</code> giving the Pi value (cost) the reach the <code>VertexInterface</code>. * the Pi value (cost) to reach it.
* @return An <code>integer</code> giving the Pi value (cost)
* to reach the <code>VertexInterface</code>.
* *
*/ */
public int getPi(VertexInterface vertex) ; public int getPi(VertexInterface vertex);
/** /**
* Sets the Pi value of an element * Sets the Pi value of an element
* @param i * @param i
* The Pi value (cost) to set. * The Pi value (cost) to set.
* @param vertex * @param vertex
* The <code>VertexInterface</code> on which the Pi value (cost) is to be set. * The <code>VertexInterface</code> on
* which the Pi value (cost) is to be set.
*/ */
public void setPi(int i, VertexInterface vertex) ; public void setPi(int i, VertexInterface vertex);
} }
+18 -18
View File
@@ -4,37 +4,37 @@ import java.util.ArrayList;
import java.util.Hashtable; import java.util.Hashtable;
public class Previous implements PreviousInterface { public class Previous implements PreviousInterface {
private Hashtable<VertexInterface, VertexInterface> hashPrevious = new Hashtable<VertexInterface, VertexInterface>() ;
private ArrayList<VertexInterface> shortestPath = new ArrayList<VertexInterface>() ;
public Previous(Hashtable<VertexInterface, VertexInterface> hashPrevious, ArrayList<VertexInterface> shortestPath) { private Hashtable<VertexInterface, VertexInterface> hashPrevious;
private ArrayList<VertexInterface> shortestPath;
public Previous(Hashtable<VertexInterface, VertexInterface> hashPrevious,
ArrayList<VertexInterface> shortestPath) {
this.hashPrevious = new Hashtable<VertexInterface, VertexInterface>(hashPrevious); this.hashPrevious = new Hashtable<VertexInterface, VertexInterface>(hashPrevious);
this.shortestPath = new ArrayList<VertexInterface>(shortestPath); this.shortestPath = new ArrayList<VertexInterface>(shortestPath);
} }
public VertexInterface getPrevious(VertexInterface vertex) { public VertexInterface getPrevious(VertexInterface vertex) {
return hashPrevious.get(vertex) ; return hashPrevious.get(vertex);
} }
public void setPrevious(VertexInterface father, VertexInterface son) { public void setPrevious(VertexInterface father, VertexInterface son) {
hashPrevious.put(son, father) ;
hashPrevious.put(son, father);
} }
public ArrayList<VertexInterface> getShortestPathTo(VertexInterface vertex) { public ArrayList<VertexInterface> 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);
}
}
} }
+17 -12
View File
@@ -3,20 +3,25 @@ package dijkstra;
import java.util.ArrayList; import java.util.ArrayList;
/** /**
* The <code>PreviousInterface</code> contains information about the father (as in the graph's theory) of some <code>VertexInterface</code>. * The <code>PreviousInterface</code> contains information
* The father of a <code>VertexInterface</code> is the <code>VertexInterface</code> used to reach it. * about the father (as in the graph's theory)
* Typically in Dijkstra, it's the <code>VertexInterface</code> used to reach <code>vertex</code> with the lowest cost. * of some <code>VertexInterface</code>.
* The father of a <code>VertexInterface</code> is
* the <code>VertexInterface</code> used to reach it.
* Typically in Dijkstra, it's the <code>VertexInterface</code> used
* to reach <code>vertex</code> with the lowest cost.
*/ */
public interface PreviousInterface { public interface PreviousInterface {
/** /**
* Returns the father of a <code>VertexInterface</code>. * Returns the father of a <code>VertexInterface</code>.
* @param vertex * @param vertex
* The <code>VertexInterface</code> from which we get the father (previous <code>VertexInterface</code>). * The <code>VertexInterface</code> from which we get
* the father (previous <code>VertexInterface</code>).
* @return The direct previous element used to reach <code>vertex</code>. * @return The direct previous element used to reach <code>vertex</code>.
*/ */
public VertexInterface getPrevious(VertexInterface 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 * @param father
@@ -24,14 +29,14 @@ public interface PreviousInterface {
* @param son * @param son
* The element that will be assigned a new father. * 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. * Shortest path from root to a vertex.
* @param vertex * @param vertex
* The vertex to which the shortest path will be computed. * The vertex to which the shortest path will be computed.
* @return An <code>ArrayList</code> of the <code>VertexInterface</code> used to reach vertex with the lowest cost. * @return An <code>ArrayList</code> of the <code>VertexInterface</code>
* used to reach vertex with the lowest cost.
*/ */
public ArrayList<VertexInterface> getShortestPathTo(VertexInterface vertex) ; public ArrayList<VertexInterface> getShortestPathTo(VertexInterface vertex);
} }
+2 -3
View File
@@ -5,10 +5,9 @@ package dijkstra;
* *
*/ */
public interface VertexInterface { public interface VertexInterface {
/** /**
* @return The label of the element. * @return The label of the element.
*/ */
public String getLabel() ; public String getLabel();
} }
+9 -10
View File
@@ -1,19 +1,18 @@
package maze; package maze;
/** /**
* <code>ABox</code> is an arrival <code>Box</code>. It's the last <code>Box</code> to be walked through and it's an exit to the <code>Maze</code>. * <code>ABox</code> is an arrival <code>Box</code>.
* It's the last <code>Box</code> to be walked through and
* it's an exit to the <code>Maze</code>.
*/ */
public class ABox extends EBox{ public class ABox extends EBox{
public ABox (String label, int x, int y, Maze maze) { public ABox (String label, int x, int y, Maze maze) {
super(label, x, y, maze);
super(label,x,y,maze) ;
}
@Override
public boolean isArrival() {
return true ;
} }
@Override
public boolean isArrival() {
return true;
}
} }
+22 -32
View File
@@ -5,11 +5,12 @@ import dijkstra.VertexInterface;
/** <h1> Box class </h1> /** <h1> Box class </h1>
* <p> * <p>
* *
* This class describes a Box and implements {@link dijkstra.VertexInterface}. * This class describes a Box and implements {@link dijkstra.VertexInterface}.
* <p> * <p>
* 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 Antonin Godard
* @author Sylvain Rager * @author Sylvain Rager
* *
@@ -18,40 +19,29 @@ import dijkstra.VertexInterface;
public abstract class Box public abstract class Box
implements VertexInterface { implements VertexInterface {
private int x ; private int x;
private int y ; private int y;
private String label = null ; private String label;
private Maze maze ; private Maze maze;
private ArrayList<Box> neighbourList = new ArrayList<Box>() ;
public Box (String label, int x, int y, Maze maze) { public Box (String label, int x, int y, Maze maze) {
this.label = label ; this.label = new String(label);
this.x = x ; this.x = x;
this.y = y ; this.y = y;
this.maze = maze ; this.maze = maze;
} }
public ArrayList<VertexInterface> generateNeighbors() {
return maze.generateNeighbours(this) ;
}
public boolean isWalkable() { public boolean isWalkable() {
return true ; return true;
} }
public boolean isArrival() { public boolean isArrival() {
return false ; return false;
} }
public boolean isDeparture() { public boolean isDeparture() {
return false ; return false;
}
public ArrayList<Box> getNeighbourList() {
return neighbourList;
} }
public int getX() { public int getX() {
@@ -65,9 +55,9 @@ public abstract class Box
public String getLabel() { public String getLabel() {
return label; return label;
} }
public String toString() { public String toString() {
return "Label: " + label + ", x: " + x + ", y: " + y; return "Label: " + label + ", x: " + x + ", y: " + y;
} }
} }
+5 -7
View File
@@ -1,20 +1,18 @@
package maze; package maze;
/** /**
* A <code>DBox</code> is the <code>Box</code> on which the user is first in the <code>Maze</code>. * A <code>DBox</code> is the <code>Box</code> on which the user
* is first in the <code>Maze</code>.
*/ */
public class DBox extends EBox{ public class DBox extends EBox{
public DBox (String label, int x, int y, Maze maze) { public DBox (String label, int x, int y, Maze maze) {
super(label, x, y, maze);
super(label,x,y,maze) ;
} }
@Override @Override
public boolean isDeparture() { public boolean isDeparture() {
return true ; return true;
} }
} }
+5 -8
View File
@@ -1,16 +1,13 @@
package maze; package maze;
/** /**
* An <code>EBox</code> is a <code>Box</code> that can be walked through (an empty box). * An <code>EBox</code> is a <code>Box</code>
* that can be walked through (an empty box).
*/ */
public class EBox extends Box{ public class EBox extends Box{
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);
}
} }
+113 -107
View File
@@ -11,189 +11,196 @@ import dijkstra.VertexInterface;
/** <h1> Maze class </h1> /** <h1> Maze class </h1>
* <p> * <p>
* 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 Antonin Godard
* @author Sylvain Rager * @author Sylvain Rager
* *
*/ */
public class Maze public class Maze
implements GraphInterface { implements GraphInterface {
private int dimensionX;
private int dimensionY;
private Box[][] maze;
private int dimensionX ;
private int dimensionY ;
private Box[][] maze ;
public Maze(String filename) { public Maze(String filename) {
initFromTextFile(filename);
initFromTextFile(filename) ;
} }
// public Maze(Box[][] maze){ // public Maze(Box[][] maze){
// //Replacement for setMaze() // //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 // //and then set dimensionX and dimensionX
// } // }
public int getCost(VertexInterface start, VertexInterface end) { public int getCost(VertexInterface start, VertexInterface end) {
Box startBox = (Box) start ;
Box 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() { public int getVertexNumber() {
return dimensionX*dimensionY ; return dimensionX * dimensionY;
} }
public ArrayList<VertexInterface> getVertexes() { public ArrayList<VertexInterface> getVertexes() {
ArrayList<VertexInterface> vertexList = new ArrayList<VertexInterface>() ; ArrayList<VertexInterface> vertexList = new ArrayList<VertexInterface>();
for (int i = 0 ; i < dimensionX ; i++) { for (int i = 0; i < dimensionX; i++) {
for (int j = 0 ; j < dimensionY ; j++) { for (int j = 0; j < dimensionY; j++) {
if (maze[i][j].isWalkable()) { if (maze[i][j].isWalkable()) {
vertexList.add(maze[i][j]) ; vertexList.add(maze[i][j]);
} }
} }
} }
return vertexList ; return vertexList;
} }
public ArrayList<VertexInterface> generateNeighbours(VertexInterface vertex) { public ArrayList<VertexInterface> generateNeighbours(VertexInterface vertex) {
Box box = (Box)vertex ; Box box = (Box) vertex;
int x = box.getX() ; int x = box.getX();
int y = box.getY() ; int y = box.getY();
ArrayList<VertexInterface> neighbourList = new ArrayList<VertexInterface>() ; ArrayList<VertexInterface> neighbourList = new ArrayList<VertexInterface>();
// Always returns an empty list if the departure box is a wall // Always returns an empty list if the departure box is a wall
if (! box.isWalkable()) { if (! box.isWalkable()) {
return neighbourList ; return neighbourList;
} }
if (x > 0 && maze[x-1][y].isWalkable()) { 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()) { 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()) { 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()) { 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. /** Constructs the maze from a text file, according to each
* @param filename * single character on each line.
* @param filename
* The path to the file containing the representation of the maze. * The path to the file containing the representation of the maze.
*/ */
private final void initFromTextFile(String filename) { private final void initFromTextFile(String filename) {
FileReader fr = null ; FileReader fr = null;
BufferedReader br = null ; BufferedReader br = null;
try { try {
fr = new FileReader(filename) ; fr = new FileReader(filename);
br = new BufferedReader(fr) ; br = new BufferedReader(fr);
while (br.readLine() != null) dimensionY++; while (br.readLine() != null) dimensionY++;
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} finally { } finally {
try { br.close() ;} catch (Exception e) {} try { br.close(); } catch (Exception e) {}
} }
try { //generates the maze try { //generates the maze
fr = new FileReader(filename) ; fr = new FileReader(filename);
br = new BufferedReader(fr) ; br = new BufferedReader(fr);
String currentLine = null ; String currentLine = null;
currentLine = br.readLine() ; currentLine = br.readLine();
dimensionX = currentLine.length() ; //maze's width dimensionX = currentLine.length(); //maze's width
int currentLineLength ; int currentLineLength;
int compteurLigne = 0 ; int compteurLigne = 0;
this.maze = new Box[dimensionX][dimensionY] ; this.maze = new Box[dimensionX][dimensionY];
while (currentLine != null) while (currentLine != null)
{ {
currentLineLength = currentLine.length() ; currentLineLength = currentLine.length();
for (int compteurColonne = 0 ; compteurColonne < currentLineLength ; compteurColonne++) { for (int compteurColonne = 0;
compteurColonne < currentLineLength;
char currentChar = currentLine.charAt(compteurColonne) ; compteurColonne++) {
char currentChar = currentLine.charAt(compteurColonne);
switch(currentChar) { 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; break;
case 'E' : maze[compteurColonne][compteurLigne] = new EBox("E", compteurColonne, compteurLigne, this) ; case 'E':
maze[compteurColonne][compteurLigne] =
new EBox("E", compteurColonne, compteurLigne, this);
break; break;
case 'D' : maze[compteurColonne][compteurLigne] = new DBox("D", compteurColonne, compteurLigne, this) ; case 'D':
maze[compteurColonne][compteurLigne] =
new DBox("D", compteurColonne, compteurLigne, this);
break; break;
case 'A' : maze[compteurColonne][compteurLigne] = new ABox("A", compteurColonne, compteurLigne, this) ; case 'A':
maze[compteurColonne][compteurLigne] =
new ABox("A", compteurColonne, compteurLigne, this);
break; break;
default : throw new MazeReadingException(filename) ; default:
throw new MazeReadingException(filename);
} }
} }
if (currentLineLength != dimensionX) { if (currentLineLength != dimensionX) {
throw new MazeReadingException(filename) ; throw new MazeReadingException(filename);
} }
currentLine = br.readLine() ; currentLine = br.readLine();
compteurLigne++ ; compteurLigne++;
} }
} catch (MazeReadingException mre) { } catch (MazeReadingException mre) {
mre.printStackTrace(); mre.printStackTrace();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} finally { } finally {
try { br.close() ;} catch (Exception e) {} try { br.close(); } catch (Exception e) {}
} }
} }
public final void saveToTextFile(String fileName) { public final void saveToTextFile(String fileName) {
FileOutputStream fos = null ; FileOutputStream fos = null;
PrintWriter pw = null ; PrintWriter pw = null;
try { try {
fos = new FileOutputStream(fileName) ; fos = new FileOutputStream(fileName);
pw = new PrintWriter(fos) ; pw = new PrintWriter(fos);
for (int i = 0; i < dimensionX ; i++) { for (int i = 0; i < dimensionY; i++) {
for (int j = 0 ; j < dimensionY ; j++) { for (int j = 0; j < dimensionX; j++) {
pw.print(maze[j][i].getLabel());
pw.print(maze[i][j].getLabel());
} }
pw.print("\n"); pw.print("\n");
} }
} catch(Exception e) { } catch(Exception e) {
e.printStackTrace(); e.printStackTrace();
} finally { } finally {
try { pw.close();} catch(Exception e) {} try { pw.close(); } catch(Exception e) {}
} }
} }
public int getDimensionX() { public int getDimensionX() {
return dimensionX; return dimensionX;
} }
@@ -205,5 +212,4 @@ public class Maze
public Box[][] getMaze() { public Box[][] getMaze() {
return maze; return maze;
} }
} }
+7 -9
View File
@@ -2,20 +2,18 @@ package maze;
/**<h1> MazeReadingException exception </h1> /**<h1> MazeReadingException exception </h1>
* <p> * <p>
* 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 { extends Exception {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
public MazeReadingException (String filename) { public MazeReadingException (String filename) {
super("Invalid maze format : " + filename);
super("Invalid maze format : " + filename) ;
} }
} }
+7 -12
View File
@@ -1,23 +1,18 @@
package maze; package maze;
/** /**
* A <code>WBox</code> is a <code>Box</code> that can't be walked through (a wall). * A <code>WBox</code> is a <code>Box</code>
* that can't be walked through (a wall).
*/ */
public class WBox extends Box { public class WBox extends Box {
public WBox (String label, int x, int y, Maze maze) { public WBox (String label, int x, int y, Maze maze) {
super(label, x, y, maze);
super(label,x,y,maze) ;
} }
@Override @Override
public boolean isWalkable() { public boolean isWalkable() {
return false ; return false;
} }
} }