summaryrefslogtreecommitdiff
path: root/addons/life/life.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-06 14:27:06 -0300
committerGabriel Schneider <[email protected]>2026-11-06 14:53:45 -0300
commit333cdea69956adda91f462980a18f5bac6548dda (patch)
tree641e3226c4ce0192bbdab3ad167248f120d9ab8b /addons/life/life.zig
parent31706a1d1c13c26c69f67f107b72179019cbd255 (diff)
downloadpardes-333cdea69956adda91f462980a18f5bac6548dda.tar.gz
pardes-333cdea69956adda91f462980a18f5bac6548dda.zip
kinds #27, life: addons/life/life.zig is a second shipped Zig addon, Conway's Life in a pane written clean-room against src/kinds/addon.zig and the header alone (steps ten generations a second while focused, space pauses, r reseeds, hjkl and x edit, the prefix the generation and alive count, the body the board, dump and load the board), with its script and golden under addon-test and its own zig tests; the module's timing test and the dogfood that found Kind.step's stale tick clock
Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
Diffstat (limited to 'addons/life/life.zig')
-rw-r--r--addons/life/life.zig127
1 files changed, 127 insertions, 0 deletions
diff --git a/addons/life/life.zig b/addons/life/life.zig
new file mode 100644
index 00000000..5ea9c302
--- /dev/null
+++ b/addons/life/life.zig
@@ -0,0 +1,127 @@
+//! Conway's Game of Life in a pane: ten generations a second while the
+//! pane has the keyboard. Space pauses, r reseeds, hjkl move the cursor,
+//! x toggles the cell under it.
+const std = @import("std");
+const addon = @import("addon");
+
+export const pardes_addon = addon.ops(Life);
+
+const W = 32;
+const H = 16;
+const period = std.time.ns_per_s / 10;
+
+const Life = extern struct {
+ cells: [H][W]bool,
+ /// The pane's, kept: r after a restore reseeds as before it.
+ seed: u64,
+ gen: u32,
+ cx: u8,
+ cy: u8,
+ paused: bool,
+ next: addon.Deadline,
+
+ pub const head: addon.Head = .{ .name = "life", .version = 1, .words = &.{.{ .name = "Life", .opens = true, .doc = "a pane of Conway's Game of Life" }} };
+
+ pub fn open(s: *Life, _: ?[]const u8) void {
+ s.seed = addon.seed();
+ s.reseed();
+ }
+
+ pub fn tick(s: *Life, now: u64, flags: addon.Flags) u64 {
+ if (!flags.focused or s.paused) return s.next.stop();
+ // A record's deadline is of another clock's origin.
+ if (s.next.at > now + period) s.next.at = 0;
+ if (!s.next.due(now)) return s.next.every(now, period);
+ s.step();
+ // From the step's due time, not now: a tick comes on a frame, late.
+ s.next.at = if (now - s.next.at < period) s.next.at + period else now + period;
+ return s.next.at;
+ }
+
+ pub fn key(s: *Life, k: addon.Key) bool {
+ switch (k.code) {
+ addon.char(' ') => s.paused = !s.paused,
+ addon.char('r') => s.reseed(),
+ addon.char('h') => s.cx = (s.cx + W - 1) % W,
+ addon.char('l') => s.cx = (s.cx + 1) % W,
+ addon.char('k') => s.cy = (s.cy + H - 1) % H,
+ addon.char('j') => s.cy = (s.cy + 1) % H,
+ addon.char('x') => s.cells[s.cy][s.cx] = !s.cells[s.cy][s.cx],
+ else => return false,
+ }
+ return true;
+ }
+
+ pub fn paint(s: *Life, w: addon.Window, p: addon.Paint) void {
+ p.prefix("gen {d} alive {d}{s}", .{ s.gen, s.alive(), if (s.paused) " paused" else "" });
+ for (s.cells, 0..) |row, y| for (row, 0..) |on, x| {
+ const cursor = w.focused and x == s.cx and y == s.cy;
+ w.put(x, y, if (on) "#" else ".", .{ .attrs = .{ .reverse = cursor } });
+ };
+ }
+
+ pub fn body(s: *Life, out: *std.Io.Writer) !void {
+ for (s.cells) |row| {
+ for (row) |on| try out.writeByte(if (on) '#' else '.');
+ try out.writeByte('\n');
+ }
+ }
+
+ pub const dump = addon.dumpBytes;
+ pub const load = addon.loadBytes;
+
+ fn reseed(s: *Life) void {
+ var prng: std.Random.DefaultPrng = .init(s.seed);
+ const r = prng.random();
+ for (&s.cells) |*row| for (row) |*on| {
+ on.* = r.uintLessThan(u8, 4) == 0;
+ };
+ s.gen = 0;
+ }
+
+ fn alive(s: *const Life) u32 {
+ var n: u32 = 0;
+ for (s.cells) |row| for (row) |on| {
+ n += @intFromBool(on);
+ };
+ return n;
+ }
+
+ fn step(s: *Life) void {
+ var next: [H][W]bool = undefined;
+ for (0..H) |y| for (0..W) |x| {
+ var n: u8 = 0;
+ for ([_]usize{ H - 1, 0, 1 }) |dy| for ([_]usize{ W - 1, 0, 1 }) |dx| {
+ if (dy == 0 and dx == 0) continue;
+ n += @intFromBool(s.cells[(y + dy) % H][(x + dx) % W]);
+ };
+ next[y][x] = n == 3 or (n == 2 and s.cells[y][x]);
+ };
+ s.cells = next;
+ s.gen += 1;
+ }
+};
+
+test "a blinker blinks, wrapping at the edges" {
+ var s = std.mem.zeroes(Life);
+ for ([_]usize{ W - 1, 0, 1 }) |x| s.cells[0][x] = true;
+ s.step();
+ try std.testing.expectEqual(3, s.alive());
+ for ([_]usize{ H - 1, 0, 1 }) |y| try std.testing.expect(s.cells[y][0]);
+ s.step();
+ try std.testing.expect(s.cells[0][W - 1] and s.cells[0][1] and !s.cells[1][0]);
+ try std.testing.expectEqual(2, s.gen);
+}
+
+test "a record loads back into a fresh pane" {
+ var a = std.mem.zeroes(Life);
+ a.seed = 42;
+ a.reseed();
+ a.step();
+ var buf: [@sizeOf(Life)]u8 = undefined;
+ try std.testing.expectEqual(buf.len, Life.dump(&a, &buf));
+ var b = std.mem.zeroes(Life);
+ try std.testing.expect(Life.load(&b, &buf, Life.head.version));
+ try std.testing.expectEqualDeep(a.cells, b.cells);
+ try std.testing.expect(!Life.load(&b, &buf, 2));
+}