added the ability to cross laser

This commit is contained in:
Lukian LEIZOUR 2024-03-02 18:53:07 +01:00
parent 7b8b996a98
commit 8f4421bc12
2 changed files with 84 additions and 55 deletions

View file

@ -8,8 +8,8 @@ public class AppLaser {
int start_i = 3; int start_i = 3;
int start_j = 1; int start_j = 1;
int start_dir = 11; int start_dir = 11;
int universe_width = 30; int universe_width = 6;
int universe_height = 12; int universe_height = 6;
int firstState_i = start_i; int firstState_i = start_i;
int firstState_j = start_j; int firstState_j = start_j;
@ -26,11 +26,10 @@ public class AppLaser {
// obstacles creation // obstacles creation
//universe.addObstacle(3, 3); universe.addObstacle(2, 3);
//universe.addObstacle(4, 3); universe.addObstacle(3, 3);
//universe.addObstacle(3, 4); universe.addObstacle(2, 4);
//universe.addObstacle(4, 4); universe.addObstacle(3, 4);
//universe.addObstacle(12, 5);
universe.print(); universe.print();
@ -51,6 +50,7 @@ public class AppLaser {
} else { } else {
break; break;
} }
universe.print();
} }
System.out.println("\n\n"); System.out.println("\n\n");

View file

@ -55,28 +55,28 @@ public class Universe {
case 0: { case 0: {
switch (d) { switch (d) {
case 10: // north case 10: // north
if (this.grid[i - 1][j] == 0) return 1; // front if (this.grid[i - 1][j] == 0 || this.grid[i - 1][j] == 2) return 1; // front
else if (this.grid[i][j - 1] == 0) return 2; // left else if (this.grid[i][j - 1] == 0 && this.grid[i][j] == 0) return 2; // left
else if (this.grid[i][j + 1] == 0) return 3; // right else if (this.grid[i][j + 1] == 0 && this.grid[i][j] == 0) return 3; // right
else return -1; // back else return -1; // back
case 11: // south case 11: // south
if (this.grid[i + 1][j] == 0) return 1; // front if (this.grid[i + 1][j] == 0 || this.grid[i + 1][j] == 2) return 1; // front
else if (this.grid[i][j + 1] == 0) return 2; // left else if (this.grid[i][j + 1] == 0 && this.grid[i][j] == 0) return 2; // left
else if (this.grid[i][j - 1] == 0) return 3; // right else if (this.grid[i][j - 1] == 0 && this.grid[i][j] == 0) return 3; // right
else return -1; // back else return -1; // back
case 12: // east case 12: // east
if (this.grid[i][j + 1] == 0) return 1; // front if (this.grid[i][j + 1] == 0 || this.grid[i][j + 1] == 1) return 1; // front
else if (this.grid[i - 1][j] == 0) return 2; // left else if (this.grid[i - 1][j] == 0 && this.grid[i][j] == 0) return 2; // left
else if (this.grid[i + 1][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 case 13: //west
if (this.grid[i][j - 1] == 0) return 1; // front if (this.grid[i][j - 1] == 0 || this.grid[i][j - 1] == 1) return 1; // front
else if (this.grid[i + 1][j] == 0) return 2; // left else if (this.grid[i + 1][j] == 0 && this.grid[i][j] == 0) return 2; // left
else if (this.grid[i - 1][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
default: {} default: {}
} }
@ -84,24 +84,24 @@ public class Universe {
case 1: { case 1: {
switch (d) { switch (d) {
case 10: // north case 10: // north
if (this.grid[i][j - 1] == 0) return 2; // left if (this.grid[i][j - 1] == 0 && this.grid[i][j] == 0) return 2; // left
else if (this.grid[i][j + 1] == 0) return 3; // right else if (this.grid[i][j + 1] == 0 && this.grid[i][j] == 0) return 3; // right
else return -1; // back else return -1; // back
case 11: // south case 11: // south
if (this.grid[i][j + 1] == 0) return 2; // left if (this.grid[i][j + 1] == 0 && this.grid[i][j] == 0) return 2; // left
else if (this.grid[i][j - 1] == 0) return 3; // right else if (this.grid[i][j - 1] == 0 && this.grid[i][j] == 0) return 3; // right
else return -1; // back else return -1; // back
case 12: // east case 12: // east
if (this.grid[i - 1][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) 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 case 13: //west
if (this.grid[i + 1][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) 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
default: {} default: {}
} }
@ -109,21 +109,20 @@ public class Universe {
case 2: { case 2: {
switch (d) { switch (d) {
case 10: // north case 10: // north
if (this.grid[i][j + 1] == 0) return 3; // right if (this.grid[i][j + 1] == 0 && this.grid[i][j] == 0) return 3; // right
else return -1; // back else return -1; // back
case 11: // south case 11: // south
if (this.grid[i][j - 1] == 0) return 3; // right if (this.grid[i][j - 1] == 0 && this.grid[i][j] == 0) return 3; // right
else return -1; // back else return -1; // back
case 12: // east case 12: // east
if (this.grid[i + 1][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 13: //west case 13: //west
if (this.grid[i - 1][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
default: {} default: {}
} }
@ -132,7 +131,7 @@ public class Universe {
return -1; return -1;
default: return -1; default: return -1;
} }
} }
public boolean canEvolve(Situation s) { public boolean canEvolve(Situation s) {
@ -147,10 +146,24 @@ public class Universe {
// new status of the box // new status of the box
if (c == 1 && (d == 10 || d == 11)) this.grid[i][j] = 1; if (c == 1 && (d == 10 || d == 11)) {
if (c == 1 && (d == 12 || d == 13)) this.grid[i][j] = 2; if (this.grid[i][j] == 0) {
if ((c == 3 && d == 10) || (c == 3 && d == 11) || (c == 2 && d == 12) || (c == 2 && d == 13)) this.grid[i][j] = 3; this.grid[i][j] = 1;
if ((c == 2 && d == 10) || (c == 2 && d == 11) || (c == 3 && d == 12) || (c == 3 && d == 13)) this.grid[i][j] = 4; } 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;
} 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;
if ((c == 2 && d == 10) || (c == 2 && d == 11) || (c == 3 && d == 12) || (c == 3 && d == 13)) this.grid[i][j] = 5;
// changing the position of the situation // changing the position of the situation
@ -177,12 +190,24 @@ public class Universe {
} }
public void reset(Situation s) { public void reset(Situation s) {
this.grid[s.pos_i][s.pos_j] = 0; int i = s.pos_i;
this.boxes_to_fill++; 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() { public boolean isSolved() {
return this.boxes_to_fill - 1 == 0; return this.boxes_to_fill == 0;
} }
public void print() { public void print() {
@ -206,11 +231,15 @@ public class Universe {
System.out.printf(" -"); System.out.printf(" -");
break; break;
case 3: case 3:
System.out.printf(" /"); System.out.printf(" +");
break; break;
case 4: case 4:
System.out.printf(" /");
break;
case 5:
System.out.printf(" \\"); System.out.printf(" \\");
break; break;
@ -231,7 +260,7 @@ public class Universe {
break; break;
default: default:
System.out.printf("%3d", this.grid[i][j]); System.out.printf("%2d", this.grid[i][j]);
} }
} }
System.out.print("\n"); System.out.print("\n");