Files
joke-app/packages/games/src/yut/bot.ts
EJClaw daac1982fa 증강 윷놀이: 증강 아이콘 101개, 사이트 추가 증강 20개 더(모두 30개), 추가 증강 기본 꺼짐
- 증강마다 48×48 SVG 아이콘(aug-icons/): 타일 색 = 종류(규칙·즉시·사용·승리 조건), 그림 = 증강.
  선택 카드·사람 카드 칩·쓰기 버튼·승리 팝업·방 설정 목록·규칙 보기에 표시. 고양이가 된 증강은 회색 고양이.
  모아 보기: apps/web/scripts/aug-icon-sheet.tsx
- 새 증강 20개: 다시 던지기, 곱빼기, 올인, 저주 인형, 얼음!, 자석, 뒤로 돌아, 꼴찌의 반란, 텃세, 따라쟁이,
  청개구리, 사냥꾼, 줍줍, 도장 깨기, 스타트 대시, 방공호, 벼락, 블랙홀, 로켓 발사, 최후의 일격 (규칙·컴퓨터·테스트·문서)
- augOff 기본값 = 이 사이트 추가 증강 30개(방에서 [추가 증강 켜기]로 켬)
- 카니발리즘: 내 말 잡기를 '잡기'로 세지 않음(ownCaptures) → 개돼지와 함께일 때 컴퓨터·시간 초과가
  자기 말을 잡고 다시 던지기를 끝없이 반복하던 문제 수정
- 방 설정 탭이 방 정보 갱신으로 다시 그려질 때 [윷놀이 설정]으로 되돌아가던 문제 수정

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-20 08:17:47 +09:00

76 lines
4.2 KiB
TypeScript

/**
* 윷놀이 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<YutView, YutAction> = ({ 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<YutAction, { type: 'MOVE' }>[];
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);
};