Gallery · Animation

Conway's Game of Life

C# seeds a glider gun and publishes the grid as JSON; an HTML cell runs the whole universe on a canvas. Play it, step it, or draw new cells straight onto the grid.

game-of-life.verso C# · HTML · 4 cells Read-only preview Download

Conway's Game of Life

Four rules and no players: a live cell with two or three neighbors survives, a dead cell with exactly three neighbors is born, and everything else dies. The C# cell below builds the starting grid (a Gosper glider gun and a pulsar) and publishes it to the variable store as JSON; the HTML cell reads it back with @lifeSeed and runs the whole universe in a <canvas>. Press play, or draw your own cells straight onto the grid with the mouse.

[1]
codeC#
1using System.Text.Json;
2
3// The Gosper glider gun: the first pattern found that grows forever.
4var gun = new[]
5{
6 (0,4),(0,5),(1,4),(1,5),(10,4),(10,5),(10,6),(11,3),(11,7),(12,2),(12,8),
7 (13,2),(13,8),(14,5),(15,3),(15,7),(16,4),(16,5),(16,6),(17,5),(20,2),
8 (20,3),(20,4),(21,2),(21,3),(21,4),(22,1),(22,5),(24,0),(24,1),(24,5),
9 (24,6),(34,2),(34,3),(35,2),(35,3)
10};
11
12var live = new HashSet<(int X, int Y)>();
13foreach (var (x, y) in gun)
14 live.Add((x + 2, y + 2));
15
16// A pulsar (period-3 oscillator), generated from its two-fold symmetry.
17var arms = new[] { 2, 3, 4, 8, 9, 10 };
18var rims = new[] { 0, 5, 7, 12 };
19foreach (var a in arms)
20 foreach (var r in rims)
21 {
22 live.Add((72 + a, 30 + r));
23 live.Add((72 + r, 30 + a));
24 }
25
26var width = 104;
27var height = 60;
28var lifeSeed = JsonSerializer.Serialize(new
29{
30 w = width,
31 h = height,
32 cells = live.OrderBy(c => c.Y).ThenBy(c => c.X).Select(c => new[] { c.X, c.Y })
33});
34
35$"Seeded {live.Count} live cells on a {width} x {height} wrap-around grid."
✓completed
Seeded 84 live cells on a 104 x 60 wrap-around grid.
[2]
html
1<div style="font-family:var(--verso-prose-font-family,sans-serif);">
2 <div style="display:flex;align-items:center;gap:8px;flex-wrap:wrap;margin-bottom:8px;">
3 <button id="life-play" type="button" style="padding: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;">Pause</button>
4 <button id="life-step" type="button" style="padding: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;">Step</button>
5 <button id="life-reset" type="button" style="padding: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;">Reset</button>
6 <button id="life-clear" type="button" style="padding: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;">Clear</button>
7 <span id="life-stats" style="font-size:12.5px;color:var(--verso-fg-muted,#777);font-variant-numeric:tabular-nums;"></span>
8 <span style="margin-left:auto;font-size:12.5px;color:var(--verso-fg-muted,#777);">draw on the grid with your mouse</span>
9 </div>
10 <canvas id="life-canvas" width="832" height="480"
11 style="width:100%;max-width:832px;display:block;border-radius:8px;touch-action:none;"></canvas>
12</div>
13<script>
14(function () {
15 var seed = @lifeSeed;
16 var W = seed.w, H = seed.h, SIZE = 8;
17 var canvas = document.getElementById('life-canvas');
18 var ctx = canvas.getContext('2d');
19 var stats = document.getElementById('life-stats');
20 var grid = new Uint8Array(W * H);
21 var next = new Uint8Array(W * H);
22 var age = new Int32Array(W * H);
23 var generation = 0;
24 var playing = true;
25
26 function reseed() {
27 grid.fill(0); age.fill(0); generation = 0;
28 for (var i = 0; i < seed.cells.length; i++) {
29 var c = seed.cells[i];
30 grid[c[1] * W + c[0]] = 1;
31 age[c[1] * W + c[0]] = 1;
32 }
33 }
34
35 function step() {
36 for (var y = 0; y < H; y++) {
37 var up = ((y + H - 1) % H) * W, row = y * W, down = ((y + 1) % H) * W;
38 for (var x = 0; x < W; x++) {
39 var l = (x + W - 1) % W, r = (x + 1) % W;
40 var n = grid[up + l] + grid[up + x] + grid[up + r]
41 + grid[row + l] + grid[row + r]
42 + grid[down + l] + grid[down + x] + grid[down + r];
43 next[row + x] = (n === 3 || (n === 2 && grid[row + x])) ? 1 : 0;
44 }
45 }
46 for (var i = 0; i < grid.length; i++) {
47 age[i] = next[i] ? (grid[i] ? age[i] + 1 : 1) : 0;
48 grid[i] = next[i];
49 }
50 generation++;
51 }
52
53 function draw() {
54 ctx.fillStyle = '#10131a';
55 ctx.fillRect(0, 0, canvas.width, canvas.height);
56 var pop = 0;
57 for (var y = 0; y < H; y++) {
58 for (var x = 0; x < W; x++) {
59 var i = y * W + x;
60 if (!grid[i]) continue;
61 pop++;
62 var t = Math.min(age[i], 24) / 24;
63 ctx.fillStyle = 'hsl(196, 90%, ' + Math.round(72 - 38 * t) + '%)';
64 ctx.fillRect(x * SIZE + 1, y * SIZE + 1, SIZE - 2, SIZE - 2);
65 }
66 }
67 stats.textContent = 'generation ' + generation + ' · population ' + pop;
68 }
69
70 var drawing = false;
71 function paint(e) {
72 var rect = canvas.getBoundingClientRect();
73 var x = Math.floor((e.clientX - rect.left) * W / rect.width);
74 var y = Math.floor((e.clientY - rect.top) * H / rect.height);
75 if (x < 0 || y < 0 || x >= W || y >= H) return;
76 grid[y * W + x] = 1;
77 age[y * W + x] = 1;
78 draw();
79 }
80 canvas.addEventListener('pointerdown', function (e) {
81 drawing = true; canvas.setPointerCapture(e.pointerId); paint(e); e.preventDefault();
82 });
83 canvas.addEventListener('pointermove', function (e) { if (drawing) paint(e); });
84 canvas.addEventListener('pointerup', function () { drawing = false; });
85
86 var playBtn = document.getElementById('life-play');
87 playBtn.addEventListener('click', function () {
88 playing = !playing;
89 playBtn.textContent = playing ? 'Pause' : 'Play';
90 });
91 document.getElementById('life-step').addEventListener('click', function () {
92 playing = false; playBtn.textContent = 'Play'; step(); draw();
93 });
94 document.getElementById('life-reset').addEventListener('click', function () {
95 reseed(); draw();
96 });
97 document.getElementById('life-clear').addEventListener('click', function () {
98 grid.fill(0); age.fill(0); generation = 0; draw();
99 });
100
101 var last = 0;
102 function loop(ts) {
103 if (!canvas.isConnected) return;
104 if (playing && ts - last > 70) { step(); draw(); last = ts; }
105 requestAnimationFrame(loop);
106 }
107 reseed();
108 draw();
109 requestAnimationFrame(loop);
110})();
111</script>
✓completed
draw on the grid with your mouse

The rules do all the work

The gun on the left fires a glider every thirty generations; the pulsar on the right just breathes. Because the edges wrap around, the glider stream eventually loops back and crashes into its own source: let it run and see what grows out of the wreckage. Then hit Clear and doodle; almost any dense scribble collapses into the familiar zoo of blinkers, blocks, and escaping gliders.

There is no game engine hiding anywhere. C# published lifeSeed as a JSON string, and the HTML cell dropped it into its script with @lifeSeed before rendering, the same substitution Mermaid and SQL cells use. Swap in your own seed, or generate one from data.

Run it yourself

Nothing on this page is executable; the outputs were baked in by a real run. Download the notebook and it runs anywhere Verso does: VS Code, the browser, or the terminal.

dotnet tool install --global Verso.Cli
verso serve game-of-life.verso

What this shows

C# seeds a glider gun and publishes the grid as JSON; an HTML cell runs the whole universe on a canvas. Play it, step it, or draw new cells straight onto the grid.

Every language in this notebook reads and writes the same variable store, so a value produced in one cell is simply there in the next, whatever the language.