sourceextensions/canvas/demos/life.xtl

1⍝# Conway's Life in a native window: the board is an X_eTaL array, each 2⍝# generation one array expression, drawn by Rust with every frame. 3⍝# Run: just demo canvas life (Space: a new board; q or close: quit) 4 5ᶜᵛ⁼u̲se< "Canvas" 6 7⍝# Life (as in X_eTaL's demos/life.xtl): every cell plus its neighbors 8⍝# by rotating along both axes and summing; alive next at 3, or 4 if alive 9ᵘl̲ife ← { ('+ r̲/₁₂ -1 0 1 o̲-₁₂ ⍵) { (⍺ = 3) + ⍵ × ⍺ = 4 } ⍵ } 10 11⍝# a random board, about one cell in four alive 12ᵘs̲eed ← { n → (n c̲at n) r̲eshape 1 = r̲oll! (n × n) r̲eshape 4 } 13 14w ← "Life -- X_eTaL" ᶜᵛo̲pen! 512 512 15 16⍝# after quitting: close the window; an empty board ends the loop's work 17ᵘq̲uit ← { b → 18 c ← ᶜᵛc̲lose! w 19 0 0 r̲eshape 0 20} 21 22⍝# one frame: draw the board, read an event, step 23ᵘf̲rame ← { b → 24 0 = t̲ally b ? b 25 e ← ᶜᵛn̲ext! w 26 ((e m̲atch "close") + e m̲atch "key q") > 0 ? ᵘq̲uit b 27 e m̲atch "key Space" ? ᵘs̲eed 96 28 s ← w ᶜᵛs̲how! b 29 ᵘl̲ife b 30} 31 32⍝# up to 100,000 frames (about half an hour at 60 a second) 33last ← 100000 'ᵘf̲rame p̲ower ᵘs̲eed 96 34"bye"