Open app on empty maze and improve load/save
model: * Add a filename attribute to MazeAppModel to keep track of the file currently being used by the app * Add saveToFile(filename) to save the maze into a specific file * Rewrite saveToFile() to use the stored filename attribute to know into which file is to be used to save the maze, instead of the hardcoded path * Remove unused models * MazeAppModel is now initialized with an empty maze, instead of a maze read from a hardcoded path (see #13) * Add a MazeAppModelMessage class to pass proper messages to observers * Replace setMaze(maze) by loadMaze(filename) for now * Move paintMaze into ui/MazeApp ui: * Add a Save As button (just a new name for the previous Save button) and implement an actual Save button (which saves into the file currently being used), disabled when no file is currently in use. (see #16) * Create the FileBox class to handle file choices for the load, save, save as buttons * Add keystrokes to trigger Load, Save or Save As actions from the menu maze: * Add a method to Maze to perform a deep copy of a matrix of boxes * Add a static method to generate an empty maze of a fixed size (10*10) This behavior might change in the future * Add a constructor to make a Maze from a matrix of boxes
This commit is contained in:
12 files changed
+232
-119
No files matched your search
+49
-9
@@ -29,31 +29,71 @@ public class Maze
|
||||
|
||||
}
|
||||
|
||||
public Maze(Maze maze){
|
||||
public Maze(Maze maze) {
|
||||
|
||||
this.dimensionX = maze.getDimensionX();
|
||||
this.dimensionY = maze.getDimensionY();
|
||||
this.maze = new Box[dimensionY][dimensionX];
|
||||
this.maze = matrixDeepCopy(maze.getMaze());
|
||||
}
|
||||
|
||||
for (int line = 0; line < dimensionY; line++) {
|
||||
for (int row = 0; row < dimensionX; row++) {
|
||||
Box box = maze.getMaze()[line][row];
|
||||
public Maze(int numberOfLines, int numberOfRows, Box[][] boxes) {
|
||||
this.dimensionY = numberOfLines;
|
||||
this.dimensionX = numberOfRows;
|
||||
this.maze = matrixDeepCopy(boxes);
|
||||
}
|
||||
|
||||
private Box[][] matrixDeepCopy (Box[][] original) {
|
||||
int numberOfLines = 0;
|
||||
int numberOfRows = 0;
|
||||
Box[][] copy;
|
||||
/*
|
||||
* We first get the dimensions of the matrix
|
||||
*/
|
||||
for (Box[] lineBoxes: original) {
|
||||
numberOfLines++;
|
||||
}
|
||||
if (numberOfLines > 0) {
|
||||
for (Box box: original[0]) {
|
||||
numberOfRows++;
|
||||
}
|
||||
}
|
||||
copy = new Box[numberOfLines][numberOfRows];
|
||||
|
||||
/*
|
||||
* Now we proceed to the deep copy of each box
|
||||
*/
|
||||
for (int line = 0; line < numberOfLines; line++) {
|
||||
for (int row = 0; row < numberOfRows; row++) {
|
||||
Box box = original[line][row];
|
||||
if (box.isWalkable()) {
|
||||
if (box.isDeparture()) {
|
||||
this.maze[line][row] = new DBox(box.getLabel(), box.getY(), box.getX(), this);
|
||||
copy[line][row] = new DBox(box.getLabel(), box.getY(), box.getX(), this);
|
||||
}
|
||||
else if (box.isArrival()) {
|
||||
this.maze[line][row] = new ABox(box.getLabel(), box.getY(), box.getX(), this);
|
||||
copy[line][row] = new ABox(box.getLabel(), box.getY(), box.getX(), this);
|
||||
}
|
||||
else {
|
||||
this.maze[line][row] = new EBox(box.getLabel(), box.getY(), box.getX(), this);
|
||||
copy[line][row] = new EBox(box.getLabel(), box.getY(), box.getX(), this);
|
||||
}
|
||||
}
|
||||
else {
|
||||
this.maze[line][row] = new WBox(box.getLabel(), box.getY(), box.getX(), this);
|
||||
copy[line][row] = new WBox(box.getLabel(), box.getY(), box.getX(), this);
|
||||
}
|
||||
}
|
||||
}
|
||||
return copy;
|
||||
}
|
||||
|
||||
public static Maze emptySquareMaze() {
|
||||
int dimensionX = 10;
|
||||
int dimensionY = 10;
|
||||
Box[][] maze = new Box[dimensionY][dimensionX];
|
||||
for (int line = 0; line < dimensionY; line++) {
|
||||
for (int row = 0; row < dimensionX; row++) {
|
||||
maze[line][row] = new EBox("E", line, row, null);
|
||||
}
|
||||
}
|
||||
return new Maze(dimensionY, dimensionX, maze);
|
||||
}
|
||||
|
||||
public int getCost(VertexInterface start, VertexInterface end) {
|
||||
|
||||
Reference in new issue
Block a user