/** * 윷놀이 computer player. Each legal move is scored by: catching enemies (worth their progress + the bonus throw), * steps of progress (shortcuts and finishing come out of the distance table), stacking a little, and the chance an * enemy lands on the piece with its next throw (estimated from the visible board and the throw odds). */ import type { BotStrategy } from '../bots/types'; import { answerRequests, best, pick, playable } from '../bots/policy'; import { FLAT_P, GOAL, THROW_STEPS, advance, defaultPrev, remainingDistance, throwProbabilities, type NodeId, type Throw } from './board'; import type { YutAction, YutLegalMove, YutView } from './index'; import { augBotAction } from './aug-bot'; const STEP = 3; // points per step of progress per piece const THROWS: Throw[] = ['backdo', 'do', 'gae', 'geol', 'yut', 'mo']; export const yutBot: BotStrategy = ({ view, legal, random }) => { const req = answerRequests(legal); if (req) return req; if (view.aug) { const a = augBotAction(view, legal, random); if (a) return a; } const moves = legal.filter((a) => a.type === 'MOVE') as Extract[]; if (!moves.length || !view.legalMoves) return pick(playable(legal), random); const o = view.options; const me = view.mySide; const dist = (n: NodeId | 'home' | typeof GOAL) => (n === GOAL ? 0 : remainingDistance(n, o.finishRule)); const home = dist('home'); const odds = throwProbabilities(FLAT_P[o.stickModel], o.backdo); const live = new Set(view.sides.filter((x) => x.id !== me && !x.out && x.finishedRank === null).map((x) => x.id)); const enemyStacks = view.stacks.filter((st) => live.has(st.side)); const enemyHome = view.sides.some((x) => live.has(x.id) && x.home > 0); // 지역 룰 backdoNoPiece = enter: an enemy with nothing on the board may put a piece on 날윷(19) with 빽도. const enemyBackdoEnter = o.backdo && o.backdoNoPiece === 'enter' && view.sides.some((x) => live.has(x.id) && x.home > 0 && x.onBoard === 0); /** Chance that some enemy can land on `node` with its next throw (bonus throws ignored). */ const risk = (node: NodeId): number => { let p = 0; for (const t of THROWS) { const hits = (t !== 'backdo' && enemyHome && advance('home', THROW_STEPS[t], o.finishRule).to === node) || (t === 'backdo' && enemyBackdoEnter && node === 19) || enemyStacks.some((st) => (t === 'backdo' ? defaultPrev(st.node) === node : advance(st.node, THROW_STEPS[t], o.finishRule).to === node)); if (hits) p += odds[t]; } return Math.min(1, p); }; /** Own pieces on `node` (several stacks may share it apart under `stacking = choose`). */ const mineAt = (node: NodeId) => view.stacks.filter((st) => st.side === me && st.node === node).reduce((a, st) => a + st.count, 0); /** What losing `n` own pieces standing on `node` would cost. */ const exposure = (node: NodeId, n: number) => STEP * n * (home - dist(node)) + 10; const score = (m: YutLegalMove): number => { let v = STEP * m.count * (dist(m.from) - dist(m.to)); if (m.from !== 'home') v += risk(m.from) * exposure(m.from, m.count); // leaving danger if (m.finishes) return v + 15 * m.count; const to = m.to as NodeId; if (m.captures) v += 30 + m.captures * (STEP * (home - dist(to)) + 10); // 카니발리즘: catching my own pieces throws their progress away. if (m.ownCaptures) v -= m.ownCaptures * (STEP * (home - dist(to)) + 10); // Standing apart still shares the node, so a catch there takes everything; only stacking earns the small bonus. const group = m.count + (m.stacks || m.stack === false ? mineAt(to) : 0); if (m.stacks) v += 6; // After a capture we throw again first, so the danger matters less. v -= (m.captures ? 0.5 : 1) * risk(to) * exposure(to, group); return v; }; const key = (x: { throwIndex: number; from: NodeId | 'home'; piece?: number; alt?: boolean; stack?: boolean }) => `${x.throwIndex}:${x.from}:${x.piece ?? ''}:${x.alt ? 1 : 0}:${x.stack === false ? 0 : 1}`; const byKey = new Map(view.legalMoves.map((m) => [key(m), m])); return best(moves, (a) => { const m = byKey.get(key(a)); return m ? score(m) : -1e9; }, random); };