diff --git a/engine/index.js b/engine/index.js index c61d94a..5e9e747 100644 --- a/engine/index.js +++ b/engine/index.js @@ -1149,15 +1149,61 @@ export function createAnalysisScenario(s, { player, seed }) { const knownRest = r.players[player]?.rest; const visibleAvailable = s.phase === "rest" && s.actor === player ? [...s._restAvailable] : null; - const pool = shuffle( - r, - REST.filter((t) => t !== knownRest && !visibleAvailable?.includes(t)), - ); - for (const p of r.players) { - p.dropped = false; - if (p.rest === "hidden") p.rest = pool.pop(); + const restKnowledge = + s._analysisRestKnowledge?.round === s.round + ? copy(s._analysisRestKnowledge.allowed) + : r.players.map(() => [...REST]); + for (const frame of s._frames) { + if ( + frame.round !== s.round || + frame.phase !== "rest" || + frame.actor !== player + ) + continue; + for (const [q, p] of frame.players.entries()) { + restKnowledge[q] = restKnowledge[q].filter((token) => + p.rest + ? !frame.restAvailable.includes(token) + : frame.restAvailable.includes(token), + ); + } } - r._restAvailable = visibleAvailable ?? pool; + if (knownRest) restKnowledge[player] = [knownRest]; + const seats = r.players.flatMap((p, q) => (p.rest ? [q] : [])); + const allocations = []; + function allocate(index, remaining, assigned) { + if (index === seats.length) { + if ( + visibleAvailable && + (remaining.length !== visibleAvailable.length || + remaining.some((token) => !visibleAvailable.includes(token))) + ) + return; + allocations.push({ remaining, assigned }); + return; + } + const seat = seats[index]; + for (const token of remaining) { + if (!restKnowledge[seat].includes(token)) continue; + allocate( + index + 1, + remaining.filter((t) => t !== token), + { ...assigned, [seat]: token }, + ); + } + } + allocate(0, [...REST], {}); + check( + allocations.length, + "No rest-token allocation matches the known information", + ); + const allocation = shuffle(r, allocations)[0]; + for (const [q, p] of r.players.entries()) { + p.dropped = false; + if (p.rest) p.rest = allocation.assigned[q]; + } + r._restAvailable = allocation.remaining; + r._analysisRestKnowledge = { round: s.round, allowed: restKnowledge }; saveFrame(r); return r; } diff --git a/test/analysis.test.js b/test/analysis.test.js index 9d93bff..f1bbd3b 100644 --- a/test/analysis.test.js +++ b/test/analysis.test.js @@ -53,3 +53,23 @@ test("observer rest tokens and re-sampling remain independent", () => { 4, ); }); + +test("rest tokens seen on an earlier personal turn constrain later scenarios and rerolls", () => { + const s = E.init(3, [], {}, "known-rest"); + s.players[0].rest = "coin"; + s.players[1].rest = "point"; + s.players[2].rest = "both"; + s._restAvailable = ["first"]; + const observed = structuredClone(s._frames[0]); + observed.phase = "rest"; + observed.actor = 0; + observed.players[1].rest = "point"; + observed.restAvailable = ["coin", "both", "first"]; + s._frames.push(observed); + for (const seed of ["one", "two", "three"]) { + const a = E.createAnalysisScenario(s, { player: 0, seed }); + assert.equal(a.players[1].rest, "point"); + const b = E.createAnalysisScenario(a, { player: 0, seed: "reroll" }); + assert.equal(b.players[1].rest, "point"); + } +});