/** * 증강 윷놀이 computer decisions: which augment to pick, how to answer prompts, and when to use an ability. * Returns null to fall back to the plain 윷놀이 bot (throwing and moving). */ import { AUGS, type AugId } from './augments'; import { NODE_POS, remainingDistance, type NodeId } from './board'; import type { YutAction, YutView } from './index'; /** Rough worth of each augment for a computer (higher = pick first). */ const VALUE: Partial> = { comeback: 9, notme: 8, combo: 7, sleight: 7, tiger: 7, vanguard: 7, thief: 6, bigred: 6, truck: 6, baduk: 6, doberman: 6, shield: 6, friendshield: 6, dokkaebi: 6, marathon: 6, speedrun: 6, piggyback: 6, summon: 6, coup: 6, murder: 6, throwyut: 6, peace: 5, catlover: 5, turtle: 5, revenge: 5, fisherman: 5, kim: 5, party: 5, burden: 5, equiv: 5, triple7: 5, wait: 5, duel: 5, doggy: 5, score: 5, twins: 5, swap: 5, shortcut: 5, cannon: 5, poison: 4, jajinmori: 4, pawn: 4, queen: 4, bear: 4, treason: 4, echo: 4, domain: 4, getoff: 4, moordo: 4, leech: 4, illusion: 4, alkkagi: 4, four: 4, mine: 4, teleport: 4, cannibal: 3, alone: 3, nofriends: 3, plan: 3, necro: 3, cancel: 3, whoami: 3, roulette: 3, fold: 3, excess: 3, dual: 3, fog: 3, sniper: 3, ox: 3, detective: 3, delivery: 3, storm: 3, sacrifice: 3, moonwalk: 3, lastdance: 1, chosen: 2, objection: 2, timetravel: 2, cat: 1, }; type A = YutAction; export function augBotAction(view: YutView, legal: A[], random: () => number): A | null { const aug = view.aug!; const me = view.mySide; const has = (t: string) => legal.filter((a) => a.type === t); // ---- picking an augment const picks = has('AUG_PICK') as Extract[]; if (picks.length) { const progress = me === null ? 0 : standing(view, me); const worth = (id: AugId) => { let v = VALUE[id] ?? 3; if (id === 'bronze') v = progress >= 0.6 ? 2 : 8; // a sure 3rd place is good when behind if (id === 'cancel') v = (aug.sides.some((x) => x.id !== me && x.augs.some((o) => ['four', 'ox', 'score', 'catlover', 'coup', 'summon', 'queen', 'kim', 'bear', 'domain', 'marathon'].includes(o.id))) ? 7 : 2); return v + random() * 1.5; }; const best = picks.reduce((x, y) => (worth(y.aug) > worth(x.aug) ? y : x)); if (best.aug === 'detective' || best.aug === 'delivery') { const same = picks.filter((p) => p.aug === best.aug); return same.reduce((x, y) => ((VALUE[y.target!] ?? 3) > (VALUE[x.target!] ?? 3) ? y : x)); } return best; } // ---- answering a prompt const answers = has('AUG_ANSWER') as Extract[]; if (answers.length) { const pr = aug.prompt; if (!pr) return answers[0]!; const leader = [...view.sides].filter((x) => x.id !== me && !x.out && x.finishedRank === null).sort((p, q) => q.done - p.done || p.remaining - q.remaining)[0]?.id; const trailer = [...view.sides].filter((x) => x.id !== me && !x.out && x.finishedRank === null).sort((p, q) => p.done - q.done || q.remaining - p.remaining)[0]?.id; const byChoice = (v: number | undefined) => answers.find((a) => a.choice === v) ?? answers[Math.floor(random() * answers.length)]!; switch (pr.kind) { case 'leech': return byChoice(leader); case 'vote': return byChoice(leader); case 'duel': return byChoice(trailer); case 'roulette': return answers[random() < 0.5 ? 0 : answers.length - 1]!; case 'plan': { const order = [...view.sides].sort((p, q) => (p.finishedRank ?? 99) - (q.finishedRank ?? 99) || +p.out - +q.out || q.done - p.done || p.remaining - q.remaining).map((x) => x.id); return { type: 'AUG_ANSWER', choice: order } as A; } case 'illusion': case 'thief': { const opts = pr.choices ?? []; const val = (label: string) => { const name = label.split(' · ')[1] ?? ''; const id = (Object.keys(VALUE) as AugId[]).find((k) => AUG_NAME[k] === name); return id ? (VALUE[id] ?? 3) : 3; }; const top = opts.reduce((x, y) => (val(y.label) > val(x.label) ? y : x), opts[0]!); return byChoice(top?.value); } default: return answers[Math.floor(random() * answers.length)]!; } } // ---- my turn: abilities before throwing / moving const uses = has('AUG_USE') as Extract[]; const opt = (a: Extract) => aug.uses.find((u) => u.uid === a.uid && u.action.piece === a.piece && u.action.node === a.node && u.action.node2 === a.node2 && u.action.target === a.target && u.action.result === a.result); const idOf = (a: Extract) => aug.sides.find((x) => x.id === me)?.augs.find((o) => o.uid === a.uid)?.id; let bestUse: { a: A; v: number } | null = null; const consider = (a: A, v: number) => { if (v > 0 && (!bestUse || v > bestUse.v)) bestUse = { a, v }; }; const leaderSide = [...view.sides].filter((x) => x.id !== me && !x.out && x.finishedRank === null).sort((p, q) => q.done - p.done || p.remaining - q.remaining)[0]?.id; for (const u of uses) { const id = idOf(u); const o = opt(u); if (!id) continue; switch (id) { case 'combo': consider(u, (o?.kills ?? 0) >= 3 ? 10 + (o?.kills ?? 0) : 0); break; case 'truck': case 'baduk': consider(u, (o?.kills ?? 0) > 0 ? 8 + (o?.kills ?? 0) : 0); break; case 'sniper': consider(u, (o?.kills ?? 0) >= 2 ? 3 + (o?.kills ?? 0) : 0); break; case 'alkkagi': { if (u.angle === undefined) break; // aim at the nearest enemy stack from this stack if (o?.at === undefined || o.at === 'home') break; const [mx, my] = NODE_POS[o.at]!; const enemy = view.stacks.filter((st) => st.side !== me && st.side >= 0).map((st) => ({ st, d: Math.hypot(NODE_POS[st.node]![0] - mx, NODE_POS[st.node]![1] - my) })).sort((p, q) => p.d - q.d)[0]; if (!enemy || enemy.d > 35) break; const [ex, ey] = NODE_POS[enemy.st.node]!; const angle = Math.round(((Math.atan2(ey - my, ex - mx) * 180) / Math.PI + 360) % 360) % 360; consider({ ...u, angle, power: 80 } as A, 7); break; } case 'doberman': { const n = Number(/(\d+)/.exec(o?.label ?? '')?.[1] ?? 0); consider(u, n >= 4 ? 6 : 0); break; } case 'sleight': consider(u, u.result === 'mo' ? 6 : u.result === 'yut' ? 5 : 0); break; case 'vanguard': case 'cannon': consider(u, (o?.label ?? '').startsWith('대기') ? 0 : 6); break; case 'dokkaebi': case 'pawn': case 'marathon': case 'fog': case 'mine': case 'twins': case 'shield': consider(u, 5); break; case 'throwyut': consider(u, u.target === leaderSide ? 6 : 1); break; case 'necro': consider(u, 4); break; case 'poison': consider(u, random() < 0.25 ? 4 : 0); break; case 'domain': consider(u, random() < 0.2 ? 3 : 0); break; case 'swap': { if (o?.at === undefined || o.at === 'home' || u.node === undefined) break; const gain = remainingDistance(o.at, view.options.finishRule) - remainingDistance(u.node as NodeId, view.options.finishRule); consider(u, gain >= 5 ? 5 : 0); break; } } } // special pieces: a free step forward (or a catch) is always worth it for (const sp of has('SPECIAL') as Extract[]) { const m = aug.specials.find((x) => x.piece === sp.piece && x.to === sp.to); if (!m) continue; const fwd = m.to === 'goal' || m.from === 'home' || remainingDistance(m.to as NodeId, view.options.finishRule) < remainingDistance(m.from as NodeId, view.options.finishRule); consider(sp, m.captures ? 9 : m.to === 'goal' ? 8 : fwd ? 4 + random() : 0); } if (bestUse) return (bestUse as { a: A }).a; // 개돼지: pick the result const throws = has('THROW') as Extract[]; if (throws.length > 1) return throws.find((t) => t.pick === 'gae') ?? throws[0]!; return null; } /** 0 (last) .. 1 (first): how well I'm doing by finished pieces and distance. */ function standing(view: YutView, me: number): number { const live = view.sides.filter((x) => !x.out && x.finishedRank === null); if (live.length <= 1) return 1; const sorted = [...live].sort((p, q) => q.done - p.done || p.remaining - q.remaining); const i = sorted.findIndex((x) => x.id === me); return i < 0 ? 0 : 1 - i / (sorted.length - 1); } const AUG_NAME = Object.fromEntries(Object.values(AUGS).map((a) => [a.id, a.name])) as Record;