Fixed last dijkstra classes. Algorithm working for squared maze but bugs for non square mazes.

This commit is contained in:
antograb committed 2017-12-06 22:30:58 +01:00
1 parent 5d7c970e1b
commit d7368f7cda
10 files changed
+64 -54

No files matched your search

+6 -23
View File
@@ -9,7 +9,7 @@ public abstract class Box
private int x ;
private int y ;
private String label ;
private String label = null ;
private Maze maze ;
private List<Box> neighbourList = new ArrayList<Box>() ;
@@ -22,27 +22,8 @@ public abstract class Box
}
public List<Box> generateNeighbors() {
Box[][] vMaze = maze.getMaze() ;
if (x > 0 && vMaze[x-1][y].isWalkable()) {
neighbourList.add(vMaze[x-1][y]) ;
}
if (x < maze.getDimensionX()-1 && vMaze[x+1][y].isWalkable()) {
neighbourList.add(vMaze[x+1][y]) ;
}
if (y > 0 && vMaze[x][y-1].isWalkable()) {
neighbourList.add(vMaze[x][y-1]) ;
}
if (y < maze.getDimensionY()-1 && vMaze[x][y+1].isWalkable()) {
neighbourList.add(vMaze[x][y+1]) ;
}
return neighbourList ;
public List<VertexInterface> generateNeighbors() {
return maze.generateNeighbours(this) ;
}
@@ -105,6 +86,8 @@ public abstract class Box
this.maze = maze;
}
public String toString() {
return label;
}
}
+27 -10
View File
@@ -54,6 +54,33 @@ public class Maze
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>() ;
if (x > 0 && maze[x-1][y].isWalkable()) {
neighbourList.add(maze[x-1][y]) ;
}
if (x < dimensionX-1 && maze[x+1][y].isWalkable()) {
neighbourList.add(maze[x+1][y]) ;
}
if (y > 0 && maze[x][y-1].isWalkable()) {
neighbourList.add(maze[x][y-1]) ;
}
if (y < dimensionY-1 && maze[x][y+1].isWalkable()) {
neighbourList.add(maze[x][y+1]) ;
}
return neighbourList ;
}
public final void initFromTextFile(String filename) {
FileReader fr = null ;
@@ -77,9 +104,7 @@ public class Maze
String currentLine = null ;
currentLine = br.readLine() ;
System.out.println(currentLine);
dimensionX = currentLine.length() ; //maze's width
System.out.println("Line length :" + dimensionX);
int currentLineLength ;
int compteurLigne = 0 ;
@@ -90,7 +115,6 @@ public class Maze
//System.out.println(currentLine);
currentLineLength = currentLine.length() ;
System.out.println("Current line length :" + currentLineLength);
for (int compteurColonne = 0 ; compteurColonne < currentLineLength ; compteurColonne++) {
@@ -130,13 +154,6 @@ public class Maze
try { br.close() ;} catch (Exception e) {}
}
//neighbour generation for each box in the maze
for (int i = 0 ; i < dimensionX ; i++) {
for (int j = 0 ; j < dimensionY ; j++) {
maze[i][j].generateNeighbors() ;
}
System.out.println();
}
}
public final void saveToTextFile(String fileName) {