Files
joke-app/packages/games/src/blackjack/bot.ts
EJClaw 13a89a645e 새 게임 블랙잭: 딜러(하우스) 상대 1~7명·놀이용 칩. 공식 규칙(6벌 S17·블랙잭 3:2·A/10 peek·아무 두 장 더블·DAS·4손패 스플릿·에이스 한 장)을 기본값으로, 한국 하우스 룰(5·6카드 찰리, 7-7-7 보너스 3:1, 배당 6:5·2:1·1:1, H17, 더블 9~11·10~11, 에이스 자유 히트·다시 스플릿, 레이트 서렌더, 인슈어런스)을 방 설정으로
- 엔진 packages/games/src/blackjack/(options·types·rules·index·bot): 베팅→인슈어런스→플레이→딜러→결과 단계 상태 기계, 비공개는 딜러 홀 카드와 슈뿐(재셔플마다 카드 id 유일), 칩은 하우스 상대로 정산
- 컴퓨터: 기본 전략(S17 차트) + 레벨 1~60(쉬움은 딜러 흉내·인슈어런스 가끔, 어려움은 차트대로·인슈어런스 거절)
- 웹 apps/web/src/games/blackjack/: 초록 테이블·손패·단계별 버튼·방 설정 변형, 30초 요약·도움말, 표준 카드 아트 재사용
- 등록: GAMES·BOT_STRATEGIES·BOT_GAMES·CATALOG·GAME_UI·GAME_ICON·게임 탭, 문서(games/blackjack.md·README·로드맵·결정 사항), e2e/games.e2e.ts
- 검증: 엔진·봇 테스트, 무작위 플레이(여러 옵션) JSON 왕복·정보 누출 없음, 전체 테스트 2,611개·타입 검사·웹 빌드 통과, bun e2e/games.e2e.ts blackjack 통과

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-10-14 05:45:41 +09:00

114 lines
4.8 KiB
TypeScript

/**
* 블랙잭 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<BlackjackView, BlackjackAction> = ({ 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' };
};