diff options
Diffstat (limited to 'js/baba-yaga/scratch/baba/life-example.baba')
-rw-r--r-- | js/baba-yaga/scratch/baba/life-example.baba | 181 |
1 files changed, 181 insertions, 0 deletions
diff --git a/js/baba-yaga/scratch/baba/life-example.baba b/js/baba-yaga/scratch/baba/life-example.baba new file mode 100644 index 0000000..7ae7164 --- /dev/null +++ b/js/baba-yaga/scratch/baba/life-example.baba @@ -0,0 +1,181 @@ +headAt : xs i -> + with ( + tmp : slice xs i (i + 1); + ) -> + tmp.0; + +get2 : grid r c -> + headAt (headAt grid r) c; + +safeGet2 : grid r c -> + with ( + rows : length grid; + cols : length (headAt grid 0); + rOk : r >= 0 and r < rows; + cOk : c >= 0 and c < cols; + ) -> + when rOk and cOk is + true then get2 grid r c + _ then 0; + +range : lo hi -> + when lo >= hi is + true then [] + _ then prepend lo (range (lo + 1) hi); + +sum : xs -> + reduce (acc x -> acc + x) 0 xs; + +deltas : [-1, 0, 1]; + +neighborsValues : grid r c -> + reduce (acc dr -> reduce (acc2 dc -> when dr = 0 and dc = 0 is + true then acc2 + _ then append acc2 (safeGet2 grid (r + dr) (c + dc))) acc deltas) [] deltas; + +countNeighbors : grid r c -> + sum (neighborsValues grid r c); + +nextCell : grid r c -> + with ( + cell : get2 grid r c; + n : countNeighbors grid r c; + isAlive : cell = 1; + born : cell = 0 and n = 3; + survive : isAlive and n = 2 or n = 3; + ) -> + when survive is + true then 1 + _ then + when born is + true then 1 + _ then 0; + +rowNext : grid w r -> + map (c -> nextCell grid r c) (range 0 w); + +step : grid -> + with ( + h : length grid; + w : length (headAt grid 0); + ) -> + map (r -> rowNext grid w r) (range 0 h); + +g0 : [ + [0, 1, 0, 0, 0], + [0, 0, 1, 0, 0], + [1, 1, 1, 0, 0], + [0, 0, 0, 0, 0], + [0, 0, 0, 0, 0] +]; +g1 : step g0; +g2 : step g1; +g3 : step g2; +g4 : step g3; +blinker0 : [[1, 1, 1], [0, 0, 0], [0, 0, 0]]; +blinker1 : step blinker0; +blinker2 : step blinker1; +toad0 : [ + [0, 1, 1, 1], + [1, 1, 1, 0], + [0, 0, 0, 0], + [0, 0, 0, 0] +]; +toad1 : step toad0; +toad2 : step toad1; +beacon0 : [ + [1, 1, 0, 0], + [1, 1, 0, 0], + [0, 0, 1, 1], + [0, 0, 1, 1] +]; +beacon1 : step beacon0; +beacon2 : step beacon1; +block : [[1, 1], [1, 1]]; +block1 : step block; +beehive : [[0, 1, 1, 0], [1, 0, 0, 1], [0, 1, 1, 0]]; +beehive1 : step beehive; +pulsar0 : [ + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0], + [0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0], + [0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1], + [0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0], + [0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1], + [0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0], + [0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0], + [0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0], + [0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0] +]; +pulsar1 : step pulsar0; +pulsar2 : step pulsar1; +pulsar3 : step pulsar2; + +// Enhanced Conway's Game of Life display using io.print +io.print ""; +io.print "Conway's Game of Life - Pattern Evolution"; +io.print "=========================================="; + +io.print ""; +io.print "Glider Pattern Evolution:"; +io.print "Step 0:"; +io.print g0; +io.print "Step 1:"; +io.print g1; +io.print "Step 2:"; +io.print g2; +io.print "Step 3:"; +io.print g3; +io.print "Step 4:"; +io.print g4; + +io.print ""; +io.print "Blinker Oscillation:"; +io.print "Step 0:"; +io.print blinker0; +io.print "Step 1:"; +io.print blinker1; +io.print "Step 2:"; +io.print blinker2; + +io.print ""; +io.print "Toad Breathing Pattern:"; +io.print "Step 0:"; +io.print toad0; +io.print "Step 1:"; +io.print toad1; +io.print "Step 2:"; +io.print toad2; + +io.print ""; +io.print "Beacon Blinking:"; +io.print "Step 0:"; +io.print beacon0; +io.print "Step 1:"; +io.print beacon1; +io.print "Step 2:"; +io.print beacon2; + +io.print ""; +io.print "Still Life Patterns:"; +io.print "Block:"; +io.print block; +io.print "Beehive:"; +io.print beehive; + +io.print ""; +io.print "Pulsar Oscillation (Period 3):"; +io.print "Step 0:"; +io.print pulsar0; +io.print "Step 1:"; +io.print pulsar1; +io.print "Step 2:"; +io.print pulsar2; +io.print "Step 3:"; +io.print pulsar3; + +// End of program - visual patterns shown above |