Merge the last Dijkstra classes #7
No files matched your search
+11
-3
@@ -1,5 +1,7 @@
|
|||||||
import dijkstra.Dijkstra;
|
import java.util.ArrayList;
|
||||||
import maze.Maze;
|
|
||||||
|
import dijkstra.*;
|
||||||
|
import maze.*;
|
||||||
|
|
||||||
public class MainTest {
|
public class MainTest {
|
||||||
|
|
||||||
@@ -7,7 +9,13 @@ public class MainTest {
|
|||||||
|
|
||||||
Maze mazeTest = new Maze("data/labyrinthe.txt") ;
|
Maze mazeTest = new Maze("data/labyrinthe.txt") ;
|
||||||
|
|
||||||
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]);
|
||||||
|
|
||||||
|
for (VertexInterface vertex : shortest) {
|
||||||
|
System.out.println(vertex);
|
||||||
|
}
|
||||||
|
|
||||||
mazeTest.saveToTextFile("data/labyOutput.txt");
|
mazeTest.saveToTextFile("data/labyOutput.txt");
|
||||||
|
|
||||||
|
|||||||
@@ -2,8 +2,6 @@ package dijkstra;
|
|||||||
|
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
|
|
||||||
import maze.Box;
|
|
||||||
|
|
||||||
public class ASet implements ASetInterface {
|
public class ASet implements ASetInterface {
|
||||||
|
|
||||||
private HashSet<VertexInterface> vertexHashSet = new HashSet<VertexInterface>() ;
|
private HashSet<VertexInterface> vertexHashSet = new HashSet<VertexInterface>() ;
|
||||||
@@ -11,20 +9,19 @@ public class ASet implements ASetInterface {
|
|||||||
|
|
||||||
public ASet(HashSet<VertexInterface> vertexHashSet) {
|
public ASet(HashSet<VertexInterface> vertexHashSet) {
|
||||||
|
|
||||||
this.vertexHashSet = vertexHashSet;
|
this.vertexHashSet = new HashSet<VertexInterface>(vertexHashSet);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void add(VertexInterface vertex) {
|
public void add(VertexInterface vertex) {
|
||||||
|
|
||||||
vertexHashSet.add((Box)vertex) ;
|
vertexHashSet.add(vertex) ;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean contains(VertexInterface vertex) {
|
public boolean contains(VertexInterface vertex) {
|
||||||
|
|
||||||
if (vertexHashSet.contains(vertex)) return true ;
|
return vertexHashSet.contains(vertex);
|
||||||
return false ;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ public class Pi implements PiInterface {
|
|||||||
|
|
||||||
public Pi(Hashtable<VertexInterface, Integer> hashPi) {
|
public Pi(Hashtable<VertexInterface, Integer> hashPi) {
|
||||||
|
|
||||||
this.hashPi = hashPi ;
|
this.hashPi = new Hashtable<VertexInterface, Integer>(hashPi);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -8,12 +8,10 @@ public class Previous implements PreviousInterface {
|
|||||||
private Hashtable<VertexInterface, VertexInterface> hashPrevious = new Hashtable<VertexInterface, VertexInterface>() ;
|
private Hashtable<VertexInterface, VertexInterface> hashPrevious = new Hashtable<VertexInterface, VertexInterface>() ;
|
||||||
private ArrayList<VertexInterface> shortestPath = new ArrayList<VertexInterface>() ;
|
private ArrayList<VertexInterface> shortestPath = new ArrayList<VertexInterface>() ;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public Previous(Hashtable<VertexInterface, VertexInterface> hashPrevious, ArrayList<VertexInterface> shortestPath) {
|
public Previous(Hashtable<VertexInterface, VertexInterface> hashPrevious, ArrayList<VertexInterface> shortestPath) {
|
||||||
|
|
||||||
this.hashPrevious = hashPrevious;
|
this.hashPrevious = new Hashtable<VertexInterface, VertexInterface>(hashPrevious);
|
||||||
this.shortestPath = shortestPath;
|
this.shortestPath = new ArrayList<VertexInterface>(shortestPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
public VertexInterface getPrevious(VertexInterface vertex) {
|
public VertexInterface getPrevious(VertexInterface vertex) {
|
||||||
@@ -37,7 +35,6 @@ public class Previous implements PreviousInterface {
|
|||||||
else {
|
else {
|
||||||
return getShortestPathTo(father) ;
|
return getShortestPathTo(father) ;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -6,16 +6,9 @@ package dijkstra;
|
|||||||
*/
|
*/
|
||||||
public interface VertexInterface {
|
public interface VertexInterface {
|
||||||
|
|
||||||
/**
|
|
||||||
* Compares a <code>VertexInterface</code> to another.
|
|
||||||
* @param vertex
|
|
||||||
* The vertex to which the equality will be tested.
|
|
||||||
* @return <code>true</code> is the two <code>VertexInterface</code> are the same, <code>false</code> if not.
|
|
||||||
*/
|
|
||||||
public boolean compareTo(VertexInterface vertex) ;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return The label of the element.
|
* @return The label of the element.
|
||||||
*/
|
*/
|
||||||
public String getLabel() ;
|
public String getLabel() ;
|
||||||
|
|
||||||
}
|
}
|
||||||
+1
-1
@@ -3,7 +3,7 @@ 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 Box{
|
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) {
|
||||||
|
|
||||||
|
|||||||
+4
-36
@@ -1,6 +1,5 @@
|
|||||||
package maze;
|
package maze;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import dijkstra.VertexInterface;
|
import dijkstra.VertexInterface;
|
||||||
|
|
||||||
@@ -23,7 +22,7 @@ public abstract class Box
|
|||||||
private int y ;
|
private int y ;
|
||||||
private String label = null ;
|
private String label = null ;
|
||||||
private Maze maze ;
|
private Maze maze ;
|
||||||
private List<Box> neighbourList = new ArrayList<Box>() ;
|
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) {
|
||||||
|
|
||||||
@@ -34,18 +33,11 @@ public abstract class Box
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<VertexInterface> generateNeighbors() {
|
public ArrayList<VertexInterface> generateNeighbors() {
|
||||||
return maze.generateNeighbours(this) ;
|
return maze.generateNeighbours(this) ;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean compareTo(VertexInterface vertex) {
|
|
||||||
if (this.getLabel() == vertex.getLabel()) {
|
|
||||||
return true ;
|
|
||||||
}
|
|
||||||
return false ;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isWalkable() {
|
public boolean isWalkable() {
|
||||||
return true ;
|
return true ;
|
||||||
}
|
}
|
||||||
@@ -58,48 +50,24 @@ public abstract class Box
|
|||||||
return false ;
|
return false ;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Box> getNeighbourList() {
|
public ArrayList<Box> getNeighbourList() {
|
||||||
return neighbourList;
|
return neighbourList;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setNeighbourList(List<Box> neighbourList) {
|
|
||||||
this.neighbourList = neighbourList;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getX() {
|
public int getX() {
|
||||||
return x;
|
return x;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setX(int x) {
|
|
||||||
this.x = x;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getY() {
|
public int getY() {
|
||||||
return y;
|
return y;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setY(int y) {
|
|
||||||
this.y = y;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getLabel() {
|
public String getLabel() {
|
||||||
return label;
|
return label;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setLabel(String label) {
|
|
||||||
this.label = label;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Maze getMaze() {
|
|
||||||
return maze;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setMaze(Maze maze) {
|
|
||||||
this.maze = maze;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return label;
|
return "Label: " + label + ", x: " + x + ", y: " + y;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
+1
-1
@@ -4,7 +4,7 @@ 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 Box{
|
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) {
|
||||||
|
|
||||||
|
|||||||
+13
-28
@@ -29,24 +29,18 @@ public class Maze
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
// public Maze(Box[][] maze){
|
||||||
*
|
// //Replacement for setMaze()
|
||||||
*/
|
// //Todo : check if the given array is a proper maze
|
||||||
|
// //and then set dimensionX and dimensionX
|
||||||
|
// }
|
||||||
|
|
||||||
public int getCost(VertexInterface start, VertexInterface end) {
|
public int getCost(VertexInterface start, VertexInterface end) {
|
||||||
|
|
||||||
Box startBox = (Box) start ;
|
Box startBox = (Box) start ;
|
||||||
Box endBox = (Box) end ;
|
Box endBox = (Box) end ;
|
||||||
|
|
||||||
if (startBox.getNeighbourList().contains(endBox)) {
|
return startBox.getNeighbourList().contains(endBox) ? 1 : 0 ;
|
||||||
if (startBox.isWalkable() && endBox.isWalkable()) {
|
|
||||||
return 1 ;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return 0 ;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0 ;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getVertexNumber() {
|
public int getVertexNumber() {
|
||||||
@@ -72,6 +66,11 @@ public class Maze
|
|||||||
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
|
||||||
|
if (! box.isWalkable()) {
|
||||||
|
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]) ;
|
||||||
}
|
}
|
||||||
@@ -97,7 +96,7 @@ public class Maze
|
|||||||
* The path to the file containing the representation of the maze.
|
* The path to the file containing the representation of the maze.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public final void initFromTextFile(String filename) {
|
private final void initFromTextFile(String filename) {
|
||||||
|
|
||||||
FileReader fr = null ;
|
FileReader fr = null ;
|
||||||
BufferedReader br = null ;
|
BufferedReader br = null ;
|
||||||
@@ -193,32 +192,18 @@ public class Maze
|
|||||||
} finally {
|
} finally {
|
||||||
try { pw.close();} catch(Exception e) {}
|
try { pw.close();} catch(Exception e) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getDimensionX() {
|
public int getDimensionX() {
|
||||||
return dimensionX;
|
return dimensionX;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setDimensionX(int dimensionX) {
|
|
||||||
this.dimensionX = dimensionX;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getDimensionY() {
|
public int getDimensionY() {
|
||||||
return dimensionY;
|
return dimensionY;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setDimensionY(int dimensionY) {
|
|
||||||
this.dimensionY = dimensionY;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Box[][] getMaze() {
|
public Box[][] getMaze() {
|
||||||
return maze;
|
return maze;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setMaze(Box[][] maze) {
|
|
||||||
this.maze = maze;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -10,17 +10,12 @@ package maze;
|
|||||||
public class MazeReadingException
|
public class MazeReadingException
|
||||||
extends Exception {
|
extends Exception {
|
||||||
|
|
||||||
private String filename ;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
public MazeReadingException (String filename) {
|
public MazeReadingException (String filename) {
|
||||||
|
|
||||||
super("Invalid maze format : " + filename) ;
|
super("Invalid maze format : " + filename) ;
|
||||||
this.filename = filename ;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getFilename() {
|
|
||||||
return filename ;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user