{"id":1426,"date":"2025-06-30T08:46:53","date_gmt":"2025-06-30T12:46:53","guid":{"rendered":"https:\/\/resrvoir.com\/?page_id=1426"},"modified":"2025-06-30T09:54:04","modified_gmt":"2025-06-30T13:54:04","slug":"game-of-life","status":"publish","type":"page","link":"https:\/\/resrvoir.com\/?page_id=1426","title":{"rendered":"Game of Life"},"content":{"rendered":"<p>Conway&#8217;s Game of Life, cellular automaton operates on a two-dimensional grid of cells, where each cell can be in one of two states:<br \/>\nAlive (1)<br \/>\nDead (0)<\/p>\n<p>Here are the rules:<br \/>\nA live cell with fewer than 2 live neighbors dies (underpopulation).<br \/>\nA live cell with 2 or 3 live neighbors stays alive (survival).<br \/>\nA live cell with more than 3 live neighbors dies (overpopulation).<br \/>\nA dead cell with exactly 3 live neighbors becomes alive (reproduction).<\/p>\n<p>The grid should be updated only after evaluating the whole grid first.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">\r\nconst rows = 3\r\nconst cols = 3\r\n\r\nconst grid = [\r\n  [0, 1, 0],\r\n  [0, 1, 0],\r\n  [0, 1, 0]\r\n]\r\n\r\nconst nextGrid = Array.from({ length: rows }, () =&gt; Array(cols).fill(0))\r\n\r\nfor (let i = 0; i &lt; rows; i++) {\r\n  for (let j = 0; j &lt; cols; j++) {\r\n    const neighbors = countNeighbors(grid, i, j, rows, cols)\r\n\r\n    console.log(\"count neigbor is\", neighbors)\r\n\r\n    const cell = grid[i][j]\r\n\r\n    if (cell === 1) {\r\n      if (neighbors &lt; 2) {\r\n        \/\/ die\r\n        nextGrid[i][j] = 0\r\n      } else if (neighbors === 2 || neighbors === 3) {\r\n        \/\/ live\r\n        nextGrid[i][j] = 1\r\n      } else if (neighbors &gt; 3) {\r\n        \/\/ die\r\n        nextGrid[i][j] = 0\r\n      }\r\n    } else {\r\n      if (neighbors === 3) {\r\n        nextGrid[i][j] = 1\r\n      }\r\n    }\r\n  }\r\n}\r\n\r\nconsole.log(\"grid is\", grid)\r\nconsole.log(\"nextGrid is\", nextGrid)\r\n\r\nfunction countNeighbors(grid, row, col, rows, cols) {\r\n  const rowAbove = row - 1\r\n  const rowBelow = row + 1\r\n  const colLeft = col - 1\r\n  const colRight = col + 1\r\n\r\n  const neighbors = [\r\n    [rowAbove, colLeft],\r\n    [rowAbove, col],\r\n    [rowAbove, colRight],\r\n    [row, colLeft],\r\n    [row, colRight],\r\n    [rowBelow, colLeft],\r\n    [rowBelow, col],\r\n    [rowBelow, colRight]\r\n  ]\r\n\r\n  let count = 0\r\n\r\n  for (let neighbor of neighbors) {\r\n    const [r, c] = neighbor\r\n\r\n    if (r >= 0 && r <= rows - 1 &#038;&#038; c >= 0 && c <= cols - 1) {\r\n      if (grid[r][c]) count++\r\n    }\r\n\r\n  return count\r\n}\r\n<\/pre>\n<p>Output is<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">\r\ngrid is [ [ 0, 1, 0 ], [ 0, 1, 0 ], [ 0, 1, 0 ] ]\r\nnextGrid is [ [ 0, 0, 0 ], [ 1, 1, 1 ], [ 0, 0, 0 ] ]\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Conway&#8217;s Game of Life, cellular automaton operates on a two-dimensional grid of cells, where each cell can be in one of two states: Alive (1) Dead (0) Here are the rules: A live cell with fewer than 2 live neighbors dies (underpopulation). A live cell with 2 or 3 live neighbors stays alive (survival). A [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":2,"menu_order":-1,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"_links":{"self":[{"href":"https:\/\/resrvoir.com\/index.php?rest_route=\/wp\/v2\/pages\/1426"}],"collection":[{"href":"https:\/\/resrvoir.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/resrvoir.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/resrvoir.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/resrvoir.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1426"}],"version-history":[{"count":6,"href":"https:\/\/resrvoir.com\/index.php?rest_route=\/wp\/v2\/pages\/1426\/revisions"}],"predecessor-version":[{"id":1435,"href":"https:\/\/resrvoir.com\/index.php?rest_route=\/wp\/v2\/pages\/1426\/revisions\/1435"}],"up":[{"embeddable":true,"href":"https:\/\/resrvoir.com\/index.php?rest_route=\/wp\/v2\/pages\/2"}],"wp:attachment":[{"href":"https:\/\/resrvoir.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1426"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}