-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathmaze.d
More file actions
83 lines (74 loc) · 1.97 KB
/
maze.d
File metadata and controls
83 lines (74 loc) · 1.97 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
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
/** Maze generator in D.
* Joe Wingbermuehle
* 20130420
*/
import std.stdio;
import std.random;
class Maze {
private enum Cell { Wall, Space };
/** Create a random maze. */
this(uint w, uint h) {
width = w * 2 + 1;
height = h * 2 + 1;
data.length = height;
for(uint y = 0; y < height; y++) {
data[y].length = width;
if(y == 0 || y == height - 1) {
data[y][] = Cell.Space;
} else {
data[y][0] = Cell.Space;
data[y][$ - 1] = Cell.Space;
data[y][1 .. $ - 1] = Cell.Wall;
}
}
carve(2, 2);
data[1][2] = Cell.Space;
data[$ - 2][$ - 3] = Cell.Space;
}
/** Carve the maze starting at x, y. */
private void carve(uint x, uint y) {
data[y][x] = Cell.Space;
immutable dirs = [ [ 1, 0 ], [ -1, 0 ], [ 0, 1 ], [ 0, -1 ] ];
uint direction = uniform(0, 4);
for(uint i = 0; i < 4; i++) {
immutable dx = dirs[direction][0];
immutable dy = dirs[direction][1];
immutable x2 = x + dx;
immutable y2 = y + dy;
immutable nx = x2 + dx;
immutable ny = y2 + dy;
if(data[y2][x2] == Cell.Wall && data[ny][nx] == Cell.Wall) {
data[y2][x2] = Cell.Space;
carve(nx, ny);
direction = uniform(0, 4);
x = nx;
y = ny;
i = 0;
}
direction = (direction + 1) % 4;
}
}
/** Display the maze. */
void print() {
for(uint y = 0; y < height; y++) {
for(uint x = 0; x < width; x++) {
final switch(data[y][x]) {
case Cell.Space:
write(" ");
break;
case Cell.Wall:
write("[]");
break;
}
}
writeln();
}
}
private uint width;
private uint height;
private Cell[][] data;
}
void main() {
Maze m = new Maze(19, 11);
m.print();
}