2026-04-03 14:17:36 +08:00
|
|
|
import {createGameCommandRegistry, Part, MutableSignal, createRegion, moveToRegion} from '@/index';
|
2026-04-02 14:39:30 +08:00
|
|
|
|
|
|
|
|
const BOARD_SIZE = 3;
|
|
|
|
|
const MAX_TURNS = BOARD_SIZE * BOARD_SIZE;
|
|
|
|
|
const WINNING_LINES: number[][][] = [
|
|
|
|
|
[[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]],
|
|
|
|
|
];
|
2026-04-01 23:58:07 +08:00
|
|
|
|
2026-04-02 15:20:34 +08:00
|
|
|
export type PlayerType = 'X' | 'O';
|
|
|
|
|
export type WinnerType = PlayerType | 'draw' | null;
|
2026-04-02 14:39:30 +08:00
|
|
|
|
|
|
|
|
type TicTacToePart = Part & { player: PlayerType };
|
|
|
|
|
|
2026-04-02 13:52:15 +08:00
|
|
|
export function createInitialState() {
|
2026-04-02 12:48:29 +08:00
|
|
|
return {
|
2026-04-03 12:46:02 +08:00
|
|
|
board: createRegion('board', [
|
|
|
|
|
{ name: 'x', min: 0, max: BOARD_SIZE - 1 },
|
|
|
|
|
{ name: 'y', min: 0, max: BOARD_SIZE - 1 },
|
|
|
|
|
]),
|
2026-04-03 13:09:28 +08:00
|
|
|
parts: {} as Record<string, TicTacToePart>,
|
2026-04-02 14:39:30 +08:00
|
|
|
currentPlayer: 'X' as PlayerType,
|
2026-04-02 14:11:35 +08:00
|
|
|
winner: null as WinnerType,
|
|
|
|
|
turn: 0,
|
2026-04-02 12:48:29 +08:00
|
|
|
};
|
|
|
|
|
}
|
2026-04-02 13:52:15 +08:00
|
|
|
export type TicTacToeState = ReturnType<typeof createInitialState>;
|
2026-04-02 14:39:30 +08:00
|
|
|
const registration = createGameCommandRegistry<TicTacToeState>();
|
|
|
|
|
export const registry = registration.registry;
|
2026-04-02 14:11:35 +08:00
|
|
|
|
2026-04-02 14:39:30 +08:00
|
|
|
registration.add('setup', async function() {
|
|
|
|
|
const {context} = this;
|
2026-04-02 14:11:35 +08:00
|
|
|
while (true) {
|
2026-04-02 14:39:30 +08:00
|
|
|
const currentPlayer = context.value.currentPlayer;
|
|
|
|
|
const turnNumber = context.value.turn + 1;
|
|
|
|
|
const turnOutput = await this.run<{winner: WinnerType}>(`turn ${currentPlayer} ${turnNumber}`);
|
2026-04-02 14:11:35 +08:00
|
|
|
if (!turnOutput.success) throw new Error(turnOutput.error);
|
|
|
|
|
|
2026-04-02 14:39:30 +08:00
|
|
|
context.produce(state => {
|
2026-04-02 14:11:35 +08:00
|
|
|
state.winner = turnOutput.result.winner;
|
2026-04-02 14:39:30 +08:00
|
|
|
if (!state.winner) {
|
|
|
|
|
state.currentPlayer = state.currentPlayer === 'X' ? 'O' : 'X';
|
|
|
|
|
state.turn = turnNumber;
|
|
|
|
|
}
|
2026-04-02 14:11:35 +08:00
|
|
|
});
|
2026-04-02 14:39:30 +08:00
|
|
|
if (context.value.winner) break;
|
2026-04-02 14:11:35 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-02 14:39:30 +08:00
|
|
|
return context.value;
|
2026-04-02 14:11:35 +08:00
|
|
|
});
|
|
|
|
|
|
2026-04-02 14:39:30 +08:00
|
|
|
registration.add('turn <player> <turn:number>', async function(cmd) {
|
|
|
|
|
const [turnPlayer, turnNumber] = cmd.params as [PlayerType, number];
|
|
|
|
|
|
2026-04-02 19:32:07 +08:00
|
|
|
const playCmd = await this.prompt(
|
|
|
|
|
'play <player> <row:number> <col:number>',
|
|
|
|
|
(command) => {
|
|
|
|
|
const [player, row, col] = command.params as [PlayerType, number, number];
|
2026-04-02 14:11:35 +08:00
|
|
|
|
2026-04-02 19:32:07 +08:00
|
|
|
if (player !== turnPlayer) {
|
|
|
|
|
return `Invalid player: ${player}. Expected ${turnPlayer}.`;
|
|
|
|
|
}
|
|
|
|
|
if (!isValidMove(row, col)) {
|
|
|
|
|
return `Invalid position: (${row}, ${col}). Must be between 0 and ${BOARD_SIZE - 1}.`;
|
|
|
|
|
}
|
|
|
|
|
if (isCellOccupied(this.context, row, col)) {
|
|
|
|
|
return `Cell (${row}, ${col}) is already occupied.`;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
const [player, row, col] = playCmd.params as [PlayerType, number, number];
|
2026-04-02 14:11:35 +08:00
|
|
|
|
2026-04-02 19:32:07 +08:00
|
|
|
placePiece(this.context, row, col, turnPlayer);
|
2026-04-02 14:11:35 +08:00
|
|
|
|
2026-04-02 19:32:07 +08:00
|
|
|
const winner = checkWinner(this.context);
|
|
|
|
|
if (winner) return { winner };
|
|
|
|
|
if (turnNumber >= MAX_TURNS) return { winner: 'draw' as WinnerType };
|
2026-04-02 14:39:30 +08:00
|
|
|
|
2026-04-02 19:32:07 +08:00
|
|
|
return { winner: null };
|
2026-04-02 14:11:35 +08:00
|
|
|
});
|
2026-04-02 12:48:29 +08:00
|
|
|
|
2026-04-02 14:39:30 +08:00
|
|
|
function isValidMove(row: number, col: number): boolean {
|
|
|
|
|
return !isNaN(row) && !isNaN(col) && row >= 0 && row < BOARD_SIZE && col >= 0 && col < BOARD_SIZE;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-03 14:17:36 +08:00
|
|
|
export function isCellOccupied(host: MutableSignal<TicTacToeState>, row: number, col: number): boolean {
|
2026-04-02 16:39:08 +08:00
|
|
|
const board = host.value.board;
|
2026-04-03 12:46:02 +08:00
|
|
|
return board.partMap[`${row},${col}`] !== undefined;
|
2026-04-01 23:58:07 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-02 11:21:57 +08:00
|
|
|
export function hasWinningLine(positions: number[][]): boolean {
|
2026-04-02 14:39:30 +08:00
|
|
|
return WINNING_LINES.some(line =>
|
2026-04-01 23:58:07 +08:00
|
|
|
line.every(([r, c]) =>
|
|
|
|
|
positions.some(([pr, pc]) => pr === r && pc === c)
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-03 14:17:36 +08:00
|
|
|
export function checkWinner(host: MutableSignal<TicTacToeState>): WinnerType {
|
2026-04-03 13:09:28 +08:00
|
|
|
const parts = Object.values(host.value.parts);
|
2026-04-02 11:21:57 +08:00
|
|
|
|
2026-04-02 14:39:30 +08:00
|
|
|
const xPositions = parts.filter((p: TicTacToePart) => p.player === 'X').map((p: TicTacToePart) => p.position);
|
|
|
|
|
const oPositions = parts.filter((p: TicTacToePart) => p.player === 'O').map((p: TicTacToePart) => p.position);
|
2026-04-02 11:21:57 +08:00
|
|
|
|
|
|
|
|
if (hasWinningLine(xPositions)) return 'X';
|
|
|
|
|
if (hasWinningLine(oPositions)) return 'O';
|
2026-04-02 14:39:30 +08:00
|
|
|
if (parts.length >= MAX_TURNS) return 'draw';
|
2026-04-02 11:21:57 +08:00
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-03 14:17:36 +08:00
|
|
|
export function placePiece(host: MutableSignal<TicTacToeState>, row: number, col: number, player: PlayerType) {
|
2026-04-02 16:39:08 +08:00
|
|
|
const board = host.value.board;
|
2026-04-03 13:09:28 +08:00
|
|
|
const moveNumber = Object.keys(host.value.parts).length + 1;
|
2026-04-02 14:39:30 +08:00
|
|
|
const piece: TicTacToePart = {
|
|
|
|
|
id: `piece-${player}-${moveNumber}`,
|
2026-04-03 12:46:02 +08:00
|
|
|
regionId: 'board',
|
2026-04-01 23:58:07 +08:00
|
|
|
position: [row, col],
|
2026-04-02 14:39:30 +08:00
|
|
|
player,
|
2026-04-01 23:58:07 +08:00
|
|
|
};
|
2026-04-02 14:39:30 +08:00
|
|
|
host.produce(state => {
|
2026-04-03 13:09:28 +08:00
|
|
|
state.parts[piece.id] = piece;
|
2026-04-03 12:46:02 +08:00
|
|
|
board.childIds.push(piece.id);
|
|
|
|
|
board.partMap[`${row},${col}`] = piece.id;
|
2026-04-02 13:52:15 +08:00
|
|
|
});
|
2026-04-02 14:39:30 +08:00
|
|
|
}
|