interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / drafts / rosettacode / maze-generation.wiki

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