Public: anyone can read this; the crew can change it.

Migrated from github.com/amirmushichge/pixel-plays

Add Patchwork Quilt: the playable page and its place in the catalogue, on the rules in `quilt/patch.js` #245

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

quill/pixel-plays:quill/218-add-patchwork-quilt-the-playable-page-an → main 1 commit fork

  • Merged. quill/218-add-patchwork-quilt-the-playable-page-an was merged into main as 275447d.

Review

Sign in to review this change.
9 files changed +467 −2
README.md +3 −2

Diff not shown — a line 2,148 characters wide (limit 2,000). +3 −2

View file
index.html +1
@@ -121,6 +121,7 @@ footer a::after { content: none; }
121121 <li data-kind="Puzzle" data-input="Keys" style="--accent:#3d6e4f"><a href="hedge-maze/">Hedge Maze</a><span>Find the way through the hedges to the fountain before the gardener rings the bell</span><span>Game forged by the crew</span><span class="tags">Puzzle · Keys</span></li>
122122 <li data-kind="Memory" data-input="Tap" style="--accent:#6b8f3a"><a href="bird-watch/">Bird Watch</a><span>Remember where each bird perched</span><span>Game forged by the crew</span><span class="tags">Memory · Tap</span></li>
123123 <li data-kind="Strategy" data-input="Tap" style="--accent:#3d7f6e"><a href="bunting/">Bunting</a><span>Hang the flags for the fete so no two neighbours match</span><span>Game forged by the crew</span><span class="tags">Strategy · Tap</span></li>
124 <li data-kind="Puzzle" data-input="Tap" style="--accent:#b5656b"><a href="quilt/">Patchwork Quilt</a><span>Turn and swap the patches until every stitch lines up</span><span>Game forged by the crew</span><span class="tags">Puzzle · Tap</span></li>
124125 </ul>
125126 <footer>Concept by <a href="https://x.com/awwbhi2">abhi</a>. Game by <a href="https://x.com/AmirMushich">AmirMushich</a>. Visual reference for the style: shawn.'s <a href="https://x.com/zzzzshawn/status/2111546123546661289">Small plays</a>; no assets or subjects were copied.<br><a href="stats/">Most played</a></footer>
126127 </main>
og/home.png
Binary file not shown.
og/quilt.png added
Binary file not shown.
quilt/index.html added +372
@@ -0,0 +1,372 @@
1 <!doctype html>
2 <html lang="en">
3 <head>
4 <meta charset="utf-8">
5 <meta name="viewport" content="width=device-width,initial-scale=1,viewport-fit=cover">
6 <title>Patchwork Quilt</title>
7 <!-- og:begin -->
8 <meta name="description" content="Turn and swap the patches until every stitch lines up">
9 <link rel="canonical" href="https://pixel-plays--forge-trevato-apps.open-platform.sh/quilt/">
10 <meta property="og:type" content="website">
11 <meta property="og:site_name" content="Pixel Plays">
12 <meta property="og:title" content="Patchwork Quilt">
13 <meta property="og:description" content="Turn and swap the patches until every stitch lines up">
14 <meta property="og:url" content="https://pixel-plays--forge-trevato-apps.open-platform.sh/quilt/">
15 <meta property="og:image" content="https://pixel-plays--forge-trevato-apps.open-platform.sh/og/quilt.png">
16 <meta property="og:image:type" content="image/png">
17 <meta property="og:image:width" content="1200">
18 <meta property="og:image:height" content="630">
19 <meta property="og:image:alt" content="Patchwork Quilt: the opening scene of the game">
20 <meta name="twitter:card" content="summary_large_image">
21 <!-- og:end -->
22 <style>
23 :root { --ink: #1c1b19; --muted: #6b675f; --paper: #fff; --accent: #b5656b; }
24 * { box-sizing: border-box; }
25 html, body { -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; -webkit-tap-highlight-color: transparent; }
26 html, body { height: 100%; margin: 0; background: var(--paper); color: var(--ink); }
27 body { font: 13px/1.35 ui-monospace, "Cascadia Mono", Consolas, Menlo, monospace; overflow: hidden; }
28 #stage { position: fixed; inset: 0; display: grid; place-items: center; }
29 canvas { display: block; image-rendering: pixelated; touch-action: none; cursor: pointer; outline: none; }
30 .hud { position: fixed; left: max(16px, env(safe-area-inset-left)); right: max(16px, env(safe-area-inset-right)); display: flex; justify-content: space-between; align-items: flex-end; gap: 16px; pointer-events: none; }
31 #scores { position: fixed; top: max(14px, env(safe-area-inset-top)); left: max(16px, env(safe-area-inset-left)); display: grid; gap: 2px; pointer-events: none; }
32 #bottom { bottom: max(20px, env(safe-area-inset-bottom)); }
33 .row { display: flex; align-items: center; gap: 8px; color: var(--accent); }
34 .row b { font-size: 16px; font-weight: 600; min-width: 1ch; font-variant-numeric: tabular-nums; }
35 .row span { color: var(--muted); }
36 #status { color: var(--muted); min-height: 1.35em; }
37 button { pointer-events: auto; font: inherit; color: var(--muted); background: none; border: 0; padding: 13px 10px; margin: -13px -10px; min-height: 44px; cursor: pointer; }
38 button:hover, button:focus-visible { color: var(--ink); }
39 button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
40 noscript p { position: fixed; inset: 40% 16px auto; margin: 0; text-align: center; color: var(--muted); }
41 </style>
42 </head>
43 <body>
44 <div id="stage"><canvas id="c" tabindex="0" aria-label="A patchwork quilt of nine square patches laid on a cottage bed, each edge stitched in one of four patterns, with a curtain at the window. Tap a patch to pick it, tap it again to turn it, tap a second patch to swap the two. Cross-stitches mark an edge that matches and a gap marks one that does not. Arrow keys move, Space picks or turns, Escape lets go. Match every edge in 24 moves."></canvas></div>
45 <noscript><p>Patchwork Quilt needs JavaScript to play.</p></noscript>
46 <div id="scores">
47 <div class="row"><b id="moves">24</b><span>moves left</span></div>
48 <div class="row"><b id="edges">0</b><span id="of">of 12 edges match</span></div>
49 <div class="row"><b id="score">0</b><span>points</span></div>
50 </div>
51 <div class="hud" id="bottom">
52 <div id="status" role="status" aria-live="polite"></div>
53 <button id="mute" type="button" aria-pressed="false">sound on</button>
54 </div>
55 <script src="../shell.js"></script>
56 <script>
57 PixelPlays.mount({
58 id: 'quilt',
59 rules: [
60 'Every patch has four stitched edges; a cross-stitch means two edges match, a gap means they do not.',
61 'Tap a patch to pick it and again to turn it; with one picked, tap another to swap.',
62 'Match every edge within 24 moves; arrows, Space and Escape work too.',
63 ],
64 });
65 </script>
66 <script>window.PP_SCENE = { originY: 8, shadowX: 1, shadowY: -.12 };</script>
67 <script src="../engine.js"></script>
68 <script src="patch.js"></script>
69 <script>
70 'use strict';
71
72 /* ---------- the room: a bed, the quilt laid on it, a window with a curtain ---------- */
73 const ZQ = 1.7, QX0 = -6.3, QY0 = -6.3, PATCH = 4.2, SIZE = Quilt.SIZE;
74 const WOOD = [150, 112, 76], WOOD_DARK = [96, 70, 50], MATTRESS = [232, 224, 204], PILLOW = [246, 242, 232];
75 const WALL = [226, 214, 190], SKY = [196, 224, 244], CURTAIN = [[204, 120, 128], [184, 100, 112]];
76 const FLOOR = [[176, 138, 100], [168, 130, 94]];
77 const FABRICS = [[181, 101, 107], [216, 164, 92], [96, 134, 138], [134, 150, 96], [120, 104, 150], [206, 120, 80]];
78 const INK_PIX = rgb(28, 27, 25), WHITE_PIX = rgb(255, 255, 255), SIGNAL_PIX = rgb(255, 214, 10);
79 const THREAD_PIX = rgb(248, 240, 222), GAP_PIX = rgb(22, 18, 30), CROSS_PIX = rgb(255, 214, 10);
80 const FABRIC_RAMP = FABRICS.map(rampOf);
81
82 function floorColor(x, y, ix, iy) {
83 const edge = Math.pow(Math.abs(x) / 11.2, 6) + Math.pow(Math.abs(y + .4) / 10.2, 6);
84 const rough = Math.pow(edge, 1 / 6) + (noise(x * 1.3, y * 1.3) - .5) * .06;
85 if (rough > 1) return 0;
86 if (rough > .95 && hash(ix, iy) < (rough - .95) / .05) return 0;
87 const t = .97 + (hash(ix, iy) - .5) * .06;
88 const plank = Math.floor((x + 11.2) / 1.4), seam = (x + 11.2) / 1.4 - plank < .05;
89 const c = FLOOR[plank & 1];
90 const k = seam ? .78 : t;
91 return rgb(c[0] * k, c[1] * k, c[2] * k);
92 }
93 const shade = (x, y, z, r) => castShadow(x, y, z, r, 1, 0);
94 function bakeBed() {
95 box(-7.3, -8.5, 0, 7.3, 7.3, 1.1, WOOD, 4);
96 box(-6.9, -8.1, 1.1, 6.9, 6.9, ZQ, MATTRESS, 6);
97 box(-7.3, -9.1, 0, 7.3, -8.5, 4.6, WOOD_DARK, 9);
98 for (const x of [-4.8, -1.6, 1.6, 4.8]) capsule([x, -8.8, 4.6], [x, -8.8, 5], .22, .18, () => WOOD_DARK, 'rough');
99 for (const [a, b] of [[[-5, -7.2], [-1.8, -7.2]], [[1.8, -7.2], [5, -7.2]]]) {
100 capsule([a[0], a[1], ZQ + .6], [b[0], b[1], ZQ + .6], .75, .75, () => PILLOW, 'rough');
101 for (let x = a[0]; x <= b[0]; x += .8) shade(x, a[1], ZQ + 1.2, .5);
102 }
103 for (let x = -7.3; x <= 7.3; x += .7) { shade(x, -9, 4.6, .4); shade(x, 7.3, ZQ, .45); }
104 for (let y = -8.5; y <= 7.3; y += .7) shade(7.3, y, ZQ, .45);
105 }
106 function bakeWindow() {
107 box(-9.8, -4.5, 0, -9.5, 4.5, 6.4, WALL, 3);
108 box(-9.5, -2.1, 2.2, -9.4, 2.1, 5.5, SKY, 5);
109 const bar = (y0, y1, z0, z1) => box(-9.5, y0, z0, -9.3, y1, z1, WOOD, 7);
110 bar(-2.35, 2.35, 5.5, 5.7); bar(-2.45, 2.45, 2, 2.2); bar(-2.35, -2.1, 2.2, 5.5); bar(2.1, 2.35, 2.2, 5.5); bar(-.06, .06, 2.2, 5.5);
111 capsule([-9.1, -3.3, 5.9], [-9.1, 3.3, 5.9], .07, .07, () => WOOD_DARK, 'solid');
112 for (let y = -4.5; y <= 4.5; y += .7) shade(-9.6, y, 6.4, .4);
113 for (let z = 0; z <= 6.4; z += .8) { shade(-9.6, -4.5, z, .4); shade(-9.6, 4.5, z, .4); }
114 }
115 function bake() {
116 ground.fill(0); shadowMask.fill(0); staticShadow.fill(0); kinds.fill(0);
117 tufts.length = 0; leaves.length = 0;
118 zb.fill(-1e9);
119 pix.fill(WHITE_PIX);
120 for (let iy = 0; iy < H; iy++) for (let ix = 0; ix < W; ix++) {
121 const g = unproject(ix + .5, iy + .5), i = iy * W + ix, c = floorColor(g.x, g.y, ix, iy);
122 if (!c) continue;
123 pix[i] = c; ground[i] = 1;
124 zb[i] = (g.x + g.y) / R2 * COS;
125 }
126 groundZ.set(zb);
127 bakeWindow(); bakeBed();
128 bakeOcclusion();
129 staticShadow.set(shadowMask);
130 applyShadows(groundZ);
131 bakedPix.set(pix);
132 bakedZ.set(zb);
133 }
134
135 /* ---------- the curtain: the only thing that moves by itself ---------- */
136 const PANELS = [[-3, -.5], [.5, 3]];
137 function drawCurtain(t) {
138 let strip = 0;
139 for (const [y0, y1] of PANELS) for (let y = y0 + .25; y < y1; y += .5, strip++) {
140 const colour = CURTAIN[strip & 1];
141 const at = j => {
142 const k = j / 4, sway = (Math.sin(t * .9 + strip * .6) + .5 * Math.sin(t * 2.1 + strip)) * .2 * Math.pow(k, 1.3);
143 return [-9 + sway, y + sway * .3, 5.8 - j * 1.075];
144 };
145 for (let j = 0; j < 4; j++) dynamicAdd(at(j), at(j + 1), .27, .27, () => colour);
146 }
147 }
148
149 /* ---------- state ---------- */
150 const nodes = {};
151 for (const k of ['moves', 'edges', 'of', 'score', 'status']) nodes[k] = document.getElementById(k);
152 const shown = {};
153 function setText(key, text) {
154 if (shown[key] === text) return;
155 shown[key] = text;
156 nodes[key].textContent = text;
157 }
158 const fresh = () => Quilt.start(Math.floor(Math.random() * 4294967296));
159 let Q = fresh();
160 const G = { time: 0, selected: null, cursor: 4, keyed: false, event: '' };
161 const MIDDLE = 'Tap a patch to pick it, tap it again to turn it, or tap another to swap';
162 const playing = () => Q.status === 'playing';
163
164 function renderHud() {
165 setText('moves', String(Q.limit - Q.moves));
166 setText('edges', String(Q.matching));
167 setText('of', `of ${Q.edges} edges match`);
168 setText('score', String(Q.score));
169 }
170 function say(text) { setText('status', Q.status === 'over' ? `${Q.line} · tap or press a key to play again` : text); }
171
172 /* ---------- geometry ---------- */
173 const cellOrigin = i => [QX0 + (i % SIZE) * PATCH, QY0 + Math.floor(i / SIZE) * PATCH];
174 // the canvas pixel at the middle of a patch
175 function cellPoint(i) {
176 const [x, y] = cellOrigin(i), p = project(x + PATCH / 2, y + PATCH / 2, ZQ);
177 return [p.sx, p.sy];
178 }
179 // the patch under a canvas point, or null off the quilt
180 function cellAt(px, py) {
181 const g = unproject(px, py + ZQ * COS * S);
182 const c = Math.floor((g.x - QX0) / PATCH), r = Math.floor((g.y - QY0) / PATCH);
183 return c < 0 || r < 0 || c >= SIZE || r >= SIZE ? null : r * SIZE + c;
184 }
185
186 /* ---------- drawing the quilt ---------- */
187 function put(ix, iy, color) {
188 if (ix >= 0 && iy >= 0 && ix < W && iy < H) pix[iy * W + ix] = color;
189 }
190 function dot(x, y, color) { const p = project(x, y, ZQ); put(p.sx | 0, p.sy | 0, color); }
191 function line(ax, ay, bx, by, color) {
192 const n = Math.max(1, Math.ceil(Math.hypot(bx - ax, by - ay) / .05));
193 for (let k = 0; k <= n; k++) dot(ax + (bx - ax) * k / n, ay + (by - ay) * k / n, color);
194 }
195 function block(x, y, color) { const p = project(x, y, ZQ), ix = p.sx | 0, iy = p.sy | 0; put(ix, iy, color); put(ix + 1, iy, color); put(ix, iy + 1, color); put(ix + 1, iy + 1, color); }
196 // a patch's fabric follows its stitches whichever way it faces, so turning it never recolours it
197 const fabricOf = patch => {
198 const key = [...patch].sort().join('');
199 let h = 0;
200 for (const ch of key) h = (h * 31 + ch.charCodeAt(0)) | 0;
201 return FABRIC_RAMP[Math.abs(h) % FABRIC_RAMP.length];
202 };
203 function fillPatch(i, patch) {
204 const [x0, y0] = cellOrigin(i), ramp = fabricOf(patch);
205 const corners = [[x0, y0], [x0 + PATCH, y0], [x0, y0 + PATCH], [x0 + PATCH, y0 + PATCH]].map(([x, y]) => project(x, y, ZQ));
206 const sx0 = Math.floor(Math.min(...corners.map(p => p.sx))), sx1 = Math.ceil(Math.max(...corners.map(p => p.sx)));
207 const sy0 = Math.floor(Math.min(...corners.map(p => p.sy))), sy1 = Math.ceil(Math.max(...corners.map(p => p.sy)));
208 for (let iy = sy0; iy <= sy1; iy++) for (let ix = sx0; ix <= sx1; ix++) {
209 const g = unproject(ix + .5, iy + .5 + ZQ * COS * S), lx = (g.x - x0) / PATCH, ly = (g.y - y0) / PATCH;
210 if (lx < 0 || ly < 0 || lx >= 1 || ly >= 1) continue;
211 const m = Math.min(lx, 1 - lx, ly, 1 - ly) * PATCH;
212 put(ix, iy, m < .12 ? ramp[0] : m < .8 ? ramp[2] : ((ix + iy) & 1 && bayerAt(ix, iy) < .3 ? ramp[4] : ramp[3]));
213 }
214 }
215 // each stitch pattern as strokes in (along the edge 0..1, depth into the patch 0..1); a stroke of no length is a dot
216 const STITCHES = [
217 [[.08, .5, .3, .5], [.4, .5, .6, .5], [.7, .5, .92, .5]],
218 [[.16, .5, .16, .5], [.38, .5, .38, .5], [.6, .5, .6, .5], [.82, .5, .82, .5]],
219 [[.1, .2, .3, .8], [.3, .8, .5, .2], [.5, .2, .7, .8], [.7, .8, .9, .2]],
220 [[.2, .15, .2, .85], [.4, .15, .4, .85], [.6, .15, .6, .85], [.8, .15, .8, .85]],
221 ];
222 // world point for (along, depth) on side k (0 top, 1 right, 2 bottom, 3 left) of the patch at (x0, y0); along always runs the way x or y grows
223 function onSide(x0, y0, k, t, d) {
224 const a = t * PATCH, b = .12 + d * .58;
225 return k === 0 ? [x0 + a, y0 + b] : k === 1 ? [x0 + PATCH - b, y0 + a] : k === 2 ? [x0 + a, y0 + PATCH - b] : [x0 + b, y0 + a];
226 }
227 function stitchSide(x0, y0, k, pattern) {
228 for (const [t0, d0, t1, d1] of STITCHES[pattern]) {
229 const [ax, ay] = onSide(x0, y0, k, t0, d0), [bx, by] = onSide(x0, y0, k, t1, d1);
230 if (t0 === t1 && d0 === d1) block(ax, ay, THREAD_PIX); else line(ax, ay, bx, by, THREAD_PIX);
231 }
232 }
233 const CROSS = [[-2, -2], [-1, -1], [0, 0], [1, 1], [2, 2], [2, -2], [1, -1], [-1, 1], [-2, 2]];
234 function cross(x, y) {
235 const p = project(x, y, ZQ), cx = p.sx | 0, cy = p.sy | 0;
236 for (const [dx, dy] of CROSS) for (const [ox, oy] of [[-1, 0], [1, 0], [0, -1], [0, 1]]) put(cx + dx + ox, cy + dy + oy, INK_PIX);
237 for (const [dx, dy] of CROSS) put(cx + dx, cy + dy, CROSS_PIX);
238 }
239 // the seam between two neighbours: three cross-stitches when the stitches meet, an open gap when they do not
240 function drawSeam({ a, b, ok }) {
241 const [ax, ay] = cellOrigin(a), across = b === a + 1;
242 const at = t => (across ? [ax + PATCH, ay + t * PATCH] : [ax + t * PATCH, ay + PATCH]);
243 if (ok) { for (const t of [.2, .5, .8]) cross(...at(t)); return; }
244 for (const w of [-.14, -.07, 0, .07, .14]) {
245 const [sx, sy] = at(0), [ex, ey] = at(1);
246 line(sx + (across ? w : 0), sy + (across ? 0 : w), ex + (across ? w : 0), ey + (across ? 0 : w), GAP_PIX);
247 }
248 }
249 function outline(i, insets, colors) {
250 const [x0, y0] = cellOrigin(i);
251 insets.forEach((n, j) => {
252 const a = x0 + n, b = x0 + PATCH - n, c = y0 + n, d = y0 + PATCH - n;
253 for (const [p, q, r, s] of [[a, c, b, c], [b, c, b, d], [b, d, a, d], [a, d, a, c]]) line(p, q, r, s, colors[j]);
254 });
255 }
256 function drawQuilt() {
257 Q.patches.forEach((patch, i) => fillPatch(i, patch));
258 Q.patches.forEach((patch, i) => {
259 const [x0, y0] = cellOrigin(i);
260 for (let k = 0; k < 4; k++) stitchSide(x0, y0, k, patch[k]);
261 });
262 for (const m of Quilt.matches(Q)) drawSeam(m);
263 if (G.keyed && playing() && G.cursor !== G.selected) outline(G.cursor, [.05, .15], [WHITE_PIX, INK_PIX]);
264 if (G.selected !== null) outline(G.selected, [.02, .1, .18, .26], [INK_PIX, SIGNAL_PIX, SIGNAL_PIX, INK_PIX]);
265 }
266 function draw() {
267 pix.set(bakedPix);
268 zb.set(bakedZ);
269 pendingShadows.length = 0;
270 drawCurtain(STILL ? 0 : Math.floor(G.time * WIND_FPS) / WIND_FPS);
271 flushShadows();
272 applyShadows(bakedZ);
273 shadeEdges();
274 drawQuilt();
275 ctx.putImageData(image, 0, 0);
276 }
277
278 /* ---------- game: every rule is the rules script's ---------- */
279 function play(r) {
280 if (!r.ok) { say('That move is not allowed'); sound(120, .08, 'square'); return false; }
281 Q = r.state;
282 renderHud();
283 say(Q.line);
284 if (r.event === 'won') { sound(523, .12, 'triangle'); sound(659, .12, 'triangle', .12); sound(784, .3, 'triangle', .24); }
285 else if (r.event === 'lost') sound(110, .5, 'sawtooth');
286 else if (r.event === 'gain') sound(560, .1, 'sine');
287 else sound(330, .06, 'triangle');
288 return true;
289 }
290 function choose(cell) {
291 ensureAudio();
292 if (!playing()) { restart(); return; }
293 if (G.selected === null) { G.selected = cell; say(`Patch ${cell + 1} picked: tap it again to turn it, or tap another to swap`); sound(440, .05, 'sine'); }
294 else if (G.selected === cell) play(Quilt.rotate(Q, cell));
295 else if (play(Quilt.swap(Q, G.selected, cell))) G.selected = null;
296 if (!playing()) G.selected = null;
297 }
298 function release() { G.selected = null; if (playing()) say(MIDDLE); }
299 function restart() {
300 Q = fresh(); G.selected = null; G.cursor = 4;
301 renderHud();
302 say(MIDDLE);
303 }
304
305 /* ---------- input ---------- */
306 const point = e => { const r = canvas.getBoundingClientRect(); return [(e.clientX - r.left) / r.width * W, (e.clientY - r.top) / r.height * H]; };
307 canvas.addEventListener('pointerdown', e => {
308 canvas.focus({ preventScroll: true });
309 const cell = cellAt(...point(e));
310 G.keyed = false;
311 if (cell === null) { ensureAudio(); if (playing()) release(); else restart(); return; }
312 G.cursor = cell;
313 choose(cell);
314 });
315 const STEP = { ArrowRight: [1, 0], ArrowLeft: [-1, 0], ArrowDown: [0, 1], ArrowUp: [0, -1] };
316 canvas.addEventListener('keydown', e => {
317 const step = STEP[e.key], pick = e.key === ' ' || e.key === 'Enter', away = e.key === 'Escape';
318 if (!step && !pick && !away) return;
319 e.preventDefault();
320 if (!playing()) { if (!e.repeat) restart(); return; }
321 G.keyed = true;
322 if (step) {
323 const c = G.cursor % SIZE + step[0], r = Math.floor(G.cursor / SIZE) + step[1];
324 if (c >= 0 && r >= 0 && c < SIZE && r < SIZE) G.cursor = r * SIZE + c;
325 say(`Patch ${G.cursor + 1}`);
326 } else if (away) release();
327 else if (!e.repeat) choose(G.cursor);
328 });
329
330 /* ---------- sound ---------- */
331 let audio = null, muted = false;
332 function ensureAudio() {
333 if (audio) { if (audio.state === 'suspended') audio.resume(); return; }
334 const AC = window.AudioContext || window.webkitAudioContext;
335 if (AC) audio = new AC();
336 }
337 function sound(freq, life, type, delay = 0) {
338 if (!audio || muted) return;
339 const now = audio.currentTime + delay, gain = audio.createGain(), osc = audio.createOscillator();
340 osc.type = type;
341 osc.frequency.setValueAtTime(freq, now);
342 gain.gain.setValueAtTime(.0001, now);
343 gain.gain.exponentialRampToValueAtTime(.07, now + .01);
344 gain.gain.exponentialRampToValueAtTime(.0005, now + life);
345 osc.connect(gain).connect(audio.destination);
346 osc.start(now); osc.stop(now + life + .02);
347 }
348 const muteButton = document.getElementById('mute');
349 muteButton.addEventListener('click', () => {
350 muted = !muted;
351 muteButton.textContent = muted ? 'sound off' : 'sound on';
352 muteButton.setAttribute('aria-pressed', String(muted));
353 });
354
355 /* ---------- fit and boot ---------- */
356 addEventListener('resize', fit);
357 fit();
358 bake();
359 say(MIDDLE);
360 renderHud();
361 canvas.focus({ preventScroll: true });
362 let last = performance.now();
363 function frame(now) {
364 G.time += Math.min(.04, (now - last) / 1000);
365 last = now;
366 draw();
367 requestAnimationFrame(frame);
368 }
369 requestAnimationFrame(frame);
370 </script>
371 </body>
372 </html>
quilt/page.test.mjs added +72
@@ -0,0 +1,72 @@
1 import { test, expect } from 'bun:test';
2 import { boot } from '../playtest.mjs';
3 import Quilt from './patch.js';
4
5 const at = (g, i) => g.read(`cellPoint(${i})`);
6 const tapCell = (g, i) => g.tap(...at(g, i));
7
8 test('the first tap picks a patch without spending a move, and it is outlined', () => {
9 const g = boot('quilt', { seed: 3 });
10 const before = g.picture();
11 tapCell(g, 0);
12 expect(g.read('G.selected')).toBe(0);
13 expect(g.read('Q.moves')).toBe(0);
14 expect(g.picture()).not.toBe(before);
15 });
16
17 test('tapping the picked patch turns it a quarter and counts a move', () => {
18 const g = boot('quilt', { seed: 3 });
19 const was = g.read('Q.patches[4]');
20 tapCell(g, 4); tapCell(g, 4);
21 expect(g.read('Q.patches[4]')).toEqual(Quilt.turn(was));
22 expect(g.el('moves').textContent).toBe('23');
23 });
24
25 test('tapping a second patch swaps the two and lets go', () => {
26 const g = boot('quilt', { seed: 3 });
27 const a = g.read('Q.patches[1]'), b = g.read('Q.patches[7]');
28 tapCell(g, 1); tapCell(g, 7);
29 expect(g.read('Q.patches[1]')).toEqual(b);
30 expect(g.read('Q.patches[7]')).toEqual(a);
31 expect(g.read('G.selected')).toBe(null);
32 expect(g.read('Q.moves')).toBe(1);
33 });
34
35 test('tapping off the quilt, or Escape, lets go of a picked patch', () => {
36 const g = boot('quilt', { seed: 3 });
37 tapCell(g, 2); g.tap(5, 275);
38 expect(g.read('G.selected')).toBe(null);
39 tapCell(g, 2); g.press('Escape');
40 expect(g.read('G.selected')).toBe(null);
41 expect(g.read('Q.moves')).toBe(0);
42 });
43
44 test('arrows move the cursor and Space picks then turns', () => {
45 const g = boot('quilt', { seed: 3 });
46 g.press('ArrowRight'); g.press('ArrowDown');
47 expect(g.read('G.cursor')).toBe(8);
48 g.press(' '); g.press(' ');
49 expect(g.read('Q.moves')).toBe(1);
50 });
51
52 test('the HUD reports the rules state', () => {
53 const g = boot('quilt', { seed: 3 });
54 tapCell(g, 0); tapCell(g, 0);
55 expect(g.el('edges').textContent).toBe(String(g.read('Q.matching')));
56 expect(g.el('of').textContent).toBe('of 12 edges match');
57 expect(g.el('score').textContent).toBe(String(g.read('Q.score')));
58 expect(g.el('status').textContent).toContain(g.read('Q.line'));
59 });
60
61 test('solving the quilt ends the game and a tap deals a new one', () => {
62 const g = boot('quilt', { seed: 5 });
63 for (const m of Quilt.solution(g.read('Q.seed'))) {
64 if (m[0] === 'r') { if (g.read('G.selected') !== m[1]) { g.press('Escape'); tapCell(g, m[1]); } tapCell(g, m[1]); }
65 else { g.press('Escape'); tapCell(g, m[1]); tapCell(g, m[2]); }
66 }
67 expect(g.read('Q.won')).toBe(true);
68 expect(g.el('status').textContent).toContain('play again');
69 g.tap(100, 100);
70 expect(g.read('Q.status')).toBe('playing');
71 expect(g.read('Q.moves')).toBe(0);
72 });
rounds.mjs +11
@@ -7,6 +7,7 @@ import { bestThrow, LINE_MAX } from './bowls-green/bowls.js';
77 import Allotment from './allotment/plot.js';
88 import { toss, MAX_AIM } from './hoopla/hoop.js';
99 import Wheel from './ferris-wheel/wheel.js';
10 import Quilt from './quilt/patch.js';
1011
1112 const MID = [180, 140];
1213 const tapping = (wait = .3) => g => { g.tap(...MID); g.run(wait); };
@@ -339,6 +340,16 @@ export const ROUNDS = {
339340 g.step(1);
340341 };
341342 })() },
343 // the rules' own solution, one move a beat: pick a patch and tap it again to turn it, or pick one and tap another to swap
344 'quilt': { over: /play again/, outcome: /Quilt finished: \d+ points/, beat: g => {
345 const at = i => g.read(`cellPoint(${i})`);
346 const next = Quilt.solution(g.read('Q.seed'))[g.read('Q.moves')];
347 if (!next) { g.run(.2); return; }
348 const [kind, a, b] = next, held = g.read('G.selected');
349 if (held !== null && held !== a) g.press('Escape');
350 if (g.read('G.selected') !== a) g.tap(...at(a));
351 g.tap(...at(kind === 'r' ? a : b));
352 } },
342353 // heave when the beat lands, once per beat
343354 'tug-of-war': { over: /play again|pull again/, outcome: /you (win|lose): \d of 5 pulls/, beat: g => {
344355 const { index, offset } = g.read('Rope.nearestBeat(R.t)');
serve.test.mjs +7
@@ -577,6 +577,13 @@ test('Bird Watch and its rules script are served without a track', async () => {
577577 expect((await fetch(base + '/bird-watch/watch.js')).status).toBe(200);
578578 });
579579
580 test('Patchwork Quilt and its rules script are served without a track', async () => {
581 const page = await fetch(base + '/quilt/');
582 expect(page.status).toBe(200);
583 expect(page.headers.get('content-type')).toBe('text/html; charset=utf-8');
584 expect((await fetch(base + '/quilt/patch.js')).status).toBe(200);
585 });
586
580587 test('Hot Air Balloon and its rules script are served without a track', async () => {
581588 const page = await fetch(base + '/hot-air-balloon/');
582589 expect(page.status).toBe(200);
shell.js +1
@@ -61,6 +61,7 @@ window.PixelPlays = (() => {
6161 { id: 'hedge-maze', title: 'Hedge Maze', blurb: 'Find the way through the hedges to the fountain before the gardener rings the bell', kind: 'Puzzle', input: 'Keys', accent: '#3d6e4f', music: false, forged: true },
6262 { id: 'bird-watch', title: 'Bird Watch', blurb: 'Remember where each bird perched', kind: 'Memory', input: 'Tap', accent: '#6b8f3a', music: false, forged: true },
6363 { id: 'bunting', title: 'Bunting', blurb: 'Hang the flags for the fete so no two neighbours match', kind: 'Strategy', input: 'Tap', accent: '#3d7f6e', music: false, forged: true },
64 { id: 'quilt', title: 'Patchwork Quilt', blurb: 'Turn and swap the patches until every stitch lines up', kind: 'Puzzle', input: 'Tap', accent: '#b5656b', music: false, forged: true },
6465 ];
6566 const MUSIC_VOLUME = .4;
6667 const CREDITS = [

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs