boardgame-core/src/samples/tic-tac-toe.ts

133 lines
4.1 KiB
TypeScript
Raw Normal View History

2026-04-02 12:48:29 +08:00
import { IGameContext, createGameCommand } from '../core/game';
2026-04-02 12:53:49 +08:00
import { createCommandRegistry, registerCommand } from '../utils/command';
import type { Part } from '../core/part';
2026-04-02 10:48:20 +08:00
export type TicTacToeState = {
currentPlayer: 'X' | 'O';
winner: 'X' | 'O' | 'draw' | null;
moveCount: number;
};
2026-04-02 12:48:29 +08:00
export type TicTacToeContext = IGameContext<TicTacToeState>;
type TurnResult = {
winner: 'X' | 'O' | 'draw' | null;
};
2026-04-02 12:48:29 +08:00
export function createInitialState(): TicTacToeState {
return {
currentPlayer: 'X',
winner: null,
moveCount: 0,
};
}
export function getBoardRegion(host: TicTacToeContext) {
2026-04-02 00:44:29 +08:00
return host.regions.get('board');
}
2026-04-02 12:48:29 +08:00
export function isCellOccupied(host: TicTacToeContext, row: number, col: number): boolean {
2026-04-02 00:44:29 +08:00
const board = getBoardRegion(host);
return board.value.children.some(
(child: { value: { position: number[] } }) => child.value.position[0] === row && child.value.position[1] === col
);
}
2026-04-02 11:21:57 +08:00
export function hasWinningLine(positions: number[][]): boolean {
const lines = [
[[0, 0], [0, 1], [0, 2]],
[[1, 0], [1, 1], [1, 2]],
[[2, 0], [2, 1], [2, 2]],
[[0, 0], [1, 0], [2, 0]],
[[0, 1], [1, 1], [2, 1]],
[[0, 2], [1, 2], [2, 2]],
[[0, 0], [1, 1], [2, 2]],
[[0, 2], [1, 1], [2, 0]],
];
return lines.some(line =>
line.every(([r, c]) =>
positions.some(([pr, pc]) => pr === r && pc === c)
)
);
}
2026-04-02 12:48:29 +08:00
export function checkWinner(host: TicTacToeContext): 'X' | 'O' | 'draw' | null {
2026-04-02 11:21:57 +08:00
const parts = Object.values(host.parts.collection.value).map((s: { value: Part }) => s.value);
const xPositions = parts.filter((_: Part, i: number) => i % 2 === 0).map((p: Part) => p.position);
const oPositions = parts.filter((_: Part, i: number) => i % 2 === 1).map((p: Part) => p.position);
if (hasWinningLine(xPositions)) return 'X';
if (hasWinningLine(oPositions)) return 'O';
return null;
}
2026-04-02 12:48:29 +08:00
export function placePiece(host: TicTacToeContext, row: number, col: number, moveCount: number) {
2026-04-02 00:44:29 +08:00
const board = getBoardRegion(host);
const piece: Part = {
id: `piece-${moveCount}`,
sides: 1,
side: 0,
region: board,
position: [row, col],
};
2026-04-02 00:44:29 +08:00
host.parts.add(piece);
board.value.children.push(host.parts.get(piece.id));
}
2026-04-02 12:53:49 +08:00
const setup = createGameCommand<TicTacToeState, { winner: 'X' | 'O' | 'draw' | null }>(
2026-04-02 10:48:20 +08:00
'setup',
async function() {
this.context.regions.add({
id: 'board',
axes: [
{ name: 'x', min: 0, max: 2 },
{ name: 'y', min: 0, max: 2 },
],
children: [],
});
let currentPlayer: 'X' | 'O' = 'X';
2026-04-02 12:48:29 +08:00
let winner: 'X' | 'O' | 'draw' | null = null;
2026-04-02 10:48:20 +08:00
let turn = 1;
while (true) {
const turnOutput = await this.run<TurnResult>(`turn ${currentPlayer} ${turn++}`);
if (!turnOutput.success) throw new Error(turnOutput.error);
2026-04-02 12:48:29 +08:00
winner = turnOutput.result.winner;
if (winner) break;
2026-04-02 10:48:20 +08:00
currentPlayer = currentPlayer === 'X' ? 'O' : 'X';
}
2026-04-02 12:48:29 +08:00
return { winner };
2026-04-02 10:48:20 +08:00
}
)
2026-04-02 12:53:49 +08:00
const turn = createGameCommand<TicTacToeState, TurnResult>(
2026-04-02 10:48:20 +08:00
'turn <player> <turn:number>',
async function(cmd) {
const [turnPlayer, turnNumber] = cmd.params as [string, number];
while (true) {
const playCmd = await this.prompt('play <player> <row:number> <col:number>');
const [player, row, col] = playCmd.params as [string, number, number];
if(turnPlayer !== player) continue;
if (isNaN(row) || isNaN(col) || row < 0 || row > 2 || col < 0 || col > 2) continue;
if (isCellOccupied(this.context, row, col)) continue;
placePiece(this.context, row, col, turnNumber);
const winner = checkWinner(this.context);
if (winner) return { winner };
if (turnNumber >= 9) return { winner: 'draw' as const };
}
}
);
2026-04-02 12:48:29 +08:00
export const registry = createCommandRegistry<TicTacToeContext>();
registerCommand(registry, setup);
registerCommand(registry, turn);