improvements

This commit is contained in:
Ninja-Jambon 2024-03-20 11:58:16 +01:00
parent 556869ebb8
commit 02e5e9c6c9
4 changed files with 605 additions and 630 deletions

View file

@ -10,304 +10,308 @@ import java.util.stream.Stream;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class AppLaser { 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++) { for (int i = 0; i < args.length; i++) {
switch (args[i]) { switch (args[i]) {
case "-cli": case "-cli":
cli = true; cli = true;
break; break;
case "--optimize-duration": case "--optimize-duration":
optimize_duration = true; optimize_duration = true;
break; break;
default: default:
System.out.println("Unknown argument " + args[i]); System.out.println("Unknown argument " + args[i]);
System.exit(0); System.exit(0);
} }
} }
int universe_width = 3; int universe_width = 3;
int universe_height = 3; int universe_height = 3;
int start_i = 0; int start_i = 0;
int start_j = 0; int start_j = 0;
int start_dir = 11; int start_dir = 11;
int firstState_i = start_i; int firstState_i = start_i;
int firstState_j = start_j; int firstState_j = start_j;
if (start_dir == 10) firstState_i = start_i - 1; if (start_dir == 10) firstState_i = start_i - 1;
else if (start_dir == 11) 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 == 12) firstState_j = start_j + 1;
else if (start_dir == 13) firstState_j = start_j - 1; else if (start_dir == 13) firstState_j = start_j - 1;
if (cli == true) { if (cli == true) {
Stack <Situation> stack = new Stack <Situation>(); Stack <Situation> stack = new Stack <Situation>();
Universe universe = new Universe(universe_width + 2, universe_height + 2, start_i + 1, start_j + 1, start_dir); 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); 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 { do {
// clear screen // clear screen
System.out.print("\033[H\033[2J"); System.out.print("\033[H\033[2J");
System.out.flush(); System.out.flush();
System.out.print("\033[?25h"); 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("\n1 - Resize universe");
System.out.println("2 - Change start position"); System.out.println("2 - Change start position");
System.out.println("3 - Add obstacle"); System.out.println("3 - Add obstacle");
System.out.println("4 - Reset universe"); System.out.println("4 - Reset universe");
System.out.println("5 - Reset obstacles"); System.out.println("5 - Reset obstacles");
System.out.println("6 - Save universe"); System.out.println("6 - Save universe");
System.out.println("7 - Load universe"); System.out.println("7 - Load universe");
System.out.println("8 - Resolve"); System.out.println("8 - Resolve");
System.out.println("0 - Exit"); System.out.println("0 - Exit");
// prompt the user // prompt the user
System.out.print("\nChoice : "); System.out.print("\nChoice : ");
choice = scanner.nextInt(); choice = scanner.nextInt();
switch (choice) { switch (choice) {
case 1: case 1:
System.out.print("Enter the width of the universe : "); System.out.print("Enter the width of the universe : ");
universe_width = scanner.nextInt(); universe_width = scanner.nextInt();
System.out.print("Enter the length of the universe : "); System.out.print("Enter the length of the universe : ");
universe_height = scanner.nextInt(); universe_height = scanner.nextInt();
universe.changeUniverseDim(universe_width + 2, universe_height + 2); universe.changeUniverseDim(universe_width + 2, universe_height + 2);
break; break;
case 2: case 2:
System.out.print("Enter the position of the start point in the first axis : "); System.out.print("Enter the position of the start point in the first axis : ");
start_i = scanner.nextInt(); start_i = scanner.nextInt();
System.out.print("Enter the position of the start point in the seccond axis : "); System.out.print("Enter the position of the start point in the seccond axis : ");
start_j = scanner.nextInt(); start_j = scanner.nextInt();
System.out.print("Enter the direction of the start point : "); System.out.print("Enter the direction of the start point : ");
start_dir = scanner.nextInt(); start_dir = scanner.nextInt();
firstState_i = start_i; firstState_i = start_i;
firstState_j = start_j; firstState_j = start_j;
if (start_dir == 10) firstState_i = start_i - 1; if (start_dir == 10) firstState_i = start_i - 1;
else if (start_dir == 11) 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 == 12) firstState_j = start_j + 1;
else if (start_dir == 13) 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: case 3:
int firstPos_i, firstPos_j, seccondPos_i, seccondPos_j; int firstPos_i, firstPos_j, seccondPos_i, seccondPos_j;
System.out.print("\nFirst position of the obstacle i : "); System.out.print("\nFirst position of the obstacle i : ");
firstPos_i = scanner.nextInt(); firstPos_i = scanner.nextInt();
System.out.print("First position of the obstacle j : "); System.out.print("First position of the obstacle j : ");
firstPos_j = scanner.nextInt(); firstPos_j = scanner.nextInt();
System.out.print("Seccond position of the obstacle i : "); System.out.print("Seccond position of the obstacle i : ");
seccondPos_i = scanner.nextInt(); seccondPos_i = scanner.nextInt();
System.out.print("Seccond position of the obstacle j : "); System.out.print("Seccond position of the obstacle j : ");
seccondPos_j = scanner.nextInt(); seccondPos_j = scanner.nextInt();
for (int i = firstPos_i + 1; i < seccondPos_i + 2; i++) { for (int i = firstPos_i + 1; i < seccondPos_i + 2; i++) {
for (int j = firstPos_j + 1; j < seccondPos_j + 2; j++) { for (int j = firstPos_j + 1; j < seccondPos_j + 2; j++) {
universe.addObstacle(i, j); universe.addObstacle(i, j);
} }
} }
break; break;
case 4: case 4:
universe.resetUniverse(); universe.resetUniverse();
break; break;
case 5: case 5:
universe.resetUniverseObstacles(); universe.resetUniverseObstacles();
break; break;
case 6: case 6:
System.out.print("\nHow do you want to name your universe : "); System.out.print("\nHow do you want to name your universe : ");
String name = ""; String name = "";
while (name.isEmpty()) { while (name.isEmpty()) {
name = scanner.nextLine(); name = scanner.nextLine();
} }
universe.save(name); universe.save(name);
break; break;
case 7: case 7:
Set<String> files = Stream.of(new File("./saves").listFiles()).filter(file -> !file.isDirectory()).map(File::getName).collect(Collectors.toSet()); Set<String> 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) { for (String element : files) {
System.out.println(element.replace(".txt", "")); System.out.println(element.replace(".txt", ""));
} }
System.out.print("\nWhat universe do you want load ? : "); System.out.print("\nWhat universe do you want load ? : ");
String name2 = ""; String name2 = "";
while (name2.isEmpty()) { while (name2.isEmpty()) {
name2 = scanner.nextLine(); name2 = scanner.nextLine();
} }
try { try {
BufferedReader reader = new BufferedReader(new FileReader("./saves/" + name2 + ".txt")); BufferedReader reader = new BufferedReader(new FileReader("./saves/" + name2 + ".txt"));
universe_height = Integer.valueOf(reader.readLine()); universe_height = Integer.valueOf(reader.readLine());
universe_width = Integer.valueOf(reader.readLine()); universe_width = Integer.valueOf(reader.readLine());
start_dir = Integer.valueOf(reader.readLine()); start_dir = Integer.valueOf(reader.readLine());
start_i = Integer.valueOf(reader.readLine()); start_i = Integer.valueOf(reader.readLine());
start_j = 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_i = start_i;
firstState_j = start_j; firstState_j = start_j;
if (start_dir == 10) firstState_i = start_i - 1; if (start_dir == 10) firstState_i = start_i - 1;
else if (start_dir == 11) 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 == 12) firstState_j = start_j + 1;
else if (start_dir == 13) 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) { while (true) {
try { try {
int pos1 = Integer.valueOf(reader.readLine()); int pos1 = Integer.valueOf(reader.readLine());
int pos2 = Integer.valueOf(reader.readLine()); int pos2 = Integer.valueOf(reader.readLine());
universe.addObstacle(pos1, pos2); universe.addObstacle(pos1, pos2);
} }
catch (Exception e) { catch (Exception e) {
break; break;
} }
} }
} }
catch (Exception e) {} catch (Exception e) {}
break; break;
case 8: case 8:
boolean display_progress = false, display_regress = false; boolean display_progress = false, display_regress = false;
System.out.println("\n1 - display progress and regress"); System.out.println("\n1 - display progress and regress");
System.out.println("2 - display progress"); System.out.println("2 - display progress");
System.out.println("3 - display regress"); System.out.println("3 - display regress");
System.out.println("4 - display nothing"); System.out.println("4 - display nothing");
System.out.print("\nChoice : "); System.out.print("\nChoice : ");
choice = scanner.nextInt(); choice = scanner.nextInt();
switch (choice) { switch (choice) {
case 1: case 1:
display_progress = true; display_progress = true;
display_regress = true; display_regress = true;
break; break;
case 2: case 2:
display_progress = true; display_progress = true;
break; break;
case 3: case 3:
display_regress = true; display_regress = true;
break; break;
case 4: case 4:
break; break;
default: default:
break; break;
} }
System.out.print("\033[?25l"); System.out.print("\033[?25l");
System.out.print("\033[H\033[2J"); System.out.print("\033[H\033[2J");
System.out.flush(); 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 [][] bestGrid = universe.copyGrid();
int best_filled_boxes = 0; int best_filled_boxes = 0;
int best_nb_mirrors = 0; int best_nb_mirrors = 0;
do { //!universe.isSolved() do {
if (universe.canEvolve(currentState)) { if (universe.canEvolve(currentState)) {
stack.push(currentState.copy(universe.possibleChoices(currentState))); stack.push(currentState.copy(universe.possibleChoices(currentState)));
currentState = universe.evolve(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) { if ((universe.getFilledBoxes() > best_filled_boxes) || (universe.getFilledBoxes() == best_filled_boxes && universe.getNbMirrors() < best_nb_mirrors)) {
bestStack = stack.copy(); bestGrid = universe.copyGrid();
} best_filled_boxes = universe.getFilledBoxes();
} best_nb_mirrors = universe.getNbMirrors();
else if (stack.size() > 0) {
currentState = stack.pop();
universe.reset(currentState);
if (display_regress == true) universe.print(universe_height + 6, 4); Universe.print(bestGrid, universe_width + 2, universe_height + 2, 2, 2 * universe_width + 10);
} else { }
break; }
} else if (stack.size() > 0) {
currentState = stack.pop();
universe.reset(currentState);
if ((int) Instant.now().getEpochSecond() - start_time > 60 && optimize_duration == true) { if (display_regress == true) Universe.print(universe.getGrid(), universe_width + 2, universe_height + 2, universe_height + 6, 4);
display_progress = false; } else {
} break;
if ((int) Instant.now().getEpochSecond() - start_time > 2 * 60 && optimize_duration == true) { }
display_regress = false;
}
} while (stack.size() != 0);
System.out.println("\n\n"); 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);
universe.print(universe_height + 6, 4); System.out.println("\n\n");
System.out.println("\nSolved in " + ((int) Instant.now().getEpochSecond() - start_time) + " secconds"); Universe.print(bestGrid, universe_width + 2, universe_height + 2, universe_height + 6, 4);
System.out.print("\033[?25h"); System.out.println("\nSolved in " + ((int) Instant.now().getEpochSecond() - start_time) + " secconds");
System.out.print("\nEnter anything to continue....");
scanner.nextInt();
break; System.out.print("\033[?25h");
System.out.print("\nEnter anything to continue....");
scanner.nextInt();
default: break;
break;
} default:
} while (choice != 0); break;
}
else { }
System.out.println("there is no GUI yet."); } while (choice != 0);
} }
} else {
System.out.println("there is no GUI yet.");
}
}
} }

