From 02e5e9c6c924af03829a4664e749cfd83a4a49c2 Mon Sep 17 00:00:00 2001 From: Ninja-Jambon Date: Wed, 20 Mar 2024 11:58:16 +0100 Subject: [PATCH] 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