diff --git a/images/crossLaser.png b/images/crossLaser.png new file mode 100644 index 0000000..7a873b0 Binary files /dev/null and b/images/crossLaser.png differ diff --git a/images/miror1.png b/images/miror1.png new file mode 100644 index 0000000..971e86a Binary files /dev/null and b/images/miror1.png differ diff --git a/images/miror2.png b/images/miror2.png new file mode 100644 index 0000000..2297314 Binary files /dev/null and b/images/miror2.png differ diff --git a/images/miror3.png b/images/miror3.png new file mode 100644 index 0000000..7ee3d2c Binary files /dev/null and b/images/miror3.png differ diff --git a/images/miror4.png b/images/miror4.png new file mode 100644 index 0000000..f8eaf63 Binary files /dev/null and b/images/miror4.png differ diff --git a/universe/Universe.java b/universe/Universe.java index c7bfc2d..d5fc66f 100644 --- a/universe/Universe.java +++ b/universe/Universe.java @@ -79,6 +79,14 @@ public class Universe { break; case 5: + System.out.printf(" /"); + break; + + case 6: + System.out.printf(" \\"); + break; + + case 7: System.out.printf(" \\"); break; @@ -295,16 +303,26 @@ public class Universe { this.grid[i][j] = 3; } } - if ((c == 3 && d == 10) || (c == 3 && d == 11) || (c == 2 && d == 12) || (c == 2 && d == 13)) { + if ((c == 3 && d == 10) || (c == 2 && d == 13)) { this.grid[i][j] = 4; this.filled_boxes ++; this.nb_mirors ++; } - if ((c == 2 && d == 10) || (c == 2 && d == 11) || (c == 3 && d == 12) || (c == 3 && d == 13)) { + if ((c == 2 && d == 12) || (c == 3 && d == 11)) { this.grid[i][j] = 5; this.filled_boxes ++; this.nb_mirors ++; } + if ((c == 2 && d == 10) || (c == 3 && d == 12)) { + this.grid[i][j] = 6; + this.filled_boxes ++; + this.nb_mirors ++; + } + if ((c == 2 && d == 11) || (c == 3 && d == 13)) { + this.grid[i][j] = 7; + this.filled_boxes ++; + this.nb_mirors ++; + } // changing the position of the situation @@ -340,7 +358,7 @@ public class Universe { this.grid[i][j] = 1; } } else { - if (this.grid[i][j] == 4 || this.grid[i][j] == 5) {this.nb_mirors--;} + if (this.grid[i][j] == 4 || this.grid[i][j] == 5 || this.grid[i][j] == 6 || this.grid[i][j] == 7) {this.nb_mirors--;} this.filled_boxes--; this.grid[i][j] = 0; } diff --git a/userInterface/Grid.java b/userInterface/Grid.java index 154330e..ae42f90 100644 --- a/userInterface/Grid.java +++ b/userInterface/Grid.java @@ -32,6 +32,7 @@ public class Grid extends JPanel { private JButton[][] mat; private int selected; private Universe universe; + private int button_width, button_height; public Grid(int width, int height, Universe universe) { super(new GridLayout(height, width)); @@ -39,6 +40,8 @@ public class Grid extends JPanel { this.height = height; this.selected = 0; this.universe = universe; + this.button_width = 600 / this.height; + this.button_height = button_width; this.mat = new JButton[height][width]; @@ -46,7 +49,7 @@ public class Grid extends JPanel { for (int j = 0; j < this.width; j++) { this.mat[i][j] = new JButton(); - this.mat[i][j].setPreferredSize(new Dimension(Math.max(50, 600 / this.height), Math.max(50, (this.width * 600 / this.height) / this.width))); + this.mat[i][j].setPreferredSize(new Dimension(this.button_width, this.button_height)); final int coord_i = i; final int coord_j = j; @@ -118,7 +121,7 @@ public class Grid extends JPanel { Image photo; switch (this.universe.getGrid()[i + 1][j + 1]) { case -1: - photo = new ImageIcon(this.getClass().getResource("../images/wall.png")).getImage(); + photo = new ImageIcon(this.getClass().getResource("../images/wall.png")).getImage().getScaledInstance(this.button_width, this.button_height, Image.SCALE_SMOOTH); this.mat[i][j].setIcon(new ImageIcon(photo)); break; case 0: @@ -126,39 +129,47 @@ public class Grid extends JPanel { this.mat[i][j].setIcon(null); break; case 1: - photo = new ImageIcon(this.getClass().getResource("../images/verticalLaser.png")).getImage(); + photo = new ImageIcon(this.getClass().getResource("../images/verticalLaser.png")).getImage().getScaledInstance(this.button_width, this.button_height, Image.SCALE_SMOOTH); this.mat[i][j].setIcon(new ImageIcon(photo)); break; case 2: - photo = new ImageIcon(this.getClass().getResource("../images/horizontalLaser.png")).getImage(); + photo = new ImageIcon(this.getClass().getResource("../images/horizontalLaser.png")).getImage().getScaledInstance(this.button_width, this.button_height, Image.SCALE_SMOOTH); this.mat[i][j].setIcon(new ImageIcon(photo)); break; case 3: - photo = new ImageIcon(this.getClass().getResource("../images/wall.png")).getImage(); + photo = new ImageIcon(this.getClass().getResource("../images/crossLaser.png")).getImage().getScaledInstance(this.button_width, this.button_height, Image.SCALE_SMOOTH); this.mat[i][j].setIcon(new ImageIcon(photo)); break; - case 4: - photo = new ImageIcon(this.getClass().getResource("../images/wall.png")).getImage(); + case 4: + photo = new ImageIcon(this.getClass().getResource("../images/miror1.png")).getImage().getScaledInstance(this.button_width, this.button_height, Image.SCALE_SMOOTH); this.mat[i][j].setIcon(new ImageIcon(photo)); break; case 5: - photo = new ImageIcon(this.getClass().getResource("../images/wall.png")).getImage(); + photo = new ImageIcon(this.getClass().getResource("../images/miror2.png")).getImage().getScaledInstance(this.button_width, this.button_height, Image.SCALE_SMOOTH); + this.mat[i][j].setIcon(new ImageIcon(photo)); + break; + case 6: + photo = new ImageIcon(this.getClass().getResource("../images/miror3.png")).getImage().getScaledInstance(this.button_width, this.button_height, Image.SCALE_SMOOTH); + this.mat[i][j].setIcon(new ImageIcon(photo)); + break; + case 7: + photo = new ImageIcon(this.getClass().getResource("../images/miror4.png")).getImage().getScaledInstance(this.button_width, this.button_height, Image.SCALE_SMOOTH); this.mat[i][j].setIcon(new ImageIcon(photo)); break; case 10: - photo = new ImageIcon(this.getClass().getResource("../images/startUp.png")).getImage(); + photo = new ImageIcon(this.getClass().getResource("../images/startUp.png")).getImage().getScaledInstance(this.button_width, this.button_height, Image.SCALE_SMOOTH); this.mat[i][j].setIcon(new ImageIcon(photo)); break; case 11: - photo = new ImageIcon(this.getClass().getResource("../images/startBot.png")).getImage(); + photo = new ImageIcon(this.getClass().getResource("../images/startBot.png")).getImage().getScaledInstance(this.button_width, this.button_height, Image.SCALE_SMOOTH); this.mat[i][j].setIcon(new ImageIcon(photo)); break; case 12: - photo = new ImageIcon(this.getClass().getResource("../images/startRight.png")).getImage(); + photo = new ImageIcon(this.getClass().getResource("../images/startRight.png")).getImage().getScaledInstance(this.button_width, this.button_height, Image.SCALE_SMOOTH); this.mat[i][j].setIcon(new ImageIcon(photo)); break; case 13: - photo = new ImageIcon(this.getClass().getResource("../images/startLeft.png")).getImage(); + photo = new ImageIcon(this.getClass().getResource("../images/startLeft.png")).getImage().getScaledInstance(this.button_width, this.button_height, Image.SCALE_SMOOTH); this.mat[i][j].setIcon(new ImageIcon(photo)); break; } @@ -207,6 +218,8 @@ public class Grid extends JPanel { best_filled_boxes = universe.getFilledBoxes(); best_nb_mirrors = universe.getNbMirrors(); + System.out.println(best_filled_boxes + " " + best_nb_mirrors); + printUniverse(); } } diff --git a/userInterface/Window.java b/userInterface/Window.java index f3e7708..a84d6ec 100644 --- a/userInterface/Window.java +++ b/userInterface/Window.java @@ -6,6 +6,13 @@ import java.awt.GridBagConstraints; import java.awt.GridLayout; import java.awt.Insets; +import java.io.FileReader; +import java.io.BufferedReader; +import java.io.File; +import java.util.Set; +import java.util.stream.Stream; +import java.util.stream.Collectors; + import javax.swing.*; import java.awt.event.*; import java.awt.*; @@ -90,6 +97,65 @@ public class Window extends JFrame { menuBar.add(fichierMenu); menuBar.add(aideMenu); menuBar.add(toolsMenu); + + nouveauItem.addActionListener(e -> { + this.universe.changeUniverseDim(5, 5); + this.universe.changeUniverseStart(1, 1, 11); + this.panel.remove(this.grid); + this.grid = new Grid(3, 3, this.universe); + this.panel.add(this.grid); + super.pack(); + super.repaint(); + }); + + enregistrerItem.addActionListener(e -> { + String name = JOptionPane.showInputDialog("Choose the universe name"); + this.universe.save(name); + }); + + ouvrirItem.addActionListener(e -> { + String message = "Choose the universe among those : "; + + Set files = Stream.of(new File("./saves").listFiles()).filter(file -> !file.isDirectory()).map(File::getName).collect(Collectors.toSet()); + + for (String element : files) { + message += "\n- " + element.replace(".txt", ""); + } + + String name = JOptionPane.showInputDialog(message); + + try { + BufferedReader reader = new BufferedReader(new FileReader("./saves/" + name + ".txt")); + int universe_height = Integer.valueOf(reader.readLine()); + int universe_width = Integer.valueOf(reader.readLine()); + int start_dir = Integer.valueOf(reader.readLine()); + int start_i = Integer.valueOf(reader.readLine()); + int start_j = Integer.valueOf(reader.readLine()); + + + this.universe.changeUniverseDim(universe_width + 2, universe_height + 2); + this.universe.changeUniverseStart(start_i, start_j, start_dir); + + while (true) { + try { + int pos1 = Integer.valueOf(reader.readLine()); + int pos2 = Integer.valueOf(reader.readLine()); + + this.universe.addObstacle(pos1, pos2); + } + catch (Exception error) { + break; + } + } + + this.panel.remove(this.grid); + this.grid = new Grid(universe_width, universe_height, this.universe); + this.panel.add(this.grid); + super.pack(); + super.repaint(); + } + catch (Exception error) {} + }); regles.addActionListener(e -> { JOptionPane.showMessageDialog(this, "Définissez la taille du plateau ainsi que l'orientation du laser, enfin ajoutez des obstacles et laissez le programme trouver le bon chemin !"); @@ -119,7 +185,6 @@ public class Window extends JFrame { this.grid.solve(); }); - reset.addActionListener(e -> { this.grid.reset(); });