refactor: update sample

This commit is contained in:
hypercross 2026-04-04 21:38:16 +08:00
parent 9e2947a8d6
commit 8f86b88c88
1 changed files with 8 additions and 10 deletions

View File

@ -59,25 +59,23 @@ async function setup(game: TicTacToeGame) {
registry.register('setup', setup);
async function turn(game: TicTacToeGame, turnPlayer: PlayerType, turnNumber: number) {
const playCmd = await game.prompt(
const {player, row, col} = await game.prompt(
'play <player> <row:number> <col:number>',
(command) => {
const [player, row, col] = command.params as [PlayerType, number, number];
if (player !== turnPlayer) {
return `Invalid player: ${player}. Expected ${turnPlayer}.`;
throw new Error(`Invalid player: ${player}. Expected ${turnPlayer}.`);
} else if (!isValidMove(row, col)) {
throw new Error(`Invalid position: (${row}, ${col}). Must be between 0 and ${BOARD_SIZE - 1}.`);
} else if (isCellOccupied(game, row, col)) {
throw new Error(`Cell (${row}, ${col}) is already occupied.`);
} else {
return { player, row, col };
}
if (!isValidMove(row, col)) {
return `Invalid position: (${row}, ${col}). Must be between 0 and ${BOARD_SIZE - 1}.`;
}
if (isCellOccupied(game, row, col)) {
return `Cell (${row}, ${col}) is already occupied.`;
}
return null;
},
game.value.currentPlayer
);
const [player, row, col] = playCmd.params as [PlayerType, number, number];
placePiece(game, row, col, turnPlayer);