{
  "verso": "1.1",
  "metadata": {
    "title": "Conway\u0027s Game of Life",
    "created": "2026-07-04T00:00:00+00:00",
    "defaultKernel": "csharp",
    "activeLayout": {
      "extensionId": "verso.layout.notebook",
      "layoutId": "notebook"
    }
  },
  "cells": [
    {
      "id": "00000005-0001-0001-0001-000000000001",
      "type": "markdown",
      "source": "# Conway\u0027s Game of Life\n\nFour rules and no players: a live cell with two or three neighbors survives, a\ndead cell with exactly three neighbors is born, and everything else dies. The\nC# cell below builds the starting grid (a Gosper glider gun and a pulsar) and\npublishes it to the variable store as JSON; the HTML cell reads it back with\n\u0060@lifeSeed\u0060 and runs the whole universe in a \u0060\u003Ccanvas\u003E\u0060. Press play, or draw\nyour own cells straight onto the grid with the mouse.",
      "outputs": [
        {
          "mimeType": "text/html",
          "content": "\u003Ch1 id=\u0022conways-game-of-life\u0022\u003EConway\u0027s Game of Life\u003C/h1\u003E\n\u003Cp\u003EFour rules and no players: a live cell with two or three neighbors survives, a\ndead cell with exactly three neighbors is born, and everything else dies. The\nC# cell below builds the starting grid (a Gosper glider gun and a pulsar) and\npublishes it to the variable store as JSON; the HTML cell reads it back with\n\u003Ccode\u003E@lifeSeed\u003C/code\u003E and runs the whole universe in a \u003Ccode\u003E\u0026lt;canvas\u0026gt;\u003C/code\u003E. Press play, or draw\nyour own cells straight onto the grid with the mouse.\u003C/p\u003E\n"
        }
      ]
    },
    {
      "id": "00000005-0001-0001-0001-000000000002",
      "type": "code",
      "language": "csharp",
      "source": "using System.Text.Json;\n\n// The Gosper glider gun: the first pattern found that grows forever.\nvar gun = new[]\n{\n    (0,4),(0,5),(1,4),(1,5),(10,4),(10,5),(10,6),(11,3),(11,7),(12,2),(12,8),\n    (13,2),(13,8),(14,5),(15,3),(15,7),(16,4),(16,5),(16,6),(17,5),(20,2),\n    (20,3),(20,4),(21,2),(21,3),(21,4),(22,1),(22,5),(24,0),(24,1),(24,5),\n    (24,6),(34,2),(34,3),(35,2),(35,3)\n};\n\nvar live = new HashSet\u003C(int X, int Y)\u003E();\nforeach (var (x, y) in gun)\n    live.Add((x \u002B 2, y \u002B 2));\n\n// A pulsar (period-3 oscillator), generated from its two-fold symmetry.\nvar arms = new[] { 2, 3, 4, 8, 9, 10 };\nvar rims = new[] { 0, 5, 7, 12 };\nforeach (var a in arms)\n    foreach (var r in rims)\n    {\n        live.Add((72 \u002B a, 30 \u002B r));\n        live.Add((72 \u002B r, 30 \u002B a));\n    }\n\nvar width = 104;\nvar height = 60;\nvar lifeSeed = JsonSerializer.Serialize(new\n{\n    w = width,\n    h = height,\n    cells = live.OrderBy(c =\u003E c.Y).ThenBy(c =\u003E c.X).Select(c =\u003E new[] { c.X, c.Y })\n});\n\n$\u0022Seeded {live.Count} live cells on a {width} x {height} wrap-around grid.\u0022\n",
      "outputs": [
        {
          "mimeType": "text/plain",
          "content": "Seeded 84 live cells on a 104 x 60 wrap-around grid."
        }
      ]
    },
    {
      "id": "00000005-0001-0001-0001-000000000003",
      "type": "html",
      "language": "html",
      "source": "\u003Cdiv style=\u0022font-family:var(--verso-prose-font-family,sans-serif);\u0022\u003E\n  \u003Cdiv style=\u0022display:flex;align-items:center;gap:8px;flex-wrap:wrap;margin-bottom:8px;\u0022\u003E\n    \u003Cbutton id=\u0022life-play\u0022 type=\u0022button\u0022 style=\u0022padding:3px 12px;border:1px solid var(--verso-border-default,#bfbfbf);border-radius:999px;background:transparent;color:inherit;font:inherit;font-size:12.5px;cursor:pointer;\u0022\u003EPause\u003C/button\u003E\n    \u003Cbutton id=\u0022life-step\u0022 type=\u0022button\u0022 style=\u0022padding:3px 12px;border:1px solid var(--verso-border-default,#bfbfbf);border-radius:999px;background:transparent;color:inherit;font:inherit;font-size:12.5px;cursor:pointer;\u0022\u003EStep\u003C/button\u003E\n    \u003Cbutton id=\u0022life-reset\u0022 type=\u0022button\u0022 style=\u0022padding:3px 12px;border:1px solid var(--verso-border-default,#bfbfbf);border-radius:999px;background:transparent;color:inherit;font:inherit;font-size:12.5px;cursor:pointer;\u0022\u003EReset\u003C/button\u003E\n    \u003Cbutton id=\u0022life-clear\u0022 type=\u0022button\u0022 style=\u0022padding:3px 12px;border:1px solid var(--verso-border-default,#bfbfbf);border-radius:999px;background:transparent;color:inherit;font:inherit;font-size:12.5px;cursor:pointer;\u0022\u003EClear\u003C/button\u003E\n    \u003Cspan id=\u0022life-stats\u0022 style=\u0022font-size:12.5px;color:var(--verso-fg-muted,#777);font-variant-numeric:tabular-nums;\u0022\u003E\u003C/span\u003E\n    \u003Cspan style=\u0022margin-left:auto;font-size:12.5px;color:var(--verso-fg-muted,#777);\u0022\u003Edraw on the grid with your mouse\u003C/span\u003E\n  \u003C/div\u003E\n  \u003Ccanvas id=\u0022life-canvas\u0022 width=\u0022832\u0022 height=\u0022480\u0022\n          style=\u0022width:100%;max-width:832px;display:block;border-radius:8px;touch-action:none;\u0022\u003E\u003C/canvas\u003E\n\u003C/div\u003E\n\u003Cscript\u003E\n(function () {\n  var seed = @lifeSeed;\n  var W = seed.w, H = seed.h, SIZE = 8;\n  var canvas = document.getElementById(\u0027life-canvas\u0027);\n  var ctx = canvas.getContext(\u00272d\u0027);\n  var stats = document.getElementById(\u0027life-stats\u0027);\n  var grid = new Uint8Array(W * H);\n  var next = new Uint8Array(W * H);\n  var age = new Int32Array(W * H);\n  var generation = 0;\n  var playing = true;\n\n  function reseed() {\n    grid.fill(0); age.fill(0); generation = 0;\n    for (var i = 0; i \u003C seed.cells.length; i\u002B\u002B) {\n      var c = seed.cells[i];\n      grid[c[1] * W \u002B c[0]] = 1;\n      age[c[1] * W \u002B c[0]] = 1;\n    }\n  }\n\n  function step() {\n    for (var y = 0; y \u003C H; y\u002B\u002B) {\n      var up = ((y \u002B H - 1) % H) * W, row = y * W, down = ((y \u002B 1) % H) * W;\n      for (var x = 0; x \u003C W; x\u002B\u002B) {\n        var l = (x \u002B W - 1) % W, r = (x \u002B 1) % W;\n        var n = grid[up \u002B l] \u002B grid[up \u002B x] \u002B grid[up \u002B r]\n              \u002B grid[row \u002B l] \u002B grid[row \u002B r]\n              \u002B grid[down \u002B l] \u002B grid[down \u002B x] \u002B grid[down \u002B r];\n        next[row \u002B x] = (n === 3 || (n === 2 \u0026\u0026 grid[row \u002B x])) ? 1 : 0;\n      }\n    }\n    for (var i = 0; i \u003C grid.length; i\u002B\u002B) {\n      age[i] = next[i] ? (grid[i] ? age[i] \u002B 1 : 1) : 0;\n      grid[i] = next[i];\n    }\n    generation\u002B\u002B;\n  }\n\n  function draw() {\n    ctx.fillStyle = \u0027#10131a\u0027;\n    ctx.fillRect(0, 0, canvas.width, canvas.height);\n    var pop = 0;\n    for (var y = 0; y \u003C H; y\u002B\u002B) {\n      for (var x = 0; x \u003C W; x\u002B\u002B) {\n        var i = y * W \u002B x;\n        if (!grid[i]) continue;\n        pop\u002B\u002B;\n        var t = Math.min(age[i], 24) / 24;\n        ctx.fillStyle = \u0027hsl(196, 90%, \u0027 \u002B Math.round(72 - 38 * t) \u002B \u0027%)\u0027;\n        ctx.fillRect(x * SIZE \u002B 1, y * SIZE \u002B 1, SIZE - 2, SIZE - 2);\n      }\n    }\n    stats.textContent = \u0027generation \u0027 \u002B generation \u002B \u0027 \u00B7 population \u0027 \u002B pop;\n  }\n\n  var drawing = false;\n  function paint(e) {\n    var rect = canvas.getBoundingClientRect();\n    var x = Math.floor((e.clientX - rect.left) * W / rect.width);\n    var y = Math.floor((e.clientY - rect.top) * H / rect.height);\n    if (x \u003C 0 || y \u003C 0 || x \u003E= W || y \u003E= H) return;\n    grid[y * W \u002B x] = 1;\n    age[y * W \u002B x] = 1;\n    draw();\n  }\n  canvas.addEventListener(\u0027pointerdown\u0027, function (e) {\n    drawing = true; canvas.setPointerCapture(e.pointerId); paint(e); e.preventDefault();\n  });\n  canvas.addEventListener(\u0027pointermove\u0027, function (e) { if (drawing) paint(e); });\n  canvas.addEventListener(\u0027pointerup\u0027, function () { drawing = false; });\n\n  var playBtn = document.getElementById(\u0027life-play\u0027);\n  playBtn.addEventListener(\u0027click\u0027, function () {\n    playing = !playing;\n    playBtn.textContent = playing ? \u0027Pause\u0027 : \u0027Play\u0027;\n  });\n  document.getElementById(\u0027life-step\u0027).addEventListener(\u0027click\u0027, function () {\n    playing = false; playBtn.textContent = \u0027Play\u0027; step(); draw();\n  });\n  document.getElementById(\u0027life-reset\u0027).addEventListener(\u0027click\u0027, function () {\n    reseed(); draw();\n  });\n  document.getElementById(\u0027life-clear\u0027).addEventListener(\u0027click\u0027, function () {\n    grid.fill(0); age.fill(0); generation = 0; draw();\n  });\n\n  var last = 0;\n  function loop(ts) {\n    if (!canvas.isConnected) return;\n    if (playing \u0026\u0026 ts - last \u003E 70) { step(); draw(); last = ts; }\n    requestAnimationFrame(loop);\n  }\n  reseed();\n  draw();\n  requestAnimationFrame(loop);\n})();\n\u003C/script\u003E",
      "outputs": [
        {
          "mimeType": "text/html",
          "content": "\u003Cdiv style=\u0022font-family:var(--verso-prose-font-family,sans-serif);\u0022\u003E\n  \u003Cdiv style=\u0022display:flex;align-items:center;gap:8px;flex-wrap:wrap;margin-bottom:8px;\u0022\u003E\n    \u003Cbutton id=\u0022life-play\u0022 type=\u0022button\u0022 style=\u0022padding:3px 12px;border:1px solid var(--verso-border-default,#bfbfbf);border-radius:999px;background:transparent;color:inherit;font:inherit;font-size:12.5px;cursor:pointer;\u0022\u003EPause\u003C/button\u003E\n    \u003Cbutton id=\u0022life-step\u0022 type=\u0022button\u0022 style=\u0022padding:3px 12px;border:1px solid var(--verso-border-default,#bfbfbf);border-radius:999px;background:transparent;color:inherit;font:inherit;font-size:12.5px;cursor:pointer;\u0022\u003EStep\u003C/button\u003E\n    \u003Cbutton id=\u0022life-reset\u0022 type=\u0022button\u0022 style=\u0022padding:3px 12px;border:1px solid var(--verso-border-default,#bfbfbf);border-radius:999px;background:transparent;color:inherit;font:inherit;font-size:12.5px;cursor:pointer;\u0022\u003EReset\u003C/button\u003E\n    \u003Cbutton id=\u0022life-clear\u0022 type=\u0022button\u0022 style=\u0022padding:3px 12px;border:1px solid var(--verso-border-default,#bfbfbf);border-radius:999px;background:transparent;color:inherit;font:inherit;font-size:12.5px;cursor:pointer;\u0022\u003EClear\u003C/button\u003E\n    \u003Cspan id=\u0022life-stats\u0022 style=\u0022font-size:12.5px;color:var(--verso-fg-muted,#777);font-variant-numeric:tabular-nums;\u0022\u003E\u003C/span\u003E\n    \u003Cspan style=\u0022margin-left:auto;font-size:12.5px;color:var(--verso-fg-muted,#777);\u0022\u003Edraw on the grid with your mouse\u003C/span\u003E\n  \u003C/div\u003E\n  \u003Ccanvas id=\u0022life-canvas\u0022 width=\u0022832\u0022 height=\u0022480\u0022\n          style=\u0022width:100%;max-width:832px;display:block;border-radius:8px;touch-action:none;\u0022\u003E\u003C/canvas\u003E\n\u003C/div\u003E\n\u003Cscript\u003E\n(function () {\n  var seed = {\u0022w\u0022:104,\u0022h\u0022:60,\u0022cells\u0022:[[26,2],[24,3],[26,3],[14,4],[15,4],[22,4],[23,4],[36,4],[37,4],[13,5],[17,5],[22,5],[23,5],[36,5],[37,5],[2,6],[3,6],[12,6],[18,6],[22,6],[23,6],[2,7],[3,7],[12,7],[16,7],[18,7],[19,7],[24,7],[26,7],[12,8],[18,8],[26,8],[13,9],[17,9],[14,10],[15,10],[74,30],[75,30],[76,30],[80,30],[81,30],[82,30],[72,32],[77,32],[79,32],[84,32],[72,33],[77,33],[79,33],[84,33],[72,34],[77,34],[79,34],[84,34],[74,35],[75,35],[76,35],[80,35],[81,35],[82,35],[74,37],[75,37],[76,37],[80,37],[81,37],[82,37],[72,38],[77,38],[79,38],[84,38],[72,39],[77,39],[79,39],[84,39],[72,40],[77,40],[79,40],[84,40],[74,42],[75,42],[76,42],[80,42],[81,42],[82,42]]};\n  var W = seed.w, H = seed.h, SIZE = 8;\n  var canvas = document.getElementById(\u0027life-canvas\u0027);\n  var ctx = canvas.getContext(\u00272d\u0027);\n  var stats = document.getElementById(\u0027life-stats\u0027);\n  var grid = new Uint8Array(W * H);\n  var next = new Uint8Array(W * H);\n  var age = new Int32Array(W * H);\n  var generation = 0;\n  var playing = true;\n\n  function reseed() {\n    grid.fill(0); age.fill(0); generation = 0;\n    for (var i = 0; i \u003C seed.cells.length; i\u002B\u002B) {\n      var c = seed.cells[i];\n      grid[c[1] * W \u002B c[0]] = 1;\n      age[c[1] * W \u002B c[0]] = 1;\n    }\n  }\n\n  function step() {\n    for (var y = 0; y \u003C H; y\u002B\u002B) {\n      var up = ((y \u002B H - 1) % H) * W, row = y * W, down = ((y \u002B 1) % H) * W;\n      for (var x = 0; x \u003C W; x\u002B\u002B) {\n        var l = (x \u002B W - 1) % W, r = (x \u002B 1) % W;\n        var n = grid[up \u002B l] \u002B grid[up \u002B x] \u002B grid[up \u002B r]\n              \u002B grid[row \u002B l] \u002B grid[row \u002B r]\n              \u002B grid[down \u002B l] \u002B grid[down \u002B x] \u002B grid[down \u002B r];\n        next[row \u002B x] = (n === 3 || (n === 2 \u0026\u0026 grid[row \u002B x])) ? 1 : 0;\n      }\n    }\n    for (var i = 0; i \u003C grid.length; i\u002B\u002B) {\n      age[i] = next[i] ? (grid[i] ? age[i] \u002B 1 : 1) : 0;\n      grid[i] = next[i];\n    }\n    generation\u002B\u002B;\n  }\n\n  function draw() {\n    ctx.fillStyle = \u0027#10131a\u0027;\n    ctx.fillRect(0, 0, canvas.width, canvas.height);\n    var pop = 0;\n    for (var y = 0; y \u003C H; y\u002B\u002B) {\n      for (var x = 0; x \u003C W; x\u002B\u002B) {\n        var i = y * W \u002B x;\n        if (!grid[i]) continue;\n        pop\u002B\u002B;\n        var t = Math.min(age[i], 24) / 24;\n        ctx.fillStyle = \u0027hsl(196, 90%, \u0027 \u002B Math.round(72 - 38 * t) \u002B \u0027%)\u0027;\n        ctx.fillRect(x * SIZE \u002B 1, y * SIZE \u002B 1, SIZE - 2, SIZE - 2);\n      }\n    }\n    stats.textContent = \u0027generation \u0027 \u002B generation \u002B \u0027 \u00B7 population \u0027 \u002B pop;\n  }\n\n  var drawing = false;\n  function paint(e) {\n    var rect = canvas.getBoundingClientRect();\n    var x = Math.floor((e.clientX - rect.left) * W / rect.width);\n    var y = Math.floor((e.clientY - rect.top) * H / rect.height);\n    if (x \u003C 0 || y \u003C 0 || x \u003E= W || y \u003E= H) return;\n    grid[y * W \u002B x] = 1;\n    age[y * W \u002B x] = 1;\n    draw();\n  }\n  canvas.addEventListener(\u0027pointerdown\u0027, function (e) {\n    drawing = true; canvas.setPointerCapture(e.pointerId); paint(e); e.preventDefault();\n  });\n  canvas.addEventListener(\u0027pointermove\u0027, function (e) { if (drawing) paint(e); });\n  canvas.addEventListener(\u0027pointerup\u0027, function () { drawing = false; });\n\n  var playBtn = document.getElementById(\u0027life-play\u0027);\n  playBtn.addEventListener(\u0027click\u0027, function () {\n    playing = !playing;\n    playBtn.textContent = playing ? \u0027Pause\u0027 : \u0027Play\u0027;\n  });\n  document.getElementById(\u0027life-step\u0027).addEventListener(\u0027click\u0027, function () {\n    playing = false; playBtn.textContent = \u0027Play\u0027; step(); draw();\n  });\n  document.getElementById(\u0027life-reset\u0027).addEventListener(\u0027click\u0027, function () {\n    reseed(); draw();\n  });\n  document.getElementById(\u0027life-clear\u0027).addEventListener(\u0027click\u0027, function () {\n    grid.fill(0); age.fill(0); generation = 0; draw();\n  });\n\n  var last = 0;\n  function loop(ts) {\n    if (!canvas.isConnected) return;\n    if (playing \u0026\u0026 ts - last \u003E 70) { step(); draw(); last = ts; }\n    requestAnimationFrame(loop);\n  }\n  reseed();\n  draw();\n  requestAnimationFrame(loop);\n})();\n\u003C/script\u003E"
        }
      ]
    },
    {
      "id": "00000005-0001-0001-0001-000000000004",
      "type": "markdown",
      "source": "## The rules do all the work\n\nThe gun on the left fires a glider every thirty generations; the pulsar on the\nright just breathes. Because the edges wrap around, the glider stream\neventually loops back and crashes into its own source: let it run and see what\ngrows out of the wreckage. Then hit Clear and doodle; almost any dense scribble\ncollapses into the familiar zoo of blinkers, blocks, and escaping gliders.\n\nThere is no game engine hiding anywhere. C# published \u0060lifeSeed\u0060 as a JSON\nstring, and the HTML cell dropped it into its script with \u0060@lifeSeed\u0060 before\nrendering, the same substitution Mermaid and SQL cells use. Swap in your own\nseed, or generate one from data.",
      "outputs": [
        {
          "mimeType": "text/html",
          "content": "\u003Ch2 id=\u0022the-rules-do-all-the-work\u0022\u003EThe rules do all the work\u003C/h2\u003E\n\u003Cp\u003EThe gun on the left fires a glider every thirty generations; the pulsar on the\nright just breathes. Because the edges wrap around, the glider stream\neventually loops back and crashes into its own source: let it run and see what\ngrows out of the wreckage. Then hit Clear and doodle; almost any dense scribble\ncollapses into the familiar zoo of blinkers, blocks, and escaping gliders.\u003C/p\u003E\n\u003Cp\u003EThere is no game engine hiding anywhere. C# published \u003Ccode\u003ElifeSeed\u003C/code\u003E as a JSON\nstring, and the HTML cell dropped it into its script with \u003Ccode\u003E{\u0026quot;w\u0026quot;:104,\u0026quot;h\u0026quot;:60,\u0026quot;cells\u0026quot;:[[26,2],[24,3],[26,3],[14,4],[15,4],[22,4],[23,4],[36,4],[37,4],[13,5],[17,5],[22,5],[23,5],[36,5],[37,5],[2,6],[3,6],[12,6],[18,6],[22,6],[23,6],[2,7],[3,7],[12,7],[16,7],[18,7],[19,7],[24,7],[26,7],[12,8],[18,8],[26,8],[13,9],[17,9],[14,10],[15,10],[74,30],[75,30],[76,30],[80,30],[81,30],[82,30],[72,32],[77,32],[79,32],[84,32],[72,33],[77,33],[79,33],[84,33],[72,34],[77,34],[79,34],[84,34],[74,35],[75,35],[76,35],[80,35],[81,35],[82,35],[74,37],[75,37],[76,37],[80,37],[81,37],[82,37],[72,38],[77,38],[79,38],[84,38],[72,39],[77,39],[79,39],[84,39],[72,40],[77,40],[79,40],[84,40],[74,42],[75,42],[76,42],[80,42],[81,42],[82,42]]}\u003C/code\u003E before\nrendering, the same substitution Mermaid and SQL cells use. Swap in your own\nseed, or generate one from data.\u003C/p\u003E\n"
        }
      ]
    }
  ]
}