- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMazeGenerator.java
More file actions
Latest commit
49 lines (41 loc) · 1.37 KB
/
Copy pathMazeGenerator.java
File metadata and controls
49 lines (41 loc) · 1.37 KB
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
importjava.util.Random;
publicclassMazeGenerator {
privatestaticfinalintWIDTH = 80;
privatestaticfinalintHEIGHT = 60;
privatestaticfinalRandomrandom = newRandom();
publicstaticchar[][] generateMaze() {
char[][] maze = newchar[HEIGHT][WIDTH];
// Inicializa o labirinto com paredes
for (inti = 0; i < HEIGHT; i++) {
for (intj = 0; j < WIDTH; j++) {
maze[i][j] = '#';
}
}
// Abre caminhos aleatórios no labirinto
for (inti = 1; i < HEIGHT - 1; i += 2) {
for (intj = 1; j < WIDTH - 1; j += 2) {
maze[i][j] = ' ';
// Decida aleatoriamente se cria um caminho para direita ou para baixo
if (j < WIDTH - 2) {
maze[i][j + 1] = (random.nextBoolean()) ? ' ' : '#';
}
if (i < HEIGHT - 2) {
maze[i + 1][j] = (random.nextBoolean()) ? ' ' : '#';
}
}
}
returnmaze;
}
publicstaticvoidprintMaze(char[][] maze) {
for (char[] row : maze) {
for (charc : row) {
System.out.print(c);
}
System.out.println();
}
}
publicstaticvoidmain(String[] args) {
char[][] maze = generateMaze();
printMaze(maze);
}
}