- [다음 판]은 딜러 카드가 다 놓여 결과가 보인 뒤에만 눌림(혼자 하는 판도 결과를 건너뛰지 않음) - 자리마다 테이블 폭을 무게(내 자리·스플릿 자리 더 넓게)로 나누고, 그 폭과 딜러 아래 높이에 맞춰 카드 크기 계산(최소 26px), 손패 3개 이상이면 두 줄 - 좁은 자리는 아바타 없이 이름만, 손패 정보 줄바꿈 - 엔진 테스트: 처음 나눠 주기·딜러 차례 연출 시간이 제한 시간에 더해지는지(1인 판) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
193 lines
8.2 KiB
TypeScript
193 lines
8.2 KiB
TypeScript
import { describe, expect, test } from 'bun:test';
|
||
import { SeededRng, playoutSeeds, randomPlayout, seedN, type CardId, type RngState } from '@bg/engine';
|
||
import {
|
||
BJ_DEAL_STEP_MS,
|
||
blackjack,
|
||
buildShoe,
|
||
chipsNonNegative,
|
||
handValue,
|
||
settleHand,
|
||
setupBlackjack,
|
||
type BlackjackOptions,
|
||
type BlackjackState,
|
||
type Hand,
|
||
} from './index';
|
||
|
||
const rng = (seed = 1) => SeededRng.fromSeed(seedN(seed));
|
||
|
||
function make(opts: Partial<BlackjackOptions> = {}, players = ['A', 'B'], shoe?: CardId[]): BlackjackState {
|
||
const options = blackjack.optionsSchema.parse(opts);
|
||
return setupBlackjack({ players, options, rng: rng(), now: 0 }, shoe ? { shoe } : undefined);
|
||
}
|
||
|
||
const hand = (cards: CardId[], over: Partial<Hand> = {}): Hand => ({
|
||
cards,
|
||
bet: 100,
|
||
status: 'stand',
|
||
fromSplit: false,
|
||
frozenAces: false,
|
||
doubled: false,
|
||
outcome: null,
|
||
net: 0,
|
||
...over,
|
||
});
|
||
|
||
// ---------------------------------------------------------------- hand value
|
||
|
||
describe('handValue', () => {
|
||
test('소프트/하드 에이스', () => {
|
||
expect(handValue(['AS', '6H'])).toEqual({ total: 17, soft: true });
|
||
expect(handValue(['AS', '6H', 'KD'])).toEqual({ total: 17, soft: false });
|
||
expect(handValue(['AS', 'AD', '9C'])).toEqual({ total: 21, soft: true });
|
||
expect(handValue(['KS', 'QD', '2C'])).toEqual({ total: 22, soft: false });
|
||
expect(handValue(['AS', 'KD'])).toEqual({ total: 21, soft: true });
|
||
});
|
||
test('덱 접미사를 무시해요', () => {
|
||
expect(handValue(['AS#0', 'KD#3'])).toEqual({ total: 21, soft: true });
|
||
});
|
||
});
|
||
|
||
// ---------------------------------------------------------------- settlement
|
||
|
||
describe('settleHand', () => {
|
||
const o = blackjack.optionsSchema.parse({});
|
||
test('내추럴 블랙잭은 3:2', () => {
|
||
expect(settleHand(hand(['AS', 'KD'], { bet: 100 }), ['9H', '7C'], false, o)).toEqual({ outcome: 'blackjack', net: 150 });
|
||
});
|
||
test('딜러 블랙잭엔 푸시', () => {
|
||
expect(settleHand(hand(['AS', 'KD']), ['AH', 'QC'], true, o)).toEqual({ outcome: 'push', net: 0 });
|
||
});
|
||
test('버스트는 베팅을 잃어요', () => {
|
||
expect(settleHand(hand(['KS', 'QD', '5C'], { status: 'bust', bet: 100 }), ['9H', '8C'], false, o)).toEqual({ outcome: 'lose', net: -100 });
|
||
});
|
||
test('서렌더는 절반만 잃어요', () => {
|
||
expect(settleHand(hand(['KS', '6D'], { status: 'surrender', bet: 100 }), ['9H', '8C'], false, o)).toEqual({ outcome: 'lose', net: -50 });
|
||
});
|
||
test('딜러 버스트면 이겨요', () => {
|
||
expect(settleHand(hand(['KS', '8D'], { bet: 100 }), ['9H', '5C', 'KD'], false, o)).toEqual({ outcome: 'win', net: 100 });
|
||
});
|
||
test('6:5 블랙잭 배당', () => {
|
||
const o65 = blackjack.optionsSchema.parse({ blackjackPayout: '6:5' });
|
||
expect(settleHand(hand(['AS', 'KD'], { bet: 100 }), ['9H', '7C'], false, o65)).toEqual({ outcome: 'blackjack', net: 120 });
|
||
});
|
||
test('5카드 찰리는 자동 승', () => {
|
||
const oc = blackjack.optionsSchema.parse({ charlie: 'five' });
|
||
expect(settleHand(hand(['2S', '3D', '2C', '4H', '5S'], { status: 'charlie', bet: 100 }), ['KH', '9C'], false, oc)).toEqual({ outcome: 'win', net: 100 });
|
||
});
|
||
test('7-7-7 보너스는 3:1', () => {
|
||
const o7 = blackjack.optionsSchema.parse({ sevenTriple: true });
|
||
expect(settleHand(hand(['7S', '7D', '7C'], { bet: 100 }), ['KH', '9C'], false, o7)).toEqual({ outcome: 'bonus', net: 300 });
|
||
});
|
||
});
|
||
|
||
// ---------------------------------------------------------------- flow
|
||
|
||
describe('flow', () => {
|
||
test('setup은 베팅 단계에서 시작해요', () => {
|
||
const s = make();
|
||
expect(s.phase).toBe('betting');
|
||
expect(blackjack.activePlayers(s).sort()).toEqual(['A', 'B']);
|
||
expect(s.seats.every((x) => x.chips === 1000)).toBe(true);
|
||
});
|
||
|
||
test('베팅을 걸면 칩이 묶이고 모두 걸면 딜이 돼요', () => {
|
||
let s = make();
|
||
s = blackjack.apply(s, 'A', { type: 'bet', amount: 100 }, { rng: rng(2), now: 1 }).state;
|
||
expect(s.seats.find((x) => x.id === 'A')!.chips).toBe(900);
|
||
expect(s.phase).toBe('betting');
|
||
s = blackjack.apply(s, 'B', { type: 'bet', amount: 50 }, { rng: rng(3), now: 2 }).state;
|
||
expect(['insurance', 'playing', 'handEnd']).toContain(s.phase);
|
||
expect(s.rounds['A']!.hands[0]!.cards).toHaveLength(2);
|
||
expect(s.dealer).toHaveLength(2);
|
||
});
|
||
|
||
test('히트로 버스트하면 손패가 끝나요', () => {
|
||
// Shoe (drawn from the front): A B dealerUp A B dealerHole, then A's hit card.
|
||
const shoe: CardId[] = ['KS', 'KH', '5D', 'QC', '9S', '7H', 'QD', '2C'];
|
||
let s = make({ insurance: false }, ['A', 'B'], shoe);
|
||
s = blackjack.apply(s, 'A', { type: 'bet', amount: 100 }, { rng: rng(), now: 1 }).state;
|
||
s = blackjack.apply(s, 'B', { type: 'bet', amount: 100 }, { rng: rng(), now: 2 }).state;
|
||
expect(s.phase).toBe('playing');
|
||
const aHand = s.rounds['A']!.hands[0]!;
|
||
expect(aHand.cards).toEqual(['KS', 'QC']); // 20
|
||
s = blackjack.apply(s, 'A', { type: 'hit' }, { rng: rng(), now: 3 }).state;
|
||
expect(s.rounds['A']!.hands[0]!.status).toBe('bust'); // +QD = 30
|
||
});
|
||
|
||
test('카드 연출 시간: 처음 나눠 주기와 딜러 차례만큼 다음 제한 시간이 늘어요(혼자 하는 판도 결과를 건너뛰지 않음)', () => {
|
||
// A: KS, 7D (17). Dealer: 5H + 6C, then draws 2S, 3D, 4C → 20.
|
||
const shoe: CardId[] = ['KS', '5H', '7D', '6C', '2S', '3D', '4C'];
|
||
let s = make({ insurance: false }, ['A'], shoe);
|
||
s = blackjack.apply(s, 'A', { type: 'bet', amount: 100 }, { rng: rng(), now: 1000 }).state;
|
||
expect(s.phase).toBe('playing');
|
||
// 4 cards dealt × 450 ms on top of the 20 s turn.
|
||
expect(s.deadlineMs).toBe(20_000 + 4 * BJ_DEAL_STEP_MS);
|
||
expect(s.deadlineAt).toBe(1000 + 20_000 + 4 * BJ_DEAL_STEP_MS);
|
||
s = blackjack.apply(s, 'A', { type: 'stand' }, { rng: rng(), now: 2000 }).state;
|
||
expect(s.phase).toBe('handEnd');
|
||
expect(s.dealer).toHaveLength(5);
|
||
// Hole card turn + 3 drawn cards, then the usual result time.
|
||
expect(s.deadlineMs).toBe(s.options.handEndSeconds * 1000 + 4 * BJ_DEAL_STEP_MS);
|
||
expect(s.animLagMs).toBe(0);
|
||
});
|
||
|
||
test('전체 흐름: 한 판을 끝까지', () => {
|
||
let s = make({ insurance: false, handLimit: 5 });
|
||
let now = 1;
|
||
const step = (p: string, a: Parameters<typeof blackjack.apply>[2]) => {
|
||
s = blackjack.apply(s, p, a, { rng: rng(now), now: now++ }).state;
|
||
};
|
||
step('A', { type: 'bet', amount: 100 });
|
||
step('B', { type: 'bet', amount: 100 });
|
||
// Play until the hand ends.
|
||
let guard = 0;
|
||
while (s.phase === 'playing' && guard++ < 50) {
|
||
const who = s.turn!;
|
||
step(who, { type: 'stand' });
|
||
}
|
||
expect(s.phase).toBe('handEnd');
|
||
expect(s.dealerDown).toBe(false);
|
||
for (const seat of s.seats) expect(seat.chips).toBeGreaterThanOrEqual(0);
|
||
});
|
||
});
|
||
|
||
// ---------------------------------------------------------------- random playouts
|
||
|
||
describe('random playouts (invariants)', () => {
|
||
const OPTS: Partial<BlackjackOptions>[] = [
|
||
{},
|
||
{ insurance: false, charlie: 'five' },
|
||
{ dealerSoft17: 'hit', surrender: 'late', sevenTriple: true, decks: 1 },
|
||
{ blackjackPayout: '6:5', splitMaxHands: 4, hitSplitAces: true, resplitAces: true, endCondition: 'lastStanding' },
|
||
{ doubleRule: '9to11', doubleAfterSplit: false, minBet: 50, startingChips: 500, decks: 2 },
|
||
];
|
||
const seeds = playoutSeeds(120);
|
||
for (const opts of OPTS) {
|
||
test(`옵션 ${JSON.stringify(opts)}`, () => {
|
||
for (let i = 1; i <= seeds; i++) {
|
||
const players = ['A', 'B', 'C'].slice(0, 2 + (i % 2));
|
||
const report = randomPlayout(blackjack, {
|
||
players,
|
||
options: blackjack.optionsSchema.parse(opts),
|
||
seed: seedN(i),
|
||
maxSteps: 4000,
|
||
invariant: (st) => {
|
||
if (!chipsNonNegative(st as BlackjackState)) throw new Error('chips went negative');
|
||
},
|
||
});
|
||
expect(report.finished).toBe(true);
|
||
const res = blackjack.result(report.finalState as BlackjackState)!;
|
||
expect(res.ranking.flat().sort()).toEqual([...players].sort());
|
||
}
|
||
});
|
||
}
|
||
});
|
||
|
||
// ---------------------------------------------------------------- deck
|
||
|
||
test('buildShoe는 덱당 52장, 중복 없는 id', () => {
|
||
const shoe = buildShoe(2);
|
||
expect(shoe).toHaveLength(104);
|
||
expect(new Set(shoe).size).toBe(104);
|
||
});
|