import fr.upd.*;
import java.util.*;
public class Labi {
public static void affiche(int [][]l) {
for (int x=0; x<l.length; x++) {
for (int y=0; y<l[x].length; y++) {
switch(l[x][y]) {
case 0:
Facile.setColor(255,255,255); break;
case 1:
Facile.setColor(0,0,0); break;
case 2:
Facile.setColor(0,0,255); break;
}
Facile.fillRect(y*50,x*50,50,50);
}
}
Facile.sleep(100);
}
public static boolean sortir(int x,int y,int [][]lab,Stack<Integer> chemin) {
if (x<0 || x>9 || y<0 || y>9) {
System.out.println("Il y a une sortie");
System.out.println("Le chemin pour y arriver est "+chemin);
return true;
}
boolean ilExisteUneSortie = false;
switch (lab[x][y]) {
case 0:
lab[x][y] = 2;
chemin.push(x);
chemin.push(y);
affiche(lab);
ilExisteUneSortie |= sortir(x,y+1,lab,chemin);
ilExisteUneSortie |= sortir(x-1,y,lab,chemin);
ilExisteUneSortie |= sortir(x+1,y,lab,chemin);
ilExisteUneSortie |= sortir(x,y-1,lab,chemin);
lab[x][y] = 0;
chemin.pop();
chemin.pop();
affiche(lab);
return ilExisteUneSortie;
case 1: // un mur
return false;
case 2: // un caillou
return false;
}
return false;
}
public static void main(String []a) {
Stack<Integer> chemin = new Stack<Integer>();
int [][]labi = {
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 0, 0, 0, 1 },
{ 1, 0, 1, 1, 1, 1, 0, 1, 1, 1 },
{ 1, 0, 1, 1, 1, 1, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 1, 0, 1, 0, 1 },
{ 1, 1, 1, 1, 0, 1, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 1, 0, 1, 1, 1 },
{ 1, 1, 1, 1, 0, 1, 0, 0, 0, 1 },
{ 1, 1, 1, 1, 0, 0, 0, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
};
Facile.startDrawings(500,500);
affiche(labi);
if (sortir(1,7,labi,chemin)) {
System.out.println("Il existe au moins une sortie");
} else {
System.out.println("Pas de sortie, vous êtes condamné à errer...");
}
Facile.stopDrawings();
}
}