(Adaptive Loop) Pass 1
Adaptive Loop — a deterministic microfluidic strategy puzzle. Pure serializable nextState(state,action) owns every committed turn; previews call the same function on a clone. 5 handcrafted scenarios, bounded solver with verified win/lose routes + shared-ownership counterfactual, exact localStorage persistence/replay, accessible SVG interface, Vitest truth/solver/persistence tests, and Playwright black-box browser proof. Scenarios 3 and 4 retuned per SPEC §9 (provably unwinnable as originally specified); proofs and exact changes in docs/scenario-tuning.md. Invariants preserved. See CONTEXT-MANIFEST.md for the disclosure boundary and residual risks. Aggregate verified: tsc --noEmit, vitest (59), vite build, playwright (10).
This commit is contained in:
31 files changed
+7113
No files matched your search
@@ -0,0 +1,77 @@
|
||||
// Adaptive Loop — board geometry. Three interlocking hexagonal rings (SPEC §4, §13).
|
||||
// Slots are placed at the vertices of three mutually-adjacent flat-top hexagons;
|
||||
// shared slots coincide at the ring overlaps. Pure data, no DOM.
|
||||
import type { LoopId } from "../domain/types";
|
||||
import { LOOPS } from "../domain/constants";
|
||||
|
||||
export interface Pt { x: number; y: number; }
|
||||
|
||||
// Three flat-top hexagon centers (mutually adjacent cluster), vertex radius R.
|
||||
const R = 78;
|
||||
const SQRT3 = Math.sqrt(3);
|
||||
// centers: A central-left, B right, C top — chosen so the three rings overlap
|
||||
// pairwise and read as interlocking from a single viewpoint.
|
||||
const CENTERS: Record<LoopId, Pt> = {
|
||||
A: { x: 150, y: 250 },
|
||||
B: { x: 150 + 1.5 * R, y: 250 - (R * SQRT3) / 2 },
|
||||
C: { x: 150, y: 250 - R * SQRT3 },
|
||||
};
|
||||
|
||||
function hexVertices(c: Pt, r: number): Pt[] {
|
||||
// flat-top: vertices at 0,60,...,300 degrees
|
||||
const v: Pt[] = [];
|
||||
for (let k = 0; k < 6; k++) {
|
||||
const a = (k * 60 * Math.PI) / 180;
|
||||
v.push({ x: c.x + r * Math.cos(a), y: c.y + r * Math.sin(a) });
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
// Build a coordinate per slot by assigning each loop's vertex order, then dedup
|
||||
// shared slots to a single point (averaged) so overlaps coincide visually.
|
||||
function buildSlots(): { slots: Pt[]; polygons: Record<LoopId, Pt[]> } {
|
||||
const rawPerLoop: Record<LoopId, { slot: number; pt: Pt }[]> = { A: [], B: [], C: [] };
|
||||
(Object.keys(LOOPS) as LoopId[]).forEach((L) => {
|
||||
const verts = hexVertices(CENTERS[L], R);
|
||||
LOOPS[L].forEach((slot, i) => {
|
||||
rawPerLoop[L].push({ slot, pt: verts[i]! });
|
||||
});
|
||||
});
|
||||
// collect each slot's candidate points (a shared slot gets >1), average them
|
||||
const acc: Record<number, Pt[]> = {};
|
||||
(Object.keys(rawPerLoop) as LoopId[]).forEach((L) => {
|
||||
for (const e of rawPerLoop[L]) {
|
||||
(acc[e.slot] ??= []).push(e.pt);
|
||||
}
|
||||
});
|
||||
const slots: Pt[] = [];
|
||||
for (let i = 0; i < 12; i++) {
|
||||
const pts = acc[i] ?? [{ x: 0, y: 0 }];
|
||||
const ax = pts.reduce((s, p) => s + p.x, 0) / pts.length;
|
||||
const ay = pts.reduce((s, p) => s + p.y, 0) / pts.length;
|
||||
slots[i] = { x: ax, y: ay };
|
||||
}
|
||||
const polygons = {} as Record<LoopId, Pt[]>;
|
||||
(Object.keys(LOOPS) as LoopId[]).forEach((L) => {
|
||||
polygons[L] = LOOPS[L].map((slot) => slots[slot]!);
|
||||
});
|
||||
return { slots, polygons };
|
||||
}
|
||||
|
||||
const built = buildSlots();
|
||||
export const SLOT_POS: Pt[] = built.slots;
|
||||
export const LOOP_POLYGONS: Record<LoopId, Pt[]> = built.polygons;
|
||||
|
||||
export const VIEWBOX = { w: 460, h: 420 };
|
||||
|
||||
export function loopCenter(L: LoopId): Pt {
|
||||
return CENTERS[L];
|
||||
}
|
||||
|
||||
/** Outlet direction unit vector at a slot (for flow arrow rendering). */
|
||||
export function outletVector(from: Pt, to: Pt): Pt {
|
||||
const dx = to.x - from.x;
|
||||
const dy = to.y - from.y;
|
||||
const len = Math.hypot(dx, dy) || 1;
|
||||
return { x: dx / len, y: dy / len };
|
||||
}
|
||||
Reference in new issue
Block a user