Files
joke-app/packages/games/src/bots/bots.test.ts
EJClaw 3fffcec52b 커넥트 포(사목) 규칙·컴퓨터 상대·화면·테스트
- 규칙 모듈 packages/games/src/connect-four: 7×6 기본, 8×7·9×7 판, 열을 고르면 맨 아래 빈칸으로
  떨어짐, 가로·세로·대각선 4개(이상) 즉시 승리(이긴 줄 전부 기록), 판이 차면 무승부. 먼저 두는 사람은
  오목·오셀로와 같은 관례(무작위/방장 먼저/방장 나중/판마다 번갈아 — gameNo). 시간: 한 수당·피셔·없음,
  시간 초과 자동 두기(승리 > 막기 > 위 칸을 내주지 않는 가운데 열), 허용 횟수 넘으면 시간패, 기권·나가기.
- 컴퓨터 상대: 네가맥스 알파베타 + 반복 심화 + 치환표, 바로 이기기/막기·강제 수 판정, 열린 줄과 홀짝 줄
  위협을 증분 평가. 레벨 1~60에 따라 1~10수 읽기(어려움은 빈칸 24개부터 끝까지 시도), 국면 5만 개 상한으로
  한 수 평균 수 ms·최악 수십 ms. humanBest로 사람 같은 실수, 낮은 단계는 4목 막기를 가끔 놓침.
- 화면 apps/web/src/games/connect-four: 구멍 뚫린 파란 판(마스크), 떨어지는 애니메이션(움직임 줄이기
  존중), 이긴 줄 강조, 열 전체 터치 영역, 키보드 ←·→·Home·End·Enter, 스크린리더 안내, 다크 모드,
  30초 요약·도움말.
- 등록: GAMES, CATALOG, BOT_GAMES, BOT_STRATEGIES, NO_LUCK, 화면 레지스트리, 아이콘·판 탭, 공통 테스트 목록.
- 문서 docs/games/connect-four.md, 목록 한 줄. E2E 계획(PC 키보드 + 휴대폰 터치, 꽉 찬 열 무시, 끝까지).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-13 03:09:33 +09:00

63 lines
2.7 KiB
TypeScript

/** Every bot-enabled game: bots play only accepted actions, react in every window and never stall a game. */
import { describe, expect, test } from 'bun:test';
import { seedN, type AnyGameDefinition } from '@bg/engine';
import { GAMES } from '../index';
import { BOT_STRATEGIES, decideBotAction } from './index';
import { botPlayout } from './playout';
const isBot = (p: string) => p.startsWith('bot:');
const seats = (n: number, humans: number) => Array.from({ length: n }, (_, i) => (i < humans ? `human${i}` : `bot:${i}:x`));
function counts(def: AnyGameDefinition): number[] {
const range = def.playersFor?.(def.defaultOptions) ?? { min: def.minPlayers, max: def.maxPlayers };
const out = new Set([Math.max(2, range.min), Math.min(range.max, 4)]);
return [...out].filter((n) => !def.playerCountError?.(def.defaultOptions, n));
}
describe.each(Object.keys(BOT_STRATEGIES))('%s', (id) => {
const def = GAMES[id]!;
test('has a game definition with legalActions', () => {
expect(def).toBeDefined();
expect(typeof def.legalActions).toBe('function');
});
for (const n of counts(def)) {
test(`${n} players: one human who only times out, the rest bots — the game finishes`, () => {
for (let s = 1; s <= 3; s++) {
const r = botPlayout(def, { players: seats(n, 1), seed: seedN(s), isBot, maxSteps: 20_000 });
expect(r.finished).toBe(true);
expect(r.botTimeouts).toBe(0);
expect(r.maxBotMs).toBeLessThan(250);
}
});
}
test('bots only: a whole game finishes without timeouts', () => {
const n = counts(def).at(-1)!;
for (let s = 1; s <= 2; s++) {
const r = botPlayout(def, { players: seats(n, 0), seed: seedN(100 + s), isBot, maxSteps: 30_000 });
expect(r.finished).toBe(true);
expect(r.botTimeouts).toBe(0);
}
});
});
test('decideBotAction returns null when the bot has nothing to do', () => {
const def = GAMES.omok!;
const s = def.setup({ players: ['a', 'bot:1:x'], options: def.defaultOptions, rng: { next: () => 0.5, int: () => 0 } as never, now: 0 });
const waiting = def.activePlayers(s)[0]!;
const other = waiting === 'a' ? 'bot:1:x' : 'a';
expect(decideBotAction(def, s, other, { now: 0 })).toBeNull();
expect(decideBotAction(def, s, waiting, { now: 0 })).not.toBeNull();
});
describe('levels (docs/09 §9.1)', () => {
const BOARD = ['omok', 'chess', 'janggi', 'othello', 'baduk', 'connect-four'];
test.each(BOARD)('%s: the weakest and strongest computers still finish a game against each other', (id) => {
const def = GAMES[id]!;
for (const level of [1, 60]) {
const r = botPlayout(def, { players: seats(2, 0), seed: seedN(7), isBot, maxSteps: 30_000, level });
expect(r.finished).toBe(true);
expect(r.botTimeouts).toBe(0);
}
});
});