Domaine et adaptateur : niveaux, analyses et chance ; cache vidé à chaque calcul

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Pierre MARTIN
2026-11-03 14:59:07 +01:00
co-authored by Claude Opus 5.5
parent 2f6eb86972
commit 23f4d47fcf
9 changed files with 476 additions and 4 deletions
+132 -2
View File
@@ -12,12 +12,20 @@ import type {
Board,
BoardResult,
BoardSide,
CubeAction,
CubeAnalysis,
CubeDecision,
Engine,
EngineCube,
EvaluatedMove,
HalfMovesCheck,
Level,
Luck,
LuckRating,
Move,
MoveAnalysis,
Ply,
Skill,
} from "../domain/game/engine.js";
import { formatHalfMoves, type HalfMove, type PositionId } from "../domain/game/position.js";
import type { Roll } from "../domain/game/roll.js";
@@ -32,6 +40,16 @@ interface CliMove {
position_id: string;
half_moves: [number, number][];
}
interface CliEvaluatedMove extends CliMove {
ply: number;
equity: number;
}
type CliSkill = "none" | "doubtful" | "bad" | "very_bad";
interface CliCubeDecision {
available: boolean;
double: boolean;
take: boolean;
}
interface CliSide {
points: number[];
bar: number;
@@ -86,8 +104,108 @@ export function createWasmEngine(run: RunCommand): Engine {
ply: Ply,
): Promise<CubeDecision> {
const args = ["--ply", String(ply), "cube", position, await matchId(cube)];
const answer = (await run(args)) as CubeDecision;
return { available: answer.available, double: answer.double, take: answer.take };
return toCubeDecision((await run(args)) as CliCubeDecision);
},
async levelMove(position: PositionId, cube: EngineCube, roll: Roll, level: Level) {
const args = ["--level", level, "bestmove", position, await matchId(cube)];
const answer = (await run([...args, diceText(roll)])) as { best_move: CliMove | null };
return answer.best_move === null ? null : toMove(answer.best_move);
},
async levelCubeDecision(
position: PositionId,
cube: EngineCube,
level: Level,
): Promise<CubeDecision> {
const args = ["--level", level, "cube", position, await matchId(cube)];
return toCubeDecision((await run(args)) as CliCubeDecision);
},
async analyseMove(
position: PositionId,
cube: EngineCube,
roll: Roll,
played: PositionId,
ply: Ply,
): Promise<MoveAnalysis | null> {
const args = ["--ply", String(ply), "analysemove", position, await matchId(cube)];
const answer = (await run([...args, diceText(roll), played])) as {
move_count: number;
rank: number | null;
played: CliEvaluatedMove | null;
best: CliEvaluatedMove | null;
equity_loss: number;
skill: CliSkill;
};
if (answer.rank === null || answer.played === null || answer.best === null) {
return null;
}
return {
moveCount: answer.move_count,
rank: answer.rank,
played: toEvaluatedMove(answer.played),
best: toEvaluatedMove(answer.best),
equityLoss: answer.equity_loss,
skill: toSkill(answer.skill),
};
},
async analyseCube(
position: PositionId,
cube: EngineCube,
action: CubeAction,
ply: Ply,
): Promise<CubeAnalysis> {
const cliAction = {
noDouble: "no_double",
double: "double",
take: "take",
pass: "pass",
}[action];
const args = ["--ply", String(ply), "analysecube", position, await matchId(cube)];
const answer = (await run([...args, cliAction])) as {
analysed: boolean;
cube: {
no_double: number;
double_take: number;
double_pass: number;
optimal: number;
} | null;
equity_loss: number;
skill: CliSkill;
};
return {
analysed: answer.analysed,
equities:
answer.cube === null
? null
: {
noDouble: answer.cube.no_double,
doubleTake: answer.cube.double_take,
doublePass: answer.cube.double_pass,
optimal: answer.cube.optimal,
},
equityLoss: answer.equity_loss,
skill: toSkill(answer.skill),
};
},
async luck(position: PositionId, cube: EngineCube, roll: Roll): Promise<Luck> {
const args = ["luck", position, await matchId(cube), diceText(roll)];
const answer = (await run(args)) as { luck: number; rating: string };
const ratings: Record<string, LuckRating> = {
very_unlucky: "veryUnlucky",
unlucky: "unlucky",
none: "none",
lucky: "lucky",
very_lucky: "veryLucky",
};
const rating = ratings[answer.rating];
if (rating === undefined) {
throw new Error(`chance inconnue : ${answer.rating}`);
}
return { value: answer.luck, rating };
},
async checkHalfMoves(
@@ -139,6 +257,18 @@ function toMove(move: CliMove): Move {
};
}
function toEvaluatedMove(move: CliEvaluatedMove): EvaluatedMove {
return { ...toMove(move), equity: move.equity, ply: move.ply };
}
function toCubeDecision(answer: CliCubeDecision): CubeDecision {
return { available: answer.available, double: answer.double, take: answer.take };
}
function toSkill(skill: CliSkill): Skill {
return skill === "very_bad" ? "veryBad" : skill;
}
function toSide(side: CliSide): BoardSide {
return { points: side.points, bar: side.bar, off: side.off, pipCount: side.pip_count };
}