[WIP] Load maze
This commit is contained in:
1 parent
9aa134eb77
commit
d9b52c35e2
6 files changed
+60
-28
No files matched your search
@@ -1,5 +1,4 @@
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import model.MazeAppModel;
|
|
||||||
import dijkstra.*;
|
import dijkstra.*;
|
||||||
import maze.*;
|
import maze.*;
|
||||||
import ui.*;
|
import ui.*;
|
||||||
|
|||||||
+25
-5
@@ -29,11 +29,31 @@ public class Maze
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// public Maze(Box[][] maze){
|
public Maze(Maze maze){
|
||||||
// //Replacement for setMaze()
|
|
||||||
// //Todo : check if the given array is a proper maze
|
this.dimensionX = maze.getDimensionX();
|
||||||
// //and then set dimensionX and dimensionX
|
this.dimensionY = maze.getDimensionY();
|
||||||
// }
|
this.maze = new Box[dimensionX][dimensionY];
|
||||||
|
for (int line = 0; line < dimensionX; line++) {
|
||||||
|
for (int row = 0; row < dimensionY; row++) {
|
||||||
|
Box box = maze.getMaze()[line][row];
|
||||||
|
if (box.isWalkable()) {
|
||||||
|
if (box.isDeparture()) {
|
||||||
|
this.maze[line][row] = new DBox(box.getLabel(), box.getX(), box.getY(), this);
|
||||||
|
}
|
||||||
|
else if (box.isArrival()) {
|
||||||
|
this.maze[line][row] = new ABox(box.getLabel(), box.getX(), box.getY(), this);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
this.maze[line][row] = new EBox(box.getLabel(), box.getX(), box.getY(), this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
this.maze[line][row] = new WBox(box.getLabel(), box.getX(), box.getY(), this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public int getCost(VertexInterface start, VertexInterface end) {
|
public int getCost(VertexInterface start, VertexInterface end) {
|
||||||
|
|
||||||
|
|||||||
@@ -5,4 +5,8 @@ public class Maze extends maze.Maze {
|
|||||||
public Maze(String filename) {
|
public Maze(String filename) {
|
||||||
super(filename);
|
super(filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Maze(Maze maze) {
|
||||||
|
super(maze);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -17,10 +17,9 @@ public class MazeAppModel extends Observable {
|
|||||||
private ArrayList<VertexInterface> shortest = previous.getShortestPathTo(arrival);
|
private ArrayList<VertexInterface> shortest = previous.getShortestPathTo(arrival);
|
||||||
private ArrayList<VertexInterface> boxList = maze.getVertexes();
|
private ArrayList<VertexInterface> boxList = maze.getVertexes();
|
||||||
private Boolean modified = false;
|
private Boolean modified = false;
|
||||||
private ArrayList<Observer> listObserver = new ArrayList<Observer>();
|
|
||||||
|
|
||||||
public void setMaze(Maze maze) {
|
public void setMaze(Maze maze) {
|
||||||
this.maze = maze;
|
this.maze = new Maze(maze);
|
||||||
modified = true;
|
modified = true;
|
||||||
setChanged();
|
setChanged();
|
||||||
notifyObservers(maze);
|
notifyObservers(maze);
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ public class DrawingPanelGrid extends JPanel {
|
|||||||
private DrawingBox[][] drawingBoxMatrix;
|
private DrawingBox[][] drawingBoxMatrix;
|
||||||
private int dimensionX;
|
private int dimensionX;
|
||||||
private int dimensionY;
|
private int dimensionY;
|
||||||
|
private GridLayout gridLayout;
|
||||||
|
|
||||||
public DrawingPanelGrid(MazeApp mazeApp) {
|
public DrawingPanelGrid(MazeApp mazeApp) {
|
||||||
|
|
||||||
@@ -20,9 +21,10 @@ public class DrawingPanelGrid extends JPanel {
|
|||||||
this.maze = mazeApp.getMazeAppModel().getMaze();
|
this.maze = mazeApp.getMazeAppModel().getMaze();
|
||||||
this.dimensionX = maze.getDimensionX();
|
this.dimensionX = maze.getDimensionX();
|
||||||
this.dimensionY = maze.getDimensionY();
|
this.dimensionY = maze.getDimensionY();
|
||||||
this.drawingBoxMatrix = new DrawingBox[dimensionX][dimensionY];
|
this.drawingBoxMatrix = new DrawingBox[dimensionY][dimensionX];
|
||||||
|
this.gridLayout = new GridLayout(this.dimensionY, this.dimensionX); //inverted coordinates
|
||||||
|
|
||||||
setLayout(new GridLayout(this.dimensionY, this.dimensionX)); //inverted coordinates
|
setLayout(gridLayout);
|
||||||
initFromMaze(maze);
|
initFromMaze(maze);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,23 +55,28 @@ public class DrawingPanelGrid extends JPanel {
|
|||||||
public void notifyForUpdates(Object param) {
|
public void notifyForUpdates(Object param) {
|
||||||
|
|
||||||
if (param instanceof Maze) {
|
if (param instanceof Maze) {
|
||||||
maze = (Maze) param;
|
this.maze = (Maze) param;
|
||||||
|
this.dimensionX = maze.getDimensionX();
|
||||||
|
this.dimensionY = maze.getDimensionY();
|
||||||
|
this.drawingBoxMatrix = new DrawingBox[dimensionY][dimensionX];
|
||||||
|
System.out.println("x : "+dimensionX+" y : "+dimensionY);
|
||||||
|
this.gridLayout = new GridLayout(dimensionY, dimensionX);
|
||||||
|
|
||||||
for (int j = 0; j < dimensionX; j++) {
|
for (int line = 0; line < dimensionX; line++) {
|
||||||
for (int i = 0; i < dimensionY; i++) {
|
for (int row = 0; row < dimensionY; row++) {
|
||||||
if (maze.getMaze()[j][i].isWalkable()) {
|
if (maze.getMaze()[line][row].isWalkable()) {
|
||||||
if (maze.getMaze()[j][i].isDeparture()) {
|
if (maze.getMaze()[line][row].isDeparture()) {
|
||||||
drawingBoxMatrix[j][i].setColor(Color.GREEN);
|
drawingBoxMatrix[row][line].setColor(Color.GREEN);
|
||||||
}
|
}
|
||||||
else if (maze.getMaze()[j][i].isArrival()) {
|
else if (maze.getMaze()[line][row].isArrival()) {
|
||||||
drawingBoxMatrix[j][i].setColor(Color.RED);
|
drawingBoxMatrix[row][line].setColor(Color.RED);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
drawingBoxMatrix[j][i].setColor(Color.BLACK);
|
drawingBoxMatrix[row][line].setColor(Color.BLACK);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
drawingBoxMatrix[j][i].setColor(Color.WHITE);
|
drawingBoxMatrix[row][line].setColor(Color.WHITE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,15 +27,18 @@ public class LoadMazeMenuItem extends JMenuItem
|
|||||||
|
|
||||||
MazeAppModel mazeAppModel = mazeApp.getMazeAppModel();
|
MazeAppModel mazeAppModel = mazeApp.getMazeAppModel();
|
||||||
JFileChooser fileChooser = new JFileChooser();
|
JFileChooser fileChooser = new JFileChooser();
|
||||||
FileNameExtensionFilter filter = new FileNameExtensionFilter(
|
|
||||||
"Text files", "txt");
|
FileNameExtensionFilter filter =
|
||||||
|
new FileNameExtensionFilter("Text files", "txt");
|
||||||
fileChooser.setFileFilter(filter);
|
fileChooser.setFileFilter(filter);
|
||||||
|
|
||||||
int returnVal = fileChooser.showOpenDialog(mazeApp);
|
int returnVal = fileChooser.showOpenDialog(mazeApp);
|
||||||
if(returnVal == JFileChooser.APPROVE_OPTION) {
|
if(returnVal == JFileChooser.APPROVE_OPTION) {
|
||||||
System.out.println("You chose to open this file: " +
|
System.out.println("You chose to open this file: " +
|
||||||
fileChooser.getSelectedFile().getName());
|
fileChooser.getSelectedFile().getName());
|
||||||
}
|
}
|
||||||
String filename = fileChooser.getSelectedFile().getPath();
|
|
||||||
mazeApp.getMazeAppModel().setMaze(new Maze(filename));
|
String filename = fileChooser.getSelectedFile().getPath();
|
||||||
|
mazeApp.getMazeAppModel().setMaze(new Maze(filename));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user