Merge branch 'fixIndexes' of antograb/TpMazeJava into mvc

This commit was merged in pull request #20.
This commit is contained in:
regar42 authored and Gogs committed 2017-12-19 22:35:22 +01:00
commit 2a4d852bac
4 files changed
+69 -48

No files matched your search

+8 -2
View File
@@ -8,11 +8,17 @@ public class MainTest {
public static void main(String[] args) { public static void main(String[] args) {
Maze mazeTest = new Maze("data/labyrinthe.txt") ; 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) { for (VertexInterface vertex : shortest) {
System.out.println(vertex); System.out.println(vertex);
} }
+1 -1
View File
@@ -24,7 +24,7 @@ public abstract class Box
private Maze maze ; private Maze maze ;
private ArrayList<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 y , int x, Maze maze) { //y first because of graphical maze representation
this.label = label ; this.label = label ;
this.x = x ; this.x = x ;
+42 -28
View File
@@ -33,9 +33,10 @@ public class Maze
this.dimensionX = maze.getDimensionX(); this.dimensionX = maze.getDimensionX();
this.dimensionY = maze.getDimensionY(); this.dimensionY = maze.getDimensionY();
this.maze = new Box[dimensionX][dimensionY]; this.maze = new Box[dimensionY][dimensionX];
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++) {
Box box = maze.getMaze()[line][row]; Box box = maze.getMaze()[line][row];
if (box.isWalkable()) { if (box.isWalkable()) {
if (box.isDeparture()) { if (box.isDeparture()) {
@@ -69,10 +70,10 @@ public class Maze
public ArrayList<VertexInterface> getVertexes() { public ArrayList<VertexInterface> getVertexes() {
ArrayList<VertexInterface> vertexList = new ArrayList<VertexInterface>() ; ArrayList<VertexInterface> vertexList = new ArrayList<VertexInterface>() ;
for (int i = 0 ; i < dimensionX ; i++) { for (int line = 0 ; line < dimensionY; line++) {
for (int j = 0 ; j < dimensionY ; j++) { for (int row = 0 ; row < dimensionX; row++) {
if (maze[i][j].isWalkable()) { if (maze[line][row].isWalkable()) {
vertexList.add(maze[i][j]) ; vertexList.add(maze[line][row]) ;
} }
} }
} }
@@ -110,6 +111,7 @@ public class Maze
public ArrayList<VertexInterface> generateNeighbours(VertexInterface vertex) { public ArrayList<VertexInterface> generateNeighbours(VertexInterface vertex) {
Box box = (Box)vertex ; Box box = (Box)vertex ;
//System.out.println(vertex);
int x = box.getX() ; int x = box.getX() ;
int y = box.getY() ; int y = box.getY() ;
ArrayList<VertexInterface> neighbourList = new ArrayList<VertexInterface>() ; ArrayList<VertexInterface> neighbourList = new ArrayList<VertexInterface>() ;
@@ -119,22 +121,23 @@ public class Maze
return neighbourList ; return neighbourList ;
} }
if (x > 0 && maze[x-1][y].isWalkable()) { if (x > 0 && maze[y][x-1].isWalkable()) {
neighbourList.add(maze[x-1][y]) ; neighbourList.add(maze[y][x-1]) ;
} }
if (x < dimensionX-1 && maze[x+1][y].isWalkable()) { if (x < dimensionX-1 && maze[y][x+1].isWalkable()) {
neighbourList.add(maze[x+1][y]) ; neighbourList.add(maze[y][x+1]) ;
} }
if (y > 0 && maze[x][y-1].isWalkable()) { if (y > 0 && maze[y-1][x].isWalkable()) {
neighbourList.add(maze[x][y-1]) ; neighbourList.add(maze[y-1][x]) ;
} }
if (y < dimensionY-1 && maze[x][y+1].isWalkable()) { if (y < dimensionY-1 && maze[y+1][x].isWalkable()) {
neighbourList.add(maze[x][y+1]) ; neighbourList.add(maze[y+1][x]) ;
} }
//System.out.println(" "+ neighbourList);
return neighbourList ; return neighbourList ;
} }
@@ -148,7 +151,7 @@ public class Maze
FileReader fr = null ; FileReader fr = null ;
BufferedReader br = null ; BufferedReader br = null ;
try { try {
fr = new FileReader(filename) ; fr = new FileReader(filename) ;
br = new BufferedReader(fr) ; br = new BufferedReader(fr) ;
@@ -168,10 +171,10 @@ public class Maze
currentLine = br.readLine() ; currentLine = br.readLine() ;
dimensionX = currentLine.length() ; //maze's width dimensionX = currentLine.length() ; //maze's width
int currentLineLength ; int currentLineLength ;
this.maze = new Box[dimensionY][dimensionX] ;
int compteurLigne = 0 ; int compteurLigne = 0 ;
this.maze = new Box[dimensionX][dimensionY] ;
while (currentLine != null) while (currentLine != null)
{ {
currentLineLength = currentLine.length() ; currentLineLength = currentLine.length() ;
@@ -182,16 +185,16 @@ public class Maze
switch(currentChar) { 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; break;
case 'E' : maze[compteurColonne][compteurLigne] = new EBox("E", compteurColonne, compteurLigne, this) ; case 'E' : maze[compteurLigne][compteurColonne] = new EBox("E", compteurLigne, compteurColonne, this) ;
break; break;
case 'D' : maze[compteurColonne][compteurLigne] = new DBox("D", compteurColonne, compteurLigne, this) ; case 'D' : maze[compteurLigne][compteurColonne] = new DBox("D", compteurLigne, compteurColonne, this) ;
break; break;
case 'A' : maze[compteurColonne][compteurLigne] = new ABox("A", compteurColonne, compteurLigne, this) ; case 'A' : maze[compteurLigne][compteurColonne] = new ABox("A", compteurLigne, compteurColonne, this) ;
break; break;
default : throw new MazeReadingException(filename) ; default : throw new MazeReadingException(filename) ;
@@ -213,7 +216,6 @@ public class Maze
} finally { } finally {
try { br.close() ;} catch (Exception e) {} try { br.close() ;} catch (Exception e) {}
} }
} }
public final void saveToTextFile(String fileName) { public final void saveToTextFile(String fileName) {
@@ -226,10 +228,10 @@ public class Maze
fos = new FileOutputStream(fileName) ; fos = new FileOutputStream(fileName) ;
pw = new PrintWriter(fos) ; pw = new PrintWriter(fos) ;
for (int i = 0; i < dimensionX ; i++) { for (int line = 0; line < dimensionY ; line++) {
for (int j = 0 ; j < dimensionY ; j++) { for (int row = 0 ; row < dimensionX ; row++) {
pw.print(maze[i][j].getLabel()); pw.print(maze[line][row].getLabel());
} }
pw.print("\n"); pw.print("\n");
@@ -241,7 +243,19 @@ public class Maze
try { pw.close();} catch(Exception e) {} 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() { public int getDimensionX() {
return dimensionX; return dimensionX;
} }
+18 -17
View File
@@ -30,24 +30,24 @@ public class DrawingPanelGrid extends JPanel {
private void initFromMaze(Maze maze) { private void initFromMaze(Maze maze) {
for (int i = 0; i < dimensionY; i++) { for (int line = 0 ; line < dimensionY; line++) {
for (int j = 0; j < dimensionX; j++) { for (int row = 0 ; row < dimensionX; row++) {
if (maze.getMaze()[j][i].isWalkable()) { if (maze.getMaze()[line][row].isWalkable()) {
if (maze.getMaze()[j][i].isDeparture()) { if (maze.getMaze()[line][row].isDeparture()) {
drawingBoxMatrix[j][i] = new DepartureDrawingBox(mazeApp, Color.GREEN); drawingBoxMatrix[line][row] = new DepartureDrawingBox(mazeApp, Color.GREEN);
} }
else if (maze.getMaze()[j][i].isArrival()) { else if (maze.getMaze()[line][row].isArrival()) {
drawingBoxMatrix[j][i] = new ArrivalDrawingBox(mazeApp, Color.RED); drawingBoxMatrix[line][row] = new ArrivalDrawingBox(mazeApp, Color.RED);
} }
else { else {
drawingBoxMatrix[j][i] = new EmptyDrawingBox(mazeApp, Color.BLACK); drawingBoxMatrix[line][row] = new EmptyDrawingBox(mazeApp, Color.BLACK);
} }
} }
else { 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) { public void notifyForUpdates(Object param) {
if (param instanceof Maze) { if (param instanceof Maze) {
// set all new parameters
this.maze = (Maze) param; this.maze = (Maze) param;
this.dimensionX = maze.getDimensionX(); this.dimensionX = maze.getDimensionX();
this.dimensionY = maze.getDimensionY(); this.dimensionY = maze.getDimensionY();
this.drawingBoxMatrix = new DrawingBox[dimensionY][dimensionX]; this.drawingBoxMatrix = new DrawingBox[dimensionY][dimensionX];
System.out.println("x : "+dimensionX+" y : "+dimensionY);
this.gridLayout = new GridLayout(dimensionY, dimensionX); this.gridLayout = new GridLayout(dimensionY, dimensionX);
setLayout(this.gridLayout);
for (int line = 0; line < dimensionX; line++) { for (int line = 0; line < dimensionY; line++) {
for (int row = 0; row < dimensionY; row++) { for (int row = 0; row < dimensionX; row++) {
if (maze.getMaze()[line][row].isWalkable()) { if (maze.getMaze()[line][row].isWalkable()) {
if (maze.getMaze()[line][row].isDeparture()) { 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()) { else if (maze.getMaze()[line][row].isArrival()) {
drawingBoxMatrix[row][line].setColor(Color.RED); drawingBoxMatrix[line][row] = new DepartureDrawingBox(mazeApp, Color.RED);
} }
else { else {
drawingBoxMatrix[row][line].setColor(Color.BLACK); drawingBoxMatrix[line][row] = new DepartureDrawingBox(mazeApp, Color.BLACK);
} }
} }
else { else {
drawingBoxMatrix[row][line].setColor(Color.WHITE); drawingBoxMatrix[line][row] = new DepartureDrawingBox(mazeApp, Color.GREEN);
} }
} }
} }