[Beug] Added

This commit is contained in:
antograb authored and antograb committed 2017-12-01 17:25:12 +01:00
1 parent dbf4ea3ab5
commit 6b92f5d725
4 files changed
+110 -16

No files matched your search

+6
View File
@@ -16,6 +16,10 @@ public abstract class Box
this.y = y ; this.y = y ;
this.maze = maze ; this.maze = maze ;
}
public List<Box> generateNeighbors() {
if (x != 0 && y != 0 && x != maze.getDimensionX() && y != maze.getDimensionY()) { if (x != 0 && y != 0 && x != maze.getDimensionX() && y != maze.getDimensionY()) {
this.neighbourList.add(maze.getMaze()[x+1][y]) ; this.neighbourList.add(maze.getMaze()[x+1][y]) ;
this.neighbourList.add(maze.getMaze()[x-1][y]) ; this.neighbourList.add(maze.getMaze()[x-1][y]) ;
@@ -82,6 +86,8 @@ public abstract class Box
this.neighbourList.add(maze.getMaze()[x][y+1]) ; this.neighbourList.add(maze.getMaze()[x][y+1]) ;
} }
} }
return neighbourList ;
} }
public boolean compareTo(VertexInterface vertex) { public boolean compareTo(VertexInterface vertex) {
+5 -3
View File
@@ -4,9 +4,11 @@ public class MainTest {
public static void main(String[] args) { public static void main(String[] args) {
// TODO Auto-generated method stub // TODO Auto-generated method stub
Box[][] box = new Box[2][2] ; Maze mazeTest = new Maze("data/labyrinthe.txt") ;
Maze mazeTest = new Maze(box) ; //mazeTest.saveToTextFile("data/out.txt");
mazeTest.initFromTextFile("data/labyrinthe.txt"); System.out.println("\n" + mazeTest);
} }
+99 -11
View File
@@ -1,5 +1,7 @@
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileReader; import java.io.FileReader;
import java.io.PrintWriter;
import java.util.ArrayList; import java.util.ArrayList;
public class Maze public class Maze
@@ -9,11 +11,9 @@ public class Maze
private int dimensionY ; private int dimensionY ;
private Box[][] maze ; private Box[][] maze ;
public Maze(Box[][] maze) { public Maze(String filename) {
this.maze = maze ; initFromTextFile(filename) ;
this.dimensionX = maze.length ;
this.dimensionY = maze[0].length ;
} }
@@ -53,22 +53,68 @@ public class Maze
FileReader fr = null ; FileReader fr = null ;
BufferedReader br = null ; BufferedReader br = null ;
try { try { //Determines the height of the maze and stores it into lineCount
fr = new FileReader(filename) ;
br = new BufferedReader(fr) ;
while (br.readLine() != null) dimensionY++;
//System.out.println("Line count :" + lineCount);
} catch (Exception e) {
e.printStackTrace();
} finally {
try { br.close() ;} catch (Exception e) {}
}
try { //generates the maze
fr = new FileReader(filename) ; fr = new FileReader(filename) ;
br = new BufferedReader(fr) ; br = new BufferedReader(fr) ;
String currentLine = null ;
int lineLength = br.readLine().length() - 1 ; currentLine = br.readLine() ;
System.out.println(currentLine);
dimensionX = currentLine.length() ; //maze's width
System.out.println("Line length :" + dimensionX);
int currentLineLength ; int currentLineLength ;
int cInt = br.read() ; int compteurLigne = 0 ;
while (cInt != -1) this.maze = new Box[dimensionX][dimensionY] ;
while (currentLine != null)
{ {
currentLineLength = br.readLine().length() ;
if (currentLineLength != lineLength) { //System.out.println(currentLine);
currentLineLength = currentLine.length() ;
System.out.println("Current line length :" + currentLineLength);
for (int compteurColonne = 0 ; compteurColonne < currentLineLength ; compteurColonne++) {
char currentChar = currentLine.charAt(compteurColonne) ;
switch(currentChar) {
case 'W' : maze[compteurLigne][compteurColonne] = new WBox("W", compteurLigne, compteurColonne, this) ;
break;
case 'E' : maze[compteurLigne][compteurColonne] = new EBox("E", compteurLigne, compteurColonne, this) ;
break;
case 'D' : maze[compteurLigne][compteurColonne] = new DBox("D", compteurLigne, compteurColonne, this) ;
break;
case 'A' : maze[compteurLigne][compteurColonne] = new ABox("A", compteurLigne, compteurColonne, this) ;
break;
default : throw new MazeReadingException(filename) ;
}
}
if (currentLineLength != dimensionX) {
throw new MazeReadingException(filename) ; throw new MazeReadingException(filename) ;
} }
cInt = br.read() ;
currentLine = br.readLine() ;
compteurLigne++ ;
} }
} catch (MazeReadingException mre) { } catch (MazeReadingException mre) {
mre.printStackTrace(); mre.printStackTrace();
@@ -78,6 +124,48 @@ public class Maze
try { br.close() ;} catch (Exception e) {} try { br.close() ;} catch (Exception e) {}
} }
System.out.println("dimX :" + dimensionX);
System.out.println("dimY :" + dimensionY);
//neighbour generation for each box in the maze
for (int i = 0 ; i < dimensionX ; i++) {
for (int j = 0 ; j < dimensionY ; j++) {
System.out.println(maze[i][j].getLabel());
System.out.println(maze[i][j].getMaze());
System.out.println("x : " + maze[i][j].getX() +" y : " + maze[i][j].getY());
maze[i][j].generateNeighbors() ;
}
System.out.println();
}
}
public final void saveToTextFile(String fileName) {
FileOutputStream fos = null ;
PrintWriter pw = null ;
try {
fos = new FileOutputStream(fileName) ;
pw = new PrintWriter(fos) ;
for (int i = 0; i < dimensionX ; i++) {
for (int j = 0 ; j < dimensionY ; j++) {
pw.print(maze[i][j].getLabel());
}
pw.print("\n");
}
} catch(Exception e) {
e.printStackTrace();
} finally {
try { pw.close();} catch(Exception e) {}
}
} }
public int getDimensionX() { public int getDimensionX() {
-2
View File
@@ -6,6 +6,4 @@ public interface VertexInterface {
public String getLabel() ; public String getLabel() ;
} }