// Conway's Game of Life - Simple Working Version // Get element at index from list (safe) at : xs i -> when (i >= 0 and i < length xs) is true then slice xs i (i + 1).0 _ then 0; // Get 2D element from grid (safe) get2d : grid row col -> when (row >= 0 and row < length grid and col >= 0) is true then with (rowData : at grid row) -> when (col < length rowData) is true then at rowData col _ then 0 _ then 0; // Count living neighbors around position (row, col) countNeighbors : grid row col -> with ( n1 : get2d grid (row - 1) (col - 1); n2 : get2d grid (row - 1) col; n3 : get2d grid (row - 1) (col + 1); n4 : get2d grid row (col - 1); n5 : get2d grid row (col + 1); n6 : get2d grid (row + 1) (col - 1); n7 : get2d grid (row + 1) col; n8 : get2d grid (row + 1) (col + 1); ) -> n1 + n2 + n3 + n4 + n5 + n6 + n7 + n8; // Apply Game of Life rules to a single cell nextCellState : 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 of the entire grid nextGeneration : grid -> with ( height : length grid; width : length (at grid 0); // Create new grid row by row newRow : rowIdx -> with ( newCol : colIdx -> nextCellState grid rowIdx colIdx; cols : [0, 1, 2, 3, 4]; // Assuming 5x5 for now ) -> map newCol cols; rows : [0, 1, 2, 3, 4]; // Assuming 5x5 for now ) -> map newRow rows; // Print a single row printRow : row -> with ( cellToChar : cell -> when cell is 1 then "#" _ then "."; chars : map cellToChar row; ) -> io.out chars; // Print entire grid with title printGrid : grid title -> with ( _ : io.out ""; _ : io.out title; _ : io.out "-----"; _ : map printRow grid; ) -> 0; // Return dummy value // Test patterns // Glider pattern (moves diagonally) 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 pattern (oscillates) 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 simulations io.out "Conway's Game of Life"; io.out "===================="; // Show initial glider dummy1 : printGrid glider "Glider - Generation 0"; g1 : nextGeneration glider; dummy2 : printGrid g1 "Glider - Generation 1"; g2 : nextGeneration g1; dummy3 : printGrid g2 "Glider - Generation 2"; // Show blinker oscillation dummy4 : printGrid blinker "Blinker - Generation 0"; b1 : nextGeneration blinker; dummy5 : printGrid b1 "Blinker - Generation 1"; b2 : nextGeneration b1; dummy6 : printGrid b2 "Blinker - Generation 2"; io.out "Done!";