Public: anyone can read this; the crew can change it.
Migrated from github.com/amirmushichge/pixel-plays
Market Barrow rules: a tested `market-barrow/barrow.js` that buys and sells a barrow of goods across village stalls over ten days, from plain data #172
Check · Review · Land · Live done — Landed and live in production.
ledger/pixel-plays:ledger/168-market-barrow-rules-a-tested-market-barr → main 2 commits fork
2 files
changed +187
−0
market-barrow/barrow.js added +84
| @@ -0,0 +1,84 @@ | ||
| 1 | 'use strict'; | |
| 2 | // The Market Barrow rules: push a barrow between four village stalls for ten days, buy where a good is cheap, sell where it is dear. | |
| 3 | // Plain data in, plain data out, no DOM, no timers, no Math.random: prices come from the seed. A refused move returns the very | |
| 4 | // same state and says why. The page draws what this decides. | |
| 5 | // | |
| 6 | // Days run 1..10 and you may trade on each. Travelling costs one day; travelling on day 10 ends the game. Coins are whole | |
| 7 | // numbers. Goods still in the barrow when the game ends count for nothing: only coins are scored. | |
| 8 | (function (root) { | |
| 9 | const STALLS = ['Mill', 'Harbour', 'Orchard', 'Forge']; | |
| 10 | const GOODS = ['wool', 'salt', 'apples', 'iron']; | |
| 11 | const DAYS = 10, COINS = 100, CAPACITY = 10, TARGET = 150; | |
| 12 | const BASE = { wool: 12, salt: 8, apples: 5, iron: 20 }; // a good's usual price in coins; each stall and day swings it | |
| 13 | ||
| 14 | const finite = v => (Number.isFinite(v) ? v : 0); | |
| 15 | // a repeatable number in [0, 1) for this seed and slot | |
| 16 | function noise(seed, n) { | |
| 17 | let h = (Math.imul(finite(seed) | 0, 374761393) + Math.imul(n | 0, 668265263)) | 0; | |
| 18 | h = Math.imul(h ^ (h >>> 13), 1274126177); | |
| 19 | return ((h ^ (h >>> 16)) >>> 0) / 4294967296; | |
| 20 | } | |
| 21 | // the price of a good at a stall on a day (1..DAYS): between half and one and a half times the usual, at least 1 coin | |
| 22 | function price(seed, stall, good, day) { | |
| 23 | const g = GOODS.indexOf(good), s = STALLS.indexOf(stall); | |
| 24 | if (g < 0 || s < 0) return 0; | |
| 25 | const slot = 1 + ((g * STALLS.length + s) * (DAYS + 1) + Math.min(Math.max(day | 0, 0), DAYS)); | |
| 26 | return Math.max(1, Math.round(BASE[good] * (0.5 + noise(finite(seed) >>> 0, slot)))); | |
| 27 | } | |
| 28 | const prices = (seed, stall, day) => Object.fromEntries(GOODS.map(g => [g, price(seed, stall, g, day)])); | |
| 29 | const load = s => GOODS.reduce((n, g) => n + s.cargo[g], 0); | |
| 30 | const copy = s => Object.assign({}, s, { cargo: Object.assign({}, s.cargo), prices: Object.assign({}, s.prices) }); | |
| 31 | ||
| 32 | function refresh(s) { | |
| 33 | s.prices = prices(s.seed, s.stall, s.day); | |
| 34 | if (s.status === 'over') s.best = Math.max(s.best, s.coins); | |
| 35 | s.line = s.status !== 'over' ? `Day ${s.day} of ${DAYS} at the ${s.stall} stall: ${s.coins} coins, barrow ${load(s)}/${s.capacity}` | |
| 36 | : s.result === 'won' ? `Won with ${s.coins} coins (target ${s.target})` | |
| 37 | : `Lost with ${s.coins} coins (target ${s.target})`; | |
| 38 | return s; | |
| 39 | } | |
| 40 | const reject = (state, why) => ({ ok: false, why, state }); | |
| 41 | const accept = state => ({ ok: true, why: '', state: refresh(state) }); | |
| 42 | ||
| 43 | function start(seed = 1, best = 0) { | |
| 44 | seed = finite(seed) >>> 0; | |
| 45 | const cargo = Object.fromEntries(GOODS.map(g => [g, 0])); | |
| 46 | return refresh({ seed, day: 1, days: DAYS, stall: STALLS[0], stalls: STALLS.slice(), goods: GOODS.slice(), coins: COINS, | |
| 47 | start: COINS, capacity: CAPACITY, target: TARGET, cargo, prices: {}, status: 'playing', result: '', best: Math.max(0, finite(best) | 0), line: '' }); | |
| 48 | } | |
| 49 | ||
| 50 | const count = n => (Number.isInteger(n) && n > 0 ? n : 0); | |
| 51 | function buy(state, good, n) { | |
| 52 | if (state.status !== 'playing') return reject(state, 'game-over'); | |
| 53 | if (!GOODS.includes(good)) return reject(state, 'unknown-good'); | |
| 54 | if (!count(n)) return reject(state, 'bad-quantity'); | |
| 55 | if (load(state) + n > state.capacity) return reject(state, 'barrow-full'); | |
| 56 | const cost = state.prices[good] * n; | |
| 57 | if (cost > state.coins) return reject(state, 'too-poor'); | |
| 58 | const s = copy(state); | |
| 59 | s.coins -= cost; s.cargo[good] += n; | |
| 60 | return accept(s); | |
| 61 | } | |
| 62 | function sell(state, good, n) { | |
| 63 | if (state.status !== 'playing') return reject(state, 'game-over'); | |
| 64 | if (!GOODS.includes(good)) return reject(state, 'unknown-good'); | |
| 65 | if (!count(n)) return reject(state, 'bad-quantity'); | |
| 66 | if (n > state.cargo[good]) return reject(state, 'not-carried'); | |
| 67 | const s = copy(state); | |
| 68 | s.coins += state.prices[good] * n; s.cargo[good] -= n; | |
| 69 | return accept(s); | |
| 70 | } | |
| 71 | function travel(state, stall) { | |
| 72 | if (state.status !== 'playing') return reject(state, 'game-over'); | |
| 73 | if (!STALLS.includes(stall)) return reject(state, 'unknown-stall'); | |
| 74 | if (stall === state.stall) return reject(state, 'already-here'); | |
| 75 | const s = copy(state); | |
| 76 | s.stall = stall; | |
| 77 | if (s.day >= DAYS) { s.status = 'over'; s.result = s.coins >= s.target ? 'won' : 'lost'; } | |
| 78 | else s.day += 1; | |
| 79 | return accept(s); | |
| 80 | } | |
| 81 | ||
| 82 | const api = { STALLS, GOODS, DAYS, COINS, CAPACITY, TARGET, price, prices, load, start, buy, sell, travel }; | |
| 83 | if (typeof module !== 'undefined') module.exports = api; else root.MarketBarrow = api; | |
| 84 | })(typeof window !== 'undefined' ? window : globalThis); |
market-barrow/barrow.test.mjs added +103
| @@ -0,0 +1,103 @@ | ||
| 1 | import { test, expect } from 'bun:test'; | |
| 2 | import { STALLS, GOODS, DAYS, COINS, CAPACITY, TARGET, price, load, start, buy, sell, travel } from './barrow.js'; | |
| 3 | ||
| 4 | const ok = r => { expect(r.why).toBe(''); expect(r.ok).toBe(true); return r.state; }; | |
| 5 | // walk to the other stalls in turn, n days | |
| 6 | const walk = (s, n) => { for (let i = 0; i < n; i++) s = ok(travel(s, STALLS[(STALLS.indexOf(s.stall) + 1) % STALLS.length])); return s; }; | |
| 7 | ||
| 8 | test('a game starts with stated coins, barrow, four stalls, three or more goods and a price for each', () => { | |
| 9 | const s = start(1); | |
| 10 | expect(s.coins).toBe(COINS); | |
| 11 | expect(s.capacity).toBe(CAPACITY); | |
| 12 | expect(s.stalls).toHaveLength(4); | |
| 13 | expect(s.goods.length).toBeGreaterThanOrEqual(3); | |
| 14 | expect(s.day).toBe(1); | |
| 15 | for (const g of GOODS) expect(s.prices[g]).toBeGreaterThan(0); | |
| 16 | }); | |
| 17 | ||
| 18 | test('the same seed gives the same prices, and they change from day to day', () => { | |
| 19 | const grid = seed => STALLS.flatMap(st => GOODS.flatMap(g => Array.from({ length: DAYS }, (_, d) => price(seed, st, g, d + 1)))); | |
| 20 | expect(grid(7)).toEqual(grid(7)); | |
| 21 | expect(grid(7)).not.toEqual(grid(8)); | |
| 22 | for (const g of GOODS) for (const st of STALLS) expect(new Set(Array.from({ length: DAYS }, (_, d) => price(7, st, g, d + 1))).size).toBeGreaterThan(1); | |
| 23 | expect(start(7).prices).toEqual(start(7).prices); | |
| 24 | }); | |
| 25 | ||
| 26 | test('buying spends coins and fills the barrow', () => { | |
| 27 | const s0 = start(1), p = s0.prices.wool; | |
| 28 | const s = ok(buy(s0, 'wool', 3)); | |
| 29 | expect(s.coins).toBe(COINS - 3 * p); | |
| 30 | expect(s.cargo.wool).toBe(3); | |
| 31 | expect(s0.cargo.wool).toBe(0); | |
| 32 | }); | |
| 33 | ||
| 34 | test('buying more than you can afford is refused and changes nothing', () => { | |
| 35 | const rich = Object.assign(start(1), { coins: 1 }); | |
| 36 | const r2 = buy(rich, 'iron', 1); | |
| 37 | expect(r2.ok).toBe(false); expect(r2.why).toBe('too-poor'); expect(r2.state).toBe(rich); | |
| 38 | }); | |
| 39 | ||
| 40 | test('carrying more than the barrow holds is refused', () => { | |
| 41 | const s = Object.assign(start(1), { coins: 100000 }); | |
| 42 | const full = ok(buy(s, 'salt', CAPACITY)); | |
| 43 | const r = buy(full, 'salt', 1); | |
| 44 | expect(r.ok).toBe(false); expect(r.why).toBe('barrow-full'); expect(r.state).toBe(full); | |
| 45 | expect(buy(s, 'salt', CAPACITY + 1).why).toBe('barrow-full'); | |
| 46 | }); | |
| 47 | ||
| 48 | test('selling what you do not carry is refused', () => { | |
| 49 | const s = ok(buy(start(1), 'apples', 2)); | |
| 50 | const r = sell(s, 'apples', 3); | |
| 51 | expect(r.ok).toBe(false); expect(r.why).toBe('not-carried'); expect(r.state).toBe(s); | |
| 52 | expect(sell(s, 'wool', 1).why).toBe('not-carried'); | |
| 53 | }); | |
| 54 | ||
| 55 | test('bad goods and quantities are refused', () => { | |
| 56 | const s = start(1); | |
| 57 | for (const n of [0, -1, 1.5, NaN, '2']) { expect(buy(s, 'wool', n).why).toBe('bad-quantity'); expect(sell(s, 'wool', n).why).toBe('bad-quantity'); } | |
| 58 | expect(buy(s, 'gold', 1).why).toBe('unknown-good'); | |
| 59 | }); | |
| 60 | ||
| 61 | test('travelling moves to the stall and costs one day; staying put is refused', () => { | |
| 62 | const s = start(1), to = STALLS[1]; | |
| 63 | const t = ok(travel(s, to)); | |
| 64 | expect(t.stall).toBe(to); | |
| 65 | expect(t.day).toBe(2); | |
| 66 | expect(t.prices.wool).toBe(price(1, to, 'wool', 2)); | |
| 67 | const r = travel(t, to); | |
| 68 | expect(r.ok).toBe(false); expect(r.why).toBe('already-here'); expect(r.state).toBe(t); expect(t.day).toBe(2); | |
| 69 | expect(travel(s, 'Moon').why).toBe('unknown-stall'); | |
| 70 | }); | |
| 71 | ||
| 72 | test('a hand-played profit: buy cheap, carry, sell dear', () => { | |
| 73 | const seed = 1; | |
| 74 | // find the best good to carry from the first stall to the second | |
| 75 | const a = STALLS[0], b = STALLS[1]; | |
| 76 | const g = GOODS.reduce((x, y) => (price(seed, b, y, 2) - price(seed, a, y, 1) > price(seed, b, x, 2) - price(seed, a, x, 1) ? y : x)); | |
| 77 | const gain = price(seed, b, g, 2) - price(seed, a, g, 1); | |
| 78 | expect(gain).toBeGreaterThan(0); | |
| 79 | let s = ok(buy(start(seed), g, 5)); | |
| 80 | s = ok(travel(s, b)); | |
| 81 | s = ok(sell(s, g, 5)); | |
| 82 | expect(s.coins).toBe(COINS + 5 * gain); | |
| 83 | expect(load(s)).toBe(0); | |
| 84 | }); | |
| 85 | ||
| 86 | test('the game ends after the tenth day, and goods left in the barrow count for nothing', () => { | |
| 87 | let s = ok(buy(start(1), 'apples', 1)); | |
| 88 | s = walk(s, DAYS - 1); | |
| 89 | expect(s.day).toBe(DAYS); | |
| 90 | expect(s.status).toBe('playing'); | |
| 91 | s = walk(s, 1); | |
| 92 | expect(s.status).toBe('over'); | |
| 93 | expect(s.coins).toBe(COINS - start(1).prices.apples); | |
| 94 | for (const r of [buy(s, 'wool', 1), sell(s, 'apples', 1), travel(s, STALLS[0])]) { expect(r.ok).toBe(false); expect(r.why).toBe('game-over'); } | |
| 95 | }); | |
| 96 | ||
| 97 | test('at or above the target is a win, below it a loss, and the best is reported', () => { | |
| 98 | const end = coins => walk(Object.assign(start(1, 40), { coins, day: DAYS }), 1); | |
| 99 | const won = end(TARGET), lost = end(TARGET - 1); | |
| 100 | expect(won.result).toBe('won'); expect(won.line).toContain('Won'); expect(won.best).toBe(TARGET); | |
| 101 | expect(lost.result).toBe('lost'); expect(lost.line).toContain('Lost'); expect(lost.best).toBe(TARGET - 1); | |
| 102 | expect(end(10).best).toBe(40); | |
| 103 | }); |
new above
offline