From 213071d3c218056bbc8e5c1572208f1a47f64064 Mon Sep 17 00:00:00 2001 From: Antonin Godard Date: Mon, 18 Dec 2017 16:32:47 +0100 Subject: [PATCH] Fix indexes confusion --- src/MainTest.java | 10 ++++-- src/maze/Box.java | 2 +- src/maze/Maze.java | 70 +++++++++++++++++++++--------------- src/ui/DrawingPanelGrid.java | 35 +++++++++--------- 4 files changed, 69 insertions(+), 48 deletions(-) diff --git a/src/MainTest.java b/src/MainTest.java index 26010af..66cdb93 100644 --- a/src/MainTest.java +++ b/src/MainTest.java @@ -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 shortest = previous.getShortestPathTo(mazeTest.getMaze()[5][5]); + ArrayList shortest = previous.getShortestPathTo(arrival); + //Print shortest path to arrival + System.out.println(arrival); for (VertexInterface vertex : shortest) { System.out.println(vertex); } diff --git a/src/maze/Box.java b/src/maze/Box.java index 00f7694..3a665e7 100644 --- a/src/maze/Box.java +++ b/src/maze/Box.java @@ -24,7 +24,7 @@ public abstract class Box private Maze maze ; private ArrayList neighbourList = new ArrayList() ; - 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 ; diff --git a/src/maze/Maze.java b/src/maze/Maze.java index b81d89c..1d9d3c3 100644 --- a/src/maze/Maze.java +++ b/src/maze/Maze.java @@ -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 getVertexes() { ArrayList vertexList = new ArrayList() ; - 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 generateNeighbours(VertexInterface vertex) { Box box = (Box)vertex ; + //System.out.println(vertex); int x = box.getX() ; int y = box.getY() ; ArrayList neighbourList = new ArrayList() ; @@ -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 ; } @@ -148,7 +151,7 @@ public class Maze FileReader fr = null ; BufferedReader br = null ; - + try { fr = new FileReader(filename) ; br = new BufferedReader(fr) ; @@ -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"); @@ -241,7 +243,19 @@ public class Maze try { pw.close();} catch(Exception e) {} } } - + + 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; } diff --git a/src/ui/DrawingPanelGrid.java b/src/ui/DrawingPanelGrid.java index 0bb3bdb..6fd3a5a 100644 --- a/src/ui/DrawingPanelGrid.java +++ b/src/ui/DrawingPanelGrid.java @@ -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); } } }