Honor exchange inferences when sampling production cards

This commit is contained in:
coyotte508 2026-09-27 19:29:55 +02:00
commit c0aefa5d14
4 changed files with 83 additions and 2 deletions

View file

@ -91,3 +91,42 @@ test("revealed sealed bids constrain every simulated hand and survive resampling
}
}
});
test("completed exchanges retain the lowest-higher-card inference across resampling", async () => {
const source = await init(3, ["kicker"], {}, "0");
for (let i = 0; i < 15; i++) {
const active = currentPlayer(source);
await moveAI(source, Array.isArray(active) ? active[0]! : active!);
}
const entry = source.log.at(-1)!;
assert.equal(entry.type, "move");
if (entry.type !== "move" || entry.move.action !== "exchange") {
assert.fail("Expected exchange fixture");
}
assert.equal(entry.info!.exchangeGiven!.v, 3);
assert.equal(entry.info!.exchangeValue, 5);
const target = entry.move.target;
const scenario = createAnalysisScenario(source, { player: entry.player, seed: "known-exchange" });
for (let i = 0; i < 10; i++) {
const sample = createAnalysisScenario(scenario, { player: entry.player, seed: String(i) });
assert.ok(sample.players[target]!.hand.some((card) => card.t === "ore" && card.v === 3));
assert.ok(sample.players[target]!.hand.every((card) => card.t !== "ore" || card.v !== 4));
}
});
test("bounced exchanges constrain the target's retained resource values", async () => {
const source = await init(3, ["kicker"], {}, "0");
for (let i = 0; i < 14; i++) {
const active = currentPlayer(source);
await moveAI(source, Array.isArray(active) ? active[0]! : active!);
}
assert.equal(source.exchange!.seat, 2);
await move(source, { action: "exchange", card: 2, target: 0 }, 2);
const entry = source.log.at(-1)!;
assert.ok(entry.type === "move" && entry.info!.exchangeTake === -1);
const scenario = createAnalysisScenario(source, { player: 2, seed: "bounce" });
for (let i = 0; i < 5; i++) {
const sample = createAnalysisScenario(scenario, { player: 2, seed: String(i) });
assert.ok(sample.players[0]!.hand.every((card) => card.t !== "water" || card.v <= 9));
}
});

View file

@ -14,7 +14,7 @@ import type { GameState, LogEntry } from "./types.js";
* Rounds are applied from the "round" log entries (market + production draws),
* so a secret-stripped log replays into a secret-stripped state.
*/
export function replay(state: GameState, options?: { to?: number }): GameState {
export function replay(state: GameState, options?: { to?: number; trackKnowledge?: boolean }): GameState {
const init = state.log[0];
if (!init || init.type !== "init") {
throw new Error("log does not start with an init entry");
@ -50,6 +50,20 @@ export function replay(state: GameState, options?: { to?: number }): GameState {
if (received && (entry.info?.exchangeValue ?? -1) >= 0) {
received.v = entry.info!.exchangeValue!;
}
if (options?.trackKnowledge && given && given.v >= 0) {
const take = entry.info?.exchangeTake ?? -1;
for (const [index, card] of replayed.players[entry.move.target]!.hand.entries()) {
if (card.t !== given.t || card.m || index === take) {
continue;
}
card.analysisBounds ??= {};
if (take < 0) {
card.analysisBounds.max = Math.min(card.analysisBounds.max ?? Infinity, given.v);
} else if (received && received.v >= 0) {
(card.analysisBounds.excluded ??= []).push([given.v, received.v]);
}
}
}
}
setReplayExchangeTake(entry.info?.exchangeTake ?? -1);
applyMove(replayed, entry.move, entry.player);

View file

@ -52,6 +52,7 @@ export type Kicker = (typeof KICKERS)[number];
export type MarketCard = Upgrade | Kicker;
export interface ProductionCard {
analysisBounds?: { max?: number; excluded?: [number, number][] };
/** Resource type. */
t: Resource;
/** Credit value. -1 when hidden by stripSecret. */

View file

@ -444,11 +444,14 @@ export function createAnalysisScenario(
data: GameState,
{ player, seed }: { player?: number; seed: string }
): GameState {
if (!canLaunchAnalysisMode(data)) {
throw new Error("Finish the current exchange before starting analysis");
}
const observed = structuredClone(stripSecret(data, player));
const knowledge =
data.analysisKnowledge ??
(() => {
const replayed = observed.log[0]?.type === "init" ? replayCore(observed) : undefined;
const replayed = observed.log[0]?.type === "init" ? replayCore(observed, { trackKnowledge: true }) : undefined;
const bidFloors = observed.players.map(() => 0);
if (observed.auction) {
let start = observed.log.length;
@ -551,6 +554,20 @@ export function createAnalysisScenario(
for (const era of [1, 2, 3] as const) {
shuffle(copy, copy.kickerPiles[era]);
}
if (
knowledge.hands.some((hand, seat) =>
hand.some((card, index) => {
const value = copy.players[seat]!.hand[index]!.v;
const bounds = card.analysisBounds;
return (
(bounds?.max !== undefined && value > bounds.max) ||
bounds?.excluded?.some(([low, high]) => value > low && value < high)
);
})
)
) {
continue;
}
const a = copy.auction;
if (a) {
if (knowledge.bidFloors.some((amount, seat) => amount > maxBid(copy, seat, a.upgrade))) {
@ -590,3 +607,13 @@ export function createAnalysisScenario(
}
throw new Error("Cannot simulate a production allocation compatible with the current auction");
}
export function canLaunchAnalysisMode(data: GameState): boolean {
if (!data.exchange || !data.exchange.parked.length) {
return true;
}
return !data.log
.filter((e) => e.type === "move" && e.move.action === "exchange")
.slice(-data.exchange.parked.length)
.some((e) => e.type === "move" && (e.info?.exchangeTake ?? -1) >= 0);
}