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

@ -62,7 +62,7 @@ public class AppLaser {
// 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
@ -184,7 +184,7 @@ public class AppLaser {
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;
@ -252,30 +252,34 @@ public class AppLaser {
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();
Universe.print(bestGrid, universe_width + 2, universe_height + 2, 2, 2 * universe_width + 10);
} }
} }
else if (stack.size() > 0) { else if (stack.size() > 0) {
currentState = stack.pop(); currentState = stack.pop();
universe.reset(currentState); universe.reset(currentState);
if (display_regress == true) universe.print(universe_height + 6, 4); if (display_regress == true) Universe.print(universe.getGrid(), universe_width + 2, universe_height + 2, universe_height + 6, 4);
} else { } else {
break; break;
} }
@ -290,7 +294,7 @@ public class AppLaser {
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.println("\nSolved in " + ((int) Instant.now().getEpochSecond() - start_time) + " secconds");

View file

@ -8,7 +8,6 @@ public class Universe {
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;
@ -19,7 +18,6 @@ public class Universe {
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.boxes_to_fill = (width - 2) * (height - 2) - 1;
this.filled_boxes = 0; this.filled_boxes = 0;
this.nb_mirors = 0; this.nb_mirors = 0;
@ -45,12 +43,12 @@ public class Universe {
// Methods // Methods
public void print(int pos_i, int pos_j) { public static void print(int [][] tab, int width, int height, int pos_i, int pos_j) {
int i, j; int i, j;
for (i = 0; i < this.height; i++) { for (i = 0; i < height; i++) {
for (j = 0; j < this.width; j++) { for (j = 0; j < width; j++) {
System.out.print("\033[" + (i + pos_i) + ";" + (j*2 + pos_j) + "H"); System.out.print("\033[" + (i + pos_i) + ";" + (j*2 + pos_j) + "H");
switch (this.grid[i][j]) { switch (tab[i][j]) {
case -1: case -1:
System.out.printf(" X"); System.out.printf(" X");
break; break;
@ -96,24 +94,13 @@ public class Universe {
break; break;
default: default:
System.out.printf("%2d", this.grid[i][j]); System.out.printf("%2d", tab[i][j]);
} }
} }
System.out.print("\n"); 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() { public void resetUniverse() {
for (int i = 1; i < this.height - 1; i++) { for (int i = 1; i < this.height - 1; i++) {
for (int j = 1; j < this.width - 1; j++) { for (int j = 1; j < this.width - 1; j++) {
@ -122,8 +109,6 @@ public class Universe {
} }
} }
} }
this.recalculateBoxesToFill();
} }
public void resetUniverseObstacles() { public void resetUniverseObstacles() {
@ -134,8 +119,6 @@ public class Universe {
} }
} }
} }
this.boxes_to_fill = ((this.height - 2) * (this.width - 2)) - 1;
} }
public void changeUniverseDim(int width, int height) { public void changeUniverseDim(int width, int height) {
@ -166,18 +149,6 @@ public class Universe {
this.grid = newgrid; this.grid = newgrid;
this.width = width; this.width = width;
this.height = height; 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) { public void changeUniverseStart(int pos_i, int pos_j, int dir) {
@ -195,8 +166,6 @@ public class Universe {
public void addObstacle(int pos_i, int pos_j) { public void addObstacle(int pos_i, int pos_j) {
if (this.grid[pos_i][pos_j] == 0) { if (this.grid[pos_i][pos_j] == 0) {
this.grid[pos_i][pos_j] = -1; this.grid[pos_i][pos_j] = -1;
//this.boxes_to_fill--;
this.recalculateBoxesToFill();
} }
else {} else {}
} }
@ -308,7 +277,6 @@ public class Universe {
this.filled_boxes ++; this.filled_boxes ++;
} else { } else {
this.grid[i][j] = 3; this.grid[i][j] = 3;
//this.boxes_to_fill++;
} }
} }
if (c == 1 && (d == 12 || d == 13)) { if (c == 1 && (d == 12 || d == 13)) {
@ -317,7 +285,6 @@ public class Universe {
this.filled_boxes ++; this.filled_boxes ++;
} else { } else {
this.grid[i][j] = 3; 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)) { if ((c == 3 && d == 10) || (c == 3 && d == 11) || (c == 2 && d == 12) || (c == 2 && d == 13)) {
@ -328,6 +295,7 @@ public class Universe {
if ((c == 2 && d == 10) || (c == 2 && d == 11) || (c == 3 && d == 12) || (c == 3 && d == 13)) { if ((c == 2 && d == 10) || (c == 2 && d == 11) || (c == 3 && d == 12) || (c == 3 && d == 13)) {
this.grid[i][j] = 5; this.grid[i][j] = 5;
this.filled_boxes ++; this.filled_boxes ++;
this.nb_mirors ++;
} }
// changing the position of the situation // changing the position of the situation
@ -349,8 +317,6 @@ public class Universe {
d = 13; d = 13;
} }
this.boxes_to_fill--;
return new Situation(i, j, d, 0); return new Situation(i, j, d, 0);
} }
@ -366,15 +332,12 @@ public class Universe {
this.grid[i][j] = 1; this.grid[i][j] = 1;
} }
} else { } else {
this.grid[s.pos_i][s.pos_j] = 0; if (this.grid[i][j] == 4 || this.grid[i][j] == 5) {this.nb_mirors--;}
this.boxes_to_fill++; this.filled_boxes--;
this.grid[i][j] = 0;
} }
} }
public boolean isSolved() {
return this.boxes_to_fill == 0;
}
public void save(String fileName) { public void save(String fileName) {
try { try {
File file = new File("./saves/" + fileName + ".txt"); File file = new File("./saves/" + fileName + ".txt");
@ -382,7 +345,7 @@ public class Universe {
file.createNewFile(); file.createNewFile();
FileWriter writer = new FileWriter("./saves/" + fileName + ".txt"); FileWriter writer = new FileWriter("./saves/" + fileName + ".txt");
writer.write(this.height + "\n" + this.width + "\n"); writer.write((this.height - 2) + "\n" + (this.width - 2) + "\n");
for (int i = 1; i < this.height - 1; i++) { for (int i = 1; i < this.height - 1; i++) {
for (int j = 1; j < this.width - 1; j++) { for (int j = 1; j < this.width - 1; j++) {
@ -408,4 +371,12 @@ public class Universe {
public int getNbMirrors() { public int getNbMirrors() {
return this.nb_mirors; return this.nb_mirors;
} }
public int[][] getGrid() {
return this.grid;
}
public int[][] copyGrid() {
return this.grid.clone();
}
} }

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