Public: anyone can read this; the crew can change it.
Migrated from github.com/amirmushichge/pixel-plays
Conkers rules: a tested `conkers/match.js` where you time a swing on a power meter to crack a run of rivals' conkers before yours cracks, from plain data, in a two-file pull with a test file that runs in under a second #262
Check · Review · Land · Live done — Landed and live in production.
vesper/pixel-plays:vesper/256-conkers-rules-a-tested-conkers-match-js → main 2 commits fork 2 threads · 2 unresolved
2 files
changed +270
−0
conkers/match.js added +116
| @@ -0,0 +1,116 @@ | ||
| 1 | 'use strict'; | |
| 2 | // The Conkers rules: a playground match against a run of rivals. You and the opponent take turns to swing at each | |
| 3 | // other's conker; you time your swing on a power meter, the opponent's meter comes from the seed. | |
| 4 | // Plain data in, plain data out, no DOM, no timers, no Math.random. A rejected call returns the very same state and | |
| 5 | // says why. The page (not yet written) draws what this decides. | |
| 6 | // | |
| 7 | // A meter reading runs from 0 to 1. Inside [SWEET_LOW, SWEET_HIGH] a swing does FULL_DAMAGE; below it, or above it | |
| 8 | // up to TANGLE_ABOVE, it does REDUCED_DAMAGE; above TANGLE_ABOVE the strings tangle: no damage, and the striker | |
| 9 | // loses the turn (a tangle is a swing that hits nothing, so the other side strikes next). Every swing, tangled or | |
| 10 | // not, passes the turn. A conker at 0 hit points is cracked. | |
| 11 | (function (root) { | |
| 12 | const OPPONENTS = Object.freeze([ // the run of rivals, in order | |
| 13 | { name: 'Smudge', hp: 6 }, | |
| 14 | { name: 'Biscuit', hp: 8 }, | |
| 15 | { name: 'Old Iron', hp: 10 }, | |
| 16 | ].map(o => Object.freeze(o))); | |
| 17 | const PLAYER_HP = 12; // your conker's hit points at the start | |
| 18 | const SWEET_LOW = 0.4; // bottom of the sweet spot | |
| 19 | const SWEET_HIGH = 0.6; // top of the sweet spot | |
| 20 | const TANGLE_ABOVE = 0.85; // a reading above this tangles the strings | |
| 21 | const FULL_DAMAGE = 4; // hit points off for a sweet-spot swing | |
| 22 | const REDUCED_DAMAGE = 1; // hit points off for any other untangled swing | |
| 23 | const BOUT_SCORE = 100; // points for cracking an opponent's conker | |
| 24 | const HP_BONUS = 10; // extra points for each hit point you have left | |
| 25 | ||
| 26 | const finite = v => (Number.isFinite(v) ? v : 0); | |
| 27 | // a repeatable number in [0, 1) for this seed and slot | |
| 28 | function noise(seed, n) { | |
| 29 | let h = (Math.imul(finite(seed) | 0, 374761393) + Math.imul(n | 0, 668265263)) | 0; | |
| 30 | h = Math.imul(h ^ (h >>> 13), 1274126177); | |
| 31 | return ((h ^ (h >>> 16)) >>> 0) / 4294967296; | |
| 32 | } | |
| 33 | const round3 = v => Math.round(v * 1000) / 1000; | |
| 34 | ||
| 35 | // damage(meter) -> hit points a swing at this reading takes off; 0 when tangled | |
| 36 | function damage(meter) { | |
| 37 | if (meter > TANGLE_ABOVE) return 0; | |
| 38 | return meter >= SWEET_LOW && meter <= SWEET_HIGH ? FULL_DAMAGE : REDUCED_DAMAGE; | |
| 39 | } | |
| 40 | ||
| 41 | function refresh(s) { | |
| 42 | const foe = OPPONENTS[s.bout]; | |
| 43 | s.opponent = foe.name; | |
| 44 | if (s.status === 'over') { | |
| 45 | s.line = s.won | |
| 46 | ? `All ${OPPONENTS.length} rivals cracked, you win: ${s.score} points` | |
| 47 | : `Your conker cracked against ${foe.name}, you lose: ${s.score} points`; | |
| 48 | } else { | |
| 49 | s.line = `Bout ${s.bout + 1} of ${OPPONENTS.length} against ${foe.name}: ${s.turn === 'player' ? 'your swing' : 'their swing'}`; | |
| 50 | } | |
| 51 | return s; | |
| 52 | } | |
| 53 | ||
| 54 | const start = (seed = 1) => { | |
| 55 | const sd = finite(seed) >>> 0; | |
| 56 | return refresh({ | |
| 57 | seed: sd, opponents: OPPONENTS.map(o => Object.assign({}, o)), bout: 0, turn: 'player', | |
| 58 | playerHp: PLAYER_HP, opponentHp: OPPONENTS[0].hp, swings: 0, score: 0, | |
| 59 | status: 'playing', won: false, opponent: '', line: '', | |
| 60 | }); | |
| 61 | }; | |
| 62 | ||
| 63 | const refuse = (s, why) => ({ ok: false, why, event: '', state: s }); | |
| 64 | ||
| 65 | // strike(n, meter) mutates the copy n: applies one swing by the side whose turn it is and returns the event | |
| 66 | function strike(n, meter) { | |
| 67 | const byPlayer = n.turn === 'player'; | |
| 68 | const dmg = damage(meter); | |
| 69 | n.swings += 1; | |
| 70 | n.turn = byPlayer ? 'opponent' : 'player'; | |
| 71 | if (dmg === 0) return 'tangle'; | |
| 72 | if (byPlayer) { | |
| 73 | n.opponentHp = Math.max(0, n.opponentHp - dmg); | |
| 74 | if (n.opponentHp > 0) return dmg === FULL_DAMAGE ? 'sweet' : 'hit'; | |
| 75 | n.score += BOUT_SCORE + HP_BONUS * n.playerHp; | |
| 76 | if (n.bout + 1 >= OPPONENTS.length) { n.status = 'over'; n.won = true; return 'won'; } | |
| 77 | n.bout += 1; | |
| 78 | n.opponentHp = OPPONENTS[n.bout].hp; // your hit points carry over | |
| 79 | n.turn = 'player'; | |
| 80 | return 'cracked'; | |
| 81 | } | |
| 82 | n.playerHp = Math.max(0, n.playerHp - dmg); | |
| 83 | if (n.playerHp > 0) return dmg === FULL_DAMAGE ? 'sweet' : 'hit'; | |
| 84 | n.status = 'over'; n.won = false; | |
| 85 | return 'lost'; | |
| 86 | } | |
| 87 | ||
| 88 | // swing(state, meter) -> { ok, why, event, state }: your swing at a reading from 0 to 1. Refused when the reading | |
| 89 | // is not a number in range, when the game is over, or when it is not your turn. event is 'sweet', 'hit', 'tangle', | |
| 90 | // 'cracked' (the opponent's conker, next rival steps up), 'won' or 'lost'. | |
| 91 | function swing(s, meter) { | |
| 92 | if (s.status !== 'playing') return refuse(s, 'over'); | |
| 93 | if (typeof meter !== 'number' || !Number.isFinite(meter) || meter < 0 || meter > 1) return refuse(s, 'bad-meter'); | |
| 94 | if (s.turn !== 'player') return refuse(s, 'not-your-turn'); | |
| 95 | const n = Object.assign({}, s); | |
| 96 | const event = strike(n, meter); | |
| 97 | return { ok: true, why: '', event, state: refresh(n) }; | |
| 98 | } | |
| 99 | ||
| 100 | // opponentTurn(state) -> { ok, why, event, state, meter }: the opponent's swing, its reading drawn from the seed | |
| 101 | // and the swing count. Refused when the game is over or when it is your turn. | |
| 102 | function opponentTurn(s) { | |
| 103 | if (s.status !== 'playing') return Object.assign(refuse(s, 'over'), { meter: -1 }); | |
| 104 | if (s.turn !== 'opponent') return Object.assign(refuse(s, 'your-turn'), { meter: -1 }); | |
| 105 | const meter = round3(noise(s.seed, s.swings)); | |
| 106 | const n = Object.assign({}, s); | |
| 107 | const event = strike(n, meter); | |
| 108 | return { ok: true, why: '', event, state: refresh(n), meter }; | |
| 109 | } | |
| 110 | ||
| 111 | const api = { | |
| 112 | OPPONENTS, PLAYER_HP, SWEET_LOW, SWEET_HIGH, TANGLE_ABOVE, FULL_DAMAGE, REDUCED_DAMAGE, BOUT_SCORE, HP_BONUS, | |
| 113 | damage, start, swing, opponentTurn, | |
| 114 | }; | |
| 115 | if (typeof module !== 'undefined') module.exports = api; else root.Conkers = api; | |
| 116 | })(typeof window !== 'undefined' ? window : globalThis); |
conkers/match.test.mjs added +154
| @@ -0,0 +1,154 @@ | ||
| 1 | import { test, expect } from 'bun:test'; | |
| 2 | import { | |
| 3 | OPPONENTS, PLAYER_HP, SWEET_LOW, SWEET_HIGH, TANGLE_ABOVE, FULL_DAMAGE, REDUCED_DAMAGE, BOUT_SCORE, HP_BONUS, | |
| 4 | damage, start, swing, opponentTurn, | |
| 5 | } from './match.js'; | |
| 6 | ||
| 7 | const SWEET = (SWEET_LOW + SWEET_HIGH) / 2; | |
| 8 | const OFF = SWEET_LOW / 2; | |
| 9 | const TANGLE = (TANGLE_ABOVE + 1) / 2; | |
| 10 | // a state with the opponent's turn | |
| 11 | const theirs = (seed = 1) => swing(start(seed), OFF).state; | |
| 12 | // the same state with the turn handed back to the player | |
| 13 | const back = s => Object.assign({}, s, { turn: 'player' }); | |
| 14 | ||
| 15 | test('start states the opponents, their hit points, and gives the player the first swing', () => { | |
| 16 | const s = start(3); | |
| 17 | expect(OPPONENTS.length).toBeGreaterThan(1); | |
| 18 | expect(s.opponents).toEqual(OPPONENTS); | |
| 19 | expect([s.bout, s.turn, s.playerHp, s.opponentHp, s.status]).toEqual([0, 'player', PLAYER_HP, OPPONENTS[0].hp, 'playing']); | |
| 20 | expect(s.opponent).toBe(OPPONENTS[0].name); | |
| 21 | expect(SWEET_LOW).toBeLessThan(SWEET_HIGH); | |
| 22 | expect(SWEET_HIGH).toBeLessThan(TANGLE_ABOVE); | |
| 23 | expect(FULL_DAMAGE).toBeGreaterThan(REDUCED_DAMAGE); | |
| 24 | }); | |
| 25 | ||
| 26 | test('a meter outside 0 to 1, or not a number, is refused and the state is unchanged', () => { | |
| 27 | const s = start(1); | |
| 28 | for (const m of [-0.01, 1.01, NaN, Infinity, '0.5', null, undefined]) { | |
| 29 | const r = swing(s, m); | |
| 30 | expect(r.ok).toBe(false); | |
| 31 | expect(r.why).toBe('bad-meter'); | |
| 32 | expect(r.state).toBe(s); | |
| 33 | } | |
| 34 | expect(swing(s, 0).ok).toBe(true); | |
| 35 | expect(swing(s, 1).ok).toBe(true); | |
| 36 | }); | |
| 37 | ||
| 38 | test('a swing out of turn is refused, and so is an opponent turn on yours', () => { | |
| 39 | const s = theirs(); | |
| 40 | expect(s.turn).toBe('opponent'); | |
| 41 | const r = swing(s, SWEET); | |
| 42 | expect([r.ok, r.why, r.state]).toEqual([false, 'not-your-turn', s]); | |
| 43 | const o = opponentTurn(start(1)); | |
| 44 | expect([o.ok, o.why]).toEqual([false, 'your-turn']); | |
| 45 | expect(o.state.turn).toBe('player'); | |
| 46 | }); | |
| 47 | ||
| 48 | test('a sweet-spot reading does full damage and any other untangled reading does reduced damage', () => { | |
| 49 | for (const m of [SWEET_LOW, SWEET, SWEET_HIGH]) { | |
| 50 | const r = swing(start(1), m); | |
| 51 | expect(r.event).toBe('sweet'); | |
| 52 | expect(r.state.opponentHp).toBe(OPPONENTS[0].hp - FULL_DAMAGE); | |
| 53 | } | |
| 54 | for (const m of [0, OFF, SWEET_HIGH + 0.01, TANGLE_ABOVE]) { | |
| 55 | const r = swing(start(1), m); | |
| 56 | expect(r.event).toBe('hit'); | |
| 57 | expect(r.state.opponentHp).toBe(OPPONENTS[0].hp - REDUCED_DAMAGE); | |
| 58 | } | |
| 59 | expect(damage(SWEET)).toBe(FULL_DAMAGE); | |
| 60 | }); | |
| 61 | ||
| 62 | test('a reading above the limit tangles the strings: no damage and the turn passes', () => { | |
| 63 | const r = swing(start(1), TANGLE); | |
| 64 | expect(r.event).toBe('tangle'); | |
| 65 | expect(r.state.opponentHp).toBe(OPPONENTS[0].hp); | |
| 66 | expect(r.state.turn).toBe('opponent'); | |
| 67 | expect(swing(start(1), 1).event).toBe('tangle'); | |
| 68 | expect(damage(1)).toBe(0); | |
| 69 | }); | |
| 70 | ||
| 71 | test('the opponent swings from the seed: the same seed gives the same match, other seeds differ', () => { | |
| 72 | const play = seed => { | |
| 73 | let s = start(seed); | |
| 74 | const log = []; | |
| 75 | for (let i = 0; i < 6 && s.status === 'playing'; i++) { | |
| 76 | s = swing(s, SWEET).state; | |
| 77 | if (s.turn === 'opponent') { const o = opponentTurn(s); log.push(o.meter, o.event); s = o.state; } | |
| 78 | } | |
| 79 | return { s, log }; | |
| 80 | }; | |
| 81 | expect(play(9)).toEqual(play(9)); | |
| 82 | expect(start(9)).toEqual(start(9)); | |
| 83 | const meters = new Set(); | |
| 84 | for (let seed = 1; seed <= 20; seed++) meters.add(opponentTurn(theirs(seed)).meter); | |
| 85 | expect(meters.size).toBeGreaterThan(10); | |
| 86 | const o = opponentTurn(theirs(4)); | |
| 87 | expect(o.meter).toBeGreaterThanOrEqual(0); | |
| 88 | expect(o.meter).toBeLessThan(1); | |
| 89 | expect(o.state.turn).toBe('player'); | |
| 90 | }); | |
| 91 | ||
| 92 | test('the opponent can damage you, and its tangle passes the turn back', () => { | |
| 93 | let found = { hit: false, tangle: false }; | |
| 94 | for (let seed = 1; seed <= 60 && !(found.hit && found.tangle); seed++) { | |
| 95 | const o = opponentTurn(theirs(seed)); | |
| 96 | if (o.meter > TANGLE_ABOVE) { found.tangle = true; expect(o.state.playerHp).toBe(PLAYER_HP); expect(o.event).toBe('tangle'); } | |
| 97 | else { found.hit = true; expect(o.state.playerHp).toBe(PLAYER_HP - damage(o.meter)); } | |
| 98 | expect(o.state.turn).toBe('player'); | |
| 99 | } | |
| 100 | expect(found).toEqual({ hit: true, tangle: true }); | |
| 101 | }); | |
| 102 | ||
| 103 | test('a conker at zero hit points is cracked, the next rival steps up and your hit points carry over', () => { | |
| 104 | let s = start(2); | |
| 105 | s.playerHp = 7; | |
| 106 | while (s.opponentHp > FULL_DAMAGE) s = back(swing(s, SWEET).state); | |
| 107 | const r = swing(s, SWEET); | |
| 108 | expect(r.event).toBe('cracked'); | |
| 109 | expect(r.state.bout).toBe(1); | |
| 110 | expect(r.state.opponent).toBe(OPPONENTS[1].name); | |
| 111 | expect(r.state.opponentHp).toBe(OPPONENTS[1].hp); | |
| 112 | expect(r.state.playerHp).toBe(7); | |
| 113 | expect(r.state.turn).toBe('player'); | |
| 114 | expect(r.state.status).toBe('playing'); | |
| 115 | }); | |
| 116 | ||
| 117 | test('cracking a conker scores the stated amount plus a bonus for each hit point left', () => { | |
| 118 | const s = start(2); | |
| 119 | s.opponentHp = 1; | |
| 120 | s.playerHp = 5; | |
| 121 | const r = swing(s, OFF); | |
| 122 | expect(r.state.score).toBe(BOUT_SCORE + HP_BONUS * 5); | |
| 123 | const t = start(2); | |
| 124 | t.opponentHp = 1; | |
| 125 | t.playerHp = 11; | |
| 126 | expect(swing(t, OFF).state.score).toBe(BOUT_SCORE + HP_BONUS * 11); | |
| 127 | }); | |
| 128 | ||
| 129 | test('your conker cracked ends the game with a loss line, and nothing more is accepted', () => { | |
Dead setup: |
||
| 130 | let s, o; | |
| 131 | // find a seed whose reply is untangled | |
| 132 | for (let seed = 1; seed < 50; seed++) { s = theirs(seed); s.playerHp = 1; s.score = 40; o = opponentTurn(s); if (o.event === 'lost') break; } | |
| 133 | expect(o.event).toBe('lost'); | |
| 134 | expect([o.state.status, o.state.won, o.state.playerHp]).toEqual(['over', false, 0]); | |
| 135 | expect(o.state.line).toBe(`Your conker cracked against ${OPPONENTS[0].name}, you lose: 40 points`); | |
| 136 | expect(swing(o.state, SWEET).why).toBe('over'); | |
| 137 | expect(opponentTurn(o.state).why).toBe('over'); | |
| 138 | }); | |
| 139 | ||
| 140 | test('beating the last opponent ends the game with a win line reporting the total score', () => { | |
| 141 | let s = start(5); | |
| 142 | let total = 0; | |
| 143 | for (let b = 0; b < OPPONENTS.length; b++) { | |
| 144 | s.playerHp = 6; | |
| 145 | s.opponentHp = 1; | |
| 146 | total += BOUT_SCORE + HP_BONUS * 6; | |
| 147 | const r = swing(s, OFF); | |
| 148 | s = r.state; | |
| 149 | expect(r.event).toBe(b === OPPONENTS.length - 1 ? 'won' : 'cracked'); | |
| 150 | } | |
| 151 | expect([s.status, s.won, s.score]).toEqual(['over', true, total]); | |
| 152 | expect(s.line).toBe(`All ${OPPONENTS.length} rivals cracked, you win: ${total} points`); | |
| 153 | expect(swing(s, SWEET).why).toBe('over'); | |
| 154 | }); |
new above
offline
This comment does not describe
back, which just hands the turn to the player. Reword it.