From dbf4ea3ab54988a13cbc5da279bb2f7c599c3fa2 Mon Sep 17 00:00:00 2001 From: Antonin Godard Date: Sat, 25 Nov 2017 08:50:22 +0100 Subject: [PATCH 1/4] Added MazeReadingException class --- ASetInterface.java | 12 +++++++++++ Dijkstra.java | 44 +++++++++++++++++++++++++++++++++++++++ MainTest.java | 13 ++++++++++++ Maze.java | 34 ++++++++++++++++++++++++++++++ MazeReadingException.java | 18 ++++++++++++++++ PiInterface.java | 10 +++++++++ PreviousInterface.java | 10 +++++++++ 7 files changed, 141 insertions(+) create mode 100644 ASetInterface.java create mode 100644 Dijkstra.java create mode 100644 MainTest.java create mode 100644 MazeReadingException.java create mode 100644 PiInterface.java create mode 100644 PreviousInterface.java diff --git a/ASetInterface.java b/ASetInterface.java new file mode 100644 index 0000000..8d4c177 --- /dev/null +++ b/ASetInterface.java @@ -0,0 +1,12 @@ + + +public interface ASetInterface { + + /** Add a vertex to the ASet */ + public void add(VertexInterface vertex) ; + + /** Check if a vertex belongs to ASet */ + public boolean contains(VertexInterface vertex) ; + + +} diff --git a/Dijkstra.java b/Dijkstra.java new file mode 100644 index 0000000..34ab762 --- /dev/null +++ b/Dijkstra.java @@ -0,0 +1,44 @@ +import java.util.ArrayList; + +public class Dijkstra { + + private PreviousInterface dijkstra(GraphInterface g, VertexInterface r, ASetInterface a, PiInterface pi, PreviousInterface previous) { + + a.add(r) ; ; + VertexInterface pivot = r ; + pi.setPi(0, r) ; + + ArrayList vertexList = g.getVertexes() ; + + for (VertexInterface vertex: vertexList) { + if (vertex != r) { + pi.setPi(Integer.MAX_VALUE, vertex); + } + } + + int n = g.getVertexNumber(); + for (int j = 0; j < n; j++) { + for (VertexInterface vertex: vertexList) { + if (! a.contains(vertex) && previous.getPrevious(vertex).compareTo(pivot)) { + if (pi.getPi(pivot) + g.getCost(pivot, vertex) < pi.getPi(vertex)) { + pi.setPi(pi.getPi(pivot) + g.getCost(pivot, vertex), vertex) ; + previous.setPrevious(pivot, vertex); + } + } + } + + int piMax = Integer.MAX_VALUE ; + for (VertexInterface vertex: vertexList) { + if (! a.contains(vertex) && pi.getPi(vertex) < piMax) { + pivot = vertex ; + piMax = pi.getPi(vertex) ; + } + } + a.add(pivot); + } + + return previous ; + + } + +} diff --git a/MainTest.java b/MainTest.java new file mode 100644 index 0000000..5e4b3b5 --- /dev/null +++ b/MainTest.java @@ -0,0 +1,13 @@ + +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"); + + } + +} diff --git a/Maze.java b/Maze.java index d131178..557f028 100644 --- a/Maze.java +++ b/Maze.java @@ -1,3 +1,5 @@ +import java.io.BufferedReader; +import java.io.FileReader; import java.util.ArrayList; public class Maze @@ -46,6 +48,38 @@ public class Maze return vertexList ; } + public final void initFromTextFile(String filename) { + + FileReader fr = null ; + BufferedReader br = null ; + + try { + fr = new FileReader(filename) ; + br = new BufferedReader(fr) ; + + + int lineLength = br.readLine().length() - 1 ; + int currentLineLength ; + int cInt = br.read() ; + + while (cInt != -1) + { + currentLineLength = br.readLine().length() ; + if (currentLineLength != lineLength) { + throw new MazeReadingException(filename) ; + } + cInt = br.read() ; + } + } catch (MazeReadingException mre) { + mre.printStackTrace(); + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { br.close() ;} catch (Exception e) {} + } + + } + public int getDimensionX() { return dimensionX; } diff --git a/MazeReadingException.java b/MazeReadingException.java new file mode 100644 index 0000000..dd80fec --- /dev/null +++ b/MazeReadingException.java @@ -0,0 +1,18 @@ + +public class MazeReadingException + extends Exception { + + private String filename ; + + public MazeReadingException (String filename) { + + super("Invalid maze format :" + filename) ; + this.filename = filename ; + + } + + public String getFilename() { + return filename ; + } + +} diff --git a/PiInterface.java b/PiInterface.java new file mode 100644 index 0000000..f1a4ac0 --- /dev/null +++ b/PiInterface.java @@ -0,0 +1,10 @@ + +public interface PiInterface { + + /** Returns the value of pi(v) */ + public int getPi(VertexInterface vertex) ; + + /** Sets the Pi value of an element */ + public void setPi(int i, VertexInterface vertex) ; + +} diff --git a/PreviousInterface.java b/PreviousInterface.java new file mode 100644 index 0000000..de81746 --- /dev/null +++ b/PreviousInterface.java @@ -0,0 +1,10 @@ + +public interface PreviousInterface { + + /** Returns the father of an element */ + public VertexInterface getPrevious(VertexInterface vertex) ; + + /** Sets the father of an element to another element */ + public void setPrevious(VertexInterface father, VertexInterface son) ; + +} From 6b92f5d725cc4cbd6cdcda3d229838e2568dd645 Mon Sep 17 00:00:00 2001 From: Antonin Godard Date: Fri, 1 Dec 2017 17:25:12 +0100 Subject: [PATCH 2/4] [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() ; - - } From 648dfc6b83c5a02f1bd668244ed2ecc45357b0b1 Mon Sep 17 00:00:00 2001 From: Antonin Godard Date: Sun, 3 Dec 2017 16:29:21 +0100 Subject: [PATCH 3/4] Added two separate folders for the src and the data. Added construction of maze from labyrinthe.txt. Added neighbourList generation for each box in the maze. Added writing into file the constructed maze. --- data/labyOutput.txt | 8 ++ data/labyrinthe.txt | 8 ++ src/ABox.java | 15 +++ src/ASetInterface.java | 12 +++ src/Box.java | 107 +++++++++++++++++++ src/DBox.java | 15 +++ src/Dijkstra.java | 44 ++++++++ src/EBox.java | 11 ++ src/GraphInterface.java | 14 +++ src/MainTest.java | 15 +++ src/Maze.java | 188 ++++++++++++++++++++++++++++++++++ src/MazeReadingException.java | 18 ++++ src/PiInterface.java | 10 ++ src/PreviousInterface.java | 10 ++ src/VertexInterface.java | 9 ++ src/WBox.java | 18 ++++ 16 files changed, 502 insertions(+) create mode 100644 data/labyOutput.txt create mode 100644 data/labyrinthe.txt create mode 100644 src/ABox.java create mode 100644 src/ASetInterface.java create mode 100644 src/Box.java create mode 100644 src/DBox.java create mode 100644 src/Dijkstra.java create mode 100644 src/EBox.java create mode 100644 src/GraphInterface.java create mode 100644 src/MainTest.java create mode 100644 src/Maze.java create mode 100644 src/MazeReadingException.java create mode 100644 src/PiInterface.java create mode 100644 src/PreviousInterface.java create mode 100644 src/VertexInterface.java create mode 100644 src/WBox.java diff --git a/data/labyOutput.txt b/data/labyOutput.txt new file mode 100644 index 0000000..c27fe74 --- /dev/null +++ b/data/labyOutput.txt @@ -0,0 +1,8 @@ +WEEEEEWW +EDWWWEEW +WEEWWWEE +WWEEEEEW +EEEWWWWW +EWWWWAWW +EWWEEEWW +EEEEWWWW diff --git a/data/labyrinthe.txt b/data/labyrinthe.txt new file mode 100644 index 0000000..6e7752a --- /dev/null +++ b/data/labyrinthe.txt @@ -0,0 +1,8 @@ +WEEEEEWW +EDWWWEEW +WEEWWWEE +WWEEEEEW +EEEWWWWW +EWWWWAWW +EWWEEEWW +EEEEWWWW \ No newline at end of file diff --git a/src/ABox.java b/src/ABox.java new file mode 100644 index 0000000..51e1186 --- /dev/null +++ b/src/ABox.java @@ -0,0 +1,15 @@ + +public class ABox extends Box{ + + public ABox (String label, int x, int y, Maze maze) { + + super(label,x,y,maze) ; + + } + + @Override + public boolean isArrival() { + return true ; + } + +} diff --git a/src/ASetInterface.java b/src/ASetInterface.java new file mode 100644 index 0000000..8d4c177 --- /dev/null +++ b/src/ASetInterface.java @@ -0,0 +1,12 @@ + + +public interface ASetInterface { + + /** Add a vertex to the ASet */ + public void add(VertexInterface vertex) ; + + /** Check if a vertex belongs to ASet */ + public boolean contains(VertexInterface vertex) ; + + +} diff --git a/src/Box.java b/src/Box.java new file mode 100644 index 0000000..940bfde --- /dev/null +++ b/src/Box.java @@ -0,0 +1,107 @@ +import java.util.ArrayList; +import java.util.List; + +public abstract class Box + implements VertexInterface { + + private int x ; + private int y ; + private String label ; + private Maze maze ; + private List neighbourList = new ArrayList() ; + + public Box (String label, int x, int y, Maze maze) { + + this.label = label ; + this.x = x ; + this.y = y ; + this.maze = maze ; + + } + + public List generateNeighbors() { + + Box[][] vMaze = maze.getMaze() ; + + if (x > 0 && vMaze[x-1][y].isWalkable()) { + neighbourList.add(vMaze[x-1][y]) ; + } + + if (x < maze.getDimensionX()-1 && vMaze[x+1][y].isWalkable()) { + neighbourList.add(vMaze[x+1][y]) ; + } + + if (y > 0 && vMaze[x][y-1].isWalkable()) { + neighbourList.add(vMaze[x][y-1]) ; + } + + if (y < maze.getDimensionY()-1 && vMaze[x][y+1].isWalkable()) { + neighbourList.add(vMaze[x][y+1]) ; + } + + return neighbourList ; + + } + + public boolean compareTo(VertexInterface vertex) { + if (this.getLabel() == vertex.getLabel()) { + return true ; + } + return false ; + } + + public boolean isWalkable() { + return true ; + } + + public boolean isArrival() { + return false ; + } + + public boolean isDeparture() { + return false ; + } + + public List getNeighbourList() { + return neighbourList; + } + + public void setNeighbourList(List neighbourList) { + this.neighbourList = neighbourList; + } + + public int getX() { + return x; + } + + public void setX(int x) { + this.x = x; + } + + public int getY() { + return y; + } + + public void setY(int y) { + this.y = y; + } + + public String getLabel() { + return label; + } + + public void setLabel(String label) { + this.label = label; + } + + public Maze getMaze() { + return maze; + } + + public void setMaze(Maze maze) { + this.maze = maze; + } + + + +} diff --git a/src/DBox.java b/src/DBox.java new file mode 100644 index 0000000..757d2e4 --- /dev/null +++ b/src/DBox.java @@ -0,0 +1,15 @@ + +public class DBox extends Box{ + + public DBox (String label, int x, int y, Maze maze) { + + super(label,x,y,maze) ; + + } + + @Override + public boolean isDeparture() { + return true ; + } + +} diff --git a/src/Dijkstra.java b/src/Dijkstra.java new file mode 100644 index 0000000..34ab762 --- /dev/null +++ b/src/Dijkstra.java @@ -0,0 +1,44 @@ +import java.util.ArrayList; + +public class Dijkstra { + + private PreviousInterface dijkstra(GraphInterface g, VertexInterface r, ASetInterface a, PiInterface pi, PreviousInterface previous) { + + a.add(r) ; ; + VertexInterface pivot = r ; + pi.setPi(0, r) ; + + ArrayList vertexList = g.getVertexes() ; + + for (VertexInterface vertex: vertexList) { + if (vertex != r) { + pi.setPi(Integer.MAX_VALUE, vertex); + } + } + + int n = g.getVertexNumber(); + for (int j = 0; j < n; j++) { + for (VertexInterface vertex: vertexList) { + if (! a.contains(vertex) && previous.getPrevious(vertex).compareTo(pivot)) { + if (pi.getPi(pivot) + g.getCost(pivot, vertex) < pi.getPi(vertex)) { + pi.setPi(pi.getPi(pivot) + g.getCost(pivot, vertex), vertex) ; + previous.setPrevious(pivot, vertex); + } + } + } + + int piMax = Integer.MAX_VALUE ; + for (VertexInterface vertex: vertexList) { + if (! a.contains(vertex) && pi.getPi(vertex) < piMax) { + pivot = vertex ; + piMax = pi.getPi(vertex) ; + } + } + a.add(pivot); + } + + return previous ; + + } + +} diff --git a/src/EBox.java b/src/EBox.java new file mode 100644 index 0000000..643b4dd --- /dev/null +++ b/src/EBox.java @@ -0,0 +1,11 @@ + +public class EBox extends Box{ + + + public EBox (String label, int x, int y, Maze maze) { + + super(label,x,y,maze) ; + + } + +} diff --git a/src/GraphInterface.java b/src/GraphInterface.java new file mode 100644 index 0000000..0841a8d --- /dev/null +++ b/src/GraphInterface.java @@ -0,0 +1,14 @@ +import java.util.ArrayList; + +public interface GraphInterface { + + /** Returns the number of Vertexes */ + public int getVertexNumber() ; + + /** Return the cost between two vertexes */ + public int getCost(VertexInterface start, VertexInterface end) ; + + /** Returns an list of Vertexes */ + public ArrayList getVertexes() ; + +} diff --git a/src/MainTest.java b/src/MainTest.java new file mode 100644 index 0000000..e22c50a --- /dev/null +++ b/src/MainTest.java @@ -0,0 +1,15 @@ + +public class MainTest { + + public static void main(String[] args) { + // TODO Auto-generated method stub + + Maze mazeTest = new Maze("data/labyrinthe.txt") ; + //mazeTest.saveToTextFile("data/out.txt"); + System.out.println("\n" + mazeTest); + + mazeTest.saveToTextFile("data/labyOutput.txt"); + + } + +} diff --git a/src/Maze.java b/src/Maze.java new file mode 100644 index 0000000..91897a2 --- /dev/null +++ b/src/Maze.java @@ -0,0 +1,188 @@ +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 { + + private int dimensionX ; + private int dimensionY ; + private Box[][] maze ; + + public Maze(String filename) { + + initFromTextFile(filename) ; + + } + + public int getCost(VertexInterface start, VertexInterface end) { + + Box startBox = (Box) start ; + Box endBox = (Box) end ; + + if (startBox.getNeighbourList().contains(endBox)) { + if (startBox.isWalkable() & endBox.isWalkable()) { + return 1 ; + } + else { + return 0 ; + } + } + + return 0 ; + } + + public int getVertexNumber() { + return dimensionX*dimensionY ; + } + + public ArrayList getVertexes() { + ArrayList vertexList = new ArrayList() ; + for (int i = 0 ; i <= dimensionX ; i++) { + for (int j = 0 ; j <= dimensionY ; j++) { + vertexList.add(maze[i][j]) ; + } + } + return vertexList ; + } + + public final void initFromTextFile(String filename) { + + FileReader fr = null ; + BufferedReader br = null ; + + 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 ; + + currentLine = br.readLine() ; + System.out.println(currentLine); + dimensionX = currentLine.length() ; //maze's width + System.out.println("Line length :" + dimensionX); + int currentLineLength ; + int compteurLigne = 0 ; + + this.maze = new Box[dimensionX][dimensionY] ; + + while (currentLine != null) + { + + //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) ; + } + + currentLine = br.readLine() ; + compteurLigne++ ; + } + } catch (MazeReadingException mre) { + mre.printStackTrace(); + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { br.close() ;} catch (Exception e) {} + } + + //neighbour generation for each box in the maze + for (int i = 0 ; i < dimensionX ; i++) { + for (int j = 0 ; j < dimensionY ; j++) { + 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() { + return dimensionX; + } + + public void setDimensionX(int dimensionX) { + this.dimensionX = dimensionX; + } + + public int getDimensionY() { + return dimensionY; + } + + public void setDimensionY(int dimensionY) { + this.dimensionY = dimensionY; + } + + public Box[][] getMaze() { + return maze; + } + + public void setMaze(Box[][] maze) { + this.maze = maze; + } + + +} diff --git a/src/MazeReadingException.java b/src/MazeReadingException.java new file mode 100644 index 0000000..62c6335 --- /dev/null +++ b/src/MazeReadingException.java @@ -0,0 +1,18 @@ + +public class MazeReadingException + extends Exception { + + private String filename ; + + public MazeReadingException (String filename) { + + super("Invalid maze format : " + filename) ; + this.filename = filename ; + + } + + public String getFilename() { + return filename ; + } + +} diff --git a/src/PiInterface.java b/src/PiInterface.java new file mode 100644 index 0000000..f1a4ac0 --- /dev/null +++ b/src/PiInterface.java @@ -0,0 +1,10 @@ + +public interface PiInterface { + + /** Returns the value of pi(v) */ + public int getPi(VertexInterface vertex) ; + + /** Sets the Pi value of an element */ + public void setPi(int i, VertexInterface vertex) ; + +} diff --git a/src/PreviousInterface.java b/src/PreviousInterface.java new file mode 100644 index 0000000..de81746 --- /dev/null +++ b/src/PreviousInterface.java @@ -0,0 +1,10 @@ + +public interface PreviousInterface { + + /** Returns the father of an element */ + public VertexInterface getPrevious(VertexInterface vertex) ; + + /** Sets the father of an element to another element */ + public void setPrevious(VertexInterface father, VertexInterface son) ; + +} diff --git a/src/VertexInterface.java b/src/VertexInterface.java new file mode 100644 index 0000000..c78bd3f --- /dev/null +++ b/src/VertexInterface.java @@ -0,0 +1,9 @@ + +public interface VertexInterface { + + /** Compare a Vertex to another */ + public boolean compareTo(VertexInterface vertex) ; + + public String getLabel() ; + +} diff --git a/src/WBox.java b/src/WBox.java new file mode 100644 index 0000000..c7759d1 --- /dev/null +++ b/src/WBox.java @@ -0,0 +1,18 @@ + +public class WBox extends Box { + + + + public WBox (String label, int x, int y, Maze maze) { + + super(label,x,y,maze) ; + + + } + + @Override + public boolean isWalkable() { + return false ; + } + +} From e2be5f8da9b5590427e7f9e1d97a64d616c59a94 Mon Sep 17 00:00:00 2001 From: Antonin Godard Date: Sun, 3 Dec 2017 16:37:30 +0100 Subject: [PATCH 4/4] Deleted original files --- ABox.java | 15 --- ASetInterface.java | 12 --- Box.java | 154 ------------------------------ DBox.java | 15 --- Dijkstra.java | 44 --------- EBox.java | 11 --- GraphInterface.java | 14 --- MainTest.java | 15 --- Maze.java | 196 -------------------------------------- MazeReadingException.java | 18 ---- PiInterface.java | 10 -- PreviousInterface.java | 10 -- VertexInterface.java | 9 -- WBox.java | 18 ---- 14 files changed, 541 deletions(-) delete mode 100644 ABox.java delete mode 100644 ASetInterface.java delete mode 100644 Box.java delete mode 100644 DBox.java delete mode 100644 Dijkstra.java delete mode 100644 EBox.java delete mode 100644 GraphInterface.java delete mode 100644 MainTest.java delete mode 100644 Maze.java delete mode 100644 MazeReadingException.java delete mode 100644 PiInterface.java delete mode 100644 PreviousInterface.java delete mode 100644 VertexInterface.java delete mode 100644 WBox.java diff --git a/ABox.java b/ABox.java deleted file mode 100644 index 51e1186..0000000 --- a/ABox.java +++ /dev/null @@ -1,15 +0,0 @@ - -public class ABox extends Box{ - - public ABox (String label, int x, int y, Maze maze) { - - super(label,x,y,maze) ; - - } - - @Override - public boolean isArrival() { - return true ; - } - -} diff --git a/ASetInterface.java b/ASetInterface.java deleted file mode 100644 index 8d4c177..0000000 --- a/ASetInterface.java +++ /dev/null @@ -1,12 +0,0 @@ - - -public interface ASetInterface { - - /** Add a vertex to the ASet */ - public void add(VertexInterface vertex) ; - - /** Check if a vertex belongs to ASet */ - public boolean contains(VertexInterface vertex) ; - - -} diff --git a/Box.java b/Box.java deleted file mode 100644 index f3e84c3..0000000 --- a/Box.java +++ /dev/null @@ -1,154 +0,0 @@ -import java.util.List; - -public abstract class Box - implements VertexInterface { - - private int x ; - private int y ; - private String label ; - private Maze maze ; - private List neighbourList ; - - public Box (String label, int x, int y, Maze maze) { - - this.label = label ; - this.x = x ; - 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]) ; - this.neighbourList.add(maze.getMaze()[x][y+1]) ; - this.neighbourList.add(maze.getMaze()[x][y-1]) ; - } - - if (x == 0) { - this.neighbourList.add(maze.getMaze()[x+1][y]) ; - - if (y == maze.getDimensionY()) { - this.neighbourList.add(maze.getMaze()[x][y-1]) ; - } - else if (y == 0) { - this.neighbourList.add(maze.getMaze()[x][y+1]) ; - } - else { - this.neighbourList.add(maze.getMaze()[x][y-1]) ; - this.neighbourList.add(maze.getMaze()[x][y+1]) ; - } - } - - if (y == 0) { - this.neighbourList.add(maze.getMaze()[x][y+1]) ; - - if (x == maze.getDimensionX()) { - this.neighbourList.add(maze.getMaze()[x-1][y]) ; - } - else if (x == 0) { - this.neighbourList.add(maze.getMaze()[x+1][y]) ; - } - else { - this.neighbourList.add(maze.getMaze()[x-1][y]) ; - this.neighbourList.add(maze.getMaze()[x+1][y]) ; - } - } - - if (y == maze.getDimensionY()) { - this.neighbourList.add(maze.getMaze()[x][y-1]) ; - - if (x == maze.getDimensionX()) { - this.neighbourList.add(maze.getMaze()[x-1][y]) ; - } - else if (x == 0) { - this.neighbourList.add(maze.getMaze()[x+1][y]) ; - } - else { - this.neighbourList.add(maze.getMaze()[x-1][y]) ; - this.neighbourList.add(maze.getMaze()[x+1][y]) ; - } - } - - if (x == maze.getDimensionX()) { - this.neighbourList.add(maze.getMaze()[x-1][y]) ; - - if (y == maze.getDimensionY()) { - this.neighbourList.add(maze.getMaze()[x][y-1]) ; - } - else if (y == 0) { - this.neighbourList.add(maze.getMaze()[x][y+1]) ; - } - else { - this.neighbourList.add(maze.getMaze()[x][y-1]) ; - this.neighbourList.add(maze.getMaze()[x][y+1]) ; - } - } - - return neighbourList ; - } - - public boolean compareTo(VertexInterface vertex) { - if (this.getLabel() == vertex.getLabel()) { - return true ; - } - return false ; - } - - public boolean isWalkable() { - return true ; - } - - public boolean isArrival() { - return false ; - } - - public boolean isDeparture() { - return false ; - } - - public List getNeighbourList() { - return neighbourList; - } - - public void setNeighbourList(List neighbourList) { - this.neighbourList = neighbourList; - } - - public int getX() { - return x; - } - - public void setX(int x) { - this.x = x; - } - - public int getY() { - return y; - } - - public void setY(int y) { - this.y = y; - } - - public String getLabel() { - return label; - } - - public void setLabel(String label) { - this.label = label; - } - - public Maze getMaze() { - return maze; - } - - public void setMaze(Maze maze) { - this.maze = maze; - } - - - -} diff --git a/DBox.java b/DBox.java deleted file mode 100644 index 757d2e4..0000000 --- a/DBox.java +++ /dev/null @@ -1,15 +0,0 @@ - -public class DBox extends Box{ - - public DBox (String label, int x, int y, Maze maze) { - - super(label,x,y,maze) ; - - } - - @Override - public boolean isDeparture() { - return true ; - } - -} diff --git a/Dijkstra.java b/Dijkstra.java deleted file mode 100644 index 34ab762..0000000 --- a/Dijkstra.java +++ /dev/null @@ -1,44 +0,0 @@ -import java.util.ArrayList; - -public class Dijkstra { - - private PreviousInterface dijkstra(GraphInterface g, VertexInterface r, ASetInterface a, PiInterface pi, PreviousInterface previous) { - - a.add(r) ; ; - VertexInterface pivot = r ; - pi.setPi(0, r) ; - - ArrayList vertexList = g.getVertexes() ; - - for (VertexInterface vertex: vertexList) { - if (vertex != r) { - pi.setPi(Integer.MAX_VALUE, vertex); - } - } - - int n = g.getVertexNumber(); - for (int j = 0; j < n; j++) { - for (VertexInterface vertex: vertexList) { - if (! a.contains(vertex) && previous.getPrevious(vertex).compareTo(pivot)) { - if (pi.getPi(pivot) + g.getCost(pivot, vertex) < pi.getPi(vertex)) { - pi.setPi(pi.getPi(pivot) + g.getCost(pivot, vertex), vertex) ; - previous.setPrevious(pivot, vertex); - } - } - } - - int piMax = Integer.MAX_VALUE ; - for (VertexInterface vertex: vertexList) { - if (! a.contains(vertex) && pi.getPi(vertex) < piMax) { - pivot = vertex ; - piMax = pi.getPi(vertex) ; - } - } - a.add(pivot); - } - - return previous ; - - } - -} diff --git a/EBox.java b/EBox.java deleted file mode 100644 index 643b4dd..0000000 --- a/EBox.java +++ /dev/null @@ -1,11 +0,0 @@ - -public class EBox extends Box{ - - - public EBox (String label, int x, int y, Maze maze) { - - super(label,x,y,maze) ; - - } - -} diff --git a/GraphInterface.java b/GraphInterface.java deleted file mode 100644 index 0841a8d..0000000 --- a/GraphInterface.java +++ /dev/null @@ -1,14 +0,0 @@ -import java.util.ArrayList; - -public interface GraphInterface { - - /** Returns the number of Vertexes */ - public int getVertexNumber() ; - - /** Return the cost between two vertexes */ - public int getCost(VertexInterface start, VertexInterface end) ; - - /** Returns an list of Vertexes */ - public ArrayList getVertexes() ; - -} diff --git a/MainTest.java b/MainTest.java deleted file mode 100644 index e89952b..0000000 --- a/MainTest.java +++ /dev/null @@ -1,15 +0,0 @@ - -public class MainTest { - - public static void main(String[] args) { - // TODO Auto-generated method stub - - 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 deleted file mode 100644 index 372dc57..0000000 --- a/Maze.java +++ /dev/null @@ -1,196 +0,0 @@ -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 { - - private int dimensionX ; - private int dimensionY ; - private Box[][] maze ; - - public Maze(String filename) { - - initFromTextFile(filename) ; - - } - - public int getCost(VertexInterface start, VertexInterface end) { - - Box startBox = (Box) start ; - Box endBox = (Box) end ; - - if (startBox.getNeighbourList().contains(endBox)) { - if (startBox.isWalkable() & endBox.isWalkable()) { - return 1 ; - } - else { - return 0 ; - } - } - - return 0 ; - } - - public int getVertexNumber() { - return dimensionX*dimensionY ; - } - - public ArrayList getVertexes() { - ArrayList vertexList = new ArrayList() ; - for (int i = 0 ; i <= dimensionX ; i++) { - for (int j = 0 ; j <= dimensionY ; j++) { - vertexList.add(maze[i][j]) ; - } - } - return vertexList ; - } - - public final void initFromTextFile(String filename) { - - FileReader fr = null ; - BufferedReader br = null ; - - 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 ; - - currentLine = br.readLine() ; - System.out.println(currentLine); - dimensionX = currentLine.length() ; //maze's width - System.out.println("Line length :" + dimensionX); - int currentLineLength ; - int compteurLigne = 0 ; - - this.maze = new Box[dimensionX][dimensionY] ; - - while (currentLine != null) - { - - //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) ; - } - - currentLine = br.readLine() ; - compteurLigne++ ; - } - } catch (MazeReadingException mre) { - mre.printStackTrace(); - } catch (Exception e) { - e.printStackTrace(); - } finally { - 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() { - return dimensionX; - } - - public void setDimensionX(int dimensionX) { - this.dimensionX = dimensionX; - } - - public int getDimensionY() { - return dimensionY; - } - - public void setDimensionY(int dimensionY) { - this.dimensionY = dimensionY; - } - - public Box[][] getMaze() { - return maze; - } - - public void setMaze(Box[][] maze) { - this.maze = maze; - } - - -} diff --git a/MazeReadingException.java b/MazeReadingException.java deleted file mode 100644 index 62c6335..0000000 --- a/MazeReadingException.java +++ /dev/null @@ -1,18 +0,0 @@ - -public class MazeReadingException - extends Exception { - - private String filename ; - - public MazeReadingException (String filename) { - - super("Invalid maze format : " + filename) ; - this.filename = filename ; - - } - - public String getFilename() { - return filename ; - } - -} diff --git a/PiInterface.java b/PiInterface.java deleted file mode 100644 index f1a4ac0..0000000 --- a/PiInterface.java +++ /dev/null @@ -1,10 +0,0 @@ - -public interface PiInterface { - - /** Returns the value of pi(v) */ - public int getPi(VertexInterface vertex) ; - - /** Sets the Pi value of an element */ - public void setPi(int i, VertexInterface vertex) ; - -} diff --git a/PreviousInterface.java b/PreviousInterface.java deleted file mode 100644 index de81746..0000000 --- a/PreviousInterface.java +++ /dev/null @@ -1,10 +0,0 @@ - -public interface PreviousInterface { - - /** Returns the father of an element */ - public VertexInterface getPrevious(VertexInterface vertex) ; - - /** Sets the father of an element to another element */ - public void setPrevious(VertexInterface father, VertexInterface son) ; - -} diff --git a/VertexInterface.java b/VertexInterface.java deleted file mode 100644 index c78bd3f..0000000 --- a/VertexInterface.java +++ /dev/null @@ -1,9 +0,0 @@ - -public interface VertexInterface { - - /** Compare a Vertex to another */ - public boolean compareTo(VertexInterface vertex) ; - - public String getLabel() ; - -} diff --git a/WBox.java b/WBox.java deleted file mode 100644 index c7759d1..0000000 --- a/WBox.java +++ /dev/null @@ -1,18 +0,0 @@ - -public class WBox extends Box { - - - - public WBox (String label, int x, int y, Maze maze) { - - super(label,x,y,maze) ; - - - } - - @Override - public boolean isWalkable() { - return false ; - } - -}