// Shelf Pop rules (no DOM, unit-tested): shelves of goods, three slots in front and more rows behind. Move a front item // to a free slot; three of a kind on one shelf pop, and the row behind slides forward. Clear every shelf before the // clock runs out. Every board is checked solvable by a solver before anyone plays it. (c) 2026 CyberMax. All rights reserved. export const GOODS = [ { id: 'apple', e: '🍎', c: '#FF5A5A' }, { id: 'lemon', e: '🍋', c: '#FFD23F' }, { id: 'grape', e: '🍇', c: '#9B6BFF' }, { id: 'carrot', e: '🥕', c: '#FF9F1C' }, { id: 'donut', e: '🍩', c: '#FF8FC8' }, { id: 'milk', e: '🥛', c: '#7FC8FF' }, { id: 'melon', e: '🍉', c: '#4CCB7A' }, { id: 'cookie', e: '🍪', c: '#C98A5A' }, { id: 'egg', e: '🥚', c: '#F2E6CC' }, { id: 'berry', e: '🫐', c: '#4D6BFF' }, { id: 'cheese', e: '🧀', c: '#FFB627' }, { id: 'mint', e: '🍬', c: '#2FD3C0' }, ]; export function hash(str) { let h = 0x811c9dc5; for (let i = 0; i < str.length; i++) { h ^= str.charCodeAt(i); h = Math.imul(h, 0x01000193); } return h >>> 0; } export function rng(seed) { let a = typeof seed === 'string' ? hash(seed) : seed >>> 0; return () => { a = (a + 0x6d2b79f5) >>> 0; let t = a; t = Math.imul(t ^ (t >>> 15), t | 1); t ^= t + Math.imul(t ^ (t >>> 7), t | 61); return ((t ^ (t >>> 14)) >>> 0) / 4294967296; }; } /** Board size for a level (1, 2, … ) or the daily ('daily'). shelves × depth rows × 3 slots, `empty` free slots in front. */ export function specOf(level) { if (level === 'daily') return { shelves: 6, depth: 3, empty: 6, kinds: 9 }; const n = Math.max(1, level | 0); if (n === 1) return { shelves: 3, depth: 1, empty: 3, kinds: 2 }; if (n === 2) return { shelves: 4, depth: 1, empty: 3, kinds: 3 }; if (n === 3) return { shelves: 4, depth: 2, empty: 3, kinds: 5 }; // at most 6 shelves (2 columns x 3, big targets on a phone) and 3 rows deep: rounds stay around two minutes const shelves = Math.min(6, 4 + Math.floor((n - 4) / 3)), depth = Math.min(3, 2 + Math.floor((n - 4) / 5)); return { shelves, depth, empty: n % 2 ? 3 : 6, kinds: Math.min(GOODS.length, 4 + Math.floor(n / 2)) }; } export const triplesOf = (sp) => (sp.shelves * sp.depth * 3 - sp.empty) / 3; /** Seconds on the clock: generous early, tighter later. */ export const timeOf = (sp) => Math.round(20 + triplesOf(sp) * (sp.depth >= 3 ? 6.5 : 7.5)); // ------------------------------------------------------------------ board state: shelves[i] = rows (front first), row = [a,b,c] (null = free) export const clone = (b) => b.map((s) => s.map((r) => r.slice())); export const front = (b, i) => b[i][0] || [null, null, null]; export const isEmpty = (b) => b.every((s) => s.length === 0 || (s.length === 1 && s[0].every((x) => x === null))); const full3 = (r) => r[0] !== null && r[0] === r[1] && r[1] === r[2]; /** Apply one move in place: item at shelf a slot i → shelf c slot j (or the first free slot). Returns events or null if illegal. */ export function move(b, a, i, c, j = -1) { if (a === c) return null; const fa = b[a][0], fc = b[c][0] || (b[c][0] = [null, null, null]); if (!fa || fa[i] == null) return null; if (j < 0 || fc[j] != null) j = fc.indexOf(null); if (j < 0) return null; const item = fa[i]; fa[i] = null; fc[j] = item; const ev = { item, from: [a, i], to: [c, j], pop: null, slid: [] }; if (full3(fc)) { ev.pop = { shelf: c, item }; b[c][0] = [null, null, null]; } for (const s of [a, c]) settle(b, s, ev); return ev; } function settle(b, s, ev) { const sh = b[s]; while (sh.length > 1 && sh[0].every((x) => x === null)) { sh.shift(); ev.slid.push(s); if (full3(sh[0])) { ev.pop2 = (ev.pop2 || []).concat({ shelf: s, item: sh[0][0] }); sh[0] = [null, null, null]; } } } /** All legal moves (front item → a shelf with a free front slot). */ export function moves(b) { const out = []; for (let a = 0; a < b.length; a++) { const fa = b[a][0]; if (!fa) continue; for (let i = 0; i < 3; i++) { if (fa[i] == null) continue; for (let c = 0; c < b.length; c++) if (c !== a && (!b[c][0] || b[c][0].includes(null))) out.push([a, i, c]); } } return out; } export const stuck = (b) => !isEmpty(b) && moves(b).length === 0; const key = (b) => b.map((s) => s.map((r) => r.map((x) => (x == null ? '.' : x.toString(36))).sort().join('')).join('/')).sort().join('|'); /** Depth-first solver with a move order a person would use (finish a triple, then join a pair, then free a row). */ export function solve(b0, limit = 40000) { const seen = new Set(); let nodes = 0; const score = (b, [a, i, c]) => { const it = b[a][0][i], fc = b[c][0] || [], same = fc.filter((x) => x === it).length, left = b[a][0].filter((x) => x != null).length; return same * 10 + (left === 1 ? 3 : 0) - (fc.every((x) => x == null) ? 2 : 0); }; const path = [], items = b0.flat(2).filter((x) => x != null).length, maxDepth = items * 2 + 6; const go = (b, last) => { if (isEmpty(b)) return true; if (++nodes > limit || path.length > maxDepth) return false; const k = key(b); if (seen.has(k)) return false; seen.add(k); const ms = moves(b).filter((m) => !(last && m[0] === last[2] && m[2] === last[0])).map((m) => [score(b, m), m]).sort((x, y) => y[0] - x[0]); for (const [, m] of ms) { const n = clone(b); move(n, m[0], m[1], m[2]); path.push(m); if (go(n, m)) return true; path.pop(); } return false; }; const ok = go(clone(b0), null); return { ok, nodes, path: ok ? path.slice() : null }; } /** A solvable board for a seed: triples dealt into the rows, free slots only in front, no ready-made triple anywhere. */ export function makeBoard(seed, sp) { for (let attempt = 0; attempt < 400; attempt++) { const r = rng(`${seed}#${attempt}`), T = triplesOf(sp); const pool = []; for (let t = 0; t < T; t++) { const k = t % sp.kinds; pool.push(k, k, k); } for (let i = pool.length - 1; i > 0; i--) { const j = Math.floor(r() * (i + 1)); [pool[i], pool[j]] = [pool[j], pool[i]]; } const holes = new Set(); while (holes.size < sp.empty) holes.add(Math.floor(r() * sp.shelves) * 3 + Math.floor(r() * 3)); const b = []; let p = 0, bad = false; for (let s = 0; s < sp.shelves; s++) { const rows = []; for (let d = 0; d < sp.depth; d++) rows.push([0, 1, 2].map((k) => (d === 0 && holes.has(s * 3 + k) ? null : pool[p++]))); if (rows.some((row) => full3(row))) bad = true; while (rows.length > 1 && rows[0].every((x) => x === null)) rows.shift(); b.push(rows); } if (bad || p !== pool.length) continue; if (moves(b).length < 2) continue; const s = solve(b, 5000); if (s.ok) return { board: b, attempt, solution: s.path }; } throw new Error('no solvable board'); } /** A whole round: the board, the clock, scoring. */ export function makeGame(seed, level) { const sp = specOf(level), { board, solution } = makeBoard(seed, sp); const g = { seed, level, sp, board, total: triplesOf(sp), popped: 0, moves: 0, time: timeOf(sp), t: 0, score: 0, combo: 0, bestCombo: 0, lastPop: -99, state: 'run', solution, popsBy: {} }; g.tick = (dt) => { if (g.state !== 'run') return; g.t += dt; if (g.t >= g.time) { g.t = g.time; g.state = 'over'; } }; g.left = () => Math.max(0, g.time - g.t); g.play = (a, i, c, j = -1) => { if (g.state !== 'run') return null; const ev = move(g.board, a, i, c, j); if (!ev) return null; g.moves++; for (const p of [ev.pop, ...(ev.pop2 || [])].filter(Boolean)) { g.combo = g.t - g.lastPop <= 4 ? g.combo + 1 : 1; g.lastPop = g.t; g.bestCombo = Math.max(g.bestCombo, g.combo); const pts = 30 * Math.min(5, g.combo); g.score += pts; p.pts = pts; p.combo = g.combo; g.popped++; g.popsBy[p.item] = (g.popsBy[p.item] || 0) + 1; } if (isEmpty(g.board)) { g.state = 'done'; g.bonus = Math.round(g.left()) * 5; g.score += g.bonus; } else if (stuck(g.board)) g.state = 'stuck'; return ev; }; g.stars = () => (g.state !== 'done' ? 0 : g.left() >= g.time * 0.4 ? 3 : g.left() >= g.time * 0.2 ? 2 : 1); return g; } export const fmt = (s) => `${Math.floor(s / 60)}:${String(Math.floor(s % 60)).padStart(2, '0')}`; /** Share line: the goods you popped, in order of the shelf, and the time. */ export function shareText(g, label) { const ems = Object.keys(g.popsBy).map(Number).sort((a, b) => a - b).map((k) => GOODS[k].e).join(''); const head = g.state === 'done' ? `cleared in ${fmt(g.t)} ${'⭐'.repeat(g.stars())}` : `${g.popped}/${g.total} popped`; return `Shelf Pop ${label} 🛒 ${g.score.toLocaleString('en-US')}\n${ems || '🛒'} ${head}\nbest combo x${g.bestCombo}`; }