Merge branch 'normalizeSpaces' of antograb/TpMazeJava into master

This commit was merged in pull request #12.
This commit is contained in:
antograb authored and Gogs committed 2017-12-16 13:26:26 +01:00
commit 81c93638da
18 files changed
+103 -118

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
+4 -2
View File
@@ -9,9 +9,11 @@ public class MainTest {
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);
+1 -9
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;
} }
} }
+6 -5
View File
@@ -3,8 +3,10 @@ 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 {
@@ -19,7 +21,8 @@ public interface ASetInterface {
* 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);
@@ -28,6 +31,4 @@ public interface ASetInterface {
* @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();
} }
+15 -14
View File
@@ -5,23 +5,25 @@ 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,
ASetInterface a,
PiInterface pi,
PreviousInterface previous) {
a.add(r) ;
VertexInterface pivot = r; VertexInterface pivot = r;
pi.setPi(0, r) ;
ArrayList<VertexInterface> vertexList = g.getVertexes(); 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 < g.getVertexNumber(); j++) {
for (int j = 0; j < n; 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)) {
@@ -42,24 +44,23 @@ public class Dijkstra {
} }
return previous; return previous;
} }
public static PreviousInterface dijkstra(GraphInterface g, VertexInterface r) { public static PreviousInterface dijkstra(GraphInterface g,
VertexInterface r) {
HashSet<VertexInterface> vertexHashSet = new HashSet<VertexInterface>(); HashSet<VertexInterface> vertexHashSet = new HashSet<VertexInterface>();
ASet aSet = new ASet(vertexHashSet); ASet aSet = new ASet(vertexHashSet);
Hashtable<VertexInterface, Integer> hashPi = new Hashtable<VertexInterface, Integer>() ; Hashtable<VertexInterface, Integer> hashPi =
new Hashtable<VertexInterface, Integer>();
Pi pi = new Pi(hashPi); Pi pi = new Pi(hashPi);
Hashtable<VertexInterface, VertexInterface> hashPrevious = new Hashtable<VertexInterface, VertexInterface>() ; Hashtable<VertexInterface, VertexInterface> hashPrevious =
new Hashtable<VertexInterface, VertexInterface>();
ArrayList<VertexInterface> shortestPath = new ArrayList<VertexInterface>(); ArrayList<VertexInterface> shortestPath = new ArrayList<VertexInterface>();
Previous previous = new Previous(hashPrevious, shortestPath); Previous previous = new Previous(hashPrevious, shortestPath);
return dijkstra(g, r, aSet, pi, previous); return dijkstra(g, r, aSet, pi, previous);
} }
} }
+11 -6
View File
@@ -2,14 +2,18 @@ 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();
@@ -18,8 +22,10 @@ public interface GraphInterface {
* @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);
@@ -36,5 +42,4 @@ public interface GraphInterface {
* @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);
} }
+1 -8
View File
@@ -4,24 +4,17 @@ 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);
} }
} }
+8 -5
View File
@@ -2,15 +2,18 @@ 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);
@@ -20,8 +23,8 @@ public interface PiInterface {
* @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);
} }
+7 -7
View File
@@ -5,10 +5,11 @@ import java.util.Hashtable;
public class Previous implements PreviousInterface { public class Previous implements PreviousInterface {
private Hashtable<VertexInterface, VertexInterface> hashPrevious = new Hashtable<VertexInterface, VertexInterface>() ; private Hashtable<VertexInterface, VertexInterface> hashPrevious;
private ArrayList<VertexInterface> shortestPath = new ArrayList<VertexInterface>() ; private ArrayList<VertexInterface> shortestPath;
public Previous(Hashtable<VertexInterface, VertexInterface> hashPrevious, 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);
@@ -17,13 +18,11 @@ public class Previous implements PreviousInterface {
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) {
@@ -31,10 +30,11 @@ public class Previous implements PreviousInterface {
VertexInterface father = getPrevious(vertex); VertexInterface father = getPrevious(vertex);
shortestPath.add(father); shortestPath.add(father);
if (getPrevious(father) == null) return shortestPath ; if (getPrevious(father) == null) {
return shortestPath;
}
else { else {
return getShortestPathTo(father); return getShortestPathTo(father);
} }
} }
} }
+11 -6
View File
@@ -3,16 +3,21 @@ 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);
@@ -30,8 +35,8 @@ public interface PreviousInterface {
* 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);
} }
-1
View File
@@ -10,5 +10,4 @@ public interface VertexInterface {
* @return The label of the element. * @return The label of the element.
*/ */
public String getLabel(); public String getLabel();
} }
+3 -4
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 @Override
public boolean isArrival() { public boolean isArrival() {
return true; return true;
} }
} }
+4 -14
View File
@@ -8,7 +8,8 @@ import dijkstra.VertexInterface;
* *
* 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
@@ -20,22 +21,15 @@ public abstract class Box
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() {
@@ -50,10 +44,6 @@ public abstract class Box
return false; return false;
} }
public ArrayList<Box> getNeighbourList() {
return neighbourList;
}
public int getX() { public int getX() {
return x; return x;
} }
+2 -4
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;
} }
} }
+2 -5
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) { public EBox (String label, int x, int y, Maze maze) {
super(label, x, y, maze); super(label, x, y, maze);
} }
} }
+23 -17
View File
@@ -24,9 +24,7 @@ public class Maze
private Box[][] maze; private Box[][] maze;
public Maze(String filename) { public Maze(String filename) {
initFromTextFile(filename); initFromTextFile(filename);
} }
// public Maze(Box[][] maze){ // public Maze(Box[][] maze){
@@ -40,7 +38,7 @@ public class Maze
Box startBox = (Box) start; Box startBox = (Box) start;
Box endBox = (Box) end; Box endBox = (Box) end;
return startBox.getNeighbourList().contains(endBox) ? 1 : 0 ; return generateNeighbours(startBox).contains(endBox) ? 1 : 0;
} }
public int getVertexNumber() { public int getVertexNumber() {
@@ -88,10 +86,10 @@ public class Maze
} }
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
* single character on each line.
* @param filename * @param filename
* The path to the file containing the representation of the maze. * The path to the file containing the representation of the maze.
*/ */
@@ -128,25 +126,36 @@ public class Maze
{ {
currentLineLength = currentLine.length(); currentLineLength = currentLine.length();
for (int compteurColonne = 0 ; compteurColonne < currentLineLength ; compteurColonne++) { for (int compteurColonne = 0;
compteurColonne < currentLineLength;
compteurColonne++) {
char currentChar = currentLine.charAt(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);
} }
} }
@@ -178,11 +187,9 @@ public class Maze
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");
} }
@@ -205,5 +212,4 @@ public class Maze
public Box[][] getMaze() { public Box[][] getMaze() {
return maze; return maze;
} }
} }
+2 -4
View File
@@ -2,7 +2,8 @@ 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.
* *
* *
*/ */
@@ -13,9 +14,6 @@ public class MazeReadingException
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);
} }
} }
+2 -7
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;
} }
} }