From 6b92f5d725cc4cbd6cdcda3d229838e2568dd645 Mon Sep 17 00:00:00 2001 From: Antonin Godard Date: Fri, 1 Dec 2017 17:25:12 +0100 Subject: [PATCH] [Beug] Added --- Box.java | 6 ++ MainTest.java | 8 ++- Maze.java | 118 +++++++++++++++++++++++++++++++++----- MazeReadingException.java | 2 +- VertexInterface.java | 2 - 5 files changed, 115 insertions(+), 21 deletions(-) diff --git a/Box.java b/Box.java index 3fe8989..f3e84c3 100644 --- a/Box.java +++ b/Box.java @@ -16,6 +16,10 @@ public abstract class Box this.y = y ; this.maze = maze ; + } + + public List 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) { diff --git a/MainTest.java b/MainTest.java index 5e4b3b5..e89952b 100644 --- a/MainTest.java +++ b/MainTest.java @@ -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); + + } diff --git a/Maze.java b/Maze.java index 557f028..372dc57 100644 --- a/Maze.java +++ b/Maze.java @@ -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) ; - - int lineLength = br.readLine().length() - 1 ; + String currentLine = null ; + + 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() { diff --git a/MazeReadingException.java b/MazeReadingException.java index dd80fec..62c6335 100644 --- a/MazeReadingException.java +++ b/MazeReadingException.java @@ -6,7 +6,7 @@ public class MazeReadingException public MazeReadingException (String filename) { - super("Invalid maze format :" + filename) ; + super("Invalid maze format : " + filename) ; this.filename = filename ; } diff --git a/VertexInterface.java b/VertexInterface.java index 6684789..c78bd3f 100644 --- a/VertexInterface.java +++ b/VertexInterface.java @@ -6,6 +6,4 @@ public interface VertexInterface { public String getLabel() ; - - }