added backtracking
This commit is contained in:
parent
00a72c289c
commit
ab7d525156
3 changed files with 186 additions and 4 deletions
|
@ -7,13 +7,27 @@ public class AppLaser {
|
||||||
int start_j = 1;
|
int start_j = 1;
|
||||||
int start_dir = 12;
|
int start_dir = 12;
|
||||||
|
|
||||||
Universe universe = new Universe(8, 8, start_i, start_j, start_dir);
|
Stack <Situation> stack = new Stack <Situation>();
|
||||||
|
|
||||||
|
Universe universe = new Universe(6, 6, start_i, start_j, start_dir);
|
||||||
|
|
||||||
Situation previousState, currentState = new Situation(start_i, start_j, start_dir, 0);
|
Situation previousState, currentState = new Situation(start_i, start_j, start_dir, 0);
|
||||||
|
|
||||||
universe.addObstacle(4, 4);
|
//universe.addObstacle(4, 4);
|
||||||
|
|
||||||
universe.print();
|
universe.print();
|
||||||
|
|
||||||
|
do {
|
||||||
|
if (universe.canEvolve(currentState)) {
|
||||||
|
stack.push(currentState.copy(universe.possibleChoices(currentState)));
|
||||||
|
currentState = universe.evolve(currentState);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
currentState = stack.pop();
|
||||||
|
}
|
||||||
|
|
||||||
|
universe.print();
|
||||||
|
} while (stack.size() != 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
public class Situation {
|
public class Situation {
|
||||||
// Atributes
|
// Atributes
|
||||||
|
|
||||||
private int pos_i, pos_j, direction, nb_choix;
|
public int pos_i, pos_j, direction, nb_choix;
|
||||||
|
|
||||||
// Constructors
|
// Constructors
|
||||||
|
|
||||||
|
|
170
Universe.java
170
Universe.java
|
@ -45,11 +45,179 @@ public class Universe {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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) 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
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
default: {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
default: {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case 2: {
|
||||||
|
switch (d) {
|
||||||
|
case 10: // north
|
||||||
|
if (this.grid[i][j + 1] == 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
|
||||||
|
|
||||||
|
case 12: // east
|
||||||
|
if (this.grid[i + 1][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
|
||||||
|
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
// adding the miror
|
||||||
|
|
||||||
|
switch (c) {
|
||||||
|
case 2:
|
||||||
|
this.grid[i][j] = 2;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 3:
|
||||||
|
this.grid[i][j] = 3;
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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 == 12 || c == 2 && d == 10 || c == 3 && d == 11) {
|
||||||
|
j --;
|
||||||
|
d = 13;
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Situation(i, j, d, c);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void reset(int i, int j) {
|
||||||
|
this.grid[i][j] = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Situation restore(Situation current, Situation prev) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
public void print() {
|
public void print() {
|
||||||
int i, j;
|
int i, j;
|
||||||
for (i = 0; i < this.height; i++) {
|
for (i = 0; i < this.height; i++) {
|
||||||
for (j = 0; j < this.length; j++) {
|
for (j = 0; j < this.length; j++) {
|
||||||
System.out.printf("%3d", this.grid[i][j]);
|
switch (this.grid[i][j]) {
|
||||||
|
case -1:
|
||||||
|
System.out.printf(" X");
|
||||||
|
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("%3d", this.grid[i][j]);
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
System.out.print("\n");
|
System.out.print("\n");
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue