diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-06 02:31:55 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-06 04:04:25 -0300 |
| commit | 1b87e20543ca4577b855b3abf705561e9b45add8 (patch) | |
| tree | 3ac89cb20ce76090639c08eaacbf454fcf733e50 | |
| parent | 9c66cdc9049b49f128336dcf5046e2bb1321e281 (diff) | |
| download | pardes-1b87e20543ca4577b855b3abf705561e9b45add8.tar.gz pardes-1b87e20543ca4577b855b3abf705561e9b45add8.zip | |
kinds #6, the snake: addons/snake/snake.c (153 lines) against ABI 2 steps only while its pane has the keyboard and asks for nothing otherwise (19 lines of boilerplate, 13 by §16.7's measure: the head, word and ops literal, the api kept, an alloc check and two of prefix; the body pair and open's unused arguments are the rest), built by the build with -Wall -Wextra -Werror and not installed, and addon-test runs it against addons/snake/snake.out: its moves, h j k l turns and no turning back, food, the ctl read and body, blur with settle asking for no ticks and the snake still, focus resuming, a dump into a fresh pane, death on the wall and a restart
Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
| -rw-r--r-- | addons/snake/snake.c | 153 | ||||
| -rw-r--r-- | addons/snake/snake.out | 386 | ||||
| -rw-r--r-- | addons/snake/snake.script | 37 | ||||
| -rw-r--r-- | build.zig | 8 |
4 files changed, 582 insertions, 2 deletions
diff --git a/addons/snake/snake.c b/addons/snake/snake.c new file mode 100644 index 00000000..e6ad96e4 --- /dev/null +++ b/addons/snake/snake.c @@ -0,0 +1,153 @@ +/* snake.c: a snake game as a pardes addon. + * cc -shared -fPIC -O2 -Wall -Wextra -Werror -I src/kinds -o snake.so addons/snake/snake.c + * h j k l steer; once dead, any key starts again. It steps only while its + * pane has the keyboard, and asks for nothing otherwise. */ +#include <stdio.h> +#include "pardes_addon.h" + +#define W 20 /* the board, in cells, inside its wall */ +#define H 5 +#define PERIOD 150000000ull /* ns a step */ +#define VERSION 1 + +typedef struct { uint8_t x, y; } pt; + +typedef struct snake { + pt body[W * H]; /* body[0] is the head */ + uint16_t len; + pt food; + int8_t dx, dy; /* the direction last stepped */ + int8_t ndx, ndy; /* the direction the next step takes */ + uint8_t dead, pad[3]; + uint32_t score, steps; + uint64_t rng, next; /* next: the deadline of the next step */ +} snake; + +static const pa_api *api; + +static uint64_t rnd(snake *s) { + s->rng ^= s->rng << 13, s->rng ^= s->rng >> 7, s->rng ^= s->rng << 17; + return s->rng; +} + +/* Is (x, y) one of the snake's first n segments? */ +static int on_snake(const snake *s, int x, int y, unsigned n) { + for (unsigned i = 0; i < n; i++) + if (s->body[i].x == x && s->body[i].y == y) return 1; + return 0; +} + +static void place_food(snake *s) { + unsigned free_cells = W * H - s->len; + if (!free_cells) { s->dead = 1; return; } /* the board is full: a win, and over */ + unsigned k = (unsigned)(rnd(s) % free_cells); + for (int y = 0; y < H; y++) + for (int x = 0; x < W; x++) + if (!on_snake(s, x, y, s->len) && k-- == 0) { s->food = (pt){(uint8_t)x, (uint8_t)y}; return; } +} + +static void restart(snake *s) { + s->len = 3; + for (unsigned i = 0; i < 3; i++) s->body[i] = (pt){(uint8_t)(W / 2 - i), H / 2}; + s->dx = s->ndx = 1, s->dy = s->ndy = 0; + s->dead = 0, s->score = 0, s->steps = 0; + place_food(s); +} + +static void step(snake *s) { + s->dx = s->ndx, s->dy = s->ndy; + int x = s->body[0].x + s->dx, y = s->body[0].y + s->dy; + int eats = x == s->food.x && y == s->food.y; + /* the tail moves away this step unless the snake grows */ + if (x < 0 || y < 0 || x >= W || y >= H || on_snake(s, x, y, eats ? s->len : s->len - 1u)) { + s->dead = 1; + return; + } + if (eats) s->len++; + memmove(&s->body[1], &s->body[0], (s->len - 1) * sizeof(pt)); + s->body[0] = (pt){(uint8_t)x, (uint8_t)y}; + s->steps++; + if (eats) s->score++, place_food(s); +} + +/* ---- the contract ---- */ + +static int init(const pa_api *a) { api = a; return PA_OK; } + +static void *open(const char *path, const uint8_t *bytes, uint64_t len) { + (void)path, (void)bytes, (void)len; + snake *s = api->alloc(sizeof *s); + if (!s) return 0; + s->rng = api->seed() | 1; + restart(s); + return s; +} + +/* Unfocused or over, it stands still and asks for nothing: a key, or the + * keyboard coming back, ticks it again. */ +static uint64_t tick(void *st, uint64_t now, uint32_t flags) { + snake *s = st; + if (!(flags & PA_FOCUSED) || s->dead) return s->next = 0; + if (s->next && now >= s->next) step(s); + if (s->dead) return s->next = 0; + if (!s->next || now >= s->next) s->next = now + PERIOD; + return s->next; +} + +static void paint(void *st, const pa_window *w, pa_paint *out) { + const snake *s = st; + for (unsigned x = 0; x < W + 2; x++) pa_put(w, 0, x, "#", PA_INDEX(8), PA_DEFAULT, 0), pa_put(w, H + 1, x, "#", PA_INDEX(8), PA_DEFAULT, 0); + for (unsigned y = 1; y <= H; y++) pa_put(w, y, 0, "#", PA_INDEX(8), PA_DEFAULT, 0), pa_put(w, y, W + 1, "#", PA_INDEX(8), PA_DEFAULT, 0); + pa_put(w, 1 + s->food.y, 1 + s->food.x, "*", PA_INDEX(1), PA_DEFAULT, PA_BOLD); + for (unsigned i = s->len; i-- > 0;) + pa_put(w, 1 + s->body[i].y, 1 + s->body[i].x, i ? "o" : s->dead ? "x" : "@", PA_INDEX(2), PA_DEFAULT, i ? 0 : PA_BOLD); + if (s->dead) pa_put(w, H + 2, 0, "game over: any key", PA_INDEX(1), PA_DEFAULT, 0); + else if (!w->focused) pa_put(w, H + 2, 0, "paused", PA_INDEX(8), PA_DEFAULT, 0); + int n = snprintf(out->prefix, sizeof out->prefix, "Snake %u%s", s->score, s->dead ? " over" : ""); + out->prefix_len = n < 0 ? 0 : (uint32_t)n; +} + +static int key(void *st, const pa_key *k) { + snake *s = st; + if (s->dead) { restart(s); return 1; } + int dx = 0, dy = 0; + switch (k->cp) { + case 'h': case PA_KEY_LEFT: dx = -1; break; + case 'l': case PA_KEY_RIGHT: dx = 1; break; + case 'k': case PA_KEY_UP: dy = -1; break; + case 'j': case PA_KEY_DOWN: dy = 1; break; + case 'r': restart(s); return 1; + default: return 0; + } + if (dx != -s->dx || dy != -s->dy) s->ndx = (int8_t)dx, s->ndy = (int8_t)dy; /* no turning back */ + return 1; +} + +static uint32_t text(const snake *s, char *buf, uint32_t n) { + int r = snprintf(buf, n, "score %u\nlength %u\nsteps %u\n%s\n", s->score, s->len, s->steps, s->dead ? "over" : "alive"); + return r < 0 ? 0 : (uint32_t)r < n ? (uint32_t)r : n; +} + +static uint64_t body_len(void *st) { char b[96]; return text(st, b, sizeof b); } + +static uint32_t body(void *st, uint64_t off, char *buf, uint32_t n) { + char b[96]; + return pa_copy(b, text(st, b, sizeof b), off, buf, n); +} + +static uint32_t ctl_read(void *st, char *buf, uint32_t n) { + const snake *s = st; + int r = snprintf(buf, n, "score %u\nlength %u\n", s->score, s->len); + return r < 0 ? 0 : (uint32_t)r < n ? (uint32_t)r : n; +} + +PA_POD(snake, VERSION) + +static const pa_word words[] = { PA_WORD("Snake", 0, PA_ARG_NONE, 1, "a game of snake: h j k l steer") }; + +const pa_ops pardes_addon = { + .head = PA_ADDON("snake", VERSION, "", words, 1), + .init = init, .open = open, .tick = tick, .paint = paint, .key = key, + .body_len = body_len, .body = body, .ctl_read = ctl_read, + .dump = snake_dump, .load = snake_load, +}; diff --git a/addons/snake/snake.out b/addons/snake/snake.out new file mode 100644 index 00000000..e53e72e0 --- /dev/null +++ b/addons/snake/snake.out @@ -0,0 +1,386 @@ +> seed 3 +> size 40 30 +window 38x13 +tag Snake 0 +|###################### +|# # +|# * # +|# oo@ # +|# # +|# # +|###################### +| +| +| +| +| +| +> tick 150 +tag Snake 0 +|###################### +|# # +|# * # +|# oo@ # +|# # +|# # +|###################### +| +| +| +| +| +| +> tick 150 +tag Snake 0 +|###################### +|# # +|# * # +|# oo@ # +|# # +|# # +|###################### +| +| +| +| +| +| +> key k +tag Snake 0 +|###################### +|# # +|# * # +|# oo@ # +|# # +|# # +|###################### +| +| +| +| +| +| +> tick 150 +tag Snake 0 +|###################### +|# # +|# @ * # +|# oo # +|# # +|# # +|###################### +| +| +| +| +| +| +> key l +tag Snake 0 +|###################### +|# # +|# @ * # +|# oo # +|# # +|# # +|###################### +| +| +| +| +| +| +> tick 150 +tag Snake 0 +|###################### +|# # +|# o@* # +|# o # +|# # +|# # +|###################### +| +| +| +| +| +| +> tick 150 +tag Snake 1 +|###################### +|# # +|# oo@ # +|# o # +|# * # +|# # +|###################### +| +| +| +| +| +| +> key h +tag Snake 1 +|###################### +|# # +|# oo@ # +|# o # +|# * # +|# # +|###################### +| +| +| +| +| +| +> tick 150 +tag Snake 1 +|###################### +|# # +|# ooo@ # +|# # +|# * # +|# # +|###################### +| +| +| +| +| +| +> key j +tag Snake 1 +|###################### +|# # +|# ooo@ # +|# # +|# * # +|# # +|###################### +| +| +| +| +| +| +> tick 150 +tag Snake 1 +|###################### +|# # +|# ooo # +|# @ # +|# * # +|# # +|###################### +| +| +| +| +| +| +> ctl +=score 1 +=length 4 +> body +=score 1 +=length 4 +=steps 6 +=alive +> blur +tag Snake 1 +|###################### +|# # +|# ooo # +|# @ # +|# * # +|# # +|###################### +|paused +| +| +| +| +| +> settle +tag Snake 1 +|###################### +|# # +|# ooo # +|# @ # +|# * # +|# # +|###################### +|paused +| +| +| +| +| +> tick 600 +tag Snake 1 +|###################### +|# # +|# ooo # +|# @ # +|# * # +|# # +|###################### +|paused +| +| +| +| +| +> focus +tag Snake 1 +|###################### +|# # +|# ooo # +|# @ # +|# * # +|# # +|###################### +| +| +| +| +| +| +> tick 150 +tag Snake 1 +|###################### +|# # +|# ooo # +|# @ # +|# * # +|# # +|###################### +| +| +| +| +| +| +> tick 150 +tag Snake 1 +|###################### +|# # +|# oo # +|# o # +|# * @ # +|# # +|###################### +| +| +| +| +| +| +> dump +dump 240 bytes +tag Snake 1 +|###################### +|# # +|# oo # +|# o # +|# * @ # +|# # +|###################### +| +| +| +| +| +| +> tick 150 +tag Snake 1 +|###################### +|# # +|# o # +|# o # +|# * o # +|# @ # +|###################### +| +| +| +| +| +| +> tick 150 +tag Snake 1 over +|###################### +|# # +|# o # +|# o # +|# * o # +|# x # +|###################### +|game over: any key +| +| +| +| +| +> tick 150 +tag Snake 1 over +|###################### +|# # +|# o # +|# o # +|# * o # +|# x # +|###################### +|game over: any key +| +| +| +| +| +> settle +tag Snake 1 over +|###################### +|# # +|# o # +|# o # +|# * o # +|# x # +|###################### +|game over: any key +| +| +| +| +| +> key x +tag Snake 0 +|###################### +|# # +|# # +|# oo@ # +|# * # +|# # +|###################### +| +| +| +| +| +| +> tick 150 +tag Snake 0 +|###################### +|# # +|# # +|# oo@ # +|# * # +|# # +|###################### +| +| +| +| +| +| diff --git a/addons/snake/snake.script b/addons/snake/snake.script new file mode 100644 index 00000000..2cf07a51 --- /dev/null +++ b/addons/snake/snake.script @@ -0,0 +1,37 @@ +# pardes --addon-test snake.so addons/snake/snake.script | diff addons/snake/snake.out - +seed 3 +size 40 30 +# It steps a cell each 150 ms while its pane has the keyboard (the first +# tick sets the pace). +tick 150 +tick 150 +# k turns it up, l right: onto the food, one longer and a point. +key k +tick 150 +key l +tick 150 +tick 150 +# h is back on itself: refused, it goes on right; then j down. +key h +tick 150 +key j +tick 150 +ctl +body +# The window loses the keyboard: it stands still and asks for no ticks. +blur +settle +tick 600 +# Back, it goes on, the first tick setting its pace again. +focus +tick 150 +tick 150 +# Its record is its state: the pane comes back from it as it was. +dump +tick 150 +# Into the bottom wall: over, asking for nothing until a key starts it again. +tick 150 +tick 150 +settle +key x +tick 150 @@ -1561,9 +1561,13 @@ pub fn build(b: *std.Build) void { const behaviour_test_step = b.step("behaviour-test", "run the behavioural tests, through the 9P tree alone (test/behaviour/)"); _ = units.add(b, behaviour_tests, behaviour_test_step); // An addon's own test: `--addon-test` over the fake (the template) + // and the snake (a shipped example, built but not installed), each // with its script, compared with what it printed when written. - const addon_test_step = b.step("addon-test", "pardes --addon-test over the fake addon, against addons/fake/fake.out"); - for ([_]struct { *std.Build.Step.Compile, []const u8 }{.{ fake_addon_lib.?, "addons/fake/fake" }}) |t| { + const addon_test_step = b.step("addon-test", "pardes --addon-test over the fake and snake addons, against addons/*/*.out"); + const snake_lib = b.addLibrary(.{ .name = "snake", .linkage = .dynamic, .root_module = b.createModule(.{ .target = target, .optimize = optimize, .link_libc = true }) }); + snake_lib.root_module.addIncludePath(b.path("src/kinds")); + snake_lib.root_module.addCSourceFile(.{ .file = b.path("addons/snake/snake.c"), .flags = &.{ "-std=c11", "-Wall", "-Wextra", "-Werror" } }); + for ([_]struct { *std.Build.Step.Compile, []const u8 }{ .{ fake_addon_lib.?, "addons/fake/fake" }, .{ snake_lib, "addons/snake/snake" } }) |t| { const run_addon_test = b.addRunArtifact(exe); run_addon_test.addArg("--addon-test"); run_addon_test.addArtifactArg(t[0]); |
