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

Migrated from github.com/amirmushichge/pixel-plays

Duck Race stream: every seed lays rocks, eddies and fast water inside the stream, checked across seeds in `duck-race/stream.test.mjs` #290

Merged opened this

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

Merged

torque/pixel-plays:torque/286-duck-race-stream-every-seed-lays-rocks-e → main 2 commits fork 3 threads · 3 unresolved

  • Merged. torque/286-duck-race-stream-every-seed-lays-rocks-e was merged into main as 5b72aef.

Review

Sign in to review this change.
2 files changed +59 −2
duck-race/stream.js +16 −1
@@ -18,6 +18,10 @@
1818 const FAST_PATCHES = 8; // patches of fast water in the stream
1919 const EDDY_HOLD = 1.5; // seconds an eddy holds a duck
2020 const STEER_COOLDOWN = 0.4; // seconds steering is refused after a lane change
21 // Guarantee: every seed has at least one rock, one eddy and one patch of fast water (FAST_PATCHES >= 1), so every
22 // stream is a race. A seed that draws fewer than two obstacles from SLOT, FIRST_SLOT and OBSTACLE_CHANCE gets more
23 // on free slots, scanning on from a seeded slot; a seed with only one kind has its first obstacle turned into the
24 // missing kind. Seeds that already had both are untouched, and it all stays a pure function of the seed.
2125 const SLOT = 5; // spacing of obstacle slots
2226 const FIRST_SLOT = 2; // no obstacle before this slot, so the start is clear
2327 const OBSTACLE_CHANCE = 0.6; // chance a slot holds a rock or an eddy
@@ -52,6 +56,17 @@
5256 const lane = Math.floor(noise(seed, slot++) * LANES);
5357 obstacles.push({ id: obstacles.length, kind, lane, at: k * SLOT });
5458 }
59 // the guarantee of a rock, an eddy and fast water in every seed is stated beside SLOT, above
60 const slots = LENGTH / SLOT - FIRST_SLOT;
outdated

This sparse-seed loop has no test that reaches it. Either find a seed that needs it and test it, or delete it.

61 for (let j = 0; obstacles.length < 2 && j < slots; j++) {
62 const k = FIRST_SLOT + (Math.floor(noise(seed, 700) * slots) + j) % slots;
63 if (obstacles.some(o => o.at === k * SLOT)) continue;
64 obstacles.push({ id: obstacles.length, kind: 'rock', lane: Math.floor(noise(seed, 701 + j) * LANES), at: k * SLOT });
65 }
66 obstacles.sort((a, b) => a.at - b.at).forEach((o, i) => { o.id = i; });
67 for (const kind of ['rock', 'eddy']) {
68 if (!obstacles.some(o => o.kind === kind)) obstacles.find(o => o.kind !== kind).kind = kind;
69 }
5570 for (let i = 0; i < FAST_PATCHES; i++) {
5671 const lane = Math.floor(noise(seed, 500 + i * 2) * LANES);
5772 const from = round3(noise(seed, 501 + i * 2) * (LENGTH - FAST_LENGTH));
@@ -177,7 +192,7 @@
177192
178193 const api = {
179194 LENGTH, LANES, TIME_LIMIT, BASE_SPEED, FAST_FACTOR, EDDY_HOLD, STEER_COOLDOWN, RIVALS, LOOKAHEAD, WIN_SCORE,
180 CLEAN_BONUS, layout, rivalTurn, start, tick, steer,
195 CLEAN_BONUS, SLOT, FIRST_SLOT, layout, rivalTurn, start, tick, steer,
181196 };
182197 if (typeof module !== 'undefined') module.exports = api; else root.DuckRace = api;
183198 })(typeof window !== 'undefined' ? window : globalThis);
duck-race/stream.test.mjs +43 −1
@@ -1,7 +1,7 @@
11 import { test, expect } from 'bun:test';
22 import {
33 LENGTH, LANES, TIME_LIMIT, BASE_SPEED, FAST_FACTOR, EDDY_HOLD, STEER_COOLDOWN, RIVALS, LOOKAHEAD, WIN_SCORE,
4 CLEAN_BONUS, layout, rivalTurn, start, tick, steer,
4 CLEAN_BONUS, SLOT, FIRST_SLOT, layout, rivalTurn, start, tick, steer,
55 } from './stream.js';
66
77 // a stream with hand-placed obstacles, so each rule is checked against a count done by hand
@@ -167,3 +167,45 @@ test('across seeds, no rival is left stuck on a rock at the end of a long run',
167167 expect(end.ducks.slice(1).every(d => d.finishTime !== null)).toBe(true);
168168 }
169169 });
170

Add a check that o.id === index for every obstacle, and a case for the sparse-seed branch of the fix.

171 test('every seed from 1 to 200 gives a real race: rocks, eddies and fast water inside the stream, ducks on the line', () => {
172 for (let seed = 1; seed <= 200; seed++) {
173 const s = start(seed);
174 expect(start(seed)).toEqual(s);
175 const why = `seed ${seed}`;
176 if (!s.obstacles.some(o => o.kind === 'rock')) throw new Error(`${why}: no rock`);
177 if (!s.obstacles.some(o => o.kind === 'eddy')) throw new Error(`${why}: no eddy`);
178 if (s.fast.length < 1) throw new Error(`${why}: no fast water`);
179 for (const f of s.fast) {
180 expect(Number.isInteger(f.lane) && f.lane >= 0 && f.lane <= LANES - 1).toBe(true);
181 expect(f.from).toBeGreaterThanOrEqual(0);
182 expect(f.from).toBeLessThan(f.to);
183 expect(f.to).toBeLessThanOrEqual(LENGTH);
184 }
185 s.obstacles.forEach((o, i) => expect(o.id).toBe(i));
186 for (const o of s.obstacles) {
187 expect(o.at).toBeGreaterThanOrEqual(FIRST_SLOT * SLOT);
188 expect(o.at).toBeLessThan(LENGTH);
189 expect(o.lane >= 0 && o.lane < LANES && Number.isInteger(o.lane)).toBe(true);
190 }
191 expect(s.ducks.length).toBe(RIVALS + 1);
192 expect(s.ducks.map(d => d.id)).toEqual([0, 1, 2, 3]);
193 expect(s.ducks.every(d => d.x === 0)).toBe(true);
194 expect(new Set(s.ducks.map(d => d.lane)).size).toBe(RIVALS + 1);
195 expect(s.ducks.filter(d => d.id === 0).length).toBe(1);
196 }
197 });
198
199 test('a seed whose base draw holds fewer than two obstacles still gets a rock and an eddy, in order, on free slots', () => {
200 // found by scanning: from the base rules these seeds draw exactly one obstacle (the scan is in the pull)
201 for (const seed of [675585, 950906, 1555868]) {
202 const s = start(seed);
203 expect(s.obstacles.length).toBeGreaterThanOrEqual(2);
204 expect(s.obstacles.some(o => o.kind === 'rock') && s.obstacles.some(o => o.kind === 'eddy')).toBe(true);
205 s.obstacles.forEach((o, i) => expect(o.id).toBe(i));
206 expect(s.obstacles.map(o => o.at)).toEqual(s.obstacles.map(o => o.at).sort((a, b) => a - b));
207 expect(new Set(s.obstacles.map(o => o.at)).size).toBe(s.obstacles.length);
208 for (const o of s.obstacles) { expect(o.at).toBeGreaterThanOrEqual(FIRST_SLOT * SLOT); expect(o.at).toBeLessThan(LENGTH); }
209 expect(start(seed)).toEqual(s);
210 }
211 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs