M9: 컴퓨터 상대 — 게임 22종 컴퓨터 전략, 서버 구동(BotDriver), 연습 방

- packages/shared/src/bots.ts: 컴퓨터 id(bot:<n>:<ulid>)·프로필(컴퓨터 n)·BOT_GAMES
  (도블·할리갈리·그림 맞히기·마피아 제외)
- packages/games/src/bots: 전략 계약(자기 view + 합법 수만), decideBotAction(validate 재검사),
  게임별 <게임>/bot.ts 22개 + 행동 테스트, 전 게임 판 끝까지 돌리는 공통 테스트
- 서버: Room.addBot/removeBot(방장·대기실만, 방은 친구만), RoomManager.createPractice
  (POST /api/rooms {practice:true}), BotDriver(사람 같은 지연, 사람과 같은 창이면 더 천천히,
  재시작 후 다시 예약), 연습 게임은 전적 제외(result_json.practice), 사람이 모두 나가면 무효
- 프로토콜 addBot/removeBot, 문서 03·06 §12·08·09 §8

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
EJClaw
2026-10-06 13:32:28 +09:00
parent c27cb0a11a
commit a3f81ded37
67 changed files with 5113 additions and 18 deletions

View File

@@ -0,0 +1,46 @@
/** 벽 쌓기 길찾기 computer player: shortest path, winning step, walls a rival who is about to win. */
import { describe, expect, test } from 'bun:test';
import { SeededRng } from '@bg/engine';
import { decideBotAction } from '../bots/index';
import { quoridor, type Cell, type QuoridorAction, type QuoridorState, type Seat } from './index';
import { emptyWallIndex, pathLength, setWall, type Wall } from './board';
/** 'e5' → { c: 4, r: 4 }. */
const at = (l: string): Cell => ({ c: 'abcdefghi'.indexOf(l[0]!), r: Number(l.slice(1)) - 1 });
/** 'h:d4' → horizontal wall at anchor d4. */
const wall = (w: string): Wall => ({ o: w[0] as 'h' | 'v', ...at(w.slice(2)) });
/** 2 players: A = 남 (the bot here, to move, goal row 9), B = 북 (goal row 1). */
function position(pos: Partial<Record<Seat, string>>, walls: string[] = []): QuoridorState {
const options = quoridor.optionsSchema.parse({ seatOrder: 'joinOrder' });
const s = quoridor.setup({ players: ['A', 'B'], options, rng: SeededRng.fromSeed([1, 2, 3, 4]), now: 0 });
for (const x of s.seats) if (pos[x.seat]) x.pos = at(pos[x.seat]!);
s.walls = walls.map(wall);
s.wallIndex = emptyWallIndex();
for (const w of s.walls) setWall(s.wallIndex, w, true);
s.turnIndex = s.seats.findIndex((x) => x.seat === 'S');
return s;
}
const decide = (s: QuoridorState, random = () => 0.5) => decideBotAction(quoridor, s, 'A', { now: 1000, random }) as QuoridorAction;
describe('quoridor bot', () => {
test('walks the shortest way around a wall', () => {
// The wall above e2/f2 blocks the straight way; d2 → d3 is shortest (f2 is blocked above as well).
const s = position({ S: 'e2', N: 'e9' }, ['h:e2']);
expect(decide(s)).toEqual({ type: 'move', to: at('d2') });
});
test('steps onto the goal row even when the rival is one step from winning', () => {
const s = position({ S: 'e8', N: 'a2' });
expect(decide(s)).toEqual({ type: 'move', to: at('e9') });
});
test('walls off a rival who is about to win', () => {
const s = position({ S: 'e5', N: 'e2' });
const a = decide(s);
expect(a.type).toBe('placeWall');
const after = quoridor.apply(s, 'A', a, { rng: SeededRng.fromSeed([1, 1, 1, 1]), now: 1000 }).state;
const north = after.seats.find((x) => x.seat === 'N')!;
expect(pathLength(after.wallIndex, north.pos!, 'N')).toBeGreaterThan(1);
});
});

View File

@@ -0,0 +1,55 @@
/**
* 벽 쌓기 길찾기 computer player: step onto the goal when possible, otherwise walk the shortest path; when the
* leading rival would get home first, put up the wall that delays that rival most compared to itself.
*/
import type { BotStrategy } from '../bots/types';
import { answerRequests, best, pick, playable, typeOf } from '../bots/policy';
import type { QuoridorView } from './index';
import { emptyWallIndex, isGoal, pathLength, setWall } from './board';
type Move = { type: 'move'; to: { c: number; r: number } };
type WallAct = { type: 'placeWall'; c: number; r: number; o: 'h' | 'v' };
export const quoridorBot: BotStrategy<QuoridorView> = ({ view, legal, random }) => {
const req = answerRequests(legal);
if (req) return req;
const moves = legal.filter((a) => typeOf(a) === 'move') as Move[];
const walls = legal.filter((a) => typeOf(a) === 'placeWall') as WallAct[];
const mine = view.seats.find((s) => s.seat === view.mySeat);
if ((!moves.length && !walls.length) || !mine?.pos) return pick(playable(legal), random);
const seat = mine.seat;
const win = moves.find((m) => isGoal(seat, m.to.c, m.to.r));
if (win) return win;
const w = emptyWallIndex();
for (const wall of view.walls) setWall(w, wall, true);
const myDist = pathLength(w, mine.pos, seat);
// The rival closest to its goal (pawns on the board only).
const leader = view.seats
.filter((s) => s.seat !== seat && s.status === 'playing' && s.pos)
.map((s) => ({ s, d: pathLength(w, s.pos!, s.seat) }))
.sort((a, b) => a.d - b.d)[0];
// Behind (I move first, so a tie is mine) — or level and in the mood: look for a wall.
const behind = !!leader && leader.d < myDist;
if (walls.length && leader && (behind || (leader.d === myDist && random() < 0.3))) {
const gain = (wall: WallAct): number => {
setWall(w, wall, true);
const dRival = pathLength(w, leader.s.pos!, leader.s.seat);
const dMe = pathLength(w, mine.pos!, seat);
setWall(w, wall, false);
return dRival - leader.d - (dMe - myDist) * 1.5;
};
const gains = new Map(walls.map((x) => [x, gain(x)]));
const top = best(walls, (x) => gains.get(x)! + random() * 0.3, random);
// Urgent (the rival gets home first): any net gain helps; otherwise only clearly good walls.
if (gains.get(top)! >= (behind ? 1 : 2) || (gains.get(top)! > 0 && !moves.length)) return top;
}
if (!moves.length) return pick(walls, random);
return best(
moves,
(m) => {
const d = pathLength(w, m.to, seat);
return d < 0 ? -1000 : -d;
},
random,
);
};