diff --git a/AppLaser.java b/AppLaser.java index b37f5c2..4ef30f9 100644 --- a/AppLaser.java +++ b/AppLaser.java @@ -48,11 +48,10 @@ public class AppLaser { else if (start_dir == 12) firstState_j = start_j + 1; else if (start_dir == 13) firstState_j = start_j - 1; - Stack stack = new Stack (); - Universe universe = new Universe(universe_width + 2, universe_height + 2, start_i + 1, start_j + 1, start_dir); - Situation currentState = new Situation(firstState_i + 1, firstState_j + 1, start_dir, 0); - if (cli == true) { + Stack stack = new Stack (); + Universe universe = new Universe(universe_width + 2, universe_height + 2, start_i + 1, start_j + 1, start_dir); + Situation currentState = new Situation(firstState_i + 1, firstState_j + 1, start_dir, 0); Scanner scanner = new Scanner(System.in); int choice; @@ -316,6 +315,7 @@ public class AppLaser { } while (choice != 0); } else { + Universe universe = new Universe(universe_width + 2, universe_height + 2, start_i + 1, start_j + 1, start_dir); UserInterface userInterface = new UserInterface(universe); userInterface.start(); } diff --git a/images/horizontalLaser.png b/images/horizontalLaser.png new file mode 100644 index 0000000..0eb7a42 Binary files /dev/null and b/images/horizontalLaser.png differ diff --git a/images/startBot.png b/images/startBot.png new file mode 100644 index 0000000..743e36c Binary files /dev/null and b/images/startBot.png differ diff --git a/images/startLeft.png b/images/startLeft.png new file mode 100644 index 0000000..a40ccc3 Binary files /dev/null and b/images/startLeft.png differ diff --git a/images/startRight.png b/images/startRight.png new file mode 100644 index 0000000..108a427 Binary files /dev/null and b/images/startRight.png differ diff --git a/images/startUp.png b/images/startUp.png new file mode 100644 index 0000000..a98bafd Binary files /dev/null and b/images/startUp.png differ diff --git a/images/verticalLaser.png b/images/verticalLaser.png new file mode 100644 index 0000000..6e9a01b Binary files /dev/null and b/images/verticalLaser.png differ diff --git a/images/wall.png b/images/wall.png new file mode 100644 index 0000000..6b1825e Binary files /dev/null and b/images/wall.png differ diff --git a/universe/Universe.java b/universe/Universe.java index 4c3bd78..c7bfc2d 100644 --- a/universe/Universe.java +++ b/universe/Universe.java @@ -10,6 +10,7 @@ public class Universe { private int[][] grid; private int width, height; + private int start_i, start_j; private int filled_boxes; private int nb_mirors; private String name; @@ -20,6 +21,8 @@ public class Universe { this.grid = new int[height][width]; this.height = height; this.width = width; + this.start_i = i_start; + this.start_j = j_start; this.filled_boxes = 0; this.nb_mirors = 0; @@ -154,15 +157,11 @@ public class Universe { } public void changeUniverseStart(int pos_i, int pos_j, int dir) { - for (int i = 0; i < this.height; i++) { - for (int j = 0; j < this.width; j++) { - if (this.grid[i][j] == 10 || this.grid[i][j] == 11 || this.grid[i][j] == 12|| this.grid[i][j] == 13) { - this.grid[i][j] = 0; - } - } - } - + this.grid[this.start_i][start_j] = 0; this.grid[pos_i][pos_j] = dir; + + this.start_i = pos_i; + this.start_j = pos_j; } public void addObstacle(int pos_i, int pos_j) { @@ -396,4 +395,11 @@ public class Universe { public int getHeight() { return this.height; } + + public int[] getStartCoords() { + int [] tab = new int[2]; + tab[0] = this.start_i; + tab[1] = this.start_j; + return tab; + } } diff --git a/userInterface/Grid.java b/userInterface/Grid.java index f214ce0..154330e 100644 --- a/userInterface/Grid.java +++ b/userInterface/Grid.java @@ -23,6 +23,8 @@ import javax.imageio.ImageIO; import java.net.URL; import universe.Universe; +import universe.Stack; +import universe.Situation; public class Grid extends JPanel { private int width; @@ -43,43 +45,183 @@ public class Grid extends JPanel { for (int i = 0; i < this.height; i++) { for (int j = 0; j < this.width; j++) { this.mat[i][j] = new JButton(); - switch (this.universe.getGrid()[i + 1][j + 1]) { - case -1: - this.mat[i][j].setBackground(Color.RED); - break; - case 0: - this.mat[i][j].setBackground(Color.WHITE); - break; - - } + this.mat[i][j].setPreferredSize(new Dimension(Math.max(50, 600 / this.height), Math.max(50, (this.width * 600 / this.height) / this.width))); final int coord_i = i; final int coord_j = j; - final int final_selected = this.selected; - final Universe tempUniverse = this.universe; this.mat[i][j].addActionListener(e -> { switch (this.universe.getGrid()[coord_i + 1][coord_j + 1]) { case 0: - this.mat[coord_i][coord_j].setBackground(Color.RED); + if (this.selected == 1) { + this.universe.changeUniverseStart(coord_i + 1, coord_j + 1, 10); + break; + } + this.universe.addObstacle(coord_i + 1, coord_j + 1); break; case -1: - this.mat[coord_i][coord_j].setBackground(Color.WHITE); this.universe.removeObstacle(coord_i + 1, coord_j + 1); break; + case 10: + if (this.selected == 1) { + this.universe.changeUniverseStart(coord_i + 1, coord_j + 1, 11); + break; + } + break; + case 11: + if (this.selected == 1) { + this.universe.changeUniverseStart(coord_i + 1, coord_j + 1, 12); + break; + } + break; + case 12: + if (this.selected == 1) { + this.universe.changeUniverseStart(coord_i + 1, coord_j + 1, 13); + break; + } + break; + case 13: + if (this.selected == 1) { + this.universe.changeUniverseStart(coord_i + 1, coord_j + 1, 10); + break; + } + break; } - //Universe.print(this.universe.getGrid(), this.width + 2, this.height + 2, 4, 4); + this.printUniverse(); }); super.add(this.mat[i][j]); } } + + this.printUniverse(); } public void setSelected(int selected) { this.selected = selected; } + + public void setButtonsEnabled(boolean enabled) { + for (int i = 0; i < this.height; i++) { + for (int j = 0; j < this.width; j ++) { + this.mat[i][j].setEnabled(enabled); + } + } + } + + public void printUniverse() { + for (int i = 0; i < this.height; i++) { + for (int j = 0; j < this.width; j ++) { + Image photo; + switch (this.universe.getGrid()[i + 1][j + 1]) { + case -1: + photo = new ImageIcon(this.getClass().getResource("../images/wall.png")).getImage(); + this.mat[i][j].setIcon(new ImageIcon(photo)); + break; + case 0: + this.mat[i][j].setBackground(Color.WHITE); + this.mat[i][j].setIcon(null); + break; + case 1: + photo = new ImageIcon(this.getClass().getResource("../images/verticalLaser.png")).getImage(); + this.mat[i][j].setIcon(new ImageIcon(photo)); + break; + case 2: + photo = new ImageIcon(this.getClass().getResource("../images/horizontalLaser.png")).getImage(); + this.mat[i][j].setIcon(new ImageIcon(photo)); + break; + case 3: + photo = new ImageIcon(this.getClass().getResource("../images/wall.png")).getImage(); + this.mat[i][j].setIcon(new ImageIcon(photo)); + break; + case 4: + photo = new ImageIcon(this.getClass().getResource("../images/wall.png")).getImage(); + this.mat[i][j].setIcon(new ImageIcon(photo)); + break; + case 5: + photo = new ImageIcon(this.getClass().getResource("../images/wall.png")).getImage(); + this.mat[i][j].setIcon(new ImageIcon(photo)); + break; + case 10: + photo = new ImageIcon(this.getClass().getResource("../images/startUp.png")).getImage(); + this.mat[i][j].setIcon(new ImageIcon(photo)); + break; + case 11: + photo = new ImageIcon(this.getClass().getResource("../images/startBot.png")).getImage(); + this.mat[i][j].setIcon(new ImageIcon(photo)); + break; + case 12: + photo = new ImageIcon(this.getClass().getResource("../images/startRight.png")).getImage(); + this.mat[i][j].setIcon(new ImageIcon(photo)); + break; + case 13: + photo = new ImageIcon(this.getClass().getResource("../images/startLeft.png")).getImage(); + this.mat[i][j].setIcon(new ImageIcon(photo)); + break; + } + } + } + } + + public void reset() { + this.universe.resetUniverse(); + this.printUniverse(); + } + + public void solve() { + this.setButtonsEnabled(false); + this.universe.resetUniverse(); + final Universe universe = this.universe; + + Thread t1 = new Thread(new Runnable() { + @Override + public void run() { + int [] startCoords = universe.getStartCoords(); + + int firstState_i = startCoords[0]; + int firstState_j = startCoords[1]; + int start_dir = universe.getGrid()[startCoords[0]][startCoords[1]]; + + if (start_dir == 10) firstState_i = firstState_i - 1; + else if (start_dir == 11) firstState_i = firstState_i + 1; + else if (start_dir == 12) firstState_j = firstState_j + 1; + else if (start_dir == 13) firstState_j = firstState_j - 1; + + Stack stack = new Stack (); + Situation currentState = new Situation(firstState_i, firstState_j, start_dir, 0); + + int [][] bestGrid = universe.copyGrid(); + int best_filled_boxes = 0; + int best_nb_mirrors = 0; + + do { + if (universe.canEvolve(currentState)) { + stack.push(currentState.copy(universe.possibleChoices(currentState))); + currentState = universe.evolve(currentState); + + if ((universe.getFilledBoxes() > best_filled_boxes) || (universe.getFilledBoxes() == best_filled_boxes && universe.getNbMirrors() < best_nb_mirrors)) { + bestGrid = universe.copyGrid(); + best_filled_boxes = universe.getFilledBoxes(); + best_nb_mirrors = universe.getNbMirrors(); + + printUniverse(); + } + } + else if (stack.size() > 0) { + currentState = stack.pop(); + universe.reset(currentState); + } else { + break; + } + } while (stack.size() != 0); + + } + }); + t1.start(); + + this.setButtonsEnabled(true); + } } \ No newline at end of file diff --git a/userInterface/Window.java b/userInterface/Window.java index 7378677..f3e7708 100644 --- a/userInterface/Window.java +++ b/userInterface/Window.java @@ -67,8 +67,9 @@ public class Window extends JFrame { buttonGroup.add(radioWall); buttonGroup.add(radioStart); - JMenuItem changeSize = new JMenuItem("Change Size"); - JMenuItem solve = new JMenuItem("Solve"); + JMenuItem changeSize = new JMenuItem("Change Size"); + JMenuItem solve = new JMenuItem("Solve"); + JMenuItem reset = new JMenuItem("Reset"); fichierMenu.add(nouveauItem); fichierMenu.add(ouvrirItem); @@ -83,6 +84,8 @@ public class Window extends JFrame { toolsMenu.add(changeSize); toolsMenu.addSeparator(); toolsMenu.add(solve); + toolsMenu.addSeparator(); + toolsMenu.add(reset); menuBar.add(fichierMenu); menuBar.add(aideMenu); @@ -105,11 +108,20 @@ public class Window extends JFrame { }); radioWall.addActionListener(e -> { - this.grid.setSelected(1); + this.grid.setSelected(0); }); radioStart.addActionListener(e -> { - this.grid.setSelected(0); + this.grid.setSelected(1); + }); + + solve.addActionListener(e -> { + this.grid.solve(); + }); + + + reset.addActionListener(e -> { + this.grid.reset(); }); this.grid = new Grid(this.universe.getHeight() - 2, this.universe.getWidth() - 2, this.universe);