diff --git a/src/MainTest.java b/src/MainTest.java index 66cdb93..5cb5c94 100644 --- a/src/MainTest.java +++ b/src/MainTest.java @@ -1,30 +1,8 @@ -import java.util.ArrayList; -import dijkstra.*; -import maze.*; import ui.*; public class MainTest { public static void main(String[] args) { - - Maze mazeTest = new Maze("data/labyrinthe.txt") ; - System.out.println(mazeTest); - mazeTest.saveToTextFile("data/labyOutput.txt"); - VertexInterface departure = mazeTest.getDeparture(); - VertexInterface arrival = mazeTest.getArrival(); - - Previous previous = (Previous) Dijkstra.dijkstra(mazeTest, departure); - - ArrayList shortest = previous.getShortestPathTo(arrival); - - //Print shortest path to arrival - System.out.println(arrival); - for (VertexInterface vertex : shortest) { - System.out.println(vertex); - } - MazeApp app = new MazeApp(); - } - } diff --git a/src/maze/Maze.java b/src/maze/Maze.java index 3290166..45c54b5 100644 --- a/src/maze/Maze.java +++ b/src/maze/Maze.java @@ -43,51 +43,89 @@ public class Maze } public Maze(Box[][] newMaze) { - this.dimensionX = newMaze[0].length; this.dimensionY = newMaze.length; + if (dimensionY > 0) { + this.dimensionX = newMaze[0].length; + } + else { + this.dimensionX = 0; + } this.maze = matrixDeepCopy(newMaze); } - 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++; + private Box[][] matrixDeepCopy(Box[][] original, + Box[][] destination, + int numberOfLines, + int numberOfRows, + int lineOffset, + int rowOffset) + { + if (destination == null) { + destination = new Box[numberOfLines][numberOfRows]; } - if (numberOfLines > 0) { - for (Box box: original[0]) { - numberOfRows++; - } + if (original.length < 1) { + return destination; } - 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++) { + for (int line = lineOffset; line < lineOffset + numberOfLines; line++) { + for (int row = rowOffset; row < rowOffset + numberOfRows; row++) { Box box = original[line][row]; if (box.isWalkable()) { if (box.isDeparture()) { - copy[line][row] = new DBox(box.getLabel(), box.getY(), box.getX(), this); + destination[line][row] = new DBox(box.getLabel(), box.getY(), box.getX(), this); } else if (box.isArrival()) { - copy[line][row] = new ABox(box.getLabel(), box.getY(), box.getX(), this); + destination[line][row] = new ABox(box.getLabel(), box.getY(), box.getX(), this); } else { - copy[line][row] = new EBox(box.getLabel(), box.getY(), box.getX(), this); + destination[line][row] = new EBox(box.getLabel(), box.getY(), box.getX(), this); } } else { - copy[line][row] = new WBox(box.getLabel(), box.getY(), box.getX(), this); + destination[line][row] = new WBox(box.getLabel(), box.getY(), box.getX(), this); } } } - return copy; + return destination; + } + + private Box[][] matrixDeepCopy(Box[][] original, + int numberOfLines, + int numberOfRows, + int lineOffset, + int rowOffset, + int numberOfDestLines, + int numberOfDestRows) { + return matrixDeepCopy(original, + new Box[numberOfDestLines][numberOfDestRows], + numberOfLines, + numberOfRows, + lineOffset, + rowOffset); + } + + private Box[][] matrixDeepCopy(Box [][] original, + int numberOfLines, + int numberOfRows, + int lineOffset, + int rowOffset) { + return matrixDeepCopy(original, + numberOfLines, + numberOfRows, + lineOffset, + rowOffset, + numberOfLines, + numberOfRows); + } + + private Box[][] matrixDeepCopy(Box[][] original, int numberOfLines, int numberOfRows) { + return matrixDeepCopy(original, numberOfLines, numberOfRows, 0, 0); + } + + private Box[][] matrixDeepCopy(Box[][] original) { + if (original.length < 1) { + return null; + } + return matrixDeepCopy(original, original.length, original[0].length); } public static Maze emptyMaze(int dimension) { @@ -130,6 +168,136 @@ public class Maze return new Maze(numberOfLines, numberOfRows, maze); } + public void addEmptyColumn(int posColumn) { + Box[][] newMaze = matrixDeepCopy(maze, + dimensionY, + posColumn, + 0, + 0, + dimensionY, + dimensionX + 1); + for (int line = 0; line < dimensionY; line++) { + newMaze[line][posColumn] = new EBox("E", line, posColumn, this); + } + for (int line = 0; line < dimensionY; line++) { + for (int row = posColumn; row < dimensionX; row++) { + Box box = maze[line][row]; + if (box.isWalkable()) { + if (box.isDeparture()) { + newMaze[line][row + 1] = new DBox(box.getLabel(), box.getY(), box.getX() + 1, this); + } + else if (box.isArrival()) { + newMaze[line][row + 1] = new ABox(box.getLabel(), box.getY(), box.getX() + 1, this); + } + else { + newMaze[line][row + 1] = new EBox(box.getLabel(), box.getY(), box.getX() + 1, this); + } + } + else { + newMaze[line][row + 1] = new WBox(box.getLabel(), box.getY(), box.getX() + 1, this); + } + } + } + maze = newMaze; + dimensionX = dimensionX + 1; + } + + public void delColumn(int posColumn) { + Box[][] newMaze = matrixDeepCopy(maze, + dimensionY, + posColumn, + 0, + 0, + dimensionY, + dimensionX - 1); + for (int line = 0; line < dimensionY; line++) { + for (int row = posColumn + 1; row < dimensionX; row++) { + Box box = maze[line][row]; + if (box.isWalkable()) { + if (box.isDeparture()) { + newMaze[line][row - 1] = new DBox(box.getLabel(), box.getY(), box.getX() - 1, this); + } + else if (box.isArrival()) { + newMaze[line][row - 1] = new ABox(box.getLabel(), box.getY(), box.getX() - 1, this); + } + else { + newMaze[line][row - 1] = new EBox(box.getLabel(), box.getY(), box.getX() - 1, this); + } + } + else { + newMaze[line][row - 1] = new WBox(box.getLabel(), box.getY(), box.getX() - 1, this); + } + } + } + maze = newMaze; + dimensionX = dimensionX - 1; + } + + public void addEmptyLine(int posLine) { + Box[][] newMaze = matrixDeepCopy(maze, + posLine, + dimensionX, + 0, + 0, + dimensionY + 1, + dimensionX); + for (int row = 0; row < dimensionX; row++) { + newMaze[posLine][row] = new EBox("E", posLine, row, this); + } + for (int line = posLine; line < dimensionY; line++) { + for (int row = 0; row < dimensionX; row++) { + Box box = maze[line][row]; + if (box.isWalkable()) { + if (box.isDeparture()) { + newMaze[line + 1][row] = new DBox(box.getLabel(), box.getY() + 1, box.getX(), this); + } + else if (box.isArrival()) { + newMaze[line + 1][row] = new ABox(box.getLabel(), box.getY() + 1, box.getX(), this); + } + else { + newMaze[line + 1][row] = new EBox(box.getLabel(), box.getY() + 1, box.getX(), this); + } + } + else { + newMaze[line + 1][row] = new WBox(box.getLabel(), box.getY() + 1, box.getX(), this); + } + } + } + dimensionY = dimensionY + 1; + maze = newMaze; + } + + public void delLine(int posLine) { + Box[][] newMaze = matrixDeepCopy(maze, + posLine, + dimensionX, + 0, + 0, + dimensionY - 1, + dimensionX); + for (int line = posLine + 1; line < dimensionY; line++) { + for (int row = 0; row < dimensionX; row++) { + Box box = maze[line][row]; + if (box.isWalkable()) { + if (box.isDeparture()) { + newMaze[line - 1][row] = new DBox(box.getLabel(), box.getY() - 1, box.getX(), this); + } + else if (box.isArrival()) { + newMaze[line - 1][row] = new ABox(box.getLabel(), box.getY() - 1, box.getX(), this); + } + else { + newMaze[line - 1][row] = new EBox(box.getLabel(), box.getY() - 1, box.getX(), this); + } + } + else { + newMaze[line - 1][row] = new WBox(box.getLabel(), box.getY() - 1, box.getX(), this); + } + } + } + maze = newMaze; + dimensionY = dimensionY - 1; + } + public int getCost(VertexInterface start, VertexInterface end) { Box startBox = (Box) start ; diff --git a/src/model/MazeAppModel.java b/src/model/MazeAppModel.java index 027064e..e2467a2 100644 --- a/src/model/MazeAppModel.java +++ b/src/model/MazeAppModel.java @@ -134,59 +134,52 @@ public class MazeAppModel extends Observable { public void newClearMaze(int numberOfLines, int numberOfRows) { - Box[][] newClearMaze = new Box[numberOfLines][numberOfRows]; - - for (int line = 0; line < numberOfLines; line++) { - for (int row = 0; row < numberOfRows; row++) { - newClearMaze[line][row] = new EBox("E", line, row, this.maze); - } - } - this.maze = new Maze(newClearMaze); + maze = new Maze(Maze.emptyMaze(numberOfLines, numberOfRows)); initModelFromMaze(); saved = false; } public void addEmptyColumn() { + addEmptyColumn(maze.getDimensionX()); + } - Box[][] previousMaze = maze.getMaze(); - int numberOfLines = maze.getDimensionY(); - int numberOfRows = maze.getDimensionX()+1; - - newClearMaze(numberOfLines, numberOfRows); - - for (int line = 0; line < numberOfLines; line++) { - for (int row = 0; row < numberOfRows; row++) { - if (row == numberOfRows-1) { - maze.getMaze()[line][row] = new EBox("E", line, row, maze); - } else { - maze.getMaze()[line][row] = previousMaze[line][row]; - } - } - } - + public void addEmptyColumn(int posColumn) { + maze.addEmptyColumn(posColumn); initModelFromMaze(); saved = false; } + public void delColumn(int posColumn) { + maze.delColumn(posColumn); + initModelFromMaze(); + saved = false; + } + + public void delColumn() { + delColumn(maze.getDimensionX() - 1); + } + public void addEmptyLine() { + addEmptyLine(maze.getDimensionY()); + } - Box[][] previousMaze = maze.getMaze(); - int numberOfLines = maze.getDimensionY()+1; - int numberOfRows = maze.getDimensionX(); - - newClearMaze(numberOfLines, numberOfRows); - - for (int line = 0; line < numberOfLines; line++) { - for (int row = 0; row < numberOfRows; row++) { - if (line == numberOfLines-1) { - maze.getMaze()[line][row] = new EBox("E", line, row, maze); - } else { - maze.getMaze()[line][row] = previousMaze[line][row]; - } - } - } - + public void addEmptyLine(int posLine) { + maze.addEmptyLine(posLine); initModelFromMaze(); saved = false; } + + public void delLine(int posLine) { + maze.delLine(posLine); + initModelFromMaze(); + saved = false; + } + + public void delLine() { + delLine(maze.getDimensionY() - 1); + } + + public void clearMaze() { + newClearMaze(maze.getDimensionY(), maze.getDimensionX()); + } } diff --git a/src/ui/AddLineMenuItem.java b/src/ui/AddLineMenuItem.java index 2652d37..095760d 100644 --- a/src/ui/AddLineMenuItem.java +++ b/src/ui/AddLineMenuItem.java @@ -9,17 +9,30 @@ public class AddLineMenuItem extends JMenuItem implements ActionListener { private final MazeApp mazeApp; + private final int lineNum; public AddLineMenuItem(MazeApp mazeApp) { super("Add line"); this.mazeApp = mazeApp; + lineNum = -1; + addActionListener(this); + } + + public AddLineMenuItem(MazeApp mazeApp, int lineNum) { + super("Add line"); + this.mazeApp = mazeApp; + this.lineNum = lineNum; addActionListener(this); } @Override - public void actionPerformed(ActionEvent arg0) { - - this.mazeApp.getMazeAppModel().addEmptyLine(); + public void actionPerformed(ActionEvent e) { + if (lineNum == -1) { + mazeApp.addEmptyLine(); + } + else { + mazeApp.addEmptyLine(lineNum); + } } } diff --git a/src/ui/AddRowMenuItem.java b/src/ui/AddRowMenuItem.java index 4eb69e1..b49dfd7 100644 --- a/src/ui/AddRowMenuItem.java +++ b/src/ui/AddRowMenuItem.java @@ -3,25 +3,37 @@ package ui; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; -import javax.swing.Action; -import javax.swing.Icon; import javax.swing.JMenuItem; public class AddRowMenuItem extends JMenuItem implements ActionListener { private final MazeApp mazeApp; + private final int rowNum; public AddRowMenuItem(MazeApp mazeApp) { super("Add row"); this.mazeApp = mazeApp; + rowNum = -1; + addActionListener(this); + } + + public AddRowMenuItem(MazeApp mazeApp, int rowNum) { + + super("Add row"); + this.mazeApp = mazeApp; + this.rowNum = rowNum; addActionListener(this); } @Override - public void actionPerformed(ActionEvent arg0) { - - this.mazeApp.getMazeAppModel().addEmptyColumn(); + public void actionPerformed(ActionEvent e) { + if (rowNum == -1) { + mazeApp.addEmptyColumn(); + } + else { + mazeApp.addEmptyColumn(rowNum); + } } } diff --git a/src/ui/BoxTypeEditionMenu.java b/src/ui/BoxTypeEditionMenu.java index 601fafc..3f07272 100644 --- a/src/ui/BoxTypeEditionMenu.java +++ b/src/ui/BoxTypeEditionMenu.java @@ -9,6 +9,10 @@ public class BoxTypeEditionMenu extends JPopupMenu { JMenuItem setEBox; JMenuItem setDBox; JMenuItem setABox; + JMenuItem addRow; + JMenuItem addLine; + JMenuItem delRow; + JMenuItem delLine; public BoxTypeEditionMenu(MazeApp mazeApp, int boxLine, int boxRow) { add(setEBox = new SetEBoxMenuItem(mazeApp, boxLine, boxRow)); @@ -21,5 +25,10 @@ public class BoxTypeEditionMenu extends JPopupMenu { setWBox.setEnabled(box.isWalkable()); setDBox.setEnabled(! box.isDeparture()); setABox.setEnabled(! box.isArrival()); + + add(addRow = new AddRowMenuItem(mazeApp, boxRow)); + add(addLine = new AddLineMenuItem(mazeApp, boxLine)); + add(delRow = new DelRowMenuItem(mazeApp, boxRow)); + add(delLine = new DelLineMenuItem(mazeApp, boxLine)); } } diff --git a/src/ui/ClearMazeMenuItem.java b/src/ui/ClearMazeMenuItem.java index 4911494..aeed823 100644 --- a/src/ui/ClearMazeMenuItem.java +++ b/src/ui/ClearMazeMenuItem.java @@ -3,8 +3,6 @@ package ui; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; -import javax.swing.Action; -import javax.swing.Icon; import javax.swing.JMenuItem; import javax.swing.KeyStroke; @@ -23,13 +21,6 @@ public class ClearMazeMenuItem extends JMenuItem @Override public void actionPerformed(ActionEvent e) { - - int numberOfLines = mazeApp.getMazeAppModel() - .getMaze().getDimensionY(); - int numberOfRows = mazeApp.getMazeAppModel() - .getMaze().getDimensionX(); - this.mazeApp.getMazeAppModel() - .newClearMaze(numberOfLines, numberOfRows); - System.out.println("test"); + mazeApp.clearMaze(); } } diff --git a/src/ui/DelLineMenuItem.java b/src/ui/DelLineMenuItem.java new file mode 100644 index 0000000..165ac6e --- /dev/null +++ b/src/ui/DelLineMenuItem.java @@ -0,0 +1,38 @@ +package ui; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.JMenuItem; + +public class DelLineMenuItem extends JMenuItem + implements ActionListener { + + private final MazeApp mazeApp; + private final int lineNum; + + public DelLineMenuItem(MazeApp mazeApp) { + + super("Delete line"); + this.mazeApp = mazeApp; + lineNum = -1; + addActionListener(this); + } + + public DelLineMenuItem(MazeApp mazeApp, int lineNum) { + super("Delete line"); + this.mazeApp = mazeApp; + this.lineNum = lineNum; + addActionListener(this); + } + + @Override + public void actionPerformed(ActionEvent e) { + if (lineNum == -1) { + mazeApp.delLine(); + } + else { + mazeApp.delLine(lineNum); + } + } +} diff --git a/src/ui/DelRowMenuItem.java b/src/ui/DelRowMenuItem.java new file mode 100644 index 0000000..243b129 --- /dev/null +++ b/src/ui/DelRowMenuItem.java @@ -0,0 +1,37 @@ +package ui; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.JMenuItem; + +public class DelRowMenuItem extends JMenuItem + implements ActionListener { + + private final MazeApp mazeApp; + private final int rowNum; + + public DelRowMenuItem(MazeApp mazeApp) { + super("Delete row"); + this.mazeApp = mazeApp; + rowNum = -1; + addActionListener(this); + } + + public DelRowMenuItem(MazeApp mazeApp, int rowNum) { + super("Delete row"); + this.mazeApp = mazeApp; + this.rowNum = rowNum; + addActionListener(this); + } + + @Override + public void actionPerformed(ActionEvent e) { + if (rowNum == -1) { + mazeApp.delColumn(); + } + else { + mazeApp.delColumn(rowNum); + } + } +} diff --git a/src/ui/MazeApp.java b/src/ui/MazeApp.java index 48c54c5..0f43f77 100644 --- a/src/ui/MazeApp.java +++ b/src/ui/MazeApp.java @@ -87,4 +87,41 @@ public class MazeApp extends JFrame mazeAppModel.setPathDrawn(false); this.repaint(); } + + public void addEmptyColumn(int posColumn) { + mazeAppModel.addEmptyColumn(posColumn); + } + + public void addEmptyColumn() { + mazeAppModel.addEmptyColumn(); + } + + public void delColumn(int posColumn) { + mazeAppModel.delColumn(posColumn); + } + + public void delColumn() { + mazeAppModel.delColumn(); + } + + public void addEmptyLine(int posLine) { + mazeAppModel.addEmptyLine(posLine); + } + + public void addEmptyLine() { + mazeAppModel.addEmptyLine(); + } + + public void delLine(int posLine) { + mazeAppModel.delLine(posLine); + } + + public void delLine() { + mazeAppModel.delLine(); + } + + public void clearMaze() { + mazeAppModel.clearMaze(); + this.repaint(); + } } diff --git a/src/ui/MazeMenu.java b/src/ui/MazeMenu.java index 91339f1..76d8639 100644 --- a/src/ui/MazeMenu.java +++ b/src/ui/MazeMenu.java @@ -11,6 +11,8 @@ public class MazeMenu extends JMenu { private final ClearMazeMenuItem clearMazeMenuItem; private final AddRowMenuItem addRowMenuItem; private final AddLineMenuItem addLineMenuItem; + private final DelRowMenuItem delRowMenuItem; + private final DelLineMenuItem delLineMenuItem; public MazeMenu(MazeApp mazeApp) { @@ -23,5 +25,7 @@ public class MazeMenu extends JMenu { add(clearMazeMenuItem = new ClearMazeMenuItem(mazeApp)); add(addRowMenuItem = new AddRowMenuItem(mazeApp)); add(addLineMenuItem = new AddLineMenuItem(mazeApp)); + add(delRowMenuItem = new DelRowMenuItem(mazeApp)); + add(delLineMenuItem = new DelLineMenuItem(mazeApp)); } } diff --git a/src/ui/NewMazeSpinnerBox.java b/src/ui/NewMazeSpinnerBox.java index ae43d2a..2e5397c 100644 --- a/src/ui/NewMazeSpinnerBox.java +++ b/src/ui/NewMazeSpinnerBox.java @@ -15,23 +15,17 @@ import javax.swing.JSpinner; import javax.swing.SpinnerModel; import javax.swing.SpinnerNumberModel; import javax.swing.SpringLayout; -import javax.swing.event.ChangeEvent; -import javax.swing.event.ChangeListener; public class NewMazeSpinnerBox extends JPanel - implements ActionListener, - ChangeListener { + implements ActionListener { private final MazeApp mazeApp; private JDialog spinnerDialog; - private int numberOfLines; - private int numberOfRows; private SpinnerModel lineSpinnerModel; private SpinnerModel rowSpinnerModel; public NewMazeSpinnerBox(MazeApp mazeApp) { - this.mazeApp = mazeApp; JPanel spinnerDialog = new JPanel(new SpringLayout()); String[] labels = {"Number of lines :", "Number of rows :"}; @@ -40,25 +34,23 @@ public class NewMazeSpinnerBox extends JPanel int maximumValue = 200; this.lineSpinnerModel = - new SpinnerNumberModel(initialValue, //initial value - initialValue, //min - maximumValue, //max - 1); - this.lineSpinnerModel.addChangeListener(this); + new SpinnerNumberModel(initialValue, //initial value + initialValue, //min + maximumValue, //max + 1); JSpinner lineSpinner = addLabeledSpinner(this, labels[0], lineSpinnerModel); this.rowSpinnerModel = - new SpinnerNumberModel(initialValue, //initial value - initialValue, //min - maximumValue, //max - 1); - this.rowSpinnerModel.addChangeListener(this); - JSpinner rowSpinner = addLabeledSpinner(this, labels[0], rowSpinnerModel); + new SpinnerNumberModel(initialValue, //initial value + initialValue, //min + maximumValue, //max + 1); + JSpinner rowSpinner = addLabeledSpinner(this, labels[1], rowSpinnerModel); } static protected JSpinner addLabeledSpinner(Container c, - String label, - SpinnerModel model) { + String label, + SpinnerModel model) { JLabel l = new JLabel(label); c.add(l); @@ -76,8 +68,8 @@ public class NewMazeSpinnerBox extends JPanel spinnerDialog.setLayout(new BorderLayout()); spinnerDialog.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); - spinnerDialog.add(new NewMazeSpinnerBox(this.mazeApp), - BorderLayout.CENTER); + spinnerDialog.add(this, + BorderLayout.CENTER); JButton OKButton = new JButton("OK"); OKButton.addActionListener(this); @@ -89,13 +81,9 @@ public class NewMazeSpinnerBox extends JPanel @Override public void actionPerformed(ActionEvent e) { + mazeApp.getMazeAppModel( + ).newClearMaze((int) lineSpinnerModel.getValue(), + (int) rowSpinnerModel.getValue()); spinnerDialog.dispose(); } - - @Override - public void stateChanged(ChangeEvent arg0) { - this.mazeApp.getMazeAppModel() - .newClearMaze((int) this.lineSpinnerModel.getValue(), - (int) this.rowSpinnerModel.getValue()); - } }