-
Notifications
You must be signed in to change notification settings - Fork 20
/
maze.4th
72 lines (64 loc) · 1.44 KB
/
maze.4th
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
\ Maze generator in Forth
\ Joe Wingbermuehle
\ 2010-10-04
\ Size of the maze, must be odd.
39 constant width
23 constant height
variable rand
variable maze width height 1 - * allot
\ Generate the next pseudo random number.
: random ( -- u )
rand @ 1061 * 3251 + dup rand ! 64 /
;
\ Display the maze.
: show-maze ( -- )
height 0 do
width 0 do
maze j width * i + + c@ 0= if
." "
else
." []"
then
loop
cr
loop
;
\ Carve the maze starting at the specified coordinates.
: carve-maze ( x y -- )
0 random 4 mod 0 0 0 0 0 0
{ x y count dir dx dy x1 y1 x2 y2 }
0 maze y width * x + + c!
begin
0 to dx 0 to dy
dir 0 = if 1 to dx else
dir 1 = if 1 to dy else
dir 2 = if -1 to dx else
-1 to dy
then then then
x dx + to x1 y dy + to y1
x1 dx + to x2 y1 dy + to y2
x2 0> x2 width < and y2 0> y2 height < and and if
maze y1 width * x1 + + c@ 1 =
maze y2 width * x2 + + c@ 1 = and if
0 maze y1 width * x1 + + c!
x2 y2 recurse
then then
count 1 + to count
dir 1 + 4 mod to dir
count 3 > until
;
\ Generate the maze.
: generate-maze ( -- )
height 0 do
width 0 do
1 maze j width * i + + c!
loop
loop
1 1 carve-maze
0 maze 1 + c!
0 maze height 1 - width * width 2 - + + c!
;
time&date + + + + + rand !
generate-maze
show-maze
bye