about summary refs log tree commit diff stats
path: root/js/baba-yaga/scratch/baba/game-of-life.baba
diff options
context:
space:
mode:
Diffstat (limited to 'js/baba-yaga/scratch/baba/game-of-life.baba')
-rw-r--r--js/baba-yaga/scratch/baba/game-of-life.baba76
1 files changed, 76 insertions, 0 deletions
diff --git a/js/baba-yaga/scratch/baba/game-of-life.baba b/js/baba-yaga/scratch/baba/game-of-life.baba
new file mode 100644
index 0000000..2721b3e
--- /dev/null
+++ b/js/baba-yaga/scratch/baba/game-of-life.baba
@@ -0,0 +1,76 @@
+// Conway's Game of Life - Working Implementation
+
+// Count neighbors for a 3x3 grid (hardcoded positions)
+countNeighbors : grid row col ->
+  when row is
+    0 then when col is
+      0 then grid.0.1 + grid.1.0 + grid.1.1
+      1 then grid.0.0 + grid.0.2 + grid.1.0 + grid.1.1 + grid.1.2
+      2 then grid.0.1 + grid.1.1 + grid.1.2
+      _ then 0
+    1 then when col is
+      0 then grid.0.0 + grid.0.1 + grid.1.1 + grid.2.0 + grid.2.1
+      1 then grid.0.0 + grid.0.1 + grid.0.2 + grid.1.0 + grid.1.2 + grid.2.0 + grid.2.1 + grid.2.2
+      2 then grid.0.1 + grid.0.2 + grid.1.1 + grid.2.1 + grid.2.2
+      _ then 0
+    2 then when col is
+      0 then grid.1.0 + grid.1.1 + grid.2.1
+      1 then grid.1.0 + grid.1.1 + grid.1.2 + grid.2.0 + grid.2.2
+      2 then grid.1.1 + grid.1.2 + grid.2.1
+      _ then 0
+    _ then 0;
+
+// Apply Game of Life rules
+nextCell : grid row col ->
+  with (
+    current : when row is
+      0 then when col is 0 then grid.0.0 1 then grid.0.1 2 then grid.0.2 _ then 0
+      1 then when col is 0 then grid.1.0 1 then grid.1.1 2 then grid.1.2 _ then 0
+      2 then when col is 0 then grid.2.0 1 then grid.2.1 2 then grid.2.2 _ then 0
+      _ then 0;
+    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 for 3x3 grid
+step : grid -> {
+  0: {
+    0: nextCell grid 0 0,
+    1: nextCell grid 0 1,
+    2: nextCell grid 0 2
+  },
+  1: {
+    0: nextCell grid 1 0,
+    1: nextCell grid 1 1,
+    2: nextCell grid 1 2
+  },
+  2: {
+    0: nextCell grid 2 0,
+    1: nextCell grid 2 1,
+    2: nextCell grid 2 2
+  }
+};
+
+// Blinker pattern (oscillator)
+blinker : {
+  0: { 0: 0, 1: 1, 2: 0 },
+  1: { 0: 0, 1: 1, 2: 0 },
+  2: { 0: 0, 1: 1, 2: 0 }
+};
+
+// Run simulation
+io.out "Conway's Game of Life - Blinker Pattern";
+io.out "Generation 0:";
+io.out blinker;
+
+gen1 : step blinker;
+io.out "Generation 1:";
+io.out gen1;
+
+gen2 : step gen1;
+io.out "Generation 2:";
+io.out gen2;
+
+io.out "Complete!";