about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--js/life.js81
1 files changed, 40 insertions, 41 deletions
diff --git a/js/life.js b/js/life.js
index 534fdf3..b6fe9de 100644
--- a/js/life.js
+++ b/js/life.js
@@ -1,43 +1,42 @@
-const getNextState = (board) => {
-  const rows = board.length;
-  const cols = board[0].length;
+function countNeighbors(grid, x, y) {
+  const neighborOffsets = [
+      [-1, -1], [-1, 0], [-1, 1],
+      [0, -1],           [0, 1],
+      [1, -1],  [1, 0],  [1, 1]
+  ];
 
-  const countLiveNeighbors = (board, x, y) => {
-    return [-1, 0, 1].reduce(
-      (acc, dx) =>
-        acc + [-1, 0, 1].reduce((acc, dy) =>
-          (dx === 0 && dy === 0) || x + dx < 0 || y + dy < 0 ||
-            x + dx >= rows || y + dy >= cols
-            ? acc
-            : acc + board[x + dx][y + dy], 0),
-      0,
-    );
-  };
+  return neighborOffsets.reduce((count, [dx, dy]) => {
+      const nx = x + dx;
+      const ny = y + dy;
+      if (nx >= 0 && ny >= 0 && nx < grid.length && ny < grid[0].length && grid[nx][ny]) {
+          return count + 1;
+      }
+      return count;
+  }, 0);
+}
 
-  return board.map((row, x) =>
-    row.map((cell, y) => {
-      const liveNeighbors = countLiveNeighbors(board, x, y);
-      return (cell === 1 && (liveNeighbors === 2 || liveNeighbors === 3)) ||
-          (cell === 0 && liveNeighbors === 3)
-        ? 1
-        : 0;
-    })
+function step(grid) {
+  return grid.map((row, x) =>
+      row.map((cell, y) => {
+          const neighbors = countNeighbors(grid, x, y);
+          return neighbors === 3 || (neighbors === 2 && cell) ? 1 : 0;
+      })
   );
-};
+}
 
-const printBoard = (board) => {
-  console.clear();
-  console.log(board.map((row) => row.join(" ")).join("\n"));
-};
+function printGrid(grid) {
+  grid.forEach(row => console.log(row.map(cell => cell ? '🟢' : '⚪️').join('')));
+  console.log('\n');
+}
+
+function simulate(initial, steps) {
+  let grid = initial;
+  Array.from({ length: steps }).forEach(() => {
+      printGrid(grid);
+      grid = step(grid);
+  });
+}
 
-const runGame = async (initialBoard, generations) => {
-  let board = initialBoard;
-  for (let i = 0; i < generations; i++) {
-    printBoard(board);
-    board = getNextState(board);
-    await new Promise((resolve) => setTimeout(resolve, 500)); // wait before printing the next step.
-  }
-};
 
 const initialBoard = [
   [0, 1, 0],
@@ -46,7 +45,7 @@ const initialBoard = [
   [0, 0, 0],
 ];
 
-// runGame(initialBoard, 100);
+simulate(initialBoard, 10);
 
 const rpentomino = [
   [0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
@@ -58,7 +57,7 @@ const rpentomino = [
   [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
 ];
 
-// runGame(rpentomino, 100);
+// simulate(rpentomino, 10);
 
 // big glider
 const bigGlider = [
@@ -71,10 +70,10 @@ const bigGlider = [
   [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
 ];
 
-// runGame(bigGlider, 100);
+// simulate(bigGlider, 10);
 
-const randomBoard = Array.from({ length: 10 }, () =>
-  Array.from({ length: 10 }, () => Math.round(Math.random()))
+const randomBoard = Array.from({ length: 22 }, () =>
+  Array.from({ length: 22 }, () => Math.round(Math.random()))
 );
 
-// runGame(randomBoard, 50);
\ No newline at end of file
+simulate(randomBoard, 50);