-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathmaze.io
70 lines (63 loc) · 1.48 KB
/
maze.io
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
/* Maze generator in io
* Joe Wingbermuehle
* 2010-11-10
*/
// The maze object.
maze := Object clone
// Method to display the maze.
maze print := method(
for(y, 0, height - 1,
for(x, 0, width - 1,
if((array at(y * width + x)) == 1, "[]", " ") print
)
"" println
)
self
)
// Method to create an empty maze of w x h.
maze init := method(w, h,
self width := w
self height := h
self rand := Random clone
self array := Sequence clone setSize(width * height)
for(x, 0, width * height, array atPut(x, 1))
self
)
// Start carving the maze at x, y.
maze carve := method(x, y,
d := rand value(4) floor
c := 0
while(c < 4,
dx := 0
dy := 0
d switch(0, dx = 1,
1, dy = 1,
2, dx = -1,
3, dy = -1)
x1 := x + dx
y1 := y + dy
x2 := x1 + dx
y2 := y1 + dy
if(x2 > 0 and x2 < width and y2 > 0 and y2 < height,
if(array at(y1 * width + x1) == 1,
if(array at(y2 * width + x2) == 1,
array atPut(y1 * width + x1, 0)
array atPut(y2 * width + x2, 0)
carve(x2, y2)
)
)
)
c = c + 1
d = (d + 1) % 4
)
)
// Generate a maze.
maze generate := method(
array atPut(1 * width + 1, 0)
carve(1, 1)
array atPut(0 * width + 1, 0)
array atPut((height - 1) * width + (width - 2), 0)
self
)
// Generate and display a random maze.
maze init(39, 23) generate print