interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / tasks / maze-generation / maze-generation.om

162 lines · 3131 bytes Raw Blame History
1note Rosetta Code task "Maze generation": https://rosettacode.org/wiki/Maze_generation
2note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3om2
4text nl "\n"
5text sp " "
6text minus "-"
7text tcorner "+"
8text thwall "---+"
9text thopen " +"
10text tvwall "|"
11text tcell " "
12text tpath " * "
13text tspace " "
14global seed ulong 1
15global ewall uchar 60
16global swall uchar 60
17global seen uchar 60
18global stk ulong 60
19global cand ulong 4
20global mark uchar 60
21note emit writes every byte of a to stdout; a short write is retried, an error exits 1
22fn emit a view uchar * to void
23o = ulong 0
24while lt o len a bound 4096
25k = write 1 a o sub len a o
26if le k 0
27exit 1
28end
29o = add o ulong k
30end
31end
32note emitu writes n in decimal; 20 digits hold every ulong
33fn emitu n ulong to void
34d = new uchar 20 0
35i = ulong 20
36v = n
37while true bound 20
38i = sub i 1
39put d i uchar add rem v 10 48
40v = div v 10
41if eq v 0
42break
43end
44end
45emit part d i sub 20 i
46end
47note rnd is Knuth's MMIX linear congruential generator, x = x*6364136223846793005 + 1442695040888963407
48note mod 2^64 (ulong arithmetic wraps), starting from seed 0; it returns the top 31 bits, x >> 33
49fn rnd to ulong
50x = add mul get seed 0 6364136223846793005 1442695040888963407
51put seed 0 x
52ret shr x 33
53end
54note the maze has 10 columns and 6 rows of cells, cell c = row*10 + column; ewall[c] and swall[c] are the
55note walls on its east and south sides, all standing at the start
56note neighbour says which cell lies in direction d (0 north, 1 east, 2 south, 3 west), 60 if none
57fn neighbour c ulong d ulong to ulong
58r = div c 10
59k = rem c 10
60if and eq d 0 gt r 0
61ret sub c 10
62end
63if and eq d 1 lt k 9
64ret add c 1
65end
66if and eq d 2 lt r 5
67ret add c 10
68end
69if and eq d 3 gt k 0
70ret sub c 1
71end
72ret 60
73end
74note knock removes the wall between neighbouring cells a and b
75fn knock a ulong b ulong to void
76if eq b add a 1
77put ewall a 0
78end
79if eq a add b 1
80put ewall b 0
81end
82if eq b add a 10
83put swall a 0
84end
85if eq a add b 10
86put swall b 0
87end
88end
89note recursive backtracker with an explicit stack: from the top cell, collect the unvisited neighbours in
90note the order north, east, south, west, pick one with rnd mod their count, knock through and push it;
91note with none left, pop; every pass pushes a new cell or pops, so 2*60 passes suffice
92fn generate to void
93for c ulong 60
94put ewall c 1
95put swall c 1
96put seen c 0
97end
98put seen 0 1
99put stk 0 0
100sp1 = ulong 1
101while gt sp1 0 bound 120
102c = get stk sub sp1 1
103k = ulong 0
104for d ulong 4
105b = neighbour c d
106if lt b 60
107if eq get seen b 0
108put cand k b
109k = add k 1
110end
111end
112end
113if eq k 0
114sp1 = sub sp1 1
115else
116b = get cand rem rnd k
117knock c b
118put seen b 1
119put stk sp1 b
120sp1 = add sp1 1
121end
122end
123end
124note draw prints the maze; cells with mark[c] = 1 show a *
125fn draw to void
126emit tcorner
127for k ulong 10
128emit thwall
129end
130emit nl
131for r ulong 6
132emit tvwall
133for k ulong 10
134c = add mul r 10 k
135if eq get mark c 1
136emit tpath
137else
138emit tcell
139end
140if eq get ewall c 1
141emit tvwall
142else
143emit tspace
144end
145end
146emit nl
147emit tcorner
148for k ulong 10
149if eq get swall add mul r 10 k 1
150emit thwall
151else
152emit thopen
153end
154end
155emit nl
156end
157end
158fn main to int
159generate
160draw
161ret 0
162end