Added IO (maze construction) #2

Merged
regar42 merged 4 commits from IO into master 2017-12-03 17:19:12 +00:00
5 changed files with 114 additions and 20 deletions
Showing only changes of commit 6b92f5d725 - Show all commits

No files matched your search

+6
View File
@@ -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
View File
@@ -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);
}
+102 -14
View File
@@ -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() {
+1 -1
View File
@@ -6,7 +6,7 @@ public class MazeReadingException
public MazeReadingException (String filename) {
super("Invalid maze format :" + filename) ;
super("Invalid maze format : " + filename) ;
this.filename = filename ;
}
-2
View File
@@ -6,6 +6,4 @@ public interface VertexInterface {
public String getLabel() ;
}