Retain observed rest-token constraints when resampling

This commit is contained in:
coyotte508 2026-09-27 19:27:24 +02:00
commit 655959d478
2 changed files with 72 additions and 6 deletions

View file

@ -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;
}

View file

@ -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");
}
});