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

View file

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