diff options
Diffstat (limited to 'js/baba-yaga/scratch/baba/conway-working.baba')
-rw-r--r-- | js/baba-yaga/scratch/baba/conway-working.baba | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/js/baba-yaga/scratch/baba/conway-working.baba b/js/baba-yaga/scratch/baba/conway-working.baba new file mode 100644 index 0000000..e010e96 --- /dev/null +++ b/js/baba-yaga/scratch/baba/conway-working.baba @@ -0,0 +1,120 @@ +// Conway's Game of Life - Minimal Working Version + +// Safe array access +at : xs i -> + when (i >= 0 and i < length xs) is + true then slice xs i (i + 1).0 + _ then 0; + +// Safe 2D grid access +get2d : grid row col -> + when (row >= 0 and row < length grid) is + true then at (at grid row) col + _ then 0; + +// Count living neighbors +countNeighbors : grid row col -> + (get2d grid (row - 1) (col - 1)) + + (get2d grid (row - 1) col) + + (get2d grid (row - 1) (col + 1)) + + (get2d grid row (col - 1)) + + (get2d grid row (col + 1)) + + (get2d grid (row + 1) (col - 1)) + + (get2d grid (row + 1) col) + + (get2d grid (row + 1) (col + 1)); + +// Apply Game of Life rules +nextCell : grid row col -> + with ( + current : get2d grid row col; + neighbors : countNeighbors grid row col; + ) -> + when current is + 1 then when (neighbors = 2 or neighbors = 3) is true then 1 _ then 0 + _ then when (neighbors = 3) is true then 1 _ then 0; + +// Generate next generation (hardcoded for 5x5) +nextGeneration : grid -> [ + [ + nextCell grid 0 0, + nextCell grid 0 1, + nextCell grid 0 2, + nextCell grid 0 3, + nextCell grid 0 4 + ], + [ + nextCell grid 1 0, + nextCell grid 1 1, + nextCell grid 1 2, + nextCell grid 1 3, + nextCell grid 1 4 + ], + [ + nextCell grid 2 0, + nextCell grid 2 1, + nextCell grid 2 2, + nextCell grid 2 3, + nextCell grid 2 4 + ], + [ + nextCell grid 3 0, + nextCell grid 3 1, + nextCell grid 3 2, + nextCell grid 3 3, + nextCell grid 3 4 + ], + [ + nextCell grid 4 0, + nextCell grid 4 1, + nextCell grid 4 2, + nextCell grid 4 3, + nextCell grid 4 4 + ] +]; + +// Test patterns +glider : [ + [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] +]; + +blinker : [ + [0, 0, 0, 0, 0], + [0, 1, 1, 1, 0], + [0, 0, 0, 0, 0], + [0, 0, 0, 0, 0], + [0, 0, 0, 0, 0] +]; + +// Run simulation +io.out "Conway's Game of Life"; +io.out "===================="; + +io.out "Glider - Generation 0:"; +io.out glider; + +g1 : nextGeneration glider; +io.out "Glider - Generation 1:"; +io.out g1; + +g2 : nextGeneration g1; +io.out "Glider - Generation 2:"; +io.out g2; + +io.out ""; +io.out "Blinker - Generation 0:"; +io.out blinker; + +b1 : nextGeneration blinker; +io.out "Blinker - Generation 1:"; +io.out b1; + +b2 : nextGeneration b1; +io.out "Blinker - Generation 2:"; +io.out b2; + +io.out ""; +io.out "Simulation complete!"; |