interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / tasks / conways-game-of-life / conways-game-of-life.om

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