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

Migrated from github.com/amirmushichge/pixel-plays

Skipping Rope rules: a tested `skipping-rope/rope.js` where you jump the turning rope as it quickens and more skippers join, from plain data, in a two-file pull with a test file that runs in under a second #291

Merged opened this

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

Merged

torque/pixel-plays:torque/270-skipping-rope-rules-a-tested-skipping-ro → main 1 commit fork

  • Merged. torque/270-skipping-rope-rules-a-tested-skipping-ro was merged into main as 3349b83.

Review

Sign in to review this change.
2 files changed +207 −0
skipping-rope/rope.js added +86
@@ -0,0 +1,86 @@
1 'use strict';
2 // The Skipping Rope rules: two friends turn a long rope and you jump each time it reaches the ground. Plain data in,
3 // plain data out, no DOM, no timers, no Math.random. A rejected call returns the very same state and says why.
4 //
5 // Units: time in seconds, the rope's angle in turns (0 and 1 are the rope at the ground, 0.5 the top of the swing).
6 // The rope's angle is a function of time alone: angle(state, t) = 1 - (nextCross - t) / period, so where the rope is
7 // does not depend on how time is sliced into ticks. A tick that spans a ground pass is split at the exact pass time,
8 // so clears, catches and the quickening happen at the same moments whatever the dt. (Times are summed in floating
9 // point; callers who need bit-exact agreement across slicings should use dts that are exact in binary, like 0.125.)
10 (function (root) {
11 const START_PERIOD = 1; // s per turn at the start
12 const PERIOD_STEP = 0.125; // s the period shortens by every CLEARS_PER_STEP clears
13 const PERIOD_FLOOR = 0.625; // s: the period never goes below this
14 const AIRTIME = 0.5; // s a skipper stays in the air after jump()
15 const CLEARS_PER_STEP = 3; // clears between quickenings (and between new skippers joining)
16 const MAX_SKIPPERS = 4; // the line never grows past this many skippers
17 const CLEAR_POINTS = 10; // score for a clear by a lone skipper
18 const SKIPPER_BONUS = 5; // extra score per joined skipper (beyond the first) on every later clear
19 const WIN_CLEARS = 12; // total clears that win the game
20
21 function describe(s) {
22 if (s.status === 'won') return `You win: ${s.skips} skips, score ${s.score}`;
23 if (s.status === 'lost') return `You are caught: ${s.skips} skips, score ${s.score}`;
24 return `Skipping: ${s.skips} skips, score ${s.score}, ${s.skippers} on the rope, ${s.period.toFixed(3)} s a turn`;
25 }
26
27 function start() {
28 const s = {
29 status: 'playing', t: 0, period: START_PERIOD, nextCross: START_PERIOD, airUntil: 0,
30 skips: 0, score: 0, skippers: 1, airtime: AIRTIME, line: '',
31 };
32 s.line = describe(s);
33 return s;
34 }
35
36 // angle(state) -> the rope's angle in turns, in [0, 1), at the state's time
37 const angle = (s, t = s.t) => {
38 const a = 1 - (s.nextCross - t) / s.period;
39 return a >= 1 ? 0 : a < 0 ? 0 : a;
40 };
41 const airborne = s => s.airUntil > s.t;
42
43 // pass(state) -> the state after the rope reaches the ground at s.nextCross
44 function pass(s) {
45 const tc = s.nextCross;
46 const n = { ...s, t: tc };
47 if (!(s.airUntil > tc)) {
48 n.status = 'lost';
49 } else {
50 n.skips = s.skips + 1;
51 n.score = s.score + CLEAR_POINTS + SKIPPER_BONUS * (s.skippers - 1); // scored by the line as it stood
52 if (n.skips % CLEARS_PER_STEP === 0) {
53 n.period = Math.max(PERIOD_FLOOR, s.period - PERIOD_STEP);
54 n.skippers = Math.min(MAX_SKIPPERS, s.skippers + 1);
55 }
56 if (n.skips >= WIN_CLEARS) n.status = 'won';
57 }
58 n.nextCross = tc + n.period;
59 return n;
60 }
61
62 // tick(state, dt) -> { ok, state, why } advances the rope by dt seconds (finite, above 0), stopping at a catch or win
63 function tick(state, dt) {
64 if (typeof dt !== 'number' || !Number.isFinite(dt) || dt <= 0) return { ok: false, state, why: 'bad-dt' };
65 if (state.status !== 'playing') return { ok: false, state, why: 'game-over' };
66 const end = state.t + dt;
67 let s = state;
68 while (s.status === 'playing' && s.nextCross <= end) s = pass(s);
69 if (s.status === 'playing') s = { ...s, t: end };
70 s.line = describe(s);
71 return { ok: true, state: s, why: '' };
72 }
73
74 // jump(state) -> { ok, state, why } lifts the skipper for AIRTIME seconds from now; refused while still in the air
75 function jump(state) {
76 if (state.status !== 'playing') return { ok: false, state, why: 'game-over' };
77 if (airborne(state)) return { ok: false, state, why: 'in-air' };
78 return { ok: true, state: { ...state, airUntil: state.t + state.airtime }, why: '' };
79 }
80
81 const api = {
82 START_PERIOD, PERIOD_STEP, PERIOD_FLOOR, AIRTIME, CLEARS_PER_STEP, MAX_SKIPPERS, CLEAR_POINTS, SKIPPER_BONUS,
83 WIN_CLEARS, start, tick, jump, angle, airborne, describe,
84 };
85 if (typeof module !== 'undefined') module.exports = api; else root.SkippingRope = api;
86 })(typeof window !== 'undefined' ? window : globalThis);
skipping-rope/rope.test.mjs added +121
@@ -0,0 +1,121 @@
1 import { test, expect } from 'bun:test';
2 import {
3 START_PERIOD, PERIOD_STEP, PERIOD_FLOOR, AIRTIME, CLEARS_PER_STEP, MAX_SKIPPERS, CLEAR_POINTS, SKIPPER_BONUS,
4 WIN_CLEARS, start, tick, jump, angle, airborne,
5 } from './rope.js';
6
7 const run = (s, dt) => tick(s, dt).state;
8 // play: jump the moment the rope is `lead` seconds before the ground, for n passes; dt of 1/64 s (exact in binary)
9 function play(n, lead = 0.125, dt = 1 / 64) {
10 let s = start();
11 while (s.status === 'playing' && s.skips < n) {
12 if (!airborne(s) && s.nextCross - s.t <= lead + 1e-9) s = jump(s).state;
13 s = run(s, dt);
14 }
15 return s;
16 }
17
18 test('start gives a rope at the stated period, on the ground at angle 0, and the stated constants hold', () => {
19 const s = start();
20 expect([s.period, s.skips, s.score, s.skippers, s.status]).toEqual([START_PERIOD, 0, 0, 1, 'playing']);
21 expect(angle(s)).toBe(0);
22 expect(angle(run(s, START_PERIOD / 2))).toBeCloseTo(0.5, 12);
23 expect(AIRTIME).toBeLessThan(START_PERIOD);
24 });
25
26 test('the rope angle depends on time, not on how it is sliced', () => {
27 const a = run(start(), 0.5);
28 let b = start();
29 for (let i = 0; i < 8; i++) b = run(b, 1 / 16);
30 expect(angle(a)).toBe(angle(b));
31 expect(angle(a)).toBe(0.5);
32 });
33
34 test('the same jumps at the same times clear the same skips however time is sliced', () => {
35 const fine = play(7, 0.125, 1 / 64);
36 const coarse = play(7, 0.125, 1 / 16);
37 expect(fine.skips).toBe(7);
38 expect([coarse.skips, coarse.score, coarse.period, coarse.skippers]).toEqual([fine.skips, fine.score, fine.period, fine.skippers]);
39 // one big tick over several passes equals many small ones, jump held in the air throughout
40 let a = jump(start()).state; a = { ...a, airtime: 100, airUntil: 100 };
41 let b = a;
42 for (let i = 0; i < 128; i++) b = run(b, 1 / 16);
43 const c = run(a, 8);
44 expect([c.skips, c.score, c.period, c.skippers]).toEqual([b.skips, b.score, b.period, b.skippers]);
45 });
46
47 test('a jump while in the air is refused and changes nothing; it works again after landing', () => {
48 const s = jump(start()).state;
49 expect(s.airUntil).toBe(AIRTIME);
50 const again = jump(s);
51 expect([again.ok, again.why]).toEqual([false, 'in-air']);
52 expect(again.state).toBe(s);
53 const landed = run(s, AIRTIME);
54 expect(airborne(landed)).toBe(false);
55 expect(jump(landed).ok).toBe(true);
56 });
57
58 test('dt must be a finite number above 0', () => {
59 const s = start();
60 for (const dt of [0, -1, NaN, Infinity, '1', null, undefined]) {
61 const r = tick(s, dt);
62 expect([r.ok, r.why]).toEqual([false, 'bad-dt']);
63 expect(r.state).toBe(s);
64 }
65 });
66
67 test('a skipper in the air when the rope passes clears it and scores the stated amount', () => {
68 let s = run(start(), 0.75);
69 s = jump(s).state; // airborne 0.75 .. 1.25, rope passes at 1
70 s = run(s, 0.5);
71 expect([s.status, s.skips, s.score]).toEqual(['playing', 1, CLEAR_POINTS]);
72 expect(s.line).toContain('1 skips');
73 });
74
75 test('a skipper on the ground is caught: the game ends with the loss line, and nothing moves after', () => {
76 let s = run(start(), 0.75);
77 s = jump(s).state; s = run(s, 0.5); // one clear
78 s = run(s, 1); // second pass with no jump
79 expect(s.status).toBe('lost');
80 expect(s.line).toBe(`You are caught: 1 skips, score ${CLEAR_POINTS}`);
81 expect(tick(s, 1).why).toBe('game-over');
82 expect(jump(s).why).toBe('game-over');
83 // landing exactly as the rope passes is a catch
84 let e = jump(start()).state; e = run(e, 0.5); e = jump(e).state; e = run(e, 0.5);
85 expect(e.status).toBe('lost');
86 });
87
88 test('every three clears the period shortens by the step, down to the floor and no further', () => {
89 const periods = [];
90 let s = start();
91 for (let n = 1; n <= WIN_CLEARS; n++) { s = play(n); periods.push(s.period); }
92 expect(periods.slice(0, 3)).toEqual([1, 1, 1 - PERIOD_STEP]);
93 expect(periods[5]).toBe(1 - 2 * PERIOD_STEP);
94 expect(periods[8]).toBe(PERIOD_FLOOR); // 1 - 3 steps lands on the floor
95 expect(1 - 3 * PERIOD_STEP).toBe(PERIOD_FLOOR);
96 expect(periods[11]).toBe(PERIOD_FLOOR); // a fourth step would go below it
97 expect(Math.min(...periods)).toBe(PERIOD_FLOOR);
98 expect(CLEARS_PER_STEP).toBe(3);
99 });
100
101 test('skippers join every three clears up to the limit and raise the score of every later clear', () => {
102 const scores = [];
103 const joined = [];
104 for (let n = 1; n <= 10; n++) { const s = play(n); scores.push(s.score); joined.push(s.skippers); }
105 const gain = scores.map((v, i) => v - (i ? scores[i - 1] : 0));
106 expect(gain.slice(0, 3)).toEqual([CLEAR_POINTS, CLEAR_POINTS, CLEAR_POINTS]); // lone skipper
107 expect(gain.slice(3, 6)).toEqual(Array(3).fill(CLEAR_POINTS + SKIPPER_BONUS)); // two on the rope
108 expect(gain[6]).toBe(CLEAR_POINTS + 2 * SKIPPER_BONUS);
109 expect(gain[9]).toBe(CLEAR_POINTS + 3 * SKIPPER_BONUS);
110 expect(joined[2]).toBe(2);
111 expect(Math.max(...joined)).toBe(MAX_SKIPPERS);
112 });
113
114 test('reaching the stated total of clears wins, with the win line and hand-counted score', () => {
115 const s = play(WIN_CLEARS);
116 expect([s.status, s.skips]).toEqual(['won', WIN_CLEARS]);
117 // 3 clears at each of 1..4 skippers: 3*(10+15+20+25)
118 expect(s.score).toBe(3 * (4 * CLEAR_POINTS + SKIPPER_BONUS * (0 + 1 + 2 + 3)));
119 expect(s.line).toBe(`You win: ${WIN_CLEARS} skips, score ${s.score}`);
120 expect(tick(s, 1).why).toBe('game-over');
121 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs