/** * 블랙잭 computer player: plays basic strategy (S17 chart), scaled by level. 어려움 computers follow the chart and * never take insurance; 쉬움 computers often just "mimic the dealer" (hit under 17, stand otherwise), skip doubles * and splits, and sometimes take insurance. Bets flat at the minimum, with stronger computers occasionally raising. */ import type { BotLevel } from '../bots/persona'; import type { BotStrategy } from '../bots/types'; import { pick } from '../bots/policy'; import { cardPoints } from './rules'; import type { BlackjackAction, BlackjackView, HandView } from './types'; type Move = 'hit' | 'stand' | 'double' | 'split' | 'surrender'; const easy = (l?: BotLevel) => l !== undefined && l <= 20; const hard = (l?: BotLevel) => l === undefined || l > 40; /** Basic strategy move for the active hand against the dealer upcard (dealer value 2..11, A = 11). */ function basicMove(h: HandView, up: number, pair: boolean): Move { const t = h.total; if (pair) { const r = cardPoints(h.cards[0]!); // 2..11 (A = 11, tens = 10) if (r === 11) return 'split'; // A,A if (r === 10) return 'stand'; if (r === 9) return up === 7 || up >= 10 ? 'stand' : 'split'; if (r === 8) return 'split'; if (r === 7) return up <= 7 ? 'split' : 'hit'; if (r === 6) return up <= 6 ? 'split' : 'hit'; if (r === 5) return up <= 9 ? 'double' : 'hit'; // play as a hard 10 if (r === 4) return 'hit'; if (r === 3 || r === 2) return up <= 7 ? 'split' : 'hit'; } if (h.soft) { // soft total: an ace counts as 11. if (t >= 19) return 'stand'; if (t === 18) return up <= 6 ? 'double' : up <= 8 ? 'stand' : 'hit'; if (t === 17) return up >= 3 && up <= 6 ? 'double' : 'hit'; if (t === 16 || t === 15) return up >= 4 && up <= 6 ? 'double' : 'hit'; return up >= 5 && up <= 6 ? 'double' : 'hit'; // soft 13,14 } // hard total if (t >= 17) return 'stand'; if (t >= 13) return up <= 6 ? 'stand' : 'hit'; if (t === 12) return up >= 4 && up <= 6 ? 'stand' : 'hit'; if (t === 11) return up <= 10 ? 'double' : 'hit'; if (t === 10) return up <= 9 ? 'double' : 'hit'; if (t === 9) return up >= 3 && up <= 6 ? 'double' : 'hit'; return 'hit'; } /** Late surrender: hard 16 vs 9/10/A, hard 15 vs 10. */ function wantsSurrender(h: HandView, up: number): boolean { if (h.soft) return false; if (h.total === 16) return up === 9 || up >= 10 || up === 11; if (h.total === 15) return up === 10; return false; } /** When the chart says double but it isn't allowed, fall back to hit (low/soft) or stand. */ function withoutDouble(h: HandView): Move { return h.soft || h.total <= 11 ? 'hit' : 'stand'; } export const blackjackBot: BotStrategy = ({ view, legal, random, level }) => { const types = new Set(legal.map((a) => a.type)); // Betting: minimum by default; strong computers sometimes bet a step higher. if (types.has('bet')) { const bets = legal.filter((a): a is { type: 'bet'; amount: number } => a.type === 'bet').sort((a, b) => a.amount - b.amount); if (hard(level) && bets.length > 1 && random() < 0.35) return bets[1]!; return bets[0]!; } // Insurance: basic strategy never insures; weak computers sometimes do. if (types.has('insurance')) { const take = legal.find((a) => a.type === 'insurance' && a.take); if (take && easy(level) && random() < 0.3) return take; return { type: 'insurance', take: false }; } if (types.has('ready')) return { type: 'ready' }; // Playing: decide for the active hand. const my = view.my; const seat = view.seats.find((x) => x.id === view.me); const h = seat?.hands?.[seat.active]; if (!my || !h) return pick(legal, random); const up = view.dealer.cards[0] ? cardPoints(view.dealer.cards[0]) : 10; const pair = h.cards.length === 2 && cardPoints(h.cards[0]!) === cardPoints(h.cards[1]!); // Surrender first (only when available and the chart calls for it). if (my.canSurrender && !easy(level) && wantsSurrender(h, up)) return { type: 'surrender' }; let move = basicMove(h, up, pair); // 쉬움 computers often just mimic the dealer and avoid fancy plays. if (easy(level) && random() < 0.5) { move = h.soft ? (h.total <= 17 ? 'hit' : 'stand') : h.total < 17 ? 'hit' : 'stand'; } else if (!hard(level) && random() < 0.12) { // 보통 computers lapse occasionally. move = move === 'double' ? withoutDouble(h) : move; } // Map the desired move onto a legal action, with sensible fallbacks. if (move === 'split' && my.canSplit) return { type: 'split' }; if (move === 'double') { if (my.canDouble) return { type: 'double' }; move = withoutDouble(h); } if (move === 'split') move = h.total <= 11 || h.soft ? 'hit' : h.total >= 17 ? 'stand' : up <= 6 ? 'stand' : 'hit'; if (move === 'surrender') move = 'hit'; if (move === 'hit' && my.canHit) return { type: 'hit' }; return { type: 'stand' }; };