omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / tasks / conways-game-of-life / conways-game-of-life.om
| 1 | note Rosetta Code task "Conway's Game of Life": https://rosettacode.org/wiki/Conway's_Game_of_Life |
| 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 tgen "generation " |
| 8 | text tdot "." |
| 9 | text thash "#" |
| 10 | global cur uchar 64 |
| 11 | global nxt uchar 64 |
| 12 | global dim ulong 2 |
| 13 | note emit writes every byte of a to stdout; a short write is retried, an error exits 1 |
| 14 | fn emit a view uchar * to void |
| 15 | o = ulong 0 |
| 16 | while lt o len a bound 4096 |
| 17 | k = write 1 a o sub len a o |
| 18 | if le k 0 |
| 19 | exit 1 |
| 20 | end |
| 21 | o = add o ulong k |
| 22 | end |
| 23 | end |
| 24 | note emitu writes n in decimal; 20 digits hold every ulong |
| 25 | fn emitu n ulong to void |
| 26 | d = new uchar 20 0 |
| 27 | i = ulong 20 |
| 28 | v = n |
| 29 | while true bound 20 |
| 30 | i = sub i 1 |
| 31 | put d i uchar add rem v 10 48 |
| 32 | v = div v 10 |
| 33 | if eq v 0 |
| 34 | break |
| 35 | end |
| 36 | end |
| 37 | emit part d i sub 20 i |
| 38 | end |
| 39 | note the board is dim[0] rows by dim[1] columns, row by row, with dead cells beyond the edges; a live |
| 40 | note cell with 2 or 3 live neighbours lives on, a dead one with exactly 3 comes alive |
| 41 | fn alive r long c long to ulong |
| 42 | if or or lt r 0 ge r long get dim 0 or lt c 0 ge c long get dim 1 |
| 43 | ret 0 |
| 44 | end |
| 45 | ret ulong get cur add mul ulong r get dim 1 ulong c |
| 46 | end |
| 47 | fn around r long c long to ulong |
| 48 | n = ulong 0 |
| 49 | for a ulong 3 |
| 50 | for b ulong 3 |
| 51 | if or ne a 1 ne b 1 |
| 52 | n = add n alive sub add r long a 1 sub add c long b 1 |
| 53 | end |
| 54 | end |
| 55 | end |
| 56 | ret n |
| 57 | end |
| 58 | fn step to void |
| 59 | w = get dim 1 |
| 60 | for r ulong get dim 0 |
| 61 | for c ulong w |
| 62 | n = around long r long c |
| 63 | v = uchar 0 |
| 64 | if eq n 3 |
| 65 | v = 1 |
| 66 | end |
| 67 | if and eq n 2 eq get cur add mul r w c 1 |
| 68 | v = 1 |
| 69 | end |
| 70 | put nxt add mul r w c v |
| 71 | end |
| 72 | end |
| 73 | for i ulong mul get dim 0 w |
| 74 | put cur i get nxt i |
| 75 | end |
| 76 | end |
| 77 | fn show g ulong to void |
| 78 | emit tgen |
| 79 | emitu g |
| 80 | emit nl |
| 81 | w = get dim 1 |
| 82 | for r ulong get dim 0 |
| 83 | for c ulong w |
| 84 | if eq get cur add mul r w c 1 |
| 85 | emit thash |
| 86 | else |
| 87 | emit tdot |
| 88 | end |
| 89 | end |
| 90 | emit nl |
| 91 | end |
| 92 | end |
| 93 | fn run gens ulong to void |
| 94 | for g ulong add gens 1 |
| 95 | show g |
| 96 | if lt g gens |
| 97 | step |
| 98 | end |
| 99 | end |
| 100 | end |
| 101 | fn main to int |
| 102 | note a blinker on a 3 x 3 board for 3 generations |
| 103 | put dim 0 3 |
| 104 | put dim 1 3 |
| 105 | for i ulong 9 |
| 106 | put cur i 0 |
| 107 | end |
| 108 | put cur 3 1 |
| 109 | put cur 4 1 |
| 110 | put cur 5 1 |
| 111 | run 2 |
| 112 | emit nl |
| 113 | note a glider on an 8 x 8 board; after 4 generations it has moved one cell down and right |
| 114 | put dim 0 8 |
| 115 | put dim 1 8 |
| 116 | for i ulong 64 |
| 117 | put cur i 0 |
| 118 | end |
| 119 | put cur 1 1 |
| 120 | put cur 10 1 |
| 121 | put cur 16 1 |
| 122 | put cur 17 1 |
| 123 | put cur 18 1 |
| 124 | run 4 |
| 125 | ret 0 |
| 126 | end |