about summary refs log tree commit diff stats
path: root/js/scripting-lang/life.txt
diff options
context:
space:
mode:
Diffstat (limited to 'js/scripting-lang/life.txt')
-rw-r--r--js/scripting-lang/life.txt164
1 files changed, 164 insertions, 0 deletions
diff --git a/js/scripting-lang/life.txt b/js/scripting-lang/life.txt
new file mode 100644
index 0000000..c97e91e
--- /dev/null
+++ b/js/scripting-lang/life.txt
@@ -0,0 +1,164 @@
+/* ========================================
+   CONWAY'S GAME OF LIFE IN BABA YAGA
+   ========================================
+   
+   This tutorial demonstrates Conway's Game of Life using Baba Yaga's
+   functional programming features. This version is designed to work
+   with both the JavaScript and C implementations.
+   
+   Game of Life Rules:
+   - Live cell with 2-3 neighbors survives
+   - Dead cell with exactly 3 neighbors becomes alive
+   - All other cells die or stay dead
+*/
+
+/* ========================================
+   STEP 1: GRID REPRESENTATION
+   ========================================
+   
+   We use a 3x3 grid with numeric indices 0-8:
+   0 1 2
+   3 4 5  
+   6 7 8
+   
+   This creates a classic "glider" pattern.
+*/
+
+grid : {
+  0: 0, 1: 1, 2: 0,
+  3: 0, 4: 0, 5: 1,
+  6: 1, 7: 1, 8: 1
+};
+
+/* ========================================
+   STEP 2: COORDINATE HELPERS
+   ========================================
+*/
+
+/* Convert 2D coordinates to linear index */
+xy_to_index : x y -> (y * 3) + x;
+
+/* Get cell value safely */
+get_cell : grid x y -> grid[xy_to_index x y];
+
+/* Check bounds */
+valid : x y -> (x >= 0) and (x < 3) and (y >= 0) and (y < 3);
+
+/* Safe cell access (0 if out of bounds) */
+safe_get : grid x y -> when (valid x y) is true then get_cell grid x y _ then 0;
+
+/* ========================================
+   STEP 3: NEIGHBOR COUNTING
+   ========================================
+*/
+
+/* Count all 8 neighbors around a cell */
+neighbors : grid x y ->
+  (safe_get grid (x-1) (y-1)) + (safe_get grid x (y-1)) + (safe_get grid (x+1) (y-1)) +
+  (safe_get grid (x-1) y) +                                (safe_get grid (x+1) y) +
+  (safe_get grid (x-1) (y+1)) + (safe_get grid x (y+1)) + (safe_get grid (x+1) (y+1));
+
+/* ========================================
+   STEP 4: GAME RULES
+   ========================================
+*/
+
+/* Apply Conway's rules */
+next_state : grid x y -> when (get_cell grid x y) (neighbors grid x y) is
+  1 2 then 1  /* Live cell with 2 neighbors survives */
+  1 3 then 1  /* Live cell with 3 neighbors survives */
+  0 3 then 1  /* Dead cell with 3 neighbors becomes alive */
+  _ _ then 0;  /* All other cases: cell dies or stays dead */
+
+/* ========================================
+   STEP 5: GRID EVOLUTION
+   ========================================
+*/
+
+/* Generate next generation */
+evolve : grid -> {
+  0: next_state grid 0 0, 1: next_state grid 1 0, 2: next_state grid 2 0,
+  3: next_state grid 0 1, 4: next_state grid 1 1, 5: next_state grid 2 1,
+  6: next_state grid 0 2, 7: next_state grid 1 2, 8: next_state grid 2 2
+};
+
+/* ========================================
+   STEP 6: VISUALIZATION
+   ========================================
+*/
+
+/* Convert cell to symbol */
+symbol : n -> when n is 1 then "█" _ then "░";
+
+/* Display grid */
+show : grid ->
+  ..out "┌───┐";
+  ..out ((symbol (get_cell grid 0 0)) + (symbol (get_cell grid 1 0)) + (symbol (get_cell grid 2 0)));
+  ..out ((symbol (get_cell grid 0 1)) + (symbol (get_cell grid 1 1)) + (symbol (get_cell grid 2 1)));
+  ..out ((symbol (get_cell grid 0 2)) + (symbol (get_cell grid 1 2)) + (symbol (get_cell grid 2 2)));
+  ..out "└───┘";
+
+/* Count live cells */
+count : grid -> 
+  (get_cell grid 0 0) + (get_cell grid 1 0) + (get_cell grid 2 0) +
+  (get_cell grid 0 1) + (get_cell grid 1 1) + (get_cell grid 2 1) +
+  (get_cell grid 0 2) + (get_cell grid 1 2) + (get_cell grid 2 2);
+
+/* ========================================
+   STEP 7: SIMULATION
+   ========================================
+*/
+
+..out "=== CONWAY'S GAME OF LIFE ===";
+..out "";
+
+..out "Generation 0:";
+show grid;
+..out ("Live: " + (count grid));
+..out "";
+
+gen1 : evolve grid;
+..out "Generation 1:";
+show gen1;
+..out ("Live: " + (count gen1));
+..out "";
+
+gen2 : evolve gen1;
+..out "Generation 2:";
+show gen2;
+..out ("Live: " + (count gen2));
+..out "";
+
+gen3 : evolve gen2;
+..out "Generation 3:";
+show gen3;
+..out ("Live: " + (count gen3));
+..out "";
+
+/* ========================================
+   STEP 8: VALIDATION
+   ========================================
+*/
+
+..out "=== TESTS ===";
+
+/* Test cell access */
+center_cell : get_cell grid 1 1;
+..out ("Center cell: " + center_cell);
+
+/* Test neighbor counting */
+center_neighbors : neighbors grid 1 1;
+..out ("Center neighbors: " + center_neighbors);
+
+/* Test evolution */
+center_next : next_state grid 1 1;
+..out ("Center next: " + center_next);
+
+..out "";
+..out "Tutorial complete! This demonstrates:";
+..out "• Pattern matching for game rules";
+..out "• Immutable data transformations";
+..out "• Functional grid operations";
+..out "• Table-based data structures";
+..out "";
+..out "Conway's Game of Life in pure Baba Yaga!";
\ No newline at end of file