[Beug] Added
This commit is contained in:
1 parent
dbf4ea3ab5
commit
6b92f5d725
5 files changed
+114
-20
No files matched your search
@@ -16,6 +16,10 @@ public abstract class Box
|
||||
this.y = y ;
|
||||
this.maze = maze ;
|
||||
|
||||
}
|
||||
|
||||
public List<Box> generateNeighbors() {
|
||||
|
||||
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]) ;
|
||||
@@ -82,6 +86,8 @@ public abstract class Box
|
||||
this.neighbourList.add(maze.getMaze()[x][y+1]) ;
|
||||
}
|
||||
}
|
||||
|
||||
return neighbourList ;
|
||||
}
|
||||
|
||||
public boolean compareTo(VertexInterface vertex) {
|
||||
|
||||
+5
-3
@@ -4,9 +4,11 @@ public class MainTest {
|
||||
public static void main(String[] args) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
Box[][] box = new Box[2][2] ;
|
||||
Maze mazeTest = new Maze(box) ;
|
||||
mazeTest.initFromTextFile("data/labyrinthe.txt");
|
||||
Maze mazeTest = new Maze("data/labyrinthe.txt") ;
|
||||
//mazeTest.saveToTextFile("data/out.txt");
|
||||
System.out.println("\n" + mazeTest);
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -1,19 +1,19 @@
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.FileReader;
|
||||
import java.io.PrintWriter;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Maze
|
||||
implements GraphInterface{
|
||||
implements GraphInterface {
|
||||
|
||||
private int dimensionX ;
|
||||
private int dimensionY ;
|
||||
private Box[][] maze ;
|
||||
|
||||
public Maze(Box[][] maze) {
|
||||
public Maze(String filename) {
|
||||
|
||||
this.maze = maze ;
|
||||
this.dimensionX = maze.length ;
|
||||
this.dimensionY = maze[0].length ;
|
||||
initFromTextFile(filename) ;
|
||||
|
||||
}
|
||||
|
||||
@@ -50,25 +50,71 @@ public class Maze
|
||||
|
||||
public final void initFromTextFile(String filename) {
|
||||
|
||||
FileReader fr = null ;
|
||||
FileReader fr = null ;
|
||||
BufferedReader br = null ;
|
||||
|
||||
try {
|
||||
fr = new FileReader(filename) ;
|
||||
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) ;
|
||||
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 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) ;
|
||||
}
|
||||
cInt = br.read() ;
|
||||
|
||||
currentLine = br.readLine() ;
|
||||
compteurLigne++ ;
|
||||
}
|
||||
} catch (MazeReadingException mre) {
|
||||
mre.printStackTrace();
|
||||
@@ -78,6 +124,48 @@ public class Maze
|
||||
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() {
|
||||
|
||||
@@ -6,7 +6,7 @@ public class MazeReadingException
|
||||
|
||||
public MazeReadingException (String filename) {
|
||||
|
||||
super("Invalid maze format :" + filename) ;
|
||||
super("Invalid maze format : " + filename) ;
|
||||
this.filename = filename ;
|
||||
|
||||
}
|
||||
|
||||
@@ -6,6 +6,4 @@ public interface VertexInterface {
|
||||
|
||||
public String getLabel() ;
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user