Add UI to the project #29
No files matched your search
+8
-2
@@ -8,11 +8,17 @@ public class MainTest {
|
||||
public static void main(String[] args) {
|
||||
|
||||
Maze mazeTest = new Maze("data/labyrinthe.txt") ;
|
||||
System.out.println(mazeTest);
|
||||
mazeTest.saveToTextFile("data/labyOutput.txt");
|
||||
VertexInterface departure = mazeTest.getDeparture();
|
||||
VertexInterface arrival = mazeTest.getArrival();
|
||||
|
||||
Previous previous = (Previous) Dijkstra.dijkstra(mazeTest, mazeTest.getMaze()[1][1]);
|
||||
Previous previous = (Previous) Dijkstra.dijkstra(mazeTest, departure);
|
||||
|
||||
ArrayList<VertexInterface> shortest = previous.getShortestPathTo(mazeTest.getMaze()[5][5]);
|
||||
ArrayList<VertexInterface> shortest = previous.getShortestPathTo(arrival);
|
||||
|
||||
//Print shortest path to arrival
|
||||
System.out.println(arrival);
|
||||
for (VertexInterface vertex : shortest) {
|
||||
System.out.println(vertex);
|
||||
}
|
||||
|
||||
+1
-1
@@ -24,7 +24,7 @@ public abstract class Box
|
||||
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 y , int x, Maze maze) { //y first because of graphical maze representation
|
||||
|
||||
this.label = label ;
|
||||
this.x = x ;
|
||||
|
||||
+40
-26
@@ -33,9 +33,10 @@ public class Maze
|
||||
|
||||
this.dimensionX = maze.getDimensionX();
|
||||
this.dimensionY = maze.getDimensionY();
|
||||
this.maze = new Box[dimensionX][dimensionY];
|
||||
for (int line = 0; line < dimensionX; line++) {
|
||||
for (int row = 0; row < dimensionY; row++) {
|
||||
this.maze = new Box[dimensionY][dimensionX];
|
||||
|
||||
for (int line = 0; line < dimensionY; line++) {
|
||||
for (int row = 0; row < dimensionX; row++) {
|
||||
Box box = maze.getMaze()[line][row];
|
||||
if (box.isWalkable()) {
|
||||
if (box.isDeparture()) {
|
||||
@@ -69,10 +70,10 @@ public class Maze
|
||||
|
||||
public ArrayList<VertexInterface> getVertexes() {
|
||||
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]) ;
|
||||
for (int line = 0 ; line < dimensionY; line++) {
|
||||
for (int row = 0 ; row < dimensionX; row++) {
|
||||
if (maze[line][row].isWalkable()) {
|
||||
vertexList.add(maze[line][row]) ;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -110,6 +111,7 @@ public class Maze
|
||||
public ArrayList<VertexInterface> generateNeighbours(VertexInterface vertex) {
|
||||
|
||||
Box box = (Box)vertex ;
|
||||
//System.out.println(vertex);
|
||||
int x = box.getX() ;
|
||||
int y = box.getY() ;
|
||||
ArrayList<VertexInterface> neighbourList = new ArrayList<VertexInterface>() ;
|
||||
@@ -119,22 +121,23 @@ public class Maze
|
||||
return neighbourList ;
|
||||
}
|
||||
|
||||
if (x > 0 && maze[x-1][y].isWalkable()) {
|
||||
neighbourList.add(maze[x-1][y]) ;
|
||||
if (x > 0 && maze[y][x-1].isWalkable()) {
|
||||
neighbourList.add(maze[y][x-1]) ;
|
||||
}
|
||||
|
||||
if (x < dimensionX-1 && maze[x+1][y].isWalkable()) {
|
||||
neighbourList.add(maze[x+1][y]) ;
|
||||
if (x < dimensionX-1 && maze[y][x+1].isWalkable()) {
|
||||
neighbourList.add(maze[y][x+1]) ;
|
||||
}
|
||||
|
||||
if (y > 0 && maze[x][y-1].isWalkable()) {
|
||||
neighbourList.add(maze[x][y-1]) ;
|
||||
if (y > 0 && maze[y-1][x].isWalkable()) {
|
||||
neighbourList.add(maze[y-1][x]) ;
|
||||
}
|
||||
|
||||
if (y < dimensionY-1 && maze[x][y+1].isWalkable()) {
|
||||
neighbourList.add(maze[x][y+1]) ;
|
||||
if (y < dimensionY-1 && maze[y+1][x].isWalkable()) {
|
||||
neighbourList.add(maze[y+1][x]) ;
|
||||
}
|
||||
|
||||
//System.out.println(" "+ neighbourList);
|
||||
return neighbourList ;
|
||||
|
||||
}
|
||||
@@ -168,10 +171,10 @@ public class Maze
|
||||
currentLine = br.readLine() ;
|
||||
dimensionX = currentLine.length() ; //maze's width
|
||||
int currentLineLength ;
|
||||
|
||||
this.maze = new Box[dimensionY][dimensionX] ;
|
||||
|
||||
int compteurLigne = 0 ;
|
||||
|
||||
this.maze = new Box[dimensionX][dimensionY] ;
|
||||
|
||||
while (currentLine != null)
|
||||
{
|
||||
currentLineLength = currentLine.length() ;
|
||||
@@ -182,16 +185,16 @@ public class Maze
|
||||
|
||||
switch(currentChar) {
|
||||
|
||||
case 'W' : maze[compteurColonne][compteurLigne] = new WBox("W", compteurColonne, compteurLigne, this) ;
|
||||
case 'W' : maze[compteurLigne][compteurColonne] = new WBox("W", compteurLigne, compteurColonne, this) ;
|
||||
break;
|
||||
|
||||
case 'E' : maze[compteurColonne][compteurLigne] = new EBox("E", compteurColonne, compteurLigne, this) ;
|
||||
case 'E' : maze[compteurLigne][compteurColonne] = new EBox("E", compteurLigne, compteurColonne, this) ;
|
||||
break;
|
||||
|
||||
case 'D' : maze[compteurColonne][compteurLigne] = new DBox("D", compteurColonne, compteurLigne, this) ;
|
||||
case 'D' : maze[compteurLigne][compteurColonne] = new DBox("D", compteurLigne, compteurColonne, this) ;
|
||||
break;
|
||||
|
||||
case 'A' : maze[compteurColonne][compteurLigne] = new ABox("A", compteurColonne, compteurLigne, this) ;
|
||||
case 'A' : maze[compteurLigne][compteurColonne] = new ABox("A", compteurLigne, compteurColonne, this) ;
|
||||
break;
|
||||
|
||||
default : throw new MazeReadingException(filename) ;
|
||||
@@ -213,7 +216,6 @@ public class Maze
|
||||
} finally {
|
||||
try { br.close() ;} catch (Exception e) {}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public final void saveToTextFile(String fileName) {
|
||||
@@ -226,10 +228,10 @@ public class Maze
|
||||
fos = new FileOutputStream(fileName) ;
|
||||
pw = new PrintWriter(fos) ;
|
||||
|
||||
for (int i = 0; i < dimensionX ; i++) {
|
||||
for (int j = 0 ; j < dimensionY ; j++) {
|
||||
for (int line = 0; line < dimensionY ; line++) {
|
||||
for (int row = 0 ; row < dimensionX ; row++) {
|
||||
|
||||
pw.print(maze[i][j].getLabel());
|
||||
pw.print(maze[line][row].getLabel());
|
||||
|
||||
}
|
||||
pw.print("\n");
|
||||
@@ -242,6 +244,18 @@ public class Maze
|
||||
}
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
|
||||
String mazeStr = "";
|
||||
for (int line = 0; line < dimensionY; line++) {
|
||||
for (int row = 0; row < dimensionX; row++) {
|
||||
mazeStr += maze[line][row].getLabel();
|
||||
}
|
||||
mazeStr += "\n";
|
||||
}
|
||||
return mazeStr;
|
||||
}
|
||||
|
||||
public int getDimensionX() {
|
||||
return dimensionX;
|
||||
}
|
||||
|
||||
@@ -30,24 +30,24 @@ public class DrawingPanelGrid extends JPanel {
|
||||
|
||||
private void initFromMaze(Maze maze) {
|
||||
|
||||
for (int i = 0; i < dimensionY; i++) {
|
||||
for (int j = 0; j < dimensionX; j++) {
|
||||
if (maze.getMaze()[j][i].isWalkable()) {
|
||||
if (maze.getMaze()[j][i].isDeparture()) {
|
||||
drawingBoxMatrix[j][i] = new DepartureDrawingBox(mazeApp, Color.GREEN);
|
||||
for (int line = 0 ; line < dimensionY; line++) {
|
||||
for (int row = 0 ; row < dimensionX; row++) {
|
||||
if (maze.getMaze()[line][row].isWalkable()) {
|
||||
if (maze.getMaze()[line][row].isDeparture()) {
|
||||
drawingBoxMatrix[line][row] = new DepartureDrawingBox(mazeApp, Color.GREEN);
|
||||
}
|
||||
else if (maze.getMaze()[j][i].isArrival()) {
|
||||
drawingBoxMatrix[j][i] = new ArrivalDrawingBox(mazeApp, Color.RED);
|
||||
else if (maze.getMaze()[line][row].isArrival()) {
|
||||
drawingBoxMatrix[line][row] = new ArrivalDrawingBox(mazeApp, Color.RED);
|
||||
}
|
||||
else {
|
||||
drawingBoxMatrix[j][i] = new EmptyDrawingBox(mazeApp, Color.BLACK);
|
||||
drawingBoxMatrix[line][row] = new EmptyDrawingBox(mazeApp, Color.BLACK);
|
||||
}
|
||||
}
|
||||
else {
|
||||
drawingBoxMatrix[j][i] = new WallDrawingBox(mazeApp, Color.WHITE);
|
||||
drawingBoxMatrix[line][row] = new WallDrawingBox(mazeApp, Color.WHITE);
|
||||
}
|
||||
|
||||
add(drawingBoxMatrix[j][i]);
|
||||
add(drawingBoxMatrix[line][row]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -55,28 +55,29 @@ public class DrawingPanelGrid extends JPanel {
|
||||
public void notifyForUpdates(Object param) {
|
||||
|
||||
if (param instanceof Maze) {
|
||||
// set all new parameters
|
||||
this.maze = (Maze) param;
|
||||
this.dimensionX = maze.getDimensionX();
|
||||
this.dimensionY = maze.getDimensionY();
|
||||
this.drawingBoxMatrix = new DrawingBox[dimensionY][dimensionX];
|
||||
System.out.println("x : "+dimensionX+" y : "+dimensionY);
|
||||
this.gridLayout = new GridLayout(dimensionY, dimensionX);
|
||||
setLayout(this.gridLayout);
|
||||
|
||||
for (int line = 0; line < dimensionX; line++) {
|
||||
for (int row = 0; row < dimensionY; row++) {
|
||||
for (int line = 0; line < dimensionY; line++) {
|
||||
for (int row = 0; row < dimensionX; row++) {
|
||||
if (maze.getMaze()[line][row].isWalkable()) {
|
||||
if (maze.getMaze()[line][row].isDeparture()) {
|
||||
drawingBoxMatrix[row][line].setColor(Color.GREEN);
|
||||
drawingBoxMatrix[line][row] = new DepartureDrawingBox(mazeApp, Color.GREEN);
|
||||
}
|
||||
else if (maze.getMaze()[line][row].isArrival()) {
|
||||
drawingBoxMatrix[row][line].setColor(Color.RED);
|
||||
drawingBoxMatrix[line][row] = new DepartureDrawingBox(mazeApp, Color.RED);
|
||||
}
|
||||
else {
|
||||
drawingBoxMatrix[row][line].setColor(Color.BLACK);
|
||||
drawingBoxMatrix[line][row] = new DepartureDrawingBox(mazeApp, Color.BLACK);
|
||||
}
|
||||
}
|
||||
else {
|
||||
drawingBoxMatrix[row][line].setColor(Color.WHITE);
|
||||
drawingBoxMatrix[line][row] = new DepartureDrawingBox(mazeApp, Color.GREEN);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user