View file

@ -4,408 +4,379 @@ import java.io.File;
import java.io.FileWriter; import java.io.FileWriter;
public class Universe { public class Universe {
// Atributes // Atributes
private int[][] grid; private int[][] grid;
private int width, height; private int width, height;
private int boxes_to_fill; private int filled_boxes;
private int filled_boxes; private int nb_mirors;
private int nb_mirors; private String name;
private String name;
// Constructors
// Constructors
public Universe(int width, int height, int i_start, int j_start, int dir_start) {
public Universe(int width, int height, int i_start, int j_start, int dir_start) { this.grid = new int[height][width];
this.grid = new int[height][width]; this.height = height;
this.height = height; this.width = width;
this.width = width; this.filled_boxes = 0;
this.boxes_to_fill = (width - 2) * (height - 2) - 1; this.nb_mirors = 0;
this.filled_boxes = 0;
this.nb_mirors = 0; int i, j;
for (i = 1; i < this.height - 1; i++) {
int i, j; for (j = 1; j < this.width - 1; j++) {
for (i = 1; i < this.height - 1; i++) { this.grid[i][j] = 0;
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;
for (i = 0; i < this.height; i++) { this.grid[i][width - 1] = -1;
this.grid[i][0] = -1; }
this.grid[i][width - 1] = -1;
} for (j = 0; j < this.width; j++) {
this.grid[0][j] = -1;
for (j = 0; j < this.width; j++) { this.grid[height - 1][j] = -1;
this.grid[0][j] = -1; }
this.grid[height - 1][j] = -1;
} this.grid[i_start][j_start] = dir_start;
}
this.grid[i_start][j_start] = dir_start;
} // Methods
// Methods public static void print(int [][] tab, int width, int height, int pos_i, int pos_j) {
int i, j;
public void print(int pos_i, int pos_j) { for (i = 0; i < height; i++) {
int i, j; for (j = 0; j < width; j++) {
for (i = 0; i < this.height; i++) { System.out.print("\033[" + (i + pos_i) + ";" + (j*2 + pos_j) + "H");
for (j = 0; j < this.width; j++) { switch (tab[i][j]) {
System.out.print("\033[" + (i + pos_i) + ";" + (j*2 + pos_j) + "H"); case -1:
switch (this.grid[i][j]) { System.out.printf(" X");
case -1: break;
System.out.printf(" X");
break; case 0:
System.out.printf(" ");
case 0: break;
System.out.printf(" ");
break; case 1:
System.out.printf(" |");
case 1: break;
System.out.printf(" |");
break; case 2:
System.out.printf(" -");
case 2: break;
System.out.printf(" -");
break; case 3:
System.out.printf(" +");
case 3: break;
System.out.printf(" +");
break; case 4:
System.out.printf(" /");
case 4: break;
System.out.printf(" /");
break; case 5:
System.out.printf(" \\");
case 5: break;
System.out.printf(" \\");
break; case 10:
System.out.printf(" ^");
case 10: break;
System.out.printf(" ^");
break; case 11:
System.out.printf(" v");
case 11: break;
System.out.printf(" v");
break; case 12:
System.out.printf(" >");
case 12: break;
System.out.printf(" >");
break; case 13:
System.out.printf(" <");
case 13: break;
System.out.printf(" <");
break; default:
System.out.printf("%2d", tab[i][j]);
default: }
System.out.printf("%2d", this.grid[i][j]); }
} System.out.print("\n");
} }
System.out.print("\n"); }
}
} public void resetUniverse() {
for (int i = 1; i < this.height - 1; i++) {
public void recalculateBoxesToFill() { for (int j = 1; j < this.width - 1; j++) {
this.boxes_to_fill = 0; 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) {
for (int i = 1; i < this.height - 1; i++) { this.grid[i][j] = 0;
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 resetUniverseObstacles() {
} for (int i = 1; i < this.height - 1; i++) {
for (int j = 1; j < this.width - 1; j++) {
public void resetUniverse() { 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) {
for (int i = 1; i < this.height - 1; i++) { this.grid[i][j] = 0;
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 changeUniverseDim(int width, int height) {
int [][] newgrid = new int[width][height];
this.recalculateBoxesToFill();
} for (int i = 1; i < height - 1; i++) {
for (int j = 1; j < width - 1; j++) {
public void resetUniverseObstacles() { newgrid[i][j] = 0;
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; 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];
} }
}
this.boxes_to_fill = ((this.height - 2) * (this.width - 2)) - 1;
} for (int i = 0; i < height; i++) {
newgrid[i][0] = -1;
public void changeUniverseDim(int width, int height) { newgrid[i][width - 1] = -1;
int [][] newgrid = new int[width][height]; }
for (int i = 1; i < height - 1; i++) { for (int j = 0; j < width; j++) {
for (int j = 1; j < width - 1; j++) { newgrid[height - 1][j] = -1;
newgrid[i][j] = 0; newgrid[0][j] = -1;
} }
}
this.grid = newgrid;
for (int i = 1; i < height - 1 && i < this.height - 1; i++) { this.width = width;
for (int j = 1; j < width - 1 && j < this.width - 1; j++) { this.height = height;
newgrid[i][j] = this.grid[i][j]; }
}
} public void changeUniverseStart(int pos_i, int pos_j, int dir) {
for (int i = 0; i < this.height; i++) {
for (int i = 0; i < height; i++) { for (int j = 0; j < this.width; j++) {
newgrid[i][0] = -1; if (this.grid[i][j] == 10 || this.grid[i][j] == 11 || this.grid[i][j] == 12|| this.grid[i][j] == 13) {
newgrid[i][width - 1] = -1; this.grid[i][j] = 0;
} }
}
for (int j = 0; j < width; j++) { }
newgrid[height - 1][j] = -1;
newgrid[0][j] = -1; this.grid[pos_i][pos_j] = dir;
} }
this.grid = newgrid; public void addObstacle(int pos_i, int pos_j) {
this.width = width; if (this.grid[pos_i][pos_j] == 0) {
this.height = height; this.grid[pos_i][pos_j] = -1;
}
/*this.boxes_to_fill = 0; else {}
}
for (int i = 1; i < height - 1; i++) {
for (int j = 1; j < width - 1; j++) { public int possibleChoices(Situation s) {
if (this.grid[i][j] == 0) { int i = s.pos_i;
this.boxes_to_fill ++; int j = s.pos_j;
} int d = s.direction;
} int c = s.nb_choix;
}*/
switch (c) {
this.recalculateBoxesToFill(); case 0: {
} switch (d) {
case 10: // north
public void changeUniverseStart(int pos_i, int pos_j, int dir) { if (this.grid[i - 1][j] == 0 || this.grid[i - 1][j] == 2) return 1; // front
for (int i = 0; i < this.height; i++) { else if (this.grid[i][j - 1] == 0 && this.grid[i][j] == 0) return 2; // left
for (int j = 0; j < this.width; j++) { else if (this.grid[i][j + 1] == 0 && this.grid[i][j] == 0) return 3; // right
if (this.grid[i][j] == 10 || this.grid[i][j] == 11 || this.grid[i][j] == 12|| this.grid[i][j] == 13) { else return -1; // back
this.grid[i][j] = 0;
} 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
this.grid[pos_i][pos_j] = dir; else return -1; // back
}
case 12: // east
public void addObstacle(int pos_i, int pos_j) { if (this.grid[i][j + 1] == 0 || this.grid[i][j + 1] == 1) return 1; // front
if (this.grid[pos_i][pos_j] == 0) { else if (this.grid[i - 1][j] == 0 && this.grid[i][j] == 0) return 2; // left
this.grid[pos_i][pos_j] = -1; else if (this.grid[i + 1][j] == 0 && this.grid[i][j] == 0) return 3; // right
//this.boxes_to_fill--; else return -1; // back
this.recalculateBoxesToFill();
} case 13: //west
else {} 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
public int possibleChoices(Situation s) { else return -1; // back
int i = s.pos_i;
int j = s.pos_j; default: {}
int d = s.direction; }
int c = s.nb_choix; }
case 1: {
switch (c) { switch (d) {
case 0: { case 10: // north
switch (d) { if (this.grid[i][j - 1] == 0 && this.grid[i][j] == 0) return 2; // left
case 10: // north else if (this.grid[i][j + 1] == 0 && this.grid[i][j] == 0) return 3; // right
if (this.grid[i - 1][j] == 0 || this.grid[i - 1][j] == 2) return 1; // front else return -1; // back
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 case 11: // south
else return -1; // back 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
case 11: // south else return -1; // back
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 case 12: // east
else if (this.grid[i][j - 1] == 0 && this.grid[i][j] == 0) return 3; // right if (this.grid[i - 1][j] == 0 && this.grid[i][j] == 0) return 2; // left
else return -1; // back else if (this.grid[i + 1][j] == 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 case 13: //west
else if (this.grid[i - 1][j] == 0 && this.grid[i][j] == 0) return 2; // left 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 if (this.grid[i - 1][j] == 0 && this.grid[i][j] == 0) return 3; // right
else return -1; // back else return -1; // back
case 13: //west default: {}
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 case 2: {
else return -1; // back switch (d) {
case 10: // north
default: {} if (this.grid[i][j + 1] == 0 && this.grid[i][j] == 0) return 3; // right
} else return -1; // back
}
case 1: { case 11: // south
switch (d) { if (this.grid[i][j - 1] == 0 && this.grid[i][j] == 0) return 3; // right
case 10: // north else return -1; // back
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 case 12: // east
else return -1; // back if (this.grid[i + 1][j] == 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 case 13: //west
else if (this.grid[i][j - 1] == 0 && this.grid[i][j] == 0) return 3; // right if (this.grid[i - 1][j] == 0 && this.grid[i][j] == 0) return 3; // right
else return -1; // back else return -1; // back
case 12: // east default: {}
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 3:
return -1;
case 13: //west
if (this.grid[i + 1][j] == 0 && this.grid[i][j] == 0) return 2; // left default: return -1;
else if (this.grid[i - 1][j] == 0 && this.grid[i][j] == 0) return 3; // right }
else return -1; // back }
default: {} public boolean canEvolve(Situation s) {
} return possibleChoices(s) != -1;
} }
case 2: {
switch (d) { public Situation evolve(Situation s) {
case 10: // north int i = s.pos_i;
if (this.grid[i][j + 1] == 0 && this.grid[i][j] == 0) return 3; // right int j = s.pos_j;
else return -1; // back int d = s.direction;
int c = possibleChoices(s);
case 11: // south
if (this.grid[i][j - 1] == 0 && this.grid[i][j] == 0) return 3; // right // new status of the box
else return -1; // back
if (c == 1 && (d == 10 || d == 11)) {
case 12: // east if (this.grid[i][j] == 0) {
if (this.grid[i + 1][j] == 0 && this.grid[i][j] == 0) return 3; // right this.grid[i][j] = 1;
else return -1; // back this.filled_boxes ++;
} else {
case 13: //west this.grid[i][j] = 3;
if (this.grid[i - 1][j] == 0 && this.grid[i][j] == 0) return 3; // right }
else return -1; // back }
if (c == 1 && (d == 12 || d == 13)) {
default: {} if (this.grid[i][j] == 0) {
} this.grid[i][j] = 2;
} this.filled_boxes ++;
case 3: } else {
return -1; this.grid[i][j] = 3;
}
default: return -1; }
} 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 ++;
public boolean canEvolve(Situation s) { this.nb_mirors ++;
return possibleChoices(s) != -1; }
} if ((c == 2 && d == 10) || (c == 2 && d == 11) || (c == 3 && d == 12) || (c == 3 && d == 13)) {
this.grid[i][j] = 5;
public Situation evolve(Situation s) { this.filled_boxes ++;
int i = s.pos_i; this.nb_mirors ++;
int j = s.pos_j; }
int d = s.direction;
int c = possibleChoices(s); // changing the position of the situation
// new status of the box if (c == 1 && d == 10 || c == 2 && d == 12 || c == 3 && d == 13) {
i --;
if (c == 1 && (d == 10 || d == 11)) { d = 10;
if (this.grid[i][j] == 0) { }
this.grid[i][j] = 1; else if (c == 1 && d == 11 || c == 2 && d == 13 || c == 3 && d == 12) {
this.filled_boxes ++; i ++;
} else { d = 11;
this.grid[i][j] = 3; }
//this.boxes_to_fill++; else if (c == 1 && d == 12 || c == 2 && d == 11 || c == 3 && d == 10) {
} j ++;
} d = 12;
if (c == 1 && (d == 12 || d == 13)) { }
if (this.grid[i][j] == 0) { else if (c == 1 && d == 13 || c == 2 && d == 10 || c == 3 && d == 11) {
this.grid[i][j] = 2; j --;
this.filled_boxes ++; d = 13;
} else { }
this.grid[i][j] = 3;
//this.boxes_to_fill++; return new Situation(i, j, d, 0);
} }
}
if ((c == 3 && d == 10) || (c == 3 && d == 11) || (c == 2 && d == 12) || (c == 2 && d == 13)) { public void reset(Situation s) {
this.grid[i][j] = 4; int i = s.pos_i;
this.filled_boxes ++; int j = s.pos_j;
this.nb_mirors ++; int d = s.direction;
}
if ((c == 2 && d == 10) || (c == 2 && d == 11) || (c == 3 && d == 12) || (c == 3 && d == 13)) { if (this.grid[i][j] == 3) {
this.grid[i][j] = 5; if (d == 10 || d == 11) {
this.filled_boxes ++; this.grid[i][j] = 2;
} } else {
this.grid[i][j] = 1;
// changing the position of the situation }
} else {
if (c == 1 && d == 10 || c == 2 && d == 12 || c == 3 && d == 13) { if (this.grid[i][j] == 4 || this.grid[i][j] == 5) {this.nb_mirors--;}
i --; this.filled_boxes--;
d = 10; this.grid[i][j] = 0;
} }
else if (c == 1 && d == 11 || c == 2 && d == 13 || c == 3 && d == 12) { }
i ++;
d = 11; public void save(String fileName) {
} try {
else if (c == 1 && d == 12 || c == 2 && d == 11 || c == 3 && d == 10) { File file = new File("./saves/" + fileName + ".txt");
j ++;
d = 12; file.createNewFile();
}
else if (c == 1 && d == 13 || c == 2 && d == 10 || c == 3 && d == 11) { FileWriter writer = new FileWriter("./saves/" + fileName + ".txt");
j --; writer.write((this.height - 2) + "\n" + (this.width - 2) + "\n");
d = 13;
} for (int i = 1; i < this.height - 1; i++) {
for (int j = 1; j < this.width - 1; j++) {
this.boxes_to_fill--; 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");
}
return new Situation(i, j, d, 0); }
}
for (int i = 1; i < this.height - 1; i++) {
public void reset(Situation s) { for (int j = 1; j < this.width - 1; j++) {
int i = s.pos_i; if (this.grid[i][j] == -1) writer.write(i + "\n" + j + "\n");
int j = s.pos_j; }
int d = s.direction; }
if (this.grid[i][j] == 3) { writer.close();
if (d == 10 || d == 11) { }
this.grid[i][j] = 2; catch (Exception e) {}
} else { }
this.grid[i][j] = 1;
} public int getFilledBoxes() {
} else { return this.filled_boxes;
this.grid[s.pos_i][s.pos_j] = 0; }
this.boxes_to_fill++;
} public int getNbMirrors() {
} return this.nb_mirors;
}
public boolean isSolved() {
return this.boxes_to_fill == 0; public int[][] getGrid() {
} return this.grid;
}
public void save(String fileName) {
try { public int[][] copyGrid() {
File file = new File("./saves/" + fileName + ".txt"); return this.grid.clone();
}
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;
}
} }

View file

@ -1,5 +1,5 @@
5 3
5 3
11 11
1 1
1 1

View file

@ -1,5 +1,5 @@
12 10
12 10
10 10
6 6
1 1