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