From 556869ebb898388e4db642eca47b7489fbbc71fc Mon Sep 17 00:00:00 2001 From: Ninja-Jambon Date: Wed, 13 Mar 2024 11:57:49 +0100 Subject: [PATCH 01/17] commit --- AppLaser.java | 12 ++++++++++-- Stack.java | 8 ++++++++ Universe.java | 52 ++++++++++++++++++++++++++++++++++++++++++++------- 3 files changed, 63 insertions(+), 9 deletions(-) diff --git a/AppLaser.java b/AppLaser.java index dceba16..095dbc0 100644 --- a/AppLaser.java +++ b/AppLaser.java @@ -256,12 +256,20 @@ public class AppLaser { int start_time = (int) Instant.now().getEpochSecond(); - while (!universe.isSolved()) { + Stack bestStack = stack.copy(); + int best_filled_boxes = 0; + int best_nb_mirrors = 0; + + do { //!universe.isSolved() if (universe.canEvolve(currentState)) { stack.push(currentState.copy(universe.possibleChoices(currentState))); currentState = universe.evolve(currentState); if (display_progress == true) universe.print(universe_height + 6, 4); + + if (universe.getFilledBoxes() > best_filled_boxes && universe.getNbMirrors() < best_nb_mirrors) { + bestStack = stack.copy(); + } } else if (stack.size() > 0) { currentState = stack.pop(); @@ -278,7 +286,7 @@ public class AppLaser { if ((int) Instant.now().getEpochSecond() - start_time > 2 * 60 && optimize_duration == true) { display_regress = false; } - } + } while (stack.size() != 0); System.out.println("\n\n"); diff --git a/Stack.java b/Stack.java index 84dde3c..9a6e064 100644 --- a/Stack.java +++ b/Stack.java @@ -13,6 +13,10 @@ public class Stack { array = new Vector(); } + public Stack(Vector array) { + array = array; + } + // Methods public void push(T x) { @@ -28,4 +32,8 @@ public class Stack { public int size() { return this.array.size(); } + + public Stack copy() { + return new Stack(new Vector(this.array)); + } } diff --git a/Universe.java b/Universe.java index 588bff6..7099e2d 100644 --- a/Universe.java +++ b/Universe.java @@ -9,6 +9,9 @@ public class Universe { private int[][] grid; private int width, height; private int boxes_to_fill; + private int filled_boxes; + private int nb_mirors; + private String name; // Constructors @@ -17,6 +20,8 @@ public class Universe { this.height = height; this.width = width; this.boxes_to_fill = (width - 2) * (height - 2) - 1; + this.filled_boxes = 0; + this.nb_mirors = 0; int i, j; for (i = 1; i < this.height - 1; i++) { @@ -98,6 +103,17 @@ public class Universe { } } + public void recalculateBoxesToFill() { + this.boxes_to_fill = 0; + for (int i = 1; i < this.height - 1; i++) { + for (int j = 1; j < this.width - 1; j++) { + if ((this.grid[i - 1][j] == 0 || this.grid[i + 1][j] == 0 || this.grid[i][j - 1] == 0 || this.grid[i][j + 1] == 0) && this.grid[i][j] == 0) { + this.boxes_to_fill ++; + } + } + } + } + public void resetUniverse() { for (int i = 1; i < this.height - 1; i++) { for (int j = 1; j < this.width - 1; j++) { @@ -106,6 +122,8 @@ public class Universe { } } } + + this.recalculateBoxesToFill(); } public void resetUniverseObstacles() { @@ -149,7 +167,7 @@ public class Universe { this.width = width; this.height = height; - this.boxes_to_fill = 0; + /*this.boxes_to_fill = 0; for (int i = 1; i < height - 1; i++) { for (int j = 1; j < width - 1; j++) { @@ -157,7 +175,9 @@ public class Universe { this.boxes_to_fill ++; } } - } + }*/ + + this.recalculateBoxesToFill(); } public void changeUniverseStart(int pos_i, int pos_j, int dir) { @@ -175,7 +195,8 @@ public class Universe { public void addObstacle(int pos_i, int pos_j) { if (this.grid[pos_i][pos_j] == 0) { this.grid[pos_i][pos_j] = -1; - this.boxes_to_fill--; + //this.boxes_to_fill--; + this.recalculateBoxesToFill(); } else {} } @@ -284,21 +305,30 @@ public class Universe { if (c == 1 && (d == 10 || d == 11)) { if (this.grid[i][j] == 0) { this.grid[i][j] = 1; + this.filled_boxes ++; } else { this.grid[i][j] = 3; - this.boxes_to_fill++; + //this.boxes_to_fill++; } } if (c == 1 && (d == 12 || d == 13)) { if (this.grid[i][j] == 0) { this.grid[i][j] = 2; + this.filled_boxes ++; } else { this.grid[i][j] = 3; - this.boxes_to_fill++; + //this.boxes_to_fill++; } } - if ((c == 3 && d == 10) || (c == 3 && d == 11) || (c == 2 && d == 12) || (c == 2 && d == 13)) this.grid[i][j] = 4; - if ((c == 2 && d == 10) || (c == 2 && d == 11) || (c == 3 && d == 12) || (c == 3 && d == 13)) this.grid[i][j] = 5; + if ((c == 3 && d == 10) || (c == 3 && d == 11) || (c == 2 && d == 12) || (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)) { + this.grid[i][j] = 5; + this.filled_boxes ++; + } // changing the position of the situation @@ -370,4 +400,12 @@ public class Universe { } catch (Exception e) {} } + + public int getFilledBoxes() { + return this.filled_boxes; + } + + public int getNbMirrors() { + return this.nb_mirors; + } } From 02e5e9c6c924af03829a4664e749cfd83a4a49c2 Mon Sep 17 00:00:00 2001 From: Ninja-Jambon Date: Wed, 20 Mar 2024 11:58:16 +0100 Subject: [PATCH 02/17] improvements --- AppLaser.java | 448 +++++++++++++------------ Universe.java | 779 +++++++++++++++++++++---------------------- saves/default.txt | 4 +- saves/rightBlock.txt | 4 +- 4 files changed, 605 insertions(+), 630 deletions(-) diff --git a/AppLaser.java b/AppLaser.java index 095dbc0..6039f0d 100644 --- a/AppLaser.java +++ b/AppLaser.java @@ -10,304 +10,308 @@ import java.util.stream.Stream; import java.util.stream.Collectors; public class AppLaser { - public static void main(String [] args) { + public static void main(String [] args) { - boolean cli = false, optimize_duration = false; + boolean cli = false, optimize_duration = false; - for (int i = 0; i < args.length; i++) { - switch (args[i]) { - case "-cli": - cli = true; - break; + for (int i = 0; i < args.length; i++) { + switch (args[i]) { + case "-cli": + cli = true; + break; - case "--optimize-duration": - optimize_duration = true; - break; + case "--optimize-duration": + optimize_duration = true; + break; - default: - System.out.println("Unknown argument " + args[i]); - System.exit(0); - } - } + default: + System.out.println("Unknown argument " + args[i]); + System.exit(0); + } + } - int universe_width = 3; - int universe_height = 3; - int start_i = 0; - int start_j = 0; - int start_dir = 11; - int firstState_i = start_i; - int firstState_j = start_j; + int universe_width = 3; + int universe_height = 3; + int start_i = 0; + int start_j = 0; + int start_dir = 11; + int firstState_i = start_i; + int firstState_j = start_j; - if (start_dir == 10) firstState_i = start_i - 1; - else if (start_dir == 11) firstState_i = start_i + 1; - else if (start_dir == 12) firstState_j = start_j + 1; - else if (start_dir == 13) firstState_j = start_j - 1; + if (start_dir == 10) firstState_i = start_i - 1; + else if (start_dir == 11) firstState_i = start_i + 1; + else if (start_dir == 12) firstState_j = start_j + 1; + else if (start_dir == 13) firstState_j = start_j - 1; - if (cli == true) { + 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); + 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); + Scanner scanner = new Scanner(System.in); - int choice; + int choice; - do { - // clear screen + do { + // clear screen - System.out.print("\033[H\033[2J"); - System.out.flush(); - System.out.print("\033[?25h"); + System.out.print("\033[H\033[2J"); + System.out.flush(); + System.out.print("\033[?25h"); - // print the universe + // print the universe - universe.print(2, 4); + Universe.print(universe.getGrid(), universe_width + 2, universe_height + 2, 2, 4); - // print the menu + // print the menu - System.out.println("\n1 - Resize universe"); - System.out.println("2 - Change start position"); - System.out.println("3 - Add obstacle"); - System.out.println("4 - Reset universe"); - System.out.println("5 - Reset obstacles"); - System.out.println("6 - Save universe"); - System.out.println("7 - Load universe"); - System.out.println("8 - Resolve"); - System.out.println("0 - Exit"); + System.out.println("\n1 - Resize universe"); + System.out.println("2 - Change start position"); + System.out.println("3 - Add obstacle"); + System.out.println("4 - Reset universe"); + System.out.println("5 - Reset obstacles"); + System.out.println("6 - Save universe"); + System.out.println("7 - Load universe"); + System.out.println("8 - Resolve"); + System.out.println("0 - Exit"); - // prompt the user + // prompt the user - System.out.print("\nChoice : "); - choice = scanner.nextInt(); + System.out.print("\nChoice : "); + choice = scanner.nextInt(); - switch (choice) { + switch (choice) { - case 1: - System.out.print("Enter the width of the universe : "); - universe_width = scanner.nextInt(); - System.out.print("Enter the length of the universe : "); - universe_height = scanner.nextInt(); + case 1: + System.out.print("Enter the width of the universe : "); + universe_width = scanner.nextInt(); + System.out.print("Enter the length of the universe : "); + universe_height = scanner.nextInt(); - universe.changeUniverseDim(universe_width + 2, universe_height + 2); + universe.changeUniverseDim(universe_width + 2, universe_height + 2); - break; + break; - case 2: - System.out.print("Enter the position of the start point in the first axis : "); - start_i = scanner.nextInt(); - System.out.print("Enter the position of the start point in the seccond axis : "); - start_j = scanner.nextInt(); - System.out.print("Enter the direction of the start point : "); - start_dir = scanner.nextInt(); + case 2: + System.out.print("Enter the position of the start point in the first axis : "); + start_i = scanner.nextInt(); + System.out.print("Enter the position of the start point in the seccond axis : "); + start_j = scanner.nextInt(); + System.out.print("Enter the direction of the start point : "); + start_dir = scanner.nextInt(); - firstState_i = start_i; - firstState_j = start_j; + firstState_i = start_i; + firstState_j = start_j; - if (start_dir == 10) firstState_i = start_i - 1; - else if (start_dir == 11) firstState_i = start_i + 1; - else if (start_dir == 12) firstState_j = start_j + 1; - else if (start_dir == 13) firstState_j = start_j - 1; + if (start_dir == 10) firstState_i = start_i - 1; + else if (start_dir == 11) firstState_i = start_i + 1; + else if (start_dir == 12) firstState_j = start_j + 1; + else if (start_dir == 13) firstState_j = start_j - 1; - currentState = new Situation(firstState_i + 1, firstState_j + 1, start_dir, 0); + currentState = new Situation(firstState_i + 1, firstState_j + 1, start_dir, 0); - universe.changeUniverseStart(start_i + 1, start_j + 1, start_dir); + universe.changeUniverseStart(start_i + 1, start_j + 1, start_dir); - break; + break; - case 3: - int firstPos_i, firstPos_j, seccondPos_i, seccondPos_j; + case 3: + int firstPos_i, firstPos_j, seccondPos_i, seccondPos_j; - System.out.print("\nFirst position of the obstacle i : "); - firstPos_i = scanner.nextInt(); - System.out.print("First position of the obstacle j : "); - firstPos_j = scanner.nextInt(); - System.out.print("Seccond position of the obstacle i : "); - seccondPos_i = scanner.nextInt(); - System.out.print("Seccond position of the obstacle j : "); - seccondPos_j = scanner.nextInt(); + System.out.print("\nFirst position of the obstacle i : "); + firstPos_i = scanner.nextInt(); + System.out.print("First position of the obstacle j : "); + firstPos_j = scanner.nextInt(); + System.out.print("Seccond position of the obstacle i : "); + seccondPos_i = scanner.nextInt(); + System.out.print("Seccond position of the obstacle j : "); + seccondPos_j = scanner.nextInt(); - for (int i = firstPos_i + 1; i < seccondPos_i + 2; i++) { - for (int j = firstPos_j + 1; j < seccondPos_j + 2; j++) { - universe.addObstacle(i, j); - } - } + for (int i = firstPos_i + 1; i < seccondPos_i + 2; i++) { + for (int j = firstPos_j + 1; j < seccondPos_j + 2; j++) { + universe.addObstacle(i, j); + } + } - break; + break; - case 4: - universe.resetUniverse(); + case 4: + universe.resetUniverse(); - break; + break; - case 5: - universe.resetUniverseObstacles(); + case 5: + universe.resetUniverseObstacles(); - break; + break; - case 6: - System.out.print("\nHow do you want to name your universe : "); - String name = ""; + case 6: + System.out.print("\nHow do you want to name your universe : "); + String name = ""; - while (name.isEmpty()) { - name = scanner.nextLine(); - } + while (name.isEmpty()) { + name = scanner.nextLine(); + } - universe.save(name); + universe.save(name); - break; + break; - case 7: + case 7: - Set files = Stream.of(new File("./saves").listFiles()).filter(file -> !file.isDirectory()).map(File::getName).collect(Collectors.toSet()); + Set files = Stream.of(new File("./saves").listFiles()).filter(file -> !file.isDirectory()).map(File::getName).collect(Collectors.toSet()); - System.out.println("\nAvailable files : \n"); + System.out.println("\nAvailable files : \n"); - for (String element : files) { - System.out.println(element.replace(".txt", "")); - } + for (String element : files) { + System.out.println(element.replace(".txt", "")); + } - System.out.print("\nWhat universe do you want load ? : "); - String name2 = ""; + System.out.print("\nWhat universe do you want load ? : "); + String name2 = ""; - while (name2.isEmpty()) { - name2 = scanner.nextLine(); - } + while (name2.isEmpty()) { + name2 = scanner.nextLine(); + } - try { - BufferedReader reader = new BufferedReader(new FileReader("./saves/" + name2 + ".txt")); - universe_height = Integer.valueOf(reader.readLine()); - universe_width = Integer.valueOf(reader.readLine()); - start_dir = Integer.valueOf(reader.readLine()); - start_i = Integer.valueOf(reader.readLine()); - start_j = Integer.valueOf(reader.readLine()); + try { + BufferedReader reader = new BufferedReader(new FileReader("./saves/" + name2 + ".txt")); + universe_height = Integer.valueOf(reader.readLine()); + universe_width = Integer.valueOf(reader.readLine()); + start_dir = Integer.valueOf(reader.readLine()); + start_i = Integer.valueOf(reader.readLine()); + start_j = Integer.valueOf(reader.readLine()); - universe.changeUniverseDim(universe_width, universe_height); + universe.changeUniverseDim(universe_width + 2, universe_height + 2); - firstState_i = start_i; - firstState_j = start_j; + firstState_i = start_i; + firstState_j = start_j; - if (start_dir == 10) firstState_i = start_i - 1; - else if (start_dir == 11) firstState_i = start_i + 1; - else if (start_dir == 12) firstState_j = start_j + 1; - else if (start_dir == 13) firstState_j = start_j - 1; + if (start_dir == 10) firstState_i = start_i - 1; + else if (start_dir == 11) firstState_i = start_i + 1; + else if (start_dir == 12) firstState_j = start_j + 1; + else if (start_dir == 13) firstState_j = start_j - 1; - currentState = new Situation(firstState_i, firstState_j, start_dir, 0); + currentState = new Situation(firstState_i, firstState_j, start_dir, 0); - universe.changeUniverseStart(start_i, start_j, start_dir); + universe.changeUniverseStart(start_i, start_j, start_dir); - while (true) { - try { - int pos1 = Integer.valueOf(reader.readLine()); - int pos2 = Integer.valueOf(reader.readLine()); + while (true) { + try { + int pos1 = Integer.valueOf(reader.readLine()); + int pos2 = Integer.valueOf(reader.readLine()); - universe.addObstacle(pos1, pos2); - } - catch (Exception e) { - break; - } - } - } - catch (Exception e) {} + universe.addObstacle(pos1, pos2); + } + catch (Exception e) { + break; + } + } + } + catch (Exception e) {} - break; + break; - case 8: - boolean display_progress = false, display_regress = false; + case 8: + boolean display_progress = false, display_regress = false; - System.out.println("\n1 - display progress and regress"); - System.out.println("2 - display progress"); - System.out.println("3 - display regress"); - System.out.println("4 - display nothing"); - System.out.print("\nChoice : "); - choice = scanner.nextInt(); + System.out.println("\n1 - display progress and regress"); + System.out.println("2 - display progress"); + System.out.println("3 - display regress"); + System.out.println("4 - display nothing"); + System.out.print("\nChoice : "); + choice = scanner.nextInt(); - switch (choice) { - case 1: - display_progress = true; - display_regress = true; - break; + switch (choice) { + case 1: + display_progress = true; + display_regress = true; + break; - case 2: - display_progress = true; - break; + case 2: + display_progress = true; + break; - case 3: - display_regress = true; - break; + case 3: + display_regress = true; + break; - case 4: - break; + case 4: + break; - default: - break; - } + default: + break; + } - System.out.print("\033[?25l"); - System.out.print("\033[H\033[2J"); - System.out.flush(); + System.out.print("\033[?25l"); + System.out.print("\033[H\033[2J"); + System.out.flush(); - universe.resetUniverse(); + universe.resetUniverse(); - universe.print(2, 4); + Universe.print(universe.getGrid(), universe_width + 2, universe_height + 2, 2, 4); - int start_time = (int) Instant.now().getEpochSecond(); + int start_time = (int) Instant.now().getEpochSecond(); - Stack bestStack = stack.copy(); - int best_filled_boxes = 0; - int best_nb_mirrors = 0; + int [][] bestGrid = universe.copyGrid(); + int best_filled_boxes = 0; + int best_nb_mirrors = 0; - do { //!universe.isSolved() - if (universe.canEvolve(currentState)) { - stack.push(currentState.copy(universe.possibleChoices(currentState))); - currentState = universe.evolve(currentState); + do { + if (universe.canEvolve(currentState)) { + stack.push(currentState.copy(universe.possibleChoices(currentState))); + currentState = universe.evolve(currentState); - if (display_progress == true) universe.print(universe_height + 6, 4); + if (display_progress == true) Universe.print(universe.getGrid(), universe_width + 2, universe_height + 2, universe_height + 6, 4); - if (universe.getFilledBoxes() > best_filled_boxes && universe.getNbMirrors() < best_nb_mirrors) { - bestStack = stack.copy(); - } - } - else if (stack.size() > 0) { - currentState = stack.pop(); - universe.reset(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(); + + Universe.print(bestGrid, universe_width + 2, universe_height + 2, 2, 2 * universe_width + 10); + } + } + else if (stack.size() > 0) { + currentState = stack.pop(); + universe.reset(currentState); - if (display_regress == true) universe.print(universe_height + 6, 4); - } else { - break; - } + if (display_regress == true) Universe.print(universe.getGrid(), universe_width + 2, universe_height + 2, universe_height + 6, 4); + } else { + break; + } - if ((int) Instant.now().getEpochSecond() - start_time > 60 && optimize_duration == true) { - display_progress = false; - } - if ((int) Instant.now().getEpochSecond() - start_time > 2 * 60 && optimize_duration == true) { - display_regress = false; - } - } while (stack.size() != 0); + if ((int) Instant.now().getEpochSecond() - start_time > 60 && optimize_duration == true) { + display_progress = false; + } + if ((int) Instant.now().getEpochSecond() - start_time > 2 * 60 && optimize_duration == true) { + display_regress = false; + } + } while (stack.size() != 0); - System.out.println("\n\n"); + System.out.println("\n\n"); - universe.print(universe_height + 6, 4); + Universe.print(bestGrid, universe_width + 2, universe_height + 2, universe_height + 6, 4); - System.out.println("\nSolved in " + ((int) Instant.now().getEpochSecond() - start_time) + " secconds"); - - System.out.print("\033[?25h"); - System.out.print("\nEnter anything to continue...."); - scanner.nextInt(); - - break; - - default: - break; - - } - } while (choice != 0); - } - else { - System.out.println("there is no GUI yet."); - } - } + System.out.println("\nSolved in " + ((int) Instant.now().getEpochSecond() - start_time) + " secconds"); + + System.out.print("\033[?25h"); + System.out.print("\nEnter anything to continue...."); + scanner.nextInt(); + + break; + + default: + break; + + } + } while (choice != 0); + } + else { + System.out.println("there is no GUI yet."); + } + } } diff --git a/Universe.java b/Universe.java index 7099e2d..812b346 100644 --- a/Universe.java +++ b/Universe.java @@ -4,408 +4,379 @@ import java.io.File; import java.io.FileWriter; public class Universe { - // Atributes - - private int[][] grid; - private int width, height; - private int boxes_to_fill; - private int filled_boxes; - private int nb_mirors; - private String name; - - // Constructors - - public Universe(int width, int height, int i_start, int j_start, int dir_start) { - this.grid = new int[height][width]; - this.height = height; - this.width = width; - this.boxes_to_fill = (width - 2) * (height - 2) - 1; - this.filled_boxes = 0; - this.nb_mirors = 0; - - int i, j; - for (i = 1; i < this.height - 1; i++) { - for (j = 1; j < this.width - 1; j++) { - this.grid[i][j] = 0; - } - } - - for (i = 0; i < this.height; i++) { - this.grid[i][0] = -1; - this.grid[i][width - 1] = -1; - } - - for (j = 0; j < this.width; j++) { - this.grid[0][j] = -1; - this.grid[height - 1][j] = -1; - } - - this.grid[i_start][j_start] = dir_start; - } - - // Methods - - public void print(int pos_i, int pos_j) { - int i, j; - for (i = 0; i < this.height; i++) { - for (j = 0; j < this.width; j++) { - System.out.print("\033[" + (i + pos_i) + ";" + (j*2 + pos_j) + "H"); - switch (this.grid[i][j]) { - case -1: - System.out.printf(" X"); - break; - - case 0: - System.out.printf(" "); - break; - - case 1: - System.out.printf(" |"); - break; - - case 2: - System.out.printf(" -"); - break; - - case 3: - System.out.printf(" +"); - break; - - case 4: - System.out.printf(" /"); - break; - - case 5: - System.out.printf(" \\"); - break; - - case 10: - System.out.printf(" ^"); - break; - - case 11: - System.out.printf(" v"); - break; - - case 12: - System.out.printf(" >"); - break; - - case 13: - System.out.printf(" <"); - break; - - default: - System.out.printf("%2d", this.grid[i][j]); - } - } - System.out.print("\n"); - } - } - - public void recalculateBoxesToFill() { - this.boxes_to_fill = 0; - for (int i = 1; i < this.height - 1; i++) { - for (int j = 1; j < this.width - 1; j++) { - if ((this.grid[i - 1][j] == 0 || this.grid[i + 1][j] == 0 || this.grid[i][j - 1] == 0 || this.grid[i][j + 1] == 0) && this.grid[i][j] == 0) { - this.boxes_to_fill ++; - } - } - } - } - - public void resetUniverse() { - for (int i = 1; i < this.height - 1; i++) { - for (int j = 1; j < this.width - 1; 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[i][j] != -1) { - this.grid[i][j] = 0; - } - } - } - - this.recalculateBoxesToFill(); - } - - public void resetUniverseObstacles() { - for (int i = 1; i < this.height - 1; i++) { - for (int j = 1; j < this.width - 1; 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[i][j] = 0; - } - } - } - - this.boxes_to_fill = ((this.height - 2) * (this.width - 2)) - 1; - } - - public void changeUniverseDim(int width, int height) { - int [][] newgrid = new int[width][height]; - - for (int i = 1; i < height - 1; i++) { - for (int j = 1; j < width - 1; j++) { - newgrid[i][j] = 0; - } - } - - for (int i = 1; i < height - 1 && i < this.height - 1; i++) { - for (int j = 1; j < width - 1 && j < this.width - 1; j++) { - newgrid[i][j] = this.grid[i][j]; - } - } - - for (int i = 0; i < height; i++) { - newgrid[i][0] = -1; - newgrid[i][width - 1] = -1; - } - - for (int j = 0; j < width; j++) { - newgrid[height - 1][j] = -1; - newgrid[0][j] = -1; - } - - this.grid = newgrid; - this.width = width; - this.height = height; - - /*this.boxes_to_fill = 0; - - for (int i = 1; i < height - 1; i++) { - for (int j = 1; j < width - 1; j++) { - if (this.grid[i][j] == 0) { - this.boxes_to_fill ++; - } - } - }*/ - - this.recalculateBoxesToFill(); - } - - 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[pos_i][pos_j] = dir; - } - - public void addObstacle(int pos_i, int pos_j) { - if (this.grid[pos_i][pos_j] == 0) { - this.grid[pos_i][pos_j] = -1; - //this.boxes_to_fill--; - this.recalculateBoxesToFill(); - } - else {} - } - - public int possibleChoices(Situation s) { - int i = s.pos_i; - int j = s.pos_j; - int d = s.direction; - int c = s.nb_choix; - - switch (c) { - case 0: { - switch (d) { - case 10: // north - if (this.grid[i - 1][j] == 0 || this.grid[i - 1][j] == 2) return 1; // front - else if (this.grid[i][j - 1] == 0 && this.grid[i][j] == 0) return 2; // left - else if (this.grid[i][j + 1] == 0 && this.grid[i][j] == 0) return 3; // right - else return -1; // back - - case 11: // south - if (this.grid[i + 1][j] == 0 || this.grid[i + 1][j] == 2) return 1; // front - else if (this.grid[i][j + 1] == 0 && this.grid[i][j] == 0) return 2; // left - else if (this.grid[i][j - 1] == 0 && this.grid[i][j] == 0) return 3; // right - else return -1; // back - - case 12: // east - if (this.grid[i][j + 1] == 0 || this.grid[i][j + 1] == 1) return 1; // front - else if (this.grid[i - 1][j] == 0 && this.grid[i][j] == 0) return 2; // left - else if (this.grid[i + 1][j] == 0 && this.grid[i][j] == 0) return 3; // right - else return -1; // back - - case 13: //west - if (this.grid[i][j - 1] == 0 || this.grid[i][j - 1] == 1) return 1; // front - else if (this.grid[i + 1][j] == 0 && this.grid[i][j] == 0) return 2; // left - else if (this.grid[i - 1][j] == 0 && this.grid[i][j] == 0) return 3; // right - else return -1; // back - - default: {} - } - } - case 1: { - switch (d) { - case 10: // north - if (this.grid[i][j - 1] == 0 && this.grid[i][j] == 0) return 2; // left - else if (this.grid[i][j + 1] == 0 && this.grid[i][j] == 0) return 3; // right - else return -1; // back - - case 11: // south - if (this.grid[i][j + 1] == 0 && this.grid[i][j] == 0) return 2; // left - else if (this.grid[i][j - 1] == 0 && this.grid[i][j] == 0) return 3; // right - else return -1; // back - - case 12: // east - if (this.grid[i - 1][j] == 0 && this.grid[i][j] == 0) return 2; // left - else if (this.grid[i + 1][j] == 0 && this.grid[i][j] == 0) return 3; // right - else return -1; // back - - case 13: //west - if (this.grid[i + 1][j] == 0 && this.grid[i][j] == 0) return 2; // left - else if (this.grid[i - 1][j] == 0 && this.grid[i][j] == 0) return 3; // right - else return -1; // back - - default: {} - } - } - case 2: { - switch (d) { - case 10: // north - if (this.grid[i][j + 1] == 0 && this.grid[i][j] == 0) return 3; // right - else return -1; // back - - case 11: // south - if (this.grid[i][j - 1] == 0 && this.grid[i][j] == 0) return 3; // right - else return -1; // back - - case 12: // east - if (this.grid[i + 1][j] == 0 && this.grid[i][j] == 0) return 3; // right - else return -1; // back - - case 13: //west - if (this.grid[i - 1][j] == 0 && this.grid[i][j] == 0) return 3; // right - else return -1; // back - - default: {} - } - } - case 3: - return -1; - - default: return -1; - } - } - - public boolean canEvolve(Situation s) { - return possibleChoices(s) != -1; - } - - public Situation evolve(Situation s) { - int i = s.pos_i; - int j = s.pos_j; - int d = s.direction; - int c = possibleChoices(s); - - // new status of the box - - if (c == 1 && (d == 10 || d == 11)) { - if (this.grid[i][j] == 0) { - this.grid[i][j] = 1; - this.filled_boxes ++; - } else { - this.grid[i][j] = 3; - //this.boxes_to_fill++; - } - } - if (c == 1 && (d == 12 || d == 13)) { - if (this.grid[i][j] == 0) { - this.grid[i][j] = 2; - this.filled_boxes ++; - } else { - this.grid[i][j] = 3; - //this.boxes_to_fill++; - } - } - if ((c == 3 && d == 10) || (c == 3 && d == 11) || (c == 2 && d == 12) || (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)) { - this.grid[i][j] = 5; - this.filled_boxes ++; - } - - // changing the position of the situation - - if (c == 1 && d == 10 || c == 2 && d == 12 || c == 3 && d == 13) { - i --; - d = 10; - } - else if (c == 1 && d == 11 || c == 2 && d == 13 || c == 3 && d == 12) { - i ++; - d = 11; - } - else if (c == 1 && d == 12 || c == 2 && d == 11 || c == 3 && d == 10) { - j ++; - d = 12; - } - else if (c == 1 && d == 13 || c == 2 && d == 10 || c == 3 && d == 11) { - j --; - d = 13; - } - - this.boxes_to_fill--; - - return new Situation(i, j, d, 0); - } - - public void reset(Situation s) { - int i = s.pos_i; - int j = s.pos_j; - int d = s.direction; - - if (this.grid[i][j] == 3) { - if (d == 10 || d == 11) { - this.grid[i][j] = 2; - } else { - this.grid[i][j] = 1; - } - } else { - this.grid[s.pos_i][s.pos_j] = 0; - this.boxes_to_fill++; - } - } - - public boolean isSolved() { - return this.boxes_to_fill == 0; - } - - public void save(String fileName) { - try { - File file = new File("./saves/" + fileName + ".txt"); - - file.createNewFile(); - - FileWriter writer = new FileWriter("./saves/" + fileName + ".txt"); - writer.write(this.height + "\n" + this.width + "\n"); - - for (int i = 1; i < this.height - 1; i++) { - for (int j = 1; j < this.width - 1; j++) { - if (this.grid[i][j] == 10 || this.grid[i][j] == 11 || this.grid[i][j] == 12 || this.grid[i][j] == 13) writer.write(this.grid[i][j] + "\n" + i + "\n" + j + "\n"); - } - } - - for (int i = 1; i < this.height - 1; i++) { - for (int j = 1; j < this.width - 1; j++) { - if (this.grid[i][j] == -1) writer.write(i + "\n" + j + "\n"); - } - } - - writer.close(); - } - catch (Exception e) {} - } - - public int getFilledBoxes() { - return this.filled_boxes; - } - - public int getNbMirrors() { - return this.nb_mirors; - } + // Atributes + + private int[][] grid; + private int width, height; + private int filled_boxes; + private int nb_mirors; + private String name; + + // Constructors + + public Universe(int width, int height, int i_start, int j_start, int dir_start) { + this.grid = new int[height][width]; + this.height = height; + this.width = width; + this.filled_boxes = 0; + this.nb_mirors = 0; + + int i, j; + for (i = 1; i < this.height - 1; i++) { + for (j = 1; j < this.width - 1; j++) { + this.grid[i][j] = 0; + } + } + + for (i = 0; i < this.height; i++) { + this.grid[i][0] = -1; + this.grid[i][width - 1] = -1; + } + + for (j = 0; j < this.width; j++) { + this.grid[0][j] = -1; + this.grid[height - 1][j] = -1; + } + + this.grid[i_start][j_start] = dir_start; + } + + // Methods + + public static void print(int [][] tab, int width, int height, int pos_i, int pos_j) { + int i, j; + for (i = 0; i < height; i++) { + for (j = 0; j < width; j++) { + System.out.print("\033[" + (i + pos_i) + ";" + (j*2 + pos_j) + "H"); + switch (tab[i][j]) { + case -1: + System.out.printf(" X"); + break; + + case 0: + System.out.printf(" "); + break; + + case 1: + System.out.printf(" |"); + break; + + case 2: + System.out.printf(" -"); + break; + + case 3: + System.out.printf(" +"); + break; + + case 4: + System.out.printf(" /"); + break; + + case 5: + System.out.printf(" \\"); + break; + + case 10: + System.out.printf(" ^"); + break; + + case 11: + System.out.printf(" v"); + break; + + case 12: + System.out.printf(" >"); + break; + + case 13: + System.out.printf(" <"); + break; + + default: + System.out.printf("%2d", tab[i][j]); + } + } + System.out.print("\n"); + } + } + + public void resetUniverse() { + for (int i = 1; i < this.height - 1; i++) { + for (int j = 1; j < this.width - 1; 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[i][j] != -1) { + this.grid[i][j] = 0; + } + } + } + } + + public void resetUniverseObstacles() { + for (int i = 1; i < this.height - 1; i++) { + for (int j = 1; j < this.width - 1; 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[i][j] = 0; + } + } + } + } + + public void changeUniverseDim(int width, int height) { + int [][] newgrid = new int[width][height]; + + for (int i = 1; i < height - 1; i++) { + for (int j = 1; j < width - 1; j++) { + newgrid[i][j] = 0; + } + } + + for (int i = 1; i < height - 1 && i < this.height - 1; i++) { + for (int j = 1; j < width - 1 && j < this.width - 1; j++) { + newgrid[i][j] = this.grid[i][j]; + } + } + + for (int i = 0; i < height; i++) { + newgrid[i][0] = -1; + newgrid[i][width - 1] = -1; + } + + for (int j = 0; j < width; j++) { + newgrid[height - 1][j] = -1; + newgrid[0][j] = -1; + } + + this.grid = newgrid; + this.width = width; + this.height = height; + } + + 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[pos_i][pos_j] = dir; + } + + public void addObstacle(int pos_i, int pos_j) { + if (this.grid[pos_i][pos_j] == 0) { + this.grid[pos_i][pos_j] = -1; + } + else {} + } + + public int possibleChoices(Situation s) { + int i = s.pos_i; + int j = s.pos_j; + int d = s.direction; + int c = s.nb_choix; + + switch (c) { + case 0: { + switch (d) { + case 10: // north + if (this.grid[i - 1][j] == 0 || this.grid[i - 1][j] == 2) return 1; // front + else if (this.grid[i][j - 1] == 0 && this.grid[i][j] == 0) return 2; // left + else if (this.grid[i][j + 1] == 0 && this.grid[i][j] == 0) return 3; // right + else return -1; // back + + case 11: // south + if (this.grid[i + 1][j] == 0 || this.grid[i + 1][j] == 2) return 1; // front + else if (this.grid[i][j + 1] == 0 && this.grid[i][j] == 0) return 2; // left + else if (this.grid[i][j - 1] == 0 && this.grid[i][j] == 0) return 3; // right + else return -1; // back + + case 12: // east + if (this.grid[i][j + 1] == 0 || this.grid[i][j + 1] == 1) return 1; // front + else if (this.grid[i - 1][j] == 0 && this.grid[i][j] == 0) return 2; // left + else if (this.grid[i + 1][j] == 0 && this.grid[i][j] == 0) return 3; // right + else return -1; // back + + case 13: //west + if (this.grid[i][j - 1] == 0 || this.grid[i][j - 1] == 1) return 1; // front + else if (this.grid[i + 1][j] == 0 && this.grid[i][j] == 0) return 2; // left + else if (this.grid[i - 1][j] == 0 && this.grid[i][j] == 0) return 3; // right + else return -1; // back + + default: {} + } + } + case 1: { + switch (d) { + case 10: // north + if (this.grid[i][j - 1] == 0 && this.grid[i][j] == 0) return 2; // left + else if (this.grid[i][j + 1] == 0 && this.grid[i][j] == 0) return 3; // right + else return -1; // back + + case 11: // south + if (this.grid[i][j + 1] == 0 && this.grid[i][j] == 0) return 2; // left + else if (this.grid[i][j - 1] == 0 && this.grid[i][j] == 0) return 3; // right + else return -1; // back + + case 12: // east + if (this.grid[i - 1][j] == 0 && this.grid[i][j] == 0) return 2; // left + else if (this.grid[i + 1][j] == 0 && this.grid[i][j] == 0) return 3; // right + else return -1; // back + + case 13: //west + if (this.grid[i + 1][j] == 0 && this.grid[i][j] == 0) return 2; // left + else if (this.grid[i - 1][j] == 0 && this.grid[i][j] == 0) return 3; // right + else return -1; // back + + default: {} + } + } + case 2: { + switch (d) { + case 10: // north + if (this.grid[i][j + 1] == 0 && this.grid[i][j] == 0) return 3; // right + else return -1; // back + + case 11: // south + if (this.grid[i][j - 1] == 0 && this.grid[i][j] == 0) return 3; // right + else return -1; // back + + case 12: // east + if (this.grid[i + 1][j] == 0 && this.grid[i][j] == 0) return 3; // right + else return -1; // back + + case 13: //west + if (this.grid[i - 1][j] == 0 && this.grid[i][j] == 0) return 3; // right + else return -1; // back + + default: {} + } + } + case 3: + return -1; + + default: return -1; + } + } + + public boolean canEvolve(Situation s) { + return possibleChoices(s) != -1; + } + + public Situation evolve(Situation s) { + int i = s.pos_i; + int j = s.pos_j; + int d = s.direction; + int c = possibleChoices(s); + + // new status of the box + + if (c == 1 && (d == 10 || d == 11)) { + if (this.grid[i][j] == 0) { + this.grid[i][j] = 1; + this.filled_boxes ++; + } else { + this.grid[i][j] = 3; + } + } + if (c == 1 && (d == 12 || d == 13)) { + if (this.grid[i][j] == 0) { + this.grid[i][j] = 2; + this.filled_boxes ++; + } else { + this.grid[i][j] = 3; + } + } + if ((c == 3 && d == 10) || (c == 3 && d == 11) || (c == 2 && d == 12) || (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)) { + this.grid[i][j] = 5; + this.filled_boxes ++; + this.nb_mirors ++; + } + + // changing the position of the situation + + if (c == 1 && d == 10 || c == 2 && d == 12 || c == 3 && d == 13) { + i --; + d = 10; + } + else if (c == 1 && d == 11 || c == 2 && d == 13 || c == 3 && d == 12) { + i ++; + d = 11; + } + else if (c == 1 && d == 12 || c == 2 && d == 11 || c == 3 && d == 10) { + j ++; + d = 12; + } + else if (c == 1 && d == 13 || c == 2 && d == 10 || c == 3 && d == 11) { + j --; + d = 13; + } + + return new Situation(i, j, d, 0); + } + + public void reset(Situation s) { + int i = s.pos_i; + int j = s.pos_j; + int d = s.direction; + + if (this.grid[i][j] == 3) { + if (d == 10 || d == 11) { + this.grid[i][j] = 2; + } else { + this.grid[i][j] = 1; + } + } else { + if (this.grid[i][j] == 4 || this.grid[i][j] == 5) {this.nb_mirors--;} + this.filled_boxes--; + this.grid[i][j] = 0; + } + } + + public void save(String fileName) { + try { + File file = new File("./saves/" + fileName + ".txt"); + + file.createNewFile(); + + FileWriter writer = new FileWriter("./saves/" + fileName + ".txt"); + writer.write((this.height - 2) + "\n" + (this.width - 2) + "\n"); + + for (int i = 1; i < this.height - 1; i++) { + for (int j = 1; j < this.width - 1; j++) { + if (this.grid[i][j] == 10 || this.grid[i][j] == 11 || this.grid[i][j] == 12 || this.grid[i][j] == 13) writer.write(this.grid[i][j] + "\n" + i + "\n" + j + "\n"); + } + } + + for (int i = 1; i < this.height - 1; i++) { + for (int j = 1; j < this.width - 1; j++) { + if (this.grid[i][j] == -1) writer.write(i + "\n" + j + "\n"); + } + } + + writer.close(); + } + catch (Exception e) {} + } + + public int getFilledBoxes() { + return this.filled_boxes; + } + + public int getNbMirrors() { + return this.nb_mirors; + } + + public int[][] getGrid() { + return this.grid; + } + + public int[][] copyGrid() { + return this.grid.clone(); + } } diff --git a/saves/default.txt b/saves/default.txt index def01b1..0978dc6 100644 --- a/saves/default.txt +++ b/saves/default.txt @@ -1,5 +1,5 @@ -5 -5 +3 +3 11 1 1 diff --git a/saves/rightBlock.txt b/saves/rightBlock.txt index abbbd50..beffdd4 100644 --- a/saves/rightBlock.txt +++ b/saves/rightBlock.txt @@ -1,5 +1,5 @@ -12 -12 +10 +10 10 6 1 From 8dce30c382270a9dda5058b2f5d277509ce1d1ae Mon Sep 17 00:00:00 2001 From: Lukian LEIZOUR Date: Sat, 23 Mar 2024 22:25:48 +0100 Subject: [PATCH 03/17] commit --- AppLaser.java | 1 + Universe.java | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/AppLaser.java b/AppLaser.java index 6039f0d..ccd6258 100644 --- a/AppLaser.java +++ b/AppLaser.java @@ -273,6 +273,7 @@ public class AppLaser { best_nb_mirrors = universe.getNbMirrors(); Universe.print(bestGrid, universe_width + 2, universe_height + 2, 2, 2 * universe_width + 10); + System.out.println("Miroirs: " + best_nb_mirrors + " Cases: " + best_filled_boxes); } } else if (stack.size() > 0) { diff --git a/Universe.java b/Universe.java index 812b346..3a86c87 100644 --- a/Universe.java +++ b/Universe.java @@ -122,7 +122,7 @@ public class Universe { } public void changeUniverseDim(int width, int height) { - int [][] newgrid = new int[width][height]; + int [][] newgrid = new int[height][width]; for (int i = 1; i < height - 1; i++) { for (int j = 1; j < width - 1; j++) { From 4a63c7c01e0e16b9d5383ce815b13b05730e793c Mon Sep 17 00:00:00 2001 From: Lukian LEIZOUR Date: Sat, 23 Mar 2024 22:26:01 +0100 Subject: [PATCH 04/17] added a new save --- saves/prof.txt | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 saves/prof.txt diff --git a/saves/prof.txt b/saves/prof.txt new file mode 100644 index 0000000..1278254 --- /dev/null +++ b/saves/prof.txt @@ -0,0 +1,83 @@ +13 +20 +11 +1 +1 +2 +6 +2 +9 +2 +10 +2 +13 +2 +14 +2 +15 +2 +16 +2 +17 +3 +3 +3 +4 +3 +5 +3 +6 +3 +9 +3 +10 +4 +19 +5 +19 +6 +3 +6 +4 +6 +5 +6 +8 +6 +9 +6 +11 +6 +14 +6 +15 +6 +16 +7 +3 +7 +8 +7 +14 +8 +3 +8 +8 +8 +14 +9 +3 +9 +8 +9 +9 +9 +12 +9 +13 +9 +14 +10 +3 +11 +3 From b05b7cd13c77b07e270d5fc409a50014c5729322 Mon Sep 17 00:00:00 2001 From: Lukian LEIZOUR Date: Tue, 26 Mar 2024 21:59:12 +0100 Subject: [PATCH 05/17] hell + gui --- AppLaser.java | 6 +- makefile | 4 + saves/prof.txt | 4 +- Situation.java => universe/Situation.java | 2 + Stack.java => universe/Stack.java | 2 + Universe.java => universe/Universe.java | 2 + userInterface/Grid.java | 103 ++++++++++++++++++++++ userInterface/UserInterface.java | 12 +++ userInterface/Window.java | 80 +++++++++++++++++ 9 files changed, 212 insertions(+), 3 deletions(-) rename Situation.java => universe/Situation.java (95%) rename Stack.java => universe/Stack.java (96%) rename Universe.java => universe/Universe.java (99%) create mode 100644 userInterface/Grid.java create mode 100644 userInterface/UserInterface.java create mode 100644 userInterface/Window.java diff --git a/AppLaser.java b/AppLaser.java index ccd6258..7209609 100644 --- a/AppLaser.java +++ b/AppLaser.java @@ -9,6 +9,9 @@ import java.util.Set; import java.util.stream.Stream; import java.util.stream.Collectors; +import userInterface.UserInterface; +import universe.*; + public class AppLaser { public static void main(String [] args) { @@ -312,7 +315,8 @@ public class AppLaser { } while (choice != 0); } else { - System.out.println("there is no GUI yet."); + UserInterface userInterface= new UserInterface(); + userInterface.start(); } } } diff --git a/makefile b/makefile index b2096fa..5d2950c 100644 --- a/makefile +++ b/makefile @@ -5,3 +5,7 @@ build: run: javac --source 1.8 --target 1.8 AppLaser.java java AppLaser -cli --optimize-duration + +run_gui: + javac --source 1.8 --target 1.8 AppLaser.java + java AppLaser diff --git a/saves/prof.txt b/saves/prof.txt index 1278254..4022bef 100644 --- a/saves/prof.txt +++ b/saves/prof.txt @@ -1,7 +1,7 @@ 13 20 -11 -1 +10 +9 1 2 6 diff --git a/Situation.java b/universe/Situation.java similarity index 95% rename from Situation.java rename to universe/Situation.java index d456677..fb3fd9c 100644 --- a/Situation.java +++ b/universe/Situation.java @@ -1,5 +1,7 @@ // Antoine CRETUAL, Lukian LEIZOUR, 21/02/2024 +package universe; + public class Situation { // Atributes diff --git a/Stack.java b/universe/Stack.java similarity index 96% rename from Stack.java rename to universe/Stack.java index 9a6e064..464faec 100644 --- a/Stack.java +++ b/universe/Stack.java @@ -1,5 +1,7 @@ // Antoine CRETUAL, Lukian LEIZOUR, 21/02/2024 +package universe; + import java.util.Vector; public class Stack { diff --git a/Universe.java b/universe/Universe.java similarity index 99% rename from Universe.java rename to universe/Universe.java index 3a86c87..40fba4f 100644 --- a/Universe.java +++ b/universe/Universe.java @@ -1,5 +1,7 @@ // Antoine CRETUAL, Lukian LEIZOUR, 14/02/2024 +package universe; + import java.io.File; import java.io.FileWriter; diff --git a/userInterface/Grid.java b/userInterface/Grid.java new file mode 100644 index 0000000..db573ee --- /dev/null +++ b/userInterface/Grid.java @@ -0,0 +1,103 @@ +package userInterface; + +import javax.swing.JButton; +import javax.swing.JFrame; +import javax.swing.JPanel; + +import java.awt.Dimension; +import java.awt.Color; +import java.awt.GridBagConstraints; +import java.awt.GridLayout; +import java.awt.Insets; + +import javax.swing.*; +import java.awt.event.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import java.awt.image.BufferedImage; +import java.io.File; +import javax.imageio.ImageIO; + +public class Grid extends JPanel { + private int rows; + private int cols; + private Color[][] colors; + + public Grid(int rows, int cols) { + this.rows = rows; + this.cols = cols; + setPreferredSize(new Dimension(400, 400)); + setBackground(Color.WHITE); + + colors = new Color[rows][cols]; + for (int i = 0; i < rows; i++) { + for (int j = 0; j < cols; j++) { + colors[i][j] = Color.WHITE; + } + } + + addMouseListener(new MouseAdapter() { + @Override + public void mousePressed(MouseEvent evt) { + int cellWidth = getWidth() / cols; + int cellHeight = getHeight() / rows; + int col = evt.getX() / cellWidth; + int row = evt.getY() / cellHeight; + + if (row >= 0 && row < rows && col >= 0 && col < cols) { + if (colors[row][col] == Color.WHITE) { + colors[row][col] = Color.RED; + } else { + colors[row][col] = Color.WHITE; + } + repaint(); + } + } + + @Override + public void mouseEntered(MouseEvent e) { + try { + // Charger l'image pour le curseur personnalisé + BufferedImage cursorImage = ImageIO.read(new File("h:\\Mes documents\\INF1404\\projet\\picture\\sabre.png")); + // Créer le curseur personnalisé + Cursor customCursor = Toolkit.getDefaultToolkit().createCustomCursor(cursorImage, new Point(0, 0), "customCursor"); + // Définir le curseur personnalisé + setCursor(customCursor); + } catch (Exception ex) { + ex.printStackTrace(); + } + } + + @Override + public void mouseExited(MouseEvent e) { + setCursor(Cursor.getDefaultCursor()); // Rétablir l'icône de la souris par défaut quand elle quitte le composant + } + }); + } + + @Override + protected void paintComponent(Graphics g) { + super.paintComponent(g); + int cellWidth = getWidth() / cols; + int cellHeight = getHeight() / rows; + + // Dessiner les cases avec les couleurs stockées + for (int i = 0; i < rows; i++) { + for (int j = 0; j < cols; j++) { + g.setColor(colors[i][j]); + g.fillRect(j * cellWidth, i * cellHeight, cellWidth, cellHeight); + } + } + + // Dessiner les lignes de la grille + g.setColor(Color.BLACK); + for (int i = 0; i <= rows; i++) { + g.drawLine(0, i * cellHeight, getWidth(), i * cellHeight); + } + for (int j = 0; j <= cols; j++) { + g.drawLine(j * cellWidth, 0, j * cellWidth, getHeight()); + } + } +} \ No newline at end of file diff --git a/userInterface/UserInterface.java b/userInterface/UserInterface.java new file mode 100644 index 0000000..b1c4614 --- /dev/null +++ b/userInterface/UserInterface.java @@ -0,0 +1,12 @@ +package userInterface; + +public class UserInterface extends Thread { + private Window window; + + public UserInterface() { + window = new Window(); + } + + public void run() { + } +} \ No newline at end of file diff --git a/userInterface/Window.java b/userInterface/Window.java new file mode 100644 index 0000000..c1649c9 --- /dev/null +++ b/userInterface/Window.java @@ -0,0 +1,80 @@ +package userInterface; + +import javax.swing.JButton; +import javax.swing.JFrame; +import javax.swing.JPanel; + +import java.awt.Dimension; +import java.awt.Color; +import java.awt.GridBagConstraints; +import java.awt.GridLayout; +import java.awt.Insets; + +import javax.swing.*; +import java.awt.event.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import java.awt.image.BufferedImage; +import java.io.File; +import javax.imageio.ImageIO; + +public class Window extends JFrame { + + JPanel panel; + JMenuBar menuBar; + Grid grid; + + public Window() { + super("Laser Finder"); + + panel = new JPanel(); + panel.setPreferredSize(new Dimension(1000,800)); + panel.setBackground(Color.decode("#EBEBD3")); + + + menuBar = new JMenuBar(); + + JMenu fichierMenu = new JMenu("Fichier"); + JMenu aideMenu = new JMenu("Aide"); + + ImageIcon nouveauIcon = new ImageIcon("h:\\Mes documents\\INF1404\\projet\\picture\\nouveau.png"); + ImageIcon openIcon = new ImageIcon("h:\\Mes documents\\INF1404\\projet\\picture\\ouvrir.png"); + ImageIcon saveIcon = new ImageIcon("h:\\Mes documents\\INF1404\\projet\\picture\\sauvegarder.png"); + + JMenuItem nouveauItem = new JMenuItem("Nouveau", nouveauIcon); + JMenuItem ouvrirItem = new JMenuItem("Ouvrir", openIcon); + JMenuItem enregistrerItem = new JMenuItem("Enregistrer", saveIcon); + + JMenuItem apropos = new JMenuItem("à propos"); + JMenuItem regles = new JMenuItem("règles"); + + fichierMenu.add(nouveauItem); + fichierMenu.add(ouvrirItem); + fichierMenu.add(enregistrerItem); + + aideMenu.add(apropos); + aideMenu.add(regles); + + menuBar.add(fichierMenu); + menuBar.add(aideMenu); + + + 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 !"); + }); + + grid = new Grid(5, 5); + panel.add(grid, BorderLayout.CENTER); + + super.setJMenuBar(menuBar); + super.setContentPane(this.panel); + + super.setLocationRelativeTo(null); + super.setLocation(1000, 400); + super.pack(); + super.setVisible(true); + super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + } +} \ No newline at end of file From 29ce62150e0542675cd770160760881c63a567e9 Mon Sep 17 00:00:00 2001 From: Ninja-Jambon Date: Wed, 27 Mar 2024 11:54:38 +0100 Subject: [PATCH 06/17] commit --- AppLaser.java | 15 +++--- images/blade.png | Bin 0 -> 1610 bytes images/new.png | Bin 0 -> 861 bytes images/open.png | Bin 0 -> 621 bytes images/save.png | Bin 0 -> 720 bytes makefile | 6 +++ universe/Universe.java | 8 +++ userInterface/Grid.java | 83 +++++++++++++++++++++++++++---- userInterface/UserInterface.java | 6 ++- userInterface/Window.java | 72 ++++++++++++++++++++------- 10 files changed, 153 insertions(+), 37 deletions(-) create mode 100644 images/blade.png create mode 100644 images/new.png create mode 100644 images/open.png create mode 100644 images/save.png diff --git a/AppLaser.java b/AppLaser.java index 7209609..b37f5c2 100644 --- a/AppLaser.java +++ b/AppLaser.java @@ -10,7 +10,9 @@ import java.util.stream.Stream; import java.util.stream.Collectors; import userInterface.UserInterface; -import universe.*; +import universe.Universe; +import universe.Stack; +import universe.Situation; public class AppLaser { public static void main(String [] args) { @@ -46,12 +48,11 @@ 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; @@ -315,7 +316,7 @@ public class AppLaser { } while (choice != 0); } else { - UserInterface userInterface= new UserInterface(); + UserInterface userInterface = new UserInterface(universe); userInterface.start(); } } diff --git a/images/blade.png b/images/blade.png new file mode 100644 index 0000000000000000000000000000000000000000..4ffe69a49d6b0550eff55f48ce541de5e64dcf3d GIT binary patch literal 1610 zcmZ{kdo&by6vuyRkoO}|W0JfRvonlg#>iksMvOupwQ0~O zVmy|L84P(vg(I&l+e0KGMW$7DoIPjH*|TSVzjN>RckVr(-~Ik|(~pu5%Sl6}0RVC& z2ZF0eHMU}hgeY4gs!K(p>F?m`3_z?N0Ewvpd=-@v=KzRA05DGh0GkJZ8ZE!+7*5oX z@H=vt05-R7F~2ffRFR~UoQaZi+tsB__XZ{HIs|~YB8gz*?$F&Zd?Co~IFMZ4#C&Pw z2am4utKK)&vV&Q*O86V47AmxzvTk~YHAn}%6U0XIvE;t2YZLFDM?= zRWJ5_Q-X`MQ=$2%SiPugyuTO^Ywgj`V$y^OxXg_eYs{=7`RfMua`Zs^uxGlFoN3Q( zm{aG6d(;QHCCsZRPW~Maodv5nt&p86$*OUX`ey5v`v{j*He_f@=XvXJDC*K2xh>ES zMz$dIx2z5FQv^BtwcSQ&csKX6w}qC()+cbK%HpH>HNDa0p_}}qY(kRs=kYz>)!PGE zbDw44H-ricCi%&ZHPb^GYJWxrDrBvdR6hjK+=t5~u`%<0F+V2i+h&`Tvol0(e>}pqHz=aNn*(r;69frVY#zPfI05cta zg&e%1n4#q~-brVlJP*Mrynj2K_vG|Je*QTJxnv5j-&VmQy!ayMY*yPUoputcwo!9DLbjJJ0cstfzMOU z>6RPIUg1R{K);H5SnDHS4$;2Rw!){p>FW+UCd#a(63^no-q88qaGj_uO3^!Uk=Z<@ zP(tJ5m!{es7L2F!WnW!F+pfK&)N;Q$1_?E1s(s!>T4#tXz*Gu55MpQPRVWS=R#{Zj)4J%Y8uCh42zY0EGGTsz4c3qrG7C+gi)^_IoyF9Iiw%T!h89rU&8%6 z1AQ!TZcJMUk#pzm2leUx{UuP6Vi5Y|^X{F3;6rA0<2^c&iDj9KuV6{am}B3nYE@0x z$o<~jy|Tc1cOe7v2k)s7+c7J{$4mu*kV?@R9qlboaMd)2TTRqw7!Zg_umO3A#2}Kw z9=t;3)%Eh8CfSD8^s`#+Wn+V#LNxWwy;GCWNr?6lO8CO&t!X3-VJ(-?o@k&uHzV}n zhWj&8A{$**e|G5dPIH)Qe)wTE)91oFo0Efm(VGPs7ab=LMWlUc?~BqteV#z7yTo)3 zJg>d*gM=#8_Rki(>*=xvar1GPl^!bY=#xtB^^nvDCU*UFi(QK{?CuQc$ z4B9x)YSh~UD}}C?QTJ#mS*&%reo@OVq|IVP)}JV~A?(x*x&KA~bS(Pdnwn&VQ?yRU z(q1`@Z6hO91FDjV^Yh{qrtjxDq(Q>D6CpO_WA%O4yKwL&L2tQ2z^;JVj@SKdukN0p zooHWNoTc#=Wxua48oArs!MK(Ox;jk?UsuXAl4E>a?CXooFI(~X6YXGmwp`xbBP-rEl<%4>c;6ZvE!2 zpw330j}tyhE4XRiaziF}B8<1HLcW+!xI3;|SLVfXa2+aga{N?U>J{y|6s>c^6)iW4 zZ7z1jbIs4B*$N~`S=Tp#4oaB|0IplKF6F6SqyT4UU?_U;5XIIRa@$&@x^$u!{fsZ2 ziuDhtiUgP-Oi;!MQ)3fzcO(L9hQuN*4k8d(1VS$?0{Sl?IP^?lK+OLGo<@BYBEaCE i3G~2VY6RUknD$@B1oQV01HoIkhyf%bnNV#@N&X8cow)u0 literal 0 HcmV?d00001 diff --git a/images/new.png b/images/new.png new file mode 100644 index 0000000000000000000000000000000000000000..4d78a597bfc8fa5e49b0bfd4673d6c0aa685ad80 GIT binary patch literal 861 zcmeAS@N?(olHy`uVBq!ia0vp^A|TAc3?z4jzqJQa3dtTpz6=aiY77hwEes65fIfvhiX>JY z*wwP9%}n%kxRUK%H35Z_Zysiu20m!Fk&JR(>+8LBUGndjAreMKOMvDwCV9KNggjyH zSPJB@mw5WRvcF&!;j~Z|F{}>+3hniDaSV~ToScx5kd~Or%p6w6=cmWU=H|x6mM0fy zS0^AKAR;6t$l7US(9!8_;LxyS%9frrGU6SYYI^)v7+H9@g1Wp;X{}n(l=Vt#R@5!4 zI~q+qyQ+Q_v9)D1DI}!yC~-_QRCE^LF<`msbJl=W^o*`T*F+ZGwQIiS<^-~+zIkIB zAuBUCAas@G?j5^!Rxik8Z);V*;iJ}4rttFV!G<@!d2;#kjEwOLY>amH^`;YI7`zp< zWmwo)je#la`#PzK)4sL00zINy;u=wsl30>zm0Xkxq!^40j7)S5jCG9+LJTaeOwFte zO|=aStPBjCB&9r1H00)|WTsW()?nOVmnQ!hm}C fQX@Rme0>?TfNTyR27yb#lR=cHtDnm{r-UW|FV@-L literal 0 HcmV?d00001 diff --git a/images/open.png b/images/open.png new file mode 100644 index 0000000000000000000000000000000000000000..153e75ba3f891c4116072b8fe31b6fe9867fb9ed GIT binary patch literal 621 zcmeAS@N?(olHy`uVBq!ia0vp^A|TAc0wmQNuC@Uwg=CK)Uj~LMH3o);76yi2K%s^g z3=E|P3=FRl7#OT(FffQ0%-I!a1C(G&@^*J&_}|`tWO>_3^e&V2s1iq%&q_m zvX^-Jy0X7u7U8rqyt*%EHc;rVr;B5V#`)fh_WI3%BF8`a=g%)u2<4m_uyFO!q>oH{ zM7D0|Jo!%5aUb^M@&&;Bxo^^8g}}nzxGa}g`GZT6ZoO%n zb(QzjqU$^b2a7kXSUZ_fFO%i8oRN5Jp)^amWJHpiaz9gP2NC6e` frABzB`T8O>_%)lV(0K$w`{*CrP zLG}_)Usv`Q%pzP2a*+*&j~Eyj6FglULp09!UNX!UaTH;AkpJx&i}coQ7dSXOqHa|9 zy8Lfw&D^qK+oe-RtxGxFw`{)P#VsxE$`Pfi5I0d^qsS474Hu0UIZesQ+nhb|?&&+O zN48%*^ZVJ}+RdkXD$ncO`fz!gRwdWeL(c4tmCv`#{d!}IIA`^vMbE>|zO-^!wjo8~ z`uC{&d0%X{rks|2oo6*=C5yn86wWgfIE#v}mCg=1u+6A(tyCJYJUH>AWMX8XJ7Q!r{>J(q)fVJY$22FZYBGH%}|_ME!VlB<{qr_0BV@Qzzf$3tV9zoq6i(!}(wCTF;-fag)ZY7n$7m zc1&7Q^jq{>X17(6!Q)kpoJ+S(4CL@~ca`62`-A!RwA?j$5xqIU;8!hijVMV;EJ?LW zE=mPb3`Pbeh6Yv!23AI<+Cb94V3n&DFp7~h! z+=i0O+|=Td#M}Z*J;nwQON8dn$pY$;1nCLRPb(=;EJ|f4FE7{2%*!rLPAo{(%P&fw m{mw=TsE98$!ZXd+mq822<^W<4xD-4YM0vXUxvX= 0 && row < rows && col >= 0 && col < cols) { if (colors[row][col] == Color.WHITE) { - colors[row][col] = Color.RED; + colors[row][col] = Color.GRAY; } else { colors[row][col] = Color.WHITE; } @@ -60,7 +63,8 @@ public class Grid extends JPanel { public void mouseEntered(MouseEvent e) { try { // Charger l'image pour le curseur personnalisé - BufferedImage cursorImage = ImageIO.read(new File("h:\\Mes documents\\INF1404\\projet\\picture\\sabre.png")); + URL url = getClass().getResource("../images/blade.png"); + BufferedImage cursorImage = ImageIO.read(new File(url.getPath())); // Créer le curseur personnalisé Cursor customCursor = Toolkit.getDefaultToolkit().createCustomCursor(cursorImage, new Point(0, 0), "customCursor"); // Définir le curseur personnalisé @@ -74,18 +78,20 @@ public class Grid extends JPanel { public void mouseExited(MouseEvent e) { setCursor(Cursor.getDefaultCursor()); // Rétablir l'icône de la souris par défaut quand elle quitte le composant } - }); + }; + + this.addMouseListener(this.mouseListener); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); - int cellWidth = getWidth() / cols; - int cellHeight = getHeight() / rows; + int cellWidth = this.getWidth() / this.cols; + int cellHeight = this.getHeight() / this.rows; // Dessiner les cases avec les couleurs stockées - for (int i = 0; i < rows; i++) { - for (int j = 0; j < cols; j++) { + for (int i = 0; i < this.rows; i++) { + for (int j = 0; j < this.cols; j++) { g.setColor(colors[i][j]); g.fillRect(j * cellWidth, i * cellHeight, cellWidth, cellHeight); } @@ -100,4 +106,61 @@ public class Grid extends JPanel { g.drawLine(j * cellWidth, 0, j * cellWidth, getHeight()); } } + + public void changeDim(int width, int height) { + this.rows = height; + this.cols = width; + this.setPreferredSize(new Dimension(700, 400)); + this.setBackground(Color.WHITE); + + this.colors = new Color[height][width]; + for (int i = 0; i < rows; i++) { + for (int j = 0; j < cols; j++) { + colors[i][j] = Color.WHITE; + } + } + + this.removeMouseListener(this.mouseListener); + + this.mouseListener = new MouseAdapter() { + @Override + public void mousePressed(MouseEvent evt) { + int cellWidth = getWidth() / cols; + int cellHeight = getHeight() / rows; + int col = evt.getX() / cellWidth; + int row = evt.getY() / cellHeight; + + if (row >= 0 && row < rows && col >= 0 && col < cols) { + if (colors[row][col] == Color.WHITE) { + colors[row][col] = Color.GRAY; + } else { + colors[row][col] = Color.WHITE; + } + repaint(); + } + } + + @Override + public void mouseEntered(MouseEvent e) { + try { + // Charger l'image pour le curseur personnalisé + URL url = getClass().getResource("../images/blade.png"); + BufferedImage cursorImage = ImageIO.read(new File(url.getPath())); + // Créer le curseur personnalisé + Cursor customCursor = Toolkit.getDefaultToolkit().createCustomCursor(cursorImage, new Point(0, 0), "customCursor"); + // Définir le curseur personnalisé + setCursor(customCursor); + } catch (Exception ex) { + ex.printStackTrace(); + } + } + + @Override + public void mouseExited(MouseEvent e) { + setCursor(Cursor.getDefaultCursor()); // Rétablir l'icône de la souris par défaut quand elle quitte le composant + } + }; + + this.addMouseListener(this.mouseListener); + } } \ No newline at end of file diff --git a/userInterface/UserInterface.java b/userInterface/UserInterface.java index b1c4614..c91cd08 100644 --- a/userInterface/UserInterface.java +++ b/userInterface/UserInterface.java @@ -1,10 +1,12 @@ package userInterface; +import universe.*; + public class UserInterface extends Thread { private Window window; - public UserInterface() { - window = new Window(); + public UserInterface(Universe universe) { + window = new Window(universe); } public void run() { diff --git a/userInterface/Window.java b/userInterface/Window.java index c1649c9..33d8f8f 100644 --- a/userInterface/Window.java +++ b/userInterface/Window.java @@ -1,9 +1,5 @@ package userInterface; -import javax.swing.JButton; -import javax.swing.JFrame; -import javax.swing.JPanel; - import java.awt.Dimension; import java.awt.Color; import java.awt.GridBagConstraints; @@ -20,35 +16,59 @@ import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; +import java.net.URL; + +import universe.*; + public class Window extends JFrame { - JPanel panel; - JMenuBar menuBar; - Grid grid; + private JPanel panel; + private JMenuBar menuBar; + private Grid grid; + private Universe universe; - public Window() { + public Window(Universe universe) { super("Laser Finder"); + this.universe = universe; + panel = new JPanel(); panel.setPreferredSize(new Dimension(1000,800)); panel.setBackground(Color.decode("#EBEBD3")); - menuBar = new JMenuBar(); - JMenu fichierMenu = new JMenu("Fichier"); - JMenu aideMenu = new JMenu("Aide"); + JMenu fichierMenu = new JMenu("File"); + JMenu aideMenu = new JMenu("Help"); + JMenu toolsMenu = new JMenu("Tools"); + + URL newUrl = getClass().getResource("../images/new.png"); + URL openUrl = getClass().getResource("../images/open.png"); + URL saveUrl = getClass().getResource("../images/save.png"); - ImageIcon nouveauIcon = new ImageIcon("h:\\Mes documents\\INF1404\\projet\\picture\\nouveau.png"); - ImageIcon openIcon = new ImageIcon("h:\\Mes documents\\INF1404\\projet\\picture\\ouvrir.png"); - ImageIcon saveIcon = new ImageIcon("h:\\Mes documents\\INF1404\\projet\\picture\\sauvegarder.png"); + ImageIcon nouveauIcon = new ImageIcon(newUrl.getPath()); + ImageIcon openIcon = new ImageIcon(openUrl.getPath()); + ImageIcon saveIcon = new ImageIcon(saveUrl.getPath()); JMenuItem nouveauItem = new JMenuItem("Nouveau", nouveauIcon); JMenuItem ouvrirItem = new JMenuItem("Ouvrir", openIcon); JMenuItem enregistrerItem = new JMenuItem("Enregistrer", saveIcon); - JMenuItem apropos = new JMenuItem("à propos"); - JMenuItem regles = new JMenuItem("règles"); + JMenuItem apropos = new JMenuItem("About"); + JMenuItem regles = new JMenuItem("Rules"); + + JRadioButtonMenuItem radioWall = new JRadioButtonMenuItem("Wall"); + JRadioButtonMenuItem radioStart = new JRadioButtonMenuItem("Start"); + + radioWall.setSelected(true); + + ButtonGroup buttonGroup = new ButtonGroup(); + + buttonGroup.add(radioWall); + buttonGroup.add(radioStart); + + JMenuItem changeSize = new JMenuItem("Change Size"); + JMenuItem solve = new JMenuItem("Solve"); fichierMenu.add(nouveauItem); fichierMenu.add(ouvrirItem); @@ -57,16 +77,32 @@ public class Window extends JFrame { aideMenu.add(apropos); aideMenu.add(regles); + toolsMenu.add(radioWall); + toolsMenu.add(radioStart); + toolsMenu.addSeparator(); + toolsMenu.add(changeSize); + toolsMenu.addSeparator(); + toolsMenu.add(solve); + menuBar.add(fichierMenu); menuBar.add(aideMenu); + menuBar.add(toolsMenu); 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 !"); }); + + changeSize.addActionListener(e -> { + int width = Integer.valueOf(JOptionPane.showInputDialog("Choose the width")); + int height = Integer.valueOf(JOptionPane.showInputDialog("Choose the height")); + + this.grid.changeDim(width, height); + this.grid.repaint(); + }); - grid = new Grid(5, 5); - panel.add(grid, BorderLayout.CENTER); + this.grid = new Grid(this.universe.getHeight() - 2, this.universe.getWidth() - 2); + this.panel.add(grid, BorderLayout.CENTER); super.setJMenuBar(menuBar); super.setContentPane(this.panel); From 491cd3513633b8b170234e46d32025050bff0dab Mon Sep 17 00:00:00 2001 From: Ninja-Jambon Date: Wed, 27 Mar 2024 13:42:19 +0100 Subject: [PATCH 07/17] commit --- userInterface/Grid.java | 160 +++++++------------------------------- userInterface/Window.java | 16 +++- 2 files changed, 40 insertions(+), 136 deletions(-) diff --git a/userInterface/Grid.java b/userInterface/Grid.java index 7e5d6d0..9fc19f3 100644 --- a/userInterface/Grid.java +++ b/userInterface/Grid.java @@ -23,144 +23,38 @@ import javax.imageio.ImageIO; import java.net.URL; public class Grid extends JPanel { - private int rows; - private int cols; - private Color[][] colors; - private MouseAdapter mouseListener; + private int width; + private int height; + private JButton[][] mat; + private int selected; - public Grid(int rows, int cols) { - this.rows = rows; - this.cols = cols; - this.setPreferredSize(new Dimension(400, 400)); - this.setBackground(Color.WHITE); + public Grid(int width, int height) { + super(new GridLayout(height, width)); + this.width = width; + this.height = height; + this.selected = 0; - colors = new Color[rows][cols]; - for (int i = 0; i < rows; i++) { - for (int j = 0; j < cols; j++) { - colors[i][j] = Color.WHITE; + this.mat = new JButton[height][width]; + + for (int i = 0; i < this.height; i++) { + for (int j = 0; j < this.width; j++) { + this.mat[i][j] = new JButton(); + this.mat[i][j].setBackground(Color.WHITE); + 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; + + this.mat[i][j].addActionListener(e -> { + System.out.println(coord_i + " " + coord_j); + }); + + super.add(this.mat[i][j]); } } - - mouseListener = new MouseAdapter() { - @Override - public void mousePressed(MouseEvent evt) { - int cellWidth = getWidth() / cols; - int cellHeight = getHeight() / rows; - int col = evt.getX() / cellWidth; - int row = evt.getY() / cellHeight; - - if (row >= 0 && row < rows && col >= 0 && col < cols) { - if (colors[row][col] == Color.WHITE) { - colors[row][col] = Color.GRAY; - } else { - colors[row][col] = Color.WHITE; - } - repaint(); - } - } - - @Override - public void mouseEntered(MouseEvent e) { - try { - // Charger l'image pour le curseur personnalisé - URL url = getClass().getResource("../images/blade.png"); - BufferedImage cursorImage = ImageIO.read(new File(url.getPath())); - // Créer le curseur personnalisé - Cursor customCursor = Toolkit.getDefaultToolkit().createCustomCursor(cursorImage, new Point(0, 0), "customCursor"); - // Définir le curseur personnalisé - setCursor(customCursor); - } catch (Exception ex) { - ex.printStackTrace(); - } - } - - @Override - public void mouseExited(MouseEvent e) { - setCursor(Cursor.getDefaultCursor()); // Rétablir l'icône de la souris par défaut quand elle quitte le composant - } - }; - - this.addMouseListener(this.mouseListener); - } - - @Override - protected void paintComponent(Graphics g) { - super.paintComponent(g); - int cellWidth = this.getWidth() / this.cols; - int cellHeight = this.getHeight() / this.rows; - - // Dessiner les cases avec les couleurs stockées - for (int i = 0; i < this.rows; i++) { - for (int j = 0; j < this.cols; j++) { - g.setColor(colors[i][j]); - g.fillRect(j * cellWidth, i * cellHeight, cellWidth, cellHeight); - } - } - - // Dessiner les lignes de la grille - g.setColor(Color.BLACK); - for (int i = 0; i <= rows; i++) { - g.drawLine(0, i * cellHeight, getWidth(), i * cellHeight); - } - for (int j = 0; j <= cols; j++) { - g.drawLine(j * cellWidth, 0, j * cellWidth, getHeight()); - } } - public void changeDim(int width, int height) { - this.rows = height; - this.cols = width; - this.setPreferredSize(new Dimension(700, 400)); - this.setBackground(Color.WHITE); - - this.colors = new Color[height][width]; - for (int i = 0; i < rows; i++) { - for (int j = 0; j < cols; j++) { - colors[i][j] = Color.WHITE; - } - } - - this.removeMouseListener(this.mouseListener); - - this.mouseListener = new MouseAdapter() { - @Override - public void mousePressed(MouseEvent evt) { - int cellWidth = getWidth() / cols; - int cellHeight = getHeight() / rows; - int col = evt.getX() / cellWidth; - int row = evt.getY() / cellHeight; - - if (row >= 0 && row < rows && col >= 0 && col < cols) { - if (colors[row][col] == Color.WHITE) { - colors[row][col] = Color.GRAY; - } else { - colors[row][col] = Color.WHITE; - } - repaint(); - } - } - - @Override - public void mouseEntered(MouseEvent e) { - try { - // Charger l'image pour le curseur personnalisé - URL url = getClass().getResource("../images/blade.png"); - BufferedImage cursorImage = ImageIO.read(new File(url.getPath())); - // Créer le curseur personnalisé - Cursor customCursor = Toolkit.getDefaultToolkit().createCustomCursor(cursorImage, new Point(0, 0), "customCursor"); - // Définir le curseur personnalisé - setCursor(customCursor); - } catch (Exception ex) { - ex.printStackTrace(); - } - } - - @Override - public void mouseExited(MouseEvent e) { - setCursor(Cursor.getDefaultCursor()); // Rétablir l'icône de la souris par défaut quand elle quitte le composant - } - }; - - this.addMouseListener(this.mouseListener); + public void setSelected(int selected) { + this.selected = selected; } } \ No newline at end of file diff --git a/userInterface/Window.java b/userInterface/Window.java index 33d8f8f..2cad948 100644 --- a/userInterface/Window.java +++ b/userInterface/Window.java @@ -88,7 +88,6 @@ public class Window extends JFrame { menuBar.add(aideMenu); menuBar.add(toolsMenu); - 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 !"); }); @@ -97,8 +96,19 @@ public class Window extends JFrame { int width = Integer.valueOf(JOptionPane.showInputDialog("Choose the width")); int height = Integer.valueOf(JOptionPane.showInputDialog("Choose the height")); - this.grid.changeDim(width, height); - this.grid.repaint(); + this.panel.remove(this.grid); + this.grid = new Grid(width, height); + this.panel.add(this.grid); + super.pack(); + super.repaint(); + }); + + radioWall.addActionListener(e -> { + this.grid.setSelected(1); + }); + + radioStart.addActionListener(e -> { + this.grid.setSelected(0); }); this.grid = new Grid(this.universe.getHeight() - 2, this.universe.getWidth() - 2); From fc18dea7f44dbcd749bb905048c70151fea26177 Mon Sep 17 00:00:00 2001 From: Ninja-Jambon Date: Wed, 27 Mar 2024 14:24:27 +0100 Subject: [PATCH 08/17] improvements --- universe/Universe.java | 7 +++++++ userInterface/Grid.java | 31 ++++++++++++++++++++++++++++--- userInterface/Window.java | 7 ++++--- 3 files changed, 39 insertions(+), 6 deletions(-) diff --git a/universe/Universe.java b/universe/Universe.java index a100ec2..4c3bd78 100644 --- a/universe/Universe.java +++ b/universe/Universe.java @@ -172,6 +172,13 @@ public class Universe { else {} } + public void removeObstacle(int pos_i, int pos_j) { + if (this.grid[pos_i][pos_j] == -1) { + this.grid[pos_i][pos_j] = 0; + } + else {} + } + public int possibleChoices(Situation s) { int i = s.pos_i; int j = s.pos_j; diff --git a/userInterface/Grid.java b/userInterface/Grid.java index 9fc19f3..f214ce0 100644 --- a/userInterface/Grid.java +++ b/userInterface/Grid.java @@ -22,31 +22,56 @@ import javax.imageio.ImageIO; import java.net.URL; +import universe.Universe; + public class Grid extends JPanel { private int width; private int height; private JButton[][] mat; private int selected; + private Universe universe; - public Grid(int width, int height) { + public Grid(int width, int height, Universe universe) { super(new GridLayout(height, width)); this.width = width; this.height = height; this.selected = 0; + this.universe = universe; this.mat = new JButton[height][width]; for (int i = 0; i < this.height; i++) { for (int j = 0; j < this.width; j++) { this.mat[i][j] = new JButton(); - this.mat[i][j].setBackground(Color.WHITE); + 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 -> { - System.out.println(coord_i + " " + coord_j); + switch (this.universe.getGrid()[coord_i + 1][coord_j + 1]) { + case 0: + this.mat[coord_i][coord_j].setBackground(Color.RED); + 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; + } + + //Universe.print(this.universe.getGrid(), this.width + 2, this.height + 2, 4, 4); }); super.add(this.mat[i][j]); diff --git a/userInterface/Window.java b/userInterface/Window.java index 2cad948..7378677 100644 --- a/userInterface/Window.java +++ b/userInterface/Window.java @@ -18,7 +18,7 @@ import javax.imageio.ImageIO; import java.net.URL; -import universe.*; +import universe.Universe; public class Window extends JFrame { @@ -96,8 +96,9 @@ public class Window extends JFrame { int width = Integer.valueOf(JOptionPane.showInputDialog("Choose the width")); int height = Integer.valueOf(JOptionPane.showInputDialog("Choose the height")); + this.universe.changeUniverseDim(width + 2, height + 2); this.panel.remove(this.grid); - this.grid = new Grid(width, height); + this.grid = new Grid(width, height, this.universe); this.panel.add(this.grid); super.pack(); super.repaint(); @@ -111,7 +112,7 @@ public class Window extends JFrame { this.grid.setSelected(0); }); - this.grid = new Grid(this.universe.getHeight() - 2, this.universe.getWidth() - 2); + this.grid = new Grid(this.universe.getHeight() - 2, this.universe.getWidth() - 2, this.universe); this.panel.add(grid, BorderLayout.CENTER); super.setJMenuBar(menuBar); From 3068c5ba94852687ab597fe24f21c21905ed5c7e Mon Sep 17 00:00:00 2001 From: Lukian LEIZOUR Date: Thu, 28 Mar 2024 12:00:57 +0100 Subject: [PATCH 09/17] major improvements --- AppLaser.java | 8 +- images/horizontalLaser.png | Bin 0 -> 232 bytes images/startBot.png | Bin 0 -> 424 bytes images/startLeft.png | Bin 0 -> 456 bytes images/startRight.png | Bin 0 -> 451 bytes images/startUp.png | Bin 0 -> 475 bytes images/verticalLaser.png | Bin 0 -> 229 bytes images/wall.png | Bin 0 -> 311 bytes universe/Universe.java | 22 +++-- userInterface/Grid.java | 170 ++++++++++++++++++++++++++++++++++--- userInterface/Window.java | 20 ++++- 11 files changed, 190 insertions(+), 30 deletions(-) create mode 100644 images/horizontalLaser.png create mode 100644 images/startBot.png create mode 100644 images/startLeft.png create mode 100644 images/startRight.png create mode 100644 images/startUp.png create mode 100644 images/verticalLaser.png create mode 100644 images/wall.png 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 0000000000000000000000000000000000000000..0eb7a42c3035d9e8df840feaca0c79aee8f6f437 GIT binary patch literal 232 zcmeAS@N?(olHy`uVBq!ia0vp^Mj*_=1SBWM%0B~AjKx9jP7LeL$-D$|SkfJR9T^xl z_H+M9WCij$3p^r=85sBugD~Uq{1quc!DLSt$B+p3w^s}W8yo~!4sLf8Wn}!fcLB4Y ze^cX0y%#G*FPyx4OZl)?;?xaZ5vps1!Z=sAgdPHNuT|e;-ahZKl*JmOHJesW|6$*i zh@obKmvUrvy~)8zPi_9To&j3Nmm1-j=IhI#1!QvoF$i1=o(!TqUHx3vIVCg!0Pz(IBm9K^v!ZEe+9Gy6y8sGr?E0?^^V|J#j%RGEBQLSvWuWZ{nvc%X?>+ zDt^k+-OYNvuJOvo+iCwZ&fb|axlgXCKQVf(^0Y59k*{XGc&&6#Ve^a^-|y`TNi(^4 zPPcyInY=}R)eU3SW@gNH?KHaT(yrq*(bA{n*rEkz(iib{-!a M>FVdQ&MBb@05rp@`~Uy| literal 0 HcmV?d00001 diff --git a/images/startLeft.png b/images/startLeft.png new file mode 100644 index 0000000000000000000000000000000000000000..a40ccc3629c68fb891907561503bb21470b0d01a GIT binary patch literal 456 zcmeAS@N?(olHy`uVBq!ia0vp^Mj*_=1SBWM%0B~AjKx9jP7LeL$-D$|SkfJR9T^xl z_H+M9WCij$3p^r=85sBugD~Uq{1quc4bGk}jv*25XKzNYJ7mD)dhrmCRdU)L#VO34 zzd9aW@yj{>yS=;R%F@eX2cK?z|66E%d7aF=&-(U~eu-~E^R3FUiBqqK2}C}f)UTkt zKPkZCRc@GIuXFe{uTHaI`AI1YJJUBGb&{Fc`l+#Xj=6XH?Q_h@&d8A4@2*@J#ddWzYh$Ie-`hE(K2pQJ$`TF6*2UngDVzy+Z&1 literal 0 HcmV?d00001 diff --git a/images/startRight.png b/images/startRight.png new file mode 100644 index 0000000000000000000000000000000000000000..108a4275a90fa1b1323e6c53f1e3477dca9ec454 GIT binary patch literal 451 zcmeAS@N?(olHy`uVBq!ia0vp^Mj*_=1SBWM%0B~AjKx9jP7LeL$-D$|SkfJR9T^xl z_H+M9WCij$3p^r=85sBugD~Uq{1quc4R)R`jv*25XK%diYcddMP5i>QBl-Od?*q-b z9;$24T%Y;tU*t)(*OL>sNyz`+r~m)@=6@yMe}(AZIqLJ8>C^8+%dUP~uJdHd#)uQj z%9|!c9!pi6-E+=~Es~8pGu2aP$)v!D+uN>&-#?l9b;@?#XDbdxUEdlLH)+px!?h8Y zXQropG~al2M(UTT+fOz{#&C#h&2QRFU=B|n`)q_cRJ0OENiUiR7(vgn-hI zNP}|KDMyzmBsTT4EME6o=NQZJ*cz=F^LC#|aw{-vU~gg;juT^UYEHBfo?~fz_R-1* zAwvDE$B$Hs^|QE7Gf=en`9h`pJnLsx9!@@aoAi$h6L$1i9<2O4;i!7k$=L>49XI}_ zeO#RICd`LBsBY7b?cFDt+F}-~Pc$u1>^#o6__=j=eM_NMPkQ3|xl?r?M@gJda^sJ* zi<4N_`tgjxt5-UQSFGO5vv}P(qn=$>r?Ch`ozzOYay7Co@Ou5ZUx!j8i^CU1)@R+e sW4yLaJjoz;<#u4O^QA_3ruq6ZXaU(AKnwzxf+vG0Pgg&ebxsLQ0JT86OaK4? literal 0 HcmV?d00001 diff --git a/images/verticalLaser.png b/images/verticalLaser.png new file mode 100644 index 0000000000000000000000000000000000000000..6e9a01b0ee50fb523b6c7fd68e596c9e1a6995c5 GIT binary patch literal 229 zcmeAS@N?(olHy`uVBq!ia0vp^20(1Y!2~3yt~h@iNHG=%xjQkeJ16rJ$YDu$^mSxl z*x1kgCy^D%=PdAuEM{QfI}E~%$MaXD00k2~T^vIq+~1yC$lDMg!0LEQ!BOGGe(_fe zI{2AdZ?fG6|Ujwv@ gFEzq5&DWPf3&`dGVi33#JQ+lJy85}Sb4q9e0HxVTZ~y=R literal 0 HcmV?d00001 diff --git a/images/wall.png b/images/wall.png new file mode 100644 index 0000000000000000000000000000000000000000..6b1825e7aeb8c8edca8c2a64a6333e4ec0d6304b GIT binary patch literal 311 zcmeAS@N?(olHy`uVBq!ia0vp^Mj*_=1SBWM%0B~AjKx9jP7LeL$-D$|SkfJR9T^xl z_H+M9WCij$3p^r=85sBugD~Uq{1quc!A+hnjv*25Z*LlM9X8-;O}xM7`m2l>*)>W= zo3y=7N@rzO{1;;9`Bfn1d~0>e(@3SH?&lwK-o;5LpLsepuS({8YG3Bw@_K*6$cJaH zx%-$z?#|hC$#>f66T1W>JyH}Wwz`N3ff!TMm)`gOE_Pb)?9+&E3a`VFq~K~?r%lr< zp1dt0YEh1Pro4`>tC&wzO6IPY1;rn(iHEg5lHX*{>MJ?TfNTyR27yb#lR=cHtDnm{r-UW|-HdB? literal 0 HcmV?d00001 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); From 9afffd42976bc07976b3da1708c3389c61d15101 Mon Sep 17 00:00:00 2001 From: Lukian LEIZOUR Date: Thu, 28 Mar 2024 14:00:57 +0100 Subject: [PATCH 10/17] very major improvements --- images/crossLaser.png | Bin 0 -> 298 bytes images/miror1.png | Bin 0 -> 494 bytes images/miror2.png | Bin 0 -> 520 bytes images/miror3.png | Bin 0 -> 502 bytes images/miror4.png | Bin 0 -> 517 bytes universe/Universe.java | 24 ++++++++++++-- userInterface/Grid.java | 37 ++++++++++++++------- userInterface/Window.java | 67 +++++++++++++++++++++++++++++++++++++- 8 files changed, 112 insertions(+), 16 deletions(-) create mode 100644 images/crossLaser.png create mode 100644 images/miror1.png create mode 100644 images/miror2.png create mode 100644 images/miror3.png create mode 100644 images/miror4.png diff --git a/images/crossLaser.png b/images/crossLaser.png new file mode 100644 index 0000000000000000000000000000000000000000..7a873b07a8783311f2f0293b85711b475ce5670c GIT binary patch literal 298 zcmeAS@N?(olHy`uVBq!ia0vp^20(1Y!3HFyPDS(pDaPU;cPEB*=VV?2IV|apzK#qG z8~eHcB(eheoCO|{#S9F52SJ!|$HeTnK*6P+E{-7?&TnrRaxobSI9znGZR%Z=u<*Nm z_tvEei`@gR9Aweia{5E$kM%cw&FeR32F=e;-EjL`fL?!fbnTkw-J5LY-^!X3S?xEC z(S$jJ?E=pNNeAPGG={Sbvp@o0cirUQKb2#Do~qH6$63>b1em{fy#IdjQr$uc3%2tz zb`>4}U&ily?=tPBeA0WjL=62vfrP1_m=vEGZGH55zX{O6e5nzhX}-P;T0k}j5QD&_ Q;K?A$)78&qol`;+0N>PUBme*a literal 0 HcmV?d00001 diff --git a/images/miror1.png b/images/miror1.png new file mode 100644 index 0000000000000000000000000000000000000000..971e86a2297d17090e1eed4e008121a7a99e446f GIT binary patch literal 494 zcmeAS@N?(olHy`uVBq!ia0vp^Mj*_=1|;R|J2nC-#^NA%Cx&(BWL^R}Ea{HEjtmSN z`?>!lvI6;>1s;*b3=DkxL735kHCP3xA=A^vF(kwJ?zN41hYSRc6{;JQG;&J(Vym6E z&pOu1SV3)9iH6s2&31;P2X5}${Qg&bj{d~DuD461|8ZUW{VJpA>hVcx$+o>^$qEy4 zIfNVEI0*@yP*V{Uyw}I3D9Mq+J*m^7Oo5dng*m9h;jJQ5#|8;0=N17EiBHbT%8o2q zpVzDQIDFkcjfv@MtA=8LZHE$*p(1Cy#WW+Pm-64ZIF{_*s--wVZiy1pO9w6G2{ua@ znM#-ct$T4jJ4)=BgUmg1lgYmCt|cjzEkCsSkDpQWYD1=}&O8bMl1Dh0p3a}>bL4f7 zpYhF43alMhzZJ*$tczHqd_HyBzO51+%U$;i|Kym$7U(?Xc)N#!$YaMK!HxI%p3ZZY zQMi!YA!lvI6;>1s;*b3=DkxL735kHCP3xq1n^LF(kwJ?v0IphYds=0(Y0N8g6KCNNQ;8 zf1W%;LSmvs1LM@zMy-O*#cE!&Zf%_Z_IPX(zhvJm@tqsb=DRKUzB>BzroUyow{ZSfeGk2FconT_Q${N|Rph`iI zV->Sd=YmfYSU8IBh_tCV1tgvL;9(T)#G=}^=I&RAPQfYvFA6yX{7m$3Sg>h=3&*Dx z7M_jyoPtj{KguNC7gE;vzgV?l!KcIbL>m_rczk{+X6(B+>3Nq+fW?t{yN=G|QaG_M z_Fl{Vrx%}yPx&3by;#+0LaoJqvBocx-6s6l@Z4d7hKhL85d}*@6|*;%f?M9)UVkER zPVIx$yTgAhR{c`1#O9bNVCYiwZ$g*g6ZVxdj_rzE0w(-{O$+`CxN^*D({R2Zu}Hwt zxg&_>twNXJ6{jmo`|eD2e*EnH7BioV`WH>*%33+3oTeycum|=m5L+bUxcYy?f{z=@ zHJvV)xJWh~6KLf~?)k|$)%{p^=hJFtU>xzKMtG+A`Z8z%*&IL&0+)g(gD6i|KbLh* G2~7YCcg}wR literal 0 HcmV?d00001 diff --git a/images/miror3.png b/images/miror3.png new file mode 100644 index 0000000000000000000000000000000000000000..7ee3d2c2bb8b68b1150bd527a65f05e490c2cff3 GIT binary patch literal 502 zcmeAS@N?(olHy`uVBq!ia0vp^Mj*_=1|;R|J2nC-#^NA%Cx&(BWL^R}Ea{HEjtmSN z`?>!lvI6;>1s;*b3=Dh+K$tP>S|=w^L!qaOV@QVc-D?~D4mXUrek?gbUjh~_E&(!Av z#T8284KLkJTc+($?lXM3$@=ILfhPja&J&E!+!uJl@!6im^5C3y1@;+*0x2@aa!owR zpA=4LsO_;|)1m!1b7y7uax*UPyHh$_@7`T3@#NpDT@q^bTa2py{#^I%_3a-keGAUG zD6SB3lWFow=yJFqc)8{%ciORzy;}<&E>}I>KO>G|VP<#wzFXG2>*G`?TfNTyR27yb#lR=cHtDnm{r-UW|g1pJA literal 0 HcmV?d00001 diff --git a/images/miror4.png b/images/miror4.png new file mode 100644 index 0000000000000000000000000000000000000000..f8eaf63b3d9c92024647271f003ba7ec701b6e6d GIT binary patch literal 517 zcmeAS@N?(olHy`uVBq!ia0vp^Mj*_=1|;R|J2nC-#^NA%Cx&(BWL^R}Ea{HEjtmSN z`?>!lvI6;>1s;*b3=Dh+L6~vJ#O${~4Go?yjv*P&cW>A>|`u$5*7KO+4txQ`^Wj#&)3~w+IK}!{^s&8qqdhPXU;Hckt~10 zWbpZ-QD5Y_jzk}uH*SWuKQ|e7U)DJp79@nEsSn#O;X8E z6jms@i8V>@1;4UmOBh-Nac$lS+83L!U7_-0$;Aeb*Dmaq7T}wgoCZ#rDgmp1imF lD*ql}1o5Rtc&7RKGH3zW96$^Lmx3pQC{I^Emvv4FO#sJR%ANoK literal 0 HcmV?d00001 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(); }); From 2f51df0d82162dd2e7a5238eafcaa8daef3b507f Mon Sep 17 00:00:00 2001 From: Lukian LEIZOUR Date: Thu, 28 Mar 2024 15:31:02 +0100 Subject: [PATCH 11/17] commit --- userInterface/Grid.java | 119 +++++++++++++++++++++++--------------- userInterface/Window.java | 66 ++++++++++++++++++--- 2 files changed, 129 insertions(+), 56 deletions(-) diff --git a/userInterface/Grid.java b/userInterface/Grid.java index ae42f90..5360e52 100644 --- a/userInterface/Grid.java +++ b/userInterface/Grid.java @@ -26,6 +26,8 @@ import universe.Universe; import universe.Stack; import universe.Situation; +import java.time.Instant; + public class Grid extends JPanel { private int width; private int height; @@ -33,8 +35,10 @@ public class Grid extends JPanel { private int selected; private Universe universe; private int button_width, button_height; + private int refreshRate; + private boolean solving; - public Grid(int width, int height, Universe universe) { + public Grid(int width, int height, Universe universe, int refreshRate) { super(new GridLayout(height, width)); this.width = width; this.height = height; @@ -42,6 +46,8 @@ public class Grid extends JPanel { this.universe = universe; this.button_width = 600 / this.height; this.button_height = button_width; + this.refreshRate = refreshRate; + this.solving = false; this.mat = new JButton[height][width]; @@ -55,6 +61,10 @@ public class Grid extends JPanel { final int coord_j = j; this.mat[i][j].addActionListener(e -> { + if (solving) { + return; + } + switch (this.universe.getGrid()[coord_i + 1][coord_j + 1]) { case 0: if (this.selected == 1) { @@ -93,101 +103,113 @@ public class Grid extends JPanel { break; } - this.printUniverse(); + this.printUniverseGrid(this.universe.getGrid()); }); super.add(this.mat[i][j]); } } - this.printUniverse(); + this.printUniverseGrid(this.universe.getGrid()); } 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 setRefreshRate(int refreshRate) { + this.refreshRate = refreshRate; } - public void printUniverse() { + public void setSolving(boolean solving) { + this.solving = solving; + } + + public void printUniverseGrid(int [][] universeGrid) { 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]) { + switch (universeGrid[i + 1][j + 1]) { case -1: - 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)); + this.changeButtonState(i, j, "../images/wall.png"); break; case 0: - this.mat[i][j].setBackground(Color.WHITE); - this.mat[i][j].setIcon(null); + this.changeButtonState(i, j, null); break; case 1: - 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)); + this.changeButtonState(i, j, "../images/verticalLaser.png"); break; case 2: - 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)); + this.changeButtonState(i, j, "../images/horizontalLaser.png"); break; case 3: - 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)); + this.changeButtonState(i, j, "../images/crossLaser.png"); break; 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)); + this.changeButtonState(i, j, "../images/miror1.png"); break; case 5: - 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)); + this.changeButtonState(i, j, "../images/miror2.png"); 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)); + this.changeButtonState(i, j, "../images/miror3.png"); 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)); + this.changeButtonState(i, j, "../images/miror4.png"); break; case 10: - 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)); + this.changeButtonState(i, j, "../images/startUp.png"); break; case 11: - 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)); + this.changeButtonState(i, j, "../images/startBot.png"); break; case 12: - 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)); + this.changeButtonState(i, j, "../images/startRight.png"); break; case 13: - 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)); + this.changeButtonState(i, j, "../images/startLeft.png"); break; } } } } + public void changeButtonState(int coord_i, int coord_j, String url) { + if (url == null) { + Thread t = new Thread(new Runnable() { + @Override + public void run() { + mat[coord_i][coord_j].setBackground(Color.WHITE); + mat[coord_i][coord_j].setIcon(null); + } + }); + + t.start(); + } else { + Thread t = new Thread(new Runnable() { + @Override + public void run() { + Image image = new ImageIcon(getClass().getResource(url)).getImage().getScaledInstance(button_width, button_height, Image.SCALE_SMOOTH); + mat[coord_i][coord_j].setIcon(new ImageIcon(image)); + } + }); + + t.start(); + } + } + public void reset() { this.universe.resetUniverse(); - this.printUniverse(); + this.printUniverseGrid(this.universe.getGrid()); } public void solve() { - this.setButtonsEnabled(false); this.universe.resetUniverse(); final Universe universe = this.universe; - Thread t1 = new Thread(new Runnable() { + this.solving = true; + + Thread computeThread = new Thread(new Runnable() { @Override public void run() { int [] startCoords = universe.getStartCoords(); @@ -208,6 +230,8 @@ public class Grid extends JPanel { int best_filled_boxes = 0; int best_nb_mirrors = 0; + long lastPrinted = Instant.now().toEpochMilli(); + do { if (universe.canEvolve(currentState)) { stack.push(currentState.copy(universe.possibleChoices(currentState))); @@ -218,9 +242,9 @@ 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(); + if (Instant.now().toEpochMilli() - lastPrinted > refreshRate) { + printUniverseGrid(bestGrid); + } } } else if (stack.size() > 0) { @@ -229,12 +253,13 @@ public class Grid extends JPanel { } else { break; } - } while (stack.size() != 0); + } while (stack.size() != 0 && solving == true); + solving = false; + Universe.print(bestGrid, width + 2, height + 2, 4, 4); + printUniverseGrid(bestGrid); } }); - t1.start(); - - this.setButtonsEnabled(true); + computeThread.start(); } } \ No newline at end of file diff --git a/userInterface/Window.java b/userInterface/Window.java index a84d6ec..5eec67e 100644 --- a/userInterface/Window.java +++ b/userInterface/Window.java @@ -48,6 +48,9 @@ public class Window extends JFrame { JMenu fichierMenu = new JMenu("File"); JMenu aideMenu = new JMenu("Help"); JMenu toolsMenu = new JMenu("Tools"); + JMenu solveMenu = new JMenu("Solve"); + JMenu displayMenu = new JMenu("Display"); + JMenu refreshRate = new JMenu("Refresh Rate"); URL newUrl = getClass().getResource("../images/new.png"); URL openUrl = getClass().getResource("../images/open.png"); @@ -57,9 +60,9 @@ public class Window extends JFrame { ImageIcon openIcon = new ImageIcon(openUrl.getPath()); ImageIcon saveIcon = new ImageIcon(saveUrl.getPath()); - JMenuItem nouveauItem = new JMenuItem("Nouveau", nouveauIcon); - JMenuItem ouvrirItem = new JMenuItem("Ouvrir", openIcon); - JMenuItem enregistrerItem = new JMenuItem("Enregistrer", saveIcon); + JMenuItem nouveauItem = new JMenuItem("New", nouveauIcon); + JMenuItem ouvrirItem = new JMenuItem("Open", openIcon); + JMenuItem enregistrerItem = new JMenuItem("Save", saveIcon); JMenuItem apropos = new JMenuItem("About"); JMenuItem regles = new JMenuItem("Rules"); @@ -75,8 +78,24 @@ public class Window extends JFrame { buttonGroup.add(radioStart); JMenuItem changeSize = new JMenuItem("Change Size"); - JMenuItem solve = new JMenuItem("Solve"); JMenuItem reset = new JMenuItem("Reset"); + + JMenuItem solve = new JMenuItem("Start"); + JMenuItem stop = new JMenuItem("Stop"); + + JRadioButtonMenuItem radio10ms = new JRadioButtonMenuItem("10ms"); + JRadioButtonMenuItem radio200ms = new JRadioButtonMenuItem("200ms"); + JRadioButtonMenuItem radio500ms = new JRadioButtonMenuItem("500ms"); + JRadioButtonMenuItem radio1000ms = new JRadioButtonMenuItem("1000ms"); + + radio10ms.setSelected(true); + + ButtonGroup refreshRates = new ButtonGroup(); + + refreshRates.add(radio10ms); + refreshRates.add(radio200ms); + refreshRates.add(radio500ms); + refreshRates.add(radio1000ms); fichierMenu.add(nouveauItem); fichierMenu.add(ouvrirItem); @@ -94,15 +113,28 @@ public class Window extends JFrame { toolsMenu.addSeparator(); toolsMenu.add(reset); + solveMenu.add(solve); + solveMenu.add(stop); + + refreshRate.add(radio10ms); + refreshRate.add(radio200ms); + refreshRate.add(radio500ms); + refreshRate.add(radio1000ms); + + displayMenu.add(refreshRate); + menuBar.add(fichierMenu); menuBar.add(aideMenu); menuBar.add(toolsMenu); + menuBar.add(solveMenu); + menuBar.add(displayMenu); nouveauItem.addActionListener(e -> { - this.universe.changeUniverseDim(5, 5); this.universe.changeUniverseStart(1, 1, 11); + this.universe.changeUniverseDim(5, 5); + this.universe.resetUniverseObstacles(); this.panel.remove(this.grid); - this.grid = new Grid(3, 3, this.universe); + this.grid = new Grid(3, 3, this.universe, 10); this.panel.add(this.grid); super.pack(); super.repaint(); @@ -149,7 +181,7 @@ public class Window extends JFrame { } this.panel.remove(this.grid); - this.grid = new Grid(universe_width, universe_height, this.universe); + this.grid = new Grid(universe_width, universe_height, this.universe, 10); this.panel.add(this.grid); super.pack(); super.repaint(); @@ -167,7 +199,7 @@ public class Window extends JFrame { this.universe.changeUniverseDim(width + 2, height + 2); this.panel.remove(this.grid); - this.grid = new Grid(width, height, this.universe); + this.grid = new Grid(width, height, this.universe, 10); this.panel.add(this.grid); super.pack(); super.repaint(); @@ -185,11 +217,27 @@ public class Window extends JFrame { this.grid.solve(); }); + stop.addActionListener(e -> { + this.grid.setSolving(false); + }); + reset.addActionListener(e -> { this.grid.reset(); }); + + radio200ms.addActionListener(e -> { + this.grid.setRefreshRate(200); + }); + + radio500ms.addActionListener(e -> { + this.grid.setRefreshRate(500); + }); + + radio1000ms.addActionListener(e -> { + this.grid.setRefreshRate(1000); + }); - this.grid = new Grid(this.universe.getHeight() - 2, this.universe.getWidth() - 2, this.universe); + this.grid = new Grid(this.universe.getHeight() - 2, this.universe.getWidth() - 2, this.universe, 10); this.panel.add(grid, BorderLayout.CENTER); super.setJMenuBar(menuBar); From 42746ad5b09deaf9db0a32cbac0fd4b0acbb1910 Mon Sep 17 00:00:00 2001 From: Lukian LEIZOUR Date: Thu, 28 Mar 2024 18:15:42 +0100 Subject: [PATCH 12/17] commit --- images/horizontalLaser.png | Bin 232 -> 243 bytes images/verticalLaser.png | Bin 229 -> 247 bytes saves/maze1.txt | 187 +++++++++++++++++++++++++++++++++++++ saves/maze2.txt | 155 ++++++++++++++++++++++++++++++ userInterface/Grid.java | 17 ++-- userInterface/Window.java | 3 +- 6 files changed, 353 insertions(+), 9 deletions(-) create mode 100644 saves/maze1.txt create mode 100644 saves/maze2.txt diff --git a/images/horizontalLaser.png b/images/horizontalLaser.png index 0eb7a42c3035d9e8df840feaca0c79aee8f6f437..b7a14ba4a69ebc9cfa2180959b01d535f941992d 100644 GIT binary patch delta 155 zcmaFC_?dBnBnKM<1B0A>$Hs|@8ubSm82An{?wFYU7ATnK>EalY;r#Z-M!{wW0hWXH zn-*|6v;BO&$U=lALEy-X)weCHdy32A`ZqSb-COVP(K|s((bOrcIHoM=NgTyfuPBYA_`@qM#)$r%-q=|>r0CrPAY5)KL delta 149 zcmey&_=0hQBnJ}%1H;5v`DYUqHR=yDFz_8_Jf6QI1t^&8>Eaj?;r{lDpXy($K<>5bd(7MCJ(jXq oW3*<|%IQDsyAm-bV5CSFzm0L+9vEC2ui diff --git a/images/verticalLaser.png b/images/verticalLaser.png index 6e9a01b0ee50fb523b6c7fd68e596c9e1a6995c5..b50d083827dd98e015c5ce0beb0873b48d0ab790 100644 GIT binary patch delta 159 zcmaFL_?>ZrBnKM<1B2A5h@Odx8ubSm82An{?wFYU7ARQc>EalY;r#aeZq5b=5!Q>( zm$N7zEcmT|BEVo1OKR+gsT&M9iYJ}sO<&<#QkJWF?)kazk;^ToC7-{3e#Y-;whKH9 jBpr+!(iqM%%wjZQ&LAidA;_t}&YLTtQzvxZ#QkajFeN+w delta 149 zcmey)_>^&iBnJ}%1H;r6=WkC`)Tlqqz`%Ey@p%4<6rf;&r;B4qg!|ib3wav?1Xvw! zDL5*;*f0KSK?grm>rED(B(>%5X3jmvetgs0&%buOt~|ehs6RRxJ;F~7 W*m{b3>T7^@@ufz1ruk01B?|y&i98+v diff --git a/saves/maze1.txt b/saves/maze1.txt new file mode 100644 index 0000000..fed829f --- /dev/null +++ b/saves/maze1.txt @@ -0,0 +1,187 @@ +10 +20 +11 +1 +1 +1 +2 +1 +6 +1 +10 +1 +14 +1 +18 +1 +20 +2 +2 +2 +4 +2 +6 +2 +8 +2 +10 +2 +12 +2 +14 +2 +16 +2 +18 +2 +20 +3 +2 +3 +4 +3 +6 +3 +8 +3 +10 +3 +12 +3 +14 +3 +16 +3 +18 +3 +20 +4 +2 +4 +4 +4 +6 +4 +8 +4 +10 +4 +12 +4 +14 +4 +16 +4 +18 +4 +20 +5 +2 +5 +4 +5 +6 +5 +8 +5 +10 +5 +12 +5 +14 +5 +16 +5 +18 +5 +20 +6 +2 +6 +4 +6 +6 +6 +8 +6 +10 +6 +12 +6 +14 +6 +16 +6 +18 +6 +20 +7 +2 +7 +4 +7 +6 +7 +8 +7 +10 +7 +12 +7 +14 +7 +16 +7 +18 +7 +20 +8 +2 +8 +4 +8 +6 +8 +8 +8 +10 +8 +12 +8 +14 +8 +16 +8 +18 +8 +20 +9 +2 +9 +4 +9 +6 +9 +8 +9 +10 +9 +12 +9 +14 +9 +16 +9 +18 +9 +20 +10 +4 +10 +8 +10 +12 +10 +16 +10 +20 diff --git a/saves/maze2.txt b/saves/maze2.txt new file mode 100644 index 0000000..50d3371 --- /dev/null +++ b/saves/maze2.txt @@ -0,0 +1,155 @@ +10 +20 +11 +1 +1 +1 +2 +1 +6 +1 +10 +1 +14 +1 +18 +1 +20 +2 +2 +2 +4 +2 +6 +2 +7 +2 +8 +2 +10 +2 +12 +2 +13 +2 +14 +2 +15 +2 +16 +2 +18 +2 +20 +3 +4 +3 +18 +4 +2 +4 +4 +4 +5 +4 +6 +4 +8 +4 +9 +4 +10 +4 +12 +4 +14 +4 +16 +4 +18 +4 +19 +4 +20 +5 +12 +5 +14 +6 +2 +6 +4 +6 +6 +6 +7 +6 +8 +6 +10 +6 +11 +6 +12 +6 +14 +6 +15 +6 +16 +6 +18 +6 +19 +6 +20 +7 +1 +7 +2 +8 +2 +8 +4 +8 +6 +8 +8 +8 +10 +8 +11 +8 +12 +8 +13 +8 +14 +8 +16 +8 +17 +8 +18 +8 +20 +9 +4 +9 +12 +9 +16 +9 +18 +10 +3 +10 +4 +10 +8 +10 +12 +10 +16 +10 +20 diff --git a/userInterface/Grid.java b/userInterface/Grid.java index 5360e52..8e25f79 100644 --- a/userInterface/Grid.java +++ b/userInterface/Grid.java @@ -64,6 +64,8 @@ public class Grid extends JPanel { if (solving) { return; } + + this.universe.resetUniverse(); switch (this.universe.getGrid()[coord_i + 1][coord_j + 1]) { case 0: @@ -203,6 +205,10 @@ public class Grid extends JPanel { this.printUniverseGrid(this.universe.getGrid()); } + public void alert(String message) { + JOptionPane.showMessageDialog(this, message); + } + public void solve() { this.universe.resetUniverse(); final Universe universe = this.universe; @@ -230,7 +236,7 @@ public class Grid extends JPanel { int best_filled_boxes = 0; int best_nb_mirrors = 0; - long lastPrinted = Instant.now().toEpochMilli(); + long start = Instant.now().toEpochMilli(); do { if (universe.canEvolve(currentState)) { @@ -241,10 +247,7 @@ public class Grid extends JPanel { bestGrid = universe.copyGrid(); best_filled_boxes = universe.getFilledBoxes(); best_nb_mirrors = universe.getNbMirrors(); - - if (Instant.now().toEpochMilli() - lastPrinted > refreshRate) { - printUniverseGrid(bestGrid); - } + printUniverseGrid(bestGrid); } } else if (stack.size() > 0) { @@ -256,8 +259,8 @@ public class Grid extends JPanel { } while (stack.size() != 0 && solving == true); solving = false; - Universe.print(bestGrid, width + 2, height + 2, 4, 4); - printUniverseGrid(bestGrid); + String message = "Solved in " + ((Instant.now().toEpochMilli() - start)/1000) + "s and " + ((Instant.now().toEpochMilli() - start)%1000) + "ms \nMirrors : " + best_nb_mirrors + "\nLaser length : " + best_filled_boxes; + alert(message); } }); computeThread.start(); diff --git a/userInterface/Window.java b/userInterface/Window.java index 5eec67e..b9c4576 100644 --- a/userInterface/Window.java +++ b/userInterface/Window.java @@ -109,8 +109,6 @@ public class Window extends JFrame { toolsMenu.addSeparator(); toolsMenu.add(changeSize); toolsMenu.addSeparator(); - toolsMenu.add(solve); - toolsMenu.addSeparator(); toolsMenu.add(reset); solveMenu.add(solve); @@ -167,6 +165,7 @@ public class Window extends JFrame { this.universe.changeUniverseDim(universe_width + 2, universe_height + 2); this.universe.changeUniverseStart(start_i, start_j, start_dir); + this.universe.resetUniverseObstacles(); while (true) { try { From bb09c5afca8aaf40a0dca63a1579e40ba0c2b1eb Mon Sep 17 00:00:00 2001 From: Lukian LEIZOUR Date: Thu, 28 Mar 2024 23:33:35 +0100 Subject: [PATCH 13/17] bug resolution --- images/startBot.png | Bin 424 -> 428 bytes images/startLeft.png | Bin 456 -> 428 bytes images/startRight.png | Bin 451 -> 393 bytes images/startUp.png | Bin 475 -> 374 bytes universe/Universe.java | 10 +++++++++- userInterface/Grid.java | 16 +++++++++++++--- 6 files changed, 22 insertions(+), 4 deletions(-) diff --git a/images/startBot.png b/images/startBot.png index 743e36ce86f27e5c8ca2f072a07583bdca4d4cc7..2273518e1aa60fd1d08188ffcf341990af03edae 100644 GIT binary patch delta 343 zcmV-d0jU0{1FQp(83+ad00168h_R6&Cw~%j000tn0p4aGcmMzaCrLy>RA@u(m`w`8 zFc5{~&k@`x2yVQP7jWk_+H<(`0^Y^7OTm>#P}GSTi4X&(GcqafgV3f6d7m#yp@aJq z*8_&(T0OlH@k^_1+c2#Y%Kx!PY7>g{CO5lvp;W%?sbx(lm#6sn)sigz-C~iLu76Sz ze~QTQ+a-upvtsX&l0Z@t1m5h{P#0NkcYbIiD)P&km)swOS|h2T(&1xNL?|!~T2lMX z+7G(t=NhPrumB5HD}qJ}KqEOgFd8WUjpX3KXrur%l7j=Ikpj?24i5a!MjU+(lBQ{z z_w_Jc_z^$TK&27lTO(Siim(6+X+VmId}&O0fQU69VhxB`10uF??g5b8)Ug$Pj%AaA0VdKMnSB5N delta 339 zcmV-Z0j&P41E>R#83+OZ005C)ALfxECw~sZ000id0mpBsWB>pGBS}O-R9Hvt*+CA1 zFc3yz^ak9R8+!zNghrwPI{(Z-T+G|Zg4!o^N}_Y>YLp-j_t_MDp^)5aereI zZ)5#o@@hHW&7~poYAw43*M5Q2vD;?muGDvzX}j z-nvv~G||p2f04{?qWvc^T_%}mpGAo>)kKFZN|8BCbjG5f%x9twi`+7|iTW&Z$RZ{( zu}G2yO=M+}Q5H9mz@n}!#YCR37(D76>iMLt)|xbp!$cky87K3Z$jPGalx`FGS#&aS l#6%$$?M@#wQQG&!l@bT7rLi&_hBpX5tQD*}NcEG30VW}_oZSEb diff --git a/images/startLeft.png b/images/startLeft.png index a40ccc3629c68fb891907561503bb21470b0d01a..1705cecc0c6ed6672faad2fcb068f702f1da7394 100644 GIT binary patch delta 343 zcmV-d0jU1S1FQp(83+ad00168h_R6&Cw~%j000tn0p4aGcmMzaCrLy>RA@u(m%R}d3_(T50_?&PG^_vuA9183`EuLeb_10Y^g6Ag_wvS#BFs=8z=a=UWM2+;RAj@=b=Rx<57P$CcBV7RvT+aPKTYYswRAf4gCD# z{(9=tobCCdD|ImybnGL$klz+-=Q(mc?!V2r<{COSfjg4Sg6QnX(7$YxZTXoa%Xy(o zbN(i~udHG$&g%%jdELy|u!a97nLq^*uP9T|CyalW%-b*aBhrA7sTmqxa6}pqA`J+U pMpQ!j9>H)V6kV>sk&v{xl8htbcZgpGLrFwIR9HvN*;}r{ zFbqY}!VEfNX51l^1Bv>G;?z!T$NPtigw&Sx)RP$$dOgnC%kCM0G#n2?O+V?qL!n+b7Qlxf?x{FiF1clowyU4Pf~ zRHbyR=e3-7VzD?T3tx-nywf$3B{Hc)>zBOqPQ^`wqgU3Nc%s#8rgq1IXxBiGXXTp+Er)GeipKm4Y81sOyL)@RPt#l)mWM> R%NJD^pG9>URGX8A0VX7`uVerK diff --git a/images/startRight.png b/images/startRight.png index 108a4275a90fa1b1323e6c53f1e3477dca9ec454..dac5643c85c6f8458cd2f031953512b90ad40894 100644 GIT binary patch delta 308 zcmV-40n7fw1BnBW83+ad00168h_R6&Cw~%j000tn0p4aGcmMza1W80eRA@u(mq8A~ zAP_~-ctS7W&XtGY940PYxb;|GLmdJM)M)7pG4rSM67uPa`TYYeo9CNgfNj=9j4{vf zXpA89VFWc5yNM8psn|}0Kug7bB51e~ZMYF_{4|1e1bb^~W7Y^tdvzs(V5Yh1E`KRl zn>n_T5sX|v zQk7!ngD5_=dfs-gN)Yc6vOY=+oXsvyBo!eTg?i4x%m(S{q*##bXqM;$L=I}x-r2)l_Ora_oVq`4vD62c3t)B~US6Q7%t GodG7U)QB|z delta 366 zcmV-!0g?WR1H%K583+OZ005C)ALfxECw~sZ000id0mpBsWB>pGK1oDDR9HvN*zJyj zFc5`d^bWXV@0d%#gKbD9t<2Y%`ul9zv4{wA3(o}XR4Xyv5;Jr%0fa} zHVa8)6Ike0Hiv~qveGmb>dMN%X0m=Xlhr+7Cr@j1=$GcOZXZMwSdxchHcR-xPGt#A zD32vKq0Z+mPouM2WkSKz>b||!gjeV{(O^RHtkz*Op@S8;TWw|&qI`FX#rK#HWw)Fr zL|^$c)v6@1R3?5T+?hI&vK#;qQ8NkP1BkMz9WcgfG9phydfOhSLIRg|~iwcz9?x>^;adKg0leGaRKHzuK delta 406 zcmeyybenmCBnJ}%1H;5v`DYUq)$92VGcfQSW;~w1B87p0G0fA&F(ktM?YY39!wx(w zfqrL1Ep{>HbI<7spBAR474q`0LkEoN65+u$WU5cC|^xpcPfDPa2)Iyh@WsUWmYDq$oXSnN& z_l|Y_9$TX|W8Ur)Np1yZ4eU+K!f|5E zP0fim!gDN*&pulDAVjF2_4tuWv3?f!X$FcGKVPVHpJ)Bd%EQSgZsmjaF?jV#=kSWvn|T(mJ7?6h%jz^1fvA&ONms5$wgq0V uKlkfUiez#4qR9HJ+jflCwuvVh stack = new Stack (); Situation currentState = new Situation(firstState_i, firstState_j, start_dir, 0); - int [][] bestGrid = universe.copyGrid(); + this.bestGrid = universe.copyGrid(); + System.out.println(this.bestGrid[2][3] + " " + this.bestGrid + " " + universe.getGrid()); int best_filled_boxes = 0; int best_nb_mirrors = 0; long start = Instant.now().toEpochMilli(); + long lastRefresh = start; do { if (universe.canEvolve(currentState)) { @@ -244,10 +247,10 @@ public class Grid extends JPanel { currentState = universe.evolve(currentState); if ((universe.getFilledBoxes() > best_filled_boxes) || (universe.getFilledBoxes() == best_filled_boxes && universe.getNbMirrors() < best_nb_mirrors)) { - bestGrid = universe.copyGrid(); + this.bestGrid = universe.copyGrid(); best_filled_boxes = universe.getFilledBoxes(); best_nb_mirrors = universe.getNbMirrors(); - printUniverseGrid(bestGrid); + //printUniverseGrid(this.bestGrid); } } else if (stack.size() > 0) { @@ -256,8 +259,15 @@ public class Grid extends JPanel { } else { break; } + if (Instant.now().toEpochMilli() - lastRefresh > refreshRate) { + lastRefresh = Instant.now().toEpochMilli(); + printUniverseGrid(this.bestGrid); + + } } while (stack.size() != 0 && solving == true); + printUniverseGrid(bestGrid); + solving = false; String message = "Solved in " + ((Instant.now().toEpochMilli() - start)/1000) + "s and " + ((Instant.now().toEpochMilli() - start)%1000) + "ms \nMirrors : " + best_nb_mirrors + "\nLaser length : " + best_filled_boxes; alert(message); From 6c65f8d5518dec2fe6b07636953ad3ccd963e130 Mon Sep 17 00:00:00 2001 From: Lukian LEIZOUR Date: Thu, 28 Mar 2024 23:39:05 +0100 Subject: [PATCH 14/17] commit --- userInterface/Grid.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/userInterface/Grid.java b/userInterface/Grid.java index e7edfa5..7969a43 100644 --- a/userInterface/Grid.java +++ b/userInterface/Grid.java @@ -250,7 +250,6 @@ public class Grid extends JPanel { this.bestGrid = universe.copyGrid(); best_filled_boxes = universe.getFilledBoxes(); best_nb_mirrors = universe.getNbMirrors(); - //printUniverseGrid(this.bestGrid); } } else if (stack.size() > 0) { @@ -262,7 +261,6 @@ public class Grid extends JPanel { if (Instant.now().toEpochMilli() - lastRefresh > refreshRate) { lastRefresh = Instant.now().toEpochMilli(); printUniverseGrid(this.bestGrid); - } } while (stack.size() != 0 && solving == true); From ae86967b7925699ef3c62d74397989a99b470bb0 Mon Sep 17 00:00:00 2001 From: Ninja-Jambon Date: Fri, 29 Mar 2024 10:56:13 +0100 Subject: [PATCH 15/17] commit --- saves/roux.txt | 147 ++++++++++++++++++++++++++++++++++++++++ userInterface/Grid.java | 1 - 2 files changed, 147 insertions(+), 1 deletion(-) create mode 100644 saves/roux.txt diff --git a/saves/roux.txt b/saves/roux.txt new file mode 100644 index 0000000..bbfec18 --- /dev/null +++ b/saves/roux.txt @@ -0,0 +1,147 @@ +20 +13 +10 +20 +3 +1 +1 +2 +1 +2 +3 +2 +12 +3 +1 +3 +3 +3 +12 +4 +3 +4 +12 +5 +3 +5 +12 +6 +3 +6 +4 +6 +5 +6 +6 +6 +7 +6 +8 +6 +9 +6 +10 +6 +12 +7 +12 +8 +12 +9 +7 +9 +8 +9 +9 +9 +12 +10 +5 +10 +9 +10 +11 +10 +12 +11 +2 +11 +3 +11 +9 +12 +2 +13 +2 +14 +2 +14 +12 +15 +2 +15 +5 +15 +6 +15 +7 +15 +8 +15 +9 +15 +10 +15 +11 +15 +12 +16 +2 +16 +4 +16 +5 +16 +6 +16 +7 +16 +8 +16 +9 +16 +10 +16 +11 +16 +12 +17 +2 +18 +2 +18 +5 +18 +9 +18 +10 +18 +11 +18 +12 +19 +5 +19 +8 +19 +9 +19 +10 +19 +11 +19 +12 +20 +1 +20 +5 diff --git a/userInterface/Grid.java b/userInterface/Grid.java index 7969a43..a3ea9d5 100644 --- a/userInterface/Grid.java +++ b/userInterface/Grid.java @@ -234,7 +234,6 @@ public class Grid extends JPanel { Situation currentState = new Situation(firstState_i, firstState_j, start_dir, 0); this.bestGrid = universe.copyGrid(); - System.out.println(this.bestGrid[2][3] + " " + this.bestGrid + " " + universe.getGrid()); int best_filled_boxes = 0; int best_nb_mirrors = 0; From 5016c1a888db46ea015107745fd7d3bd8a21ed25 Mon Sep 17 00:00:00 2001 From: Ninja-Jambon Date: Fri, 29 Mar 2024 11:20:32 +0100 Subject: [PATCH 16/17] commit --- userInterface/Grid.java | 22 +++++++++++++++++++--- userInterface/Window.java | 28 ++++++++++++++++++++++++---- 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/userInterface/Grid.java b/userInterface/Grid.java index a3ea9d5..1704e98 100644 --- a/userInterface/Grid.java +++ b/userInterface/Grid.java @@ -37,8 +37,9 @@ public class Grid extends JPanel { private int button_width, button_height; private int refreshRate; private boolean solving; + private int display; - public Grid(int width, int height, Universe universe, int refreshRate) { + public Grid(int width, int height, Universe universe, int refreshRate, int display) { super(new GridLayout(height, width)); this.width = width; this.height = height; @@ -48,6 +49,7 @@ public class Grid extends JPanel { this.button_height = button_width; this.refreshRate = refreshRate; this.solving = false; + this.display = display; this.mat = new JButton[height][width]; @@ -127,6 +129,10 @@ public class Grid extends JPanel { this.solving = solving; } + public void setDisplay(int display) { + this.display = display; + } + public void printUniverseGrid(int [][] universeGrid) { for (int i = 0; i < this.height; i++) { for (int j = 0; j < this.width; j ++) { @@ -245,10 +251,20 @@ public class Grid extends JPanel { stack.push(currentState.copy(universe.possibleChoices(currentState))); currentState = universe.evolve(currentState); + if (display == 1 && Instant.now().toEpochMilli() - lastRefresh > refreshRate) { + lastRefresh = Instant.now().toEpochMilli(); + printUniverseGrid(universe.getGrid()); + } + if ((universe.getFilledBoxes() > best_filled_boxes) || (universe.getFilledBoxes() == best_filled_boxes && universe.getNbMirrors() < best_nb_mirrors)) { this.bestGrid = universe.copyGrid(); best_filled_boxes = universe.getFilledBoxes(); best_nb_mirrors = universe.getNbMirrors(); + + if (display == 2 && Instant.now().toEpochMilli() - lastRefresh > refreshRate) { + lastRefresh = Instant.now().toEpochMilli(); + printUniverseGrid(this.bestGrid); + } } } else if (stack.size() > 0) { @@ -257,9 +273,9 @@ public class Grid extends JPanel { } else { break; } - if (Instant.now().toEpochMilli() - lastRefresh > refreshRate) { + if (display == 0 && Instant.now().toEpochMilli() - lastRefresh > refreshRate) { lastRefresh = Instant.now().toEpochMilli(); - printUniverseGrid(this.bestGrid); + printUniverseGrid(universe.getGrid()); } } while (stack.size() != 0 && solving == true); diff --git a/userInterface/Window.java b/userInterface/Window.java index b9c4576..b911b27 100644 --- a/userInterface/Window.java +++ b/userInterface/Window.java @@ -96,6 +96,10 @@ public class Window extends JFrame { refreshRates.add(radio200ms); refreshRates.add(radio500ms); refreshRates.add(radio1000ms); + + JMenuItem displayAll = new JMenuItem("Display progress and regress"); + JMenuItem displayProgress = new JMenuItem("Display only progress"); + JMenuItem displayBest = new JMenuItem("Display best grid"); fichierMenu.add(nouveauItem); fichierMenu.add(ouvrirItem); @@ -120,6 +124,10 @@ public class Window extends JFrame { refreshRate.add(radio1000ms); displayMenu.add(refreshRate); + displayMenu.addSeparator(); + displayMenu.add(displayAll); + displayMenu.add(displayProgress); + displayMenu.add(displayBest); menuBar.add(fichierMenu); menuBar.add(aideMenu); @@ -132,7 +140,7 @@ public class Window extends JFrame { this.universe.changeUniverseDim(5, 5); this.universe.resetUniverseObstacles(); this.panel.remove(this.grid); - this.grid = new Grid(3, 3, this.universe, 10); + this.grid = new Grid(3, 3, this.universe, 10, 0); this.panel.add(this.grid); super.pack(); super.repaint(); @@ -180,7 +188,7 @@ public class Window extends JFrame { } this.panel.remove(this.grid); - this.grid = new Grid(universe_width, universe_height, this.universe, 10); + this.grid = new Grid(universe_width, universe_height, this.universe, 10, 0); this.panel.add(this.grid); super.pack(); super.repaint(); @@ -198,7 +206,7 @@ public class Window extends JFrame { this.universe.changeUniverseDim(width + 2, height + 2); this.panel.remove(this.grid); - this.grid = new Grid(width, height, this.universe, 10); + this.grid = new Grid(width, height, this.universe, 10, 0); this.panel.add(this.grid); super.pack(); super.repaint(); @@ -235,8 +243,20 @@ public class Window extends JFrame { radio1000ms.addActionListener(e -> { this.grid.setRefreshRate(1000); }); + + displayAll.addActionListener(e -> { + this.grid.setDisplay(0); + }); + + displayProgress.addActionListener(e -> { + this.grid.setDisplay(1); + }); + + displayBest.addActionListener(e -> { + this.grid.setDisplay(2); + }); - this.grid = new Grid(this.universe.getHeight() - 2, this.universe.getWidth() - 2, this.universe, 10); + this.grid = new Grid(this.universe.getHeight() - 2, this.universe.getWidth() - 2, this.universe, 10, 0); this.panel.add(grid, BorderLayout.CENTER); super.setJMenuBar(menuBar); From 136e4a8e69a9940b0084a1eb895818271769bbbe Mon Sep 17 00:00:00 2001 From: Ninja-Jambon Date: Fri, 29 Mar 2024 15:42:21 +0100 Subject: [PATCH 17/17] commit --- userInterface/Window.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/userInterface/Window.java b/userInterface/Window.java index b911b27..0e08ea2 100644 --- a/userInterface/Window.java +++ b/userInterface/Window.java @@ -232,6 +232,10 @@ public class Window extends JFrame { this.grid.reset(); }); + radio10ms.addActionListener(e -> { + this.grid.setRefreshRate(10); + }); + radio200ms.addActionListener(e -> { this.grid.setRefreshRate(200); });