2026-04-01 23:58:07 +08:00
|
|
|
import { GameContextInstance } from '../core/context';
|
2026-04-02 09:33:03 +08:00
|
|
|
import type { Command, CommandRunner, CommandRunnerContext } from '../utils/command';
|
2026-04-01 23:58:07 +08:00
|
|
|
import type { Part } from '../core/part';
|
|
|
|
|
import type { Region } from '../core/region';
|
|
|
|
|
import type { Context } from '../core/context';
|
2026-04-02 09:33:03 +08:00
|
|
|
import { parseCommandSchema } from '../utils/command/schema-parse';
|
2026-04-01 23:58:07 +08:00
|
|
|
|
|
|
|
|
export type TicTacToeState = Context & {
|
|
|
|
|
type: 'tic-tac-toe';
|
|
|
|
|
currentPlayer: 'X' | 'O';
|
|
|
|
|
winner: 'X' | 'O' | 'draw' | null;
|
|
|
|
|
moveCount: number;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type TurnResult = {
|
|
|
|
|
winner: 'X' | 'O' | 'draw' | null;
|
|
|
|
|
};
|
|
|
|
|
|
2026-04-02 09:33:03 +08:00
|
|
|
function getBoardRegion(host: GameContextInstance) {
|
2026-04-02 00:44:29 +08:00
|
|
|
return host.regions.get('board');
|
2026-04-01 23:58:07 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-02 09:33:03 +08:00
|
|
|
function isCellOccupied(host: GameContextInstance, row: number, col: number): boolean {
|
2026-04-02 00:44:29 +08:00
|
|
|
const board = getBoardRegion(host);
|
2026-04-01 23:58:07 +08:00
|
|
|
return board.value.children.some(
|
|
|
|
|
(child: { value: { position: number[] } }) => child.value.position[0] === row && child.value.position[1] === col
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-02 09:33:03 +08:00
|
|
|
function checkWinner(host: GameContextInstance): 'X' | 'O' | 'draw' | null {
|
2026-04-02 00:44:29 +08:00
|
|
|
const parts = Object.values(host.parts.collection.value).map((s: { value: Part }) => s.value);
|
2026-04-01 23:58:07 +08:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 09:33:03 +08:00
|
|
|
function placePiece(host: GameContextInstance, row: number, col: number, moveCount: number) {
|
2026-04-02 00:44:29 +08:00
|
|
|
const board = getBoardRegion(host);
|
2026-04-01 23:58:07 +08:00
|
|
|
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-01 23:58:07 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-02 09:33:03 +08:00
|
|
|
export function createSetupCommand(): CommandRunner<GameContextInstance, { winner: 'X' | 'O' | 'draw' | null }> {
|
|
|
|
|
return {
|
|
|
|
|
schema: parseCommandSchema('start'),
|
|
|
|
|
run: async function(this: CommandRunnerContext<GameContextInstance>) {
|
|
|
|
|
this.context.pushContext({
|
|
|
|
|
type: 'tic-tac-toe',
|
|
|
|
|
currentPlayer: 'X',
|
|
|
|
|
winner: null,
|
|
|
|
|
moveCount: 0,
|
|
|
|
|
} as TicTacToeState);
|
|
|
|
|
|
|
|
|
|
this.context.regions.add({
|
|
|
|
|
id: 'board',
|
|
|
|
|
axes: [
|
|
|
|
|
{ name: 'x', min: 0, max: 2 },
|
|
|
|
|
{ name: 'y', min: 0, max: 2 },
|
|
|
|
|
],
|
|
|
|
|
children: [],
|
|
|
|
|
} as Region);
|
|
|
|
|
|
|
|
|
|
let currentPlayer: 'X' | 'O' = 'X';
|
|
|
|
|
let turnResult: TurnResult | undefined;
|
|
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
|
const turnOutput = await this.run(`turn ${currentPlayer}`);
|
|
|
|
|
if (!turnOutput.success) throw new Error(turnOutput.error);
|
|
|
|
|
turnResult = turnOutput.result as TurnResult;
|
|
|
|
|
if (turnResult?.winner) break;
|
|
|
|
|
|
|
|
|
|
currentPlayer = currentPlayer === 'X' ? 'O' : 'X';
|
|
|
|
|
const state = this.context.latestContext<TicTacToeState>('tic-tac-toe')!;
|
|
|
|
|
state.value.currentPlayer = currentPlayer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const state = this.context.latestContext<TicTacToeState>('tic-tac-toe')!;
|
|
|
|
|
state.value.winner = turnResult?.winner ?? null;
|
|
|
|
|
return { winner: state.value.winner };
|
|
|
|
|
},
|
|
|
|
|
};
|
2026-04-01 23:58:07 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-02 09:33:03 +08:00
|
|
|
export function createTurnCommand(): CommandRunner<GameContextInstance, TurnResult> {
|
|
|
|
|
return {
|
|
|
|
|
schema: parseCommandSchema('turn <player>'),
|
|
|
|
|
run: async function(this: CommandRunnerContext<GameContextInstance>, cmd: Command) {
|
|
|
|
|
while (true) {
|
|
|
|
|
const playCmd = await this.prompt('play <player> <row:number> <col:number>');
|
2026-04-01 23:58:07 +08:00
|
|
|
|
2026-04-02 09:33:03 +08:00
|
|
|
const row = Number(playCmd.params[1]);
|
|
|
|
|
const col = Number(playCmd.params[2]);
|
2026-04-01 23:58:07 +08:00
|
|
|
|
2026-04-02 09:33:03 +08:00
|
|
|
if (isNaN(row) || isNaN(col) || row < 0 || row > 2 || col < 0 || col > 2) continue;
|
|
|
|
|
if (isCellOccupied(this.context, row, col)) continue;
|
2026-04-01 23:58:07 +08:00
|
|
|
|
2026-04-02 09:33:03 +08:00
|
|
|
const state = this.context.latestContext<TicTacToeState>('tic-tac-toe')!;
|
|
|
|
|
if (state.value.winner) continue;
|
2026-04-01 23:58:07 +08:00
|
|
|
|
2026-04-02 09:33:03 +08:00
|
|
|
placePiece(this.context, row, col, state.value.moveCount);
|
|
|
|
|
state.value.moveCount++;
|
2026-04-01 23:58:07 +08:00
|
|
|
|
2026-04-02 09:33:03 +08:00
|
|
|
const winner = checkWinner(this.context);
|
|
|
|
|
if (winner) return { winner };
|
2026-04-01 23:58:07 +08:00
|
|
|
|
2026-04-02 09:33:03 +08:00
|
|
|
if (state.value.moveCount >= 9) return { winner: 'draw' as const };
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
};
|
2026-04-01 23:58:07 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-02 09:33:03 +08:00
|
|
|
export function registerTicTacToeCommands(game: GameContextInstance) {
|
|
|
|
|
game.registerCommand('start', createSetupCommand());
|
|
|
|
|
game.registerCommand('turn', createTurnCommand());
|
2026-04-01 23:58:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function startTicTacToe(game: GameContextInstance) {
|
|
|
|
|
game.dispatchCommand('start');
|
|
|
|
|
}
|