diff --git a/ABox.java b/ABox.java index a2eb942..53af453 100644 --- a/ABox.java +++ b/ABox.java @@ -4,6 +4,8 @@ public class ABox extends Box{ public ABox (String label, int x, int y, Maze maze) { super(label,x,y,maze) ; + } + } diff --git a/Box.java b/Box.java index 5f0bf16..3b43d3a 100644 --- a/Box.java +++ b/Box.java @@ -10,12 +10,77 @@ public abstract class Box 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 ; + 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][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]) ; + } } } @@ -25,6 +90,18 @@ public abstract class Box } return false ; } + + public boolean isWalkable() { + return true ; + } + + public List getNeighbourList() { + return neighbourList; + } + + public void setNeighbourList(List neighbourList) { + this.neighbourList = neighbourList; + } public int getX() { return x; diff --git a/EBox.java b/EBox.java index 6ac4434..643b4dd 100644 --- a/EBox.java +++ b/EBox.java @@ -1,11 +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/Maze.java b/Maze.java index 915a472..d131178 100644 --- a/Maze.java +++ b/Maze.java @@ -1,14 +1,13 @@ -import java.util.Iterator; -import java.util.List; +import java.util.ArrayList; public class Maze implements GraphInterface{ - int dimensionX ; - int dimensionY ; - int[][] maze ; + private int dimensionX ; + private int dimensionY ; + private Box[][] maze ; - public Maze(int dimensionX, int dimensionY, int[][] maze) { + public Maze(Box[][] maze) { this.maze = maze ; this.dimensionX = maze.length ; @@ -16,14 +15,37 @@ public class Maze } - public int getCost(Box start, Box end) { + 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 int getDimensionX() { return dimensionX; } @@ -40,15 +62,13 @@ public class Maze this.dimensionY = dimensionY; } - public int[][] getMaze() { + public Box[][] getMaze() { return maze; } - public void setMaze(int[][] maze) { + public void setMaze(Box[][] maze) { this.maze = maze; } - - } diff --git a/VertexInterface.java b/VertexInterface.java index c78bd3f..6684789 100644 --- a/VertexInterface.java +++ b/VertexInterface.java @@ -6,4 +6,6 @@ public interface VertexInterface { public String getLabel() ; + + } diff --git a/WBox.java b/WBox.java index 69bb845..c7759d1 100644 --- a/WBox.java +++ b/WBox.java @@ -1,10 +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 ; + } + }