Merge branch 'normalizeSpaces' of antograb/TpMazeJava into master
This commit was merged in pull request #12.
This commit is contained in:
commit
81c93638da
18 files changed
+307
-322
No files matched your search
@@ -2,3 +2,4 @@
|
||||
*.css
|
||||
src/package-list
|
||||
src/script.js
|
||||
*.class
|
||||
+10
-8
@@ -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<VertexInterface> 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<VertexInterface> shortest =
|
||||
previous.getShortestPathTo(mazeTest.getMaze()[5][5]);
|
||||
|
||||
for (VertexInterface vertex : shortest) {
|
||||
System.out.println(vertex);
|
||||
}
|
||||
|
||||
|
||||
mazeTest.saveToTextFile("data/labyOutput.txt");
|
||||
|
||||
}
|
||||
|
||||
+2
-10
@@ -4,29 +4,21 @@ import java.util.HashSet;
|
||||
|
||||
public class ASet implements ASetInterface {
|
||||
|
||||
private HashSet<VertexInterface> vertexHashSet = new HashSet<VertexInterface>() ;
|
||||
private HashSet<VertexInterface> vertexHashSet;
|
||||
|
||||
|
||||
public ASet(HashSet<VertexInterface> vertexHashSet) {
|
||||
|
||||
this.vertexHashSet = new HashSet<VertexInterface>(vertexHashSet);
|
||||
|
||||
}
|
||||
|
||||
public void add(VertexInterface vertex) {
|
||||
|
||||
vertexHashSet.add(vertex) ;
|
||||
|
||||
vertexHashSet.add(vertex);
|
||||
}
|
||||
|
||||
public boolean contains(VertexInterface vertex) {
|
||||
|
||||
return vertexHashSet.contains(vertex);
|
||||
|
||||
}
|
||||
|
||||
public HashSet<VertexInterface> getVertexHashSet() {
|
||||
return vertexHashSet;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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 <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.
|
||||
* @return A <code>HashSet</code> containing the same elements as in the set.
|
||||
*/
|
||||
public HashSet<VertexInterface> getVertexHashSet();
|
||||
|
||||
|
||||
}
|
||||
+41
-40
@@ -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<VertexInterface> vertexList = g.getVertexes() ;
|
||||
|
||||
|
||||
private static PreviousInterface dijkstra(GraphInterface g,
|
||||
VertexInterface r,
|
||||
ASetInterface a,
|
||||
PiInterface pi,
|
||||
PreviousInterface previous) {
|
||||
|
||||
VertexInterface pivot = r;
|
||||
ArrayList<VertexInterface> 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<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) ;
|
||||
|
||||
|
||||
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>();
|
||||
ArrayList<VertexInterface> shortestPath = new ArrayList<VertexInterface>();
|
||||
Previous previous = new Previous(hashPrevious, shortestPath);
|
||||
|
||||
return dijkstra(g, r, aSet, pi, previous);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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 <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.
|
||||
* @param start
|
||||
* The departure vertex.
|
||||
* @param end
|
||||
* The vertex from which we must see the cost of the link to <code>start</code>.
|
||||
* @return An integer giving the cost of the link between the given <code>VertexInterface</code>.
|
||||
* The vertex from which we must see the cost
|
||||
* 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.
|
||||
* @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>.
|
||||
* @param vertex
|
||||
* The <code>VertexInterface</code> from which we will seek for neighbours.
|
||||
* @return An <code>ArrayList</code> of <code>VertexInterface</code>.
|
||||
*/
|
||||
public ArrayList<VertexInterface> generateNeighbours(VertexInterface vertex) ;
|
||||
|
||||
public ArrayList<VertexInterface> generateNeighbours(VertexInterface vertex);
|
||||
}
|
||||
+6
-13
@@ -3,25 +3,18 @@ package dijkstra;
|
||||
import java.util.Hashtable;
|
||||
|
||||
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) {
|
||||
|
||||
this.hashPi = new Hashtable<VertexInterface, Integer>(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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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 <code>VertexInterface</code>
|
||||
* @param vertex
|
||||
* The <code>VertexInterface</code> from which we get the Pi value (cost) to reach it.
|
||||
* @return An <code>integer</code> giving the Pi value (cost) the reach the <code>VertexInterface</code>.
|
||||
* The <code>VertexInterface</code> from which we get
|
||||
* 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
|
||||
* @param i
|
||||
* The Pi value (cost) to set.
|
||||
* @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
@@ -4,37 +4,37 @@ import java.util.ArrayList;
|
||||
import java.util.Hashtable;
|
||||
|
||||
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.shortestPath = new ArrayList<VertexInterface>(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<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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,20 +3,25 @@ package dijkstra;
|
||||
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 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.
|
||||
* The <code>PreviousInterface</code> contains information
|
||||
* about the father (as in the graph's theory)
|
||||
* 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 {
|
||||
|
||||
|
||||
/**
|
||||
* Returns the father of a <code>VertexInterface</code>.
|
||||
* @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>.
|
||||
*/
|
||||
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 <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);
|
||||
}
|
||||
@@ -5,10 +5,9 @@ package dijkstra;
|
||||
*
|
||||
*/
|
||||
public interface VertexInterface {
|
||||
|
||||
|
||||
/**
|
||||
* @return The label of the element.
|
||||
*/
|
||||
public String getLabel() ;
|
||||
|
||||
public String getLabel();
|
||||
}
|
||||
+9
-10
@@ -1,19 +1,18 @@
|
||||
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 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;
|
||||
}
|
||||
}
|
||||
+22
-32
@@ -5,11 +5,12 @@ import dijkstra.VertexInterface;
|
||||
|
||||
/** <h1> Box class </h1>
|
||||
* <p>
|
||||
*
|
||||
*
|
||||
* This class describes a Box and implements {@link dijkstra.VertexInterface}.
|
||||
* <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 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<Box> neighbourList = new ArrayList<Box>() ;
|
||||
|
||||
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<VertexInterface> 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<Box> 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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+5
-7
@@ -1,20 +1,18 @@
|
||||
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 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;
|
||||
}
|
||||
|
||||
}
|
||||
+5
-8
@@ -1,16 +1,13 @@
|
||||
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 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
@@ -11,189 +11,196 @@ import dijkstra.VertexInterface;
|
||||
/** <h1> Maze class </h1>
|
||||
* <p>
|
||||
* 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<VertexInterface> getVertexes() {
|
||||
ArrayList<VertexInterface> vertexList = new ArrayList<VertexInterface>() ;
|
||||
for (int i = 0 ; i < dimensionX ; i++) {
|
||||
for (int j = 0 ; j < dimensionY ; j++) {
|
||||
ArrayList<VertexInterface> vertexList = new ArrayList<VertexInterface>();
|
||||
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<VertexInterface> generateNeighbours(VertexInterface vertex) {
|
||||
|
||||
Box box = (Box)vertex ;
|
||||
int x = box.getX() ;
|
||||
int y = box.getY() ;
|
||||
ArrayList<VertexInterface> neighbourList = new ArrayList<VertexInterface>() ;
|
||||
|
||||
Box box = (Box) vertex;
|
||||
int x = box.getX();
|
||||
int y = box.getY();
|
||||
ArrayList<VertexInterface> neighbourList = new ArrayList<VertexInterface>();
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -2,20 +2,18 @@ package maze;
|
||||
|
||||
/**<h1> MazeReadingException exception </h1>
|
||||
* <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 {
|
||||
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
|
||||
public MazeReadingException (String filename) {
|
||||
|
||||
super("Invalid maze format : " + filename) ;
|
||||
|
||||
super("Invalid maze format : " + filename);
|
||||
}
|
||||
|
||||
}
|
||||
+7
-12
@@ -1,23 +1,18 @@
|
||||
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 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;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user