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