Hiii Haaan !
 Groupe : Mascotte
|
Un générateur de labyrinthes parfaits en Java; la seule méthode employée pour le moment est «l'exploration exhaustive».
Voici les sources (au moins la forme est à retravailler) :
Cell.java
Secret (cliquez pour afficher)Code : Java 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 | class Cell {
private boolean visited;
private boolean n,e,w,s;
boolean isOpenned(char c) {
boolean ret;
switch(c) {
case 'n' :
ret = n;
break;
case 's':
ret = s;
break;
case 'w':
ret = w;
break;
case 'e':
ret = e;
break;
default:
ret = false;
}
return ret;
}
void open(char c) {
switch(c) {
case 'n' :
n = true;;
break;
case 's':
s = true;
break;
case 'w':
w = true;
break;
case 'e':
e = true;
break;
}
}
boolean isVisited() {
return visited;
}
void setVisited() {
visited = true;
}
}
|
Generateur.java (générateur de nombres aléatoires uniques).
Secret (cliquez pour afficher)Code : Java 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 | public class Generateur {
private int n;
private int[] nombres;
public Generateur(int max) {
n = max;
nombres = new int[max];
for(int i=0;i<max;i++)
nombres[i] = i;
}
public int tirer() {
int i = (int)(Math.random()*n);
int ret = nombres[i];
System.out.print("nombreds["+i+"] = " + nombres[i] +" dans :");
for(int j=0;j<n;j++)
System.out.print(nombres[j]);
System.out.println();
nombres[i] = nombres[n-1];
--n;
return ret;
}
}
|
Labyrinthe.java
Secret (cliquez pour afficher)Code : Java 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83 | class Labyrinthe {
private Cell[][] lab;
char[] doors = {'n','s','w','e'};
Labyrinthe(int h,int l) {
lab = new Cell[h][l];
for(int i=0;i<h;i++) {
for(int j=0;j<l;j++)
lab[i][j] = new Cell();
}
}
void print() {
char[][] tab = new char[lab.length*2+1][lab[0].length*2+1];
for(int i=0;i<tab.length;i++) {
for(int j=0;j<tab[0].length;j++)
tab[i][j]='#';
}
for(int i=0,ii=1;i<lab.length;i++,ii+=2) {
for(int j=0,jj=1;j<lab[0].length;j++,jj+=2) {
tab[ii][jj] = ' ';
if(lab[i][j].isOpenned('s') && i!=lab.length-1)
tab[ii+1][jj] = ' ';
if(lab[i][j].isOpenned('e') && j!=lab[0].length-1)
tab[ii][jj+1] = ' ';
}
}
for(int i=0;i<tab.length;i++) {
for(int j=0;j<tab[0].length;j++)
System.out.print(tab[i][j]);
System.out.print('\n');
}
}
void generer() {
generer(0,0);
}
void generer(int c1,int c2) {
lab[c1][c2].setVisited();
Generateur g = new Generateur(4);
try {
while(true) {
switch(doors[g.tirer()]) { // Exception quand plus de nombre.
case 'n':
if(c1>0 && !lab[c1-1][c2].isVisited()) {
lab[c1][c2].open('n');
lab[c1-1][c2].open('s');
generer(c1-1,c2);
}
break;
case 's':
if(c1<lab.length-1 && !lab[c1+1][c2].isVisited()) {
lab[c1][c2].open('s');
lab[c1+1][c2].open('n');
generer(c1+1,c2);
}
break;
case 'w':
if(c2>0 && !lab[c1][c2-1].isVisited()) {
lab[c1][c2].open('w');
lab[c1][c2-1].open('e');
generer(c1,c2-1);
}
break;
case 'e':
if(c2<lab[0].length-1 && !lab[c1][c2+1].isVisited()) {
lab[c1][c2].open('e');
lab[c1][c2+1].open('w');
generer(c1,c2+1);
}
break;
}
}
}
catch(Exception e) {
}
}
}
|
Main.java (histoire de tester le tout)
Secret (cliquez pour afficher)Code : Java 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 | public class Main {
public static void main(String[] args) {
int h = Integer.parseInt(args[0]);
int l = Integer.parseInt(args[1]);
Labyrinthe lab = new Labyrinthe(h,l);
try {
lab.generer();
lab.print();
}
catch(StackOverflowError e) {
System.out.println(">> "+e);
}
}
}
|
Prochaines étapes :
-> Résolution,
-> Interface graphique minimaliste,
-> Backtrace de la génération.
|