refactor: update sample
This commit is contained in:
parent
9e2947a8d6
commit
8f86b88c88
|
|
@ -59,25 +59,23 @@ async function setup(game: TicTacToeGame) {
|
||||||
registry.register('setup', setup);
|
registry.register('setup', setup);
|
||||||
|
|
||||||
async function turn(game: TicTacToeGame, turnPlayer: PlayerType, turnNumber: number) {
|
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>',
|
'play <player> <row:number> <col:number>',
|
||||||
(command) => {
|
(command) => {
|
||||||
const [player, row, col] = command.params as [PlayerType, number, number];
|
const [player, row, col] = command.params as [PlayerType, number, number];
|
||||||
|
|
||||||
if (player !== turnPlayer) {
|
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
|
game.value.currentPlayer
|
||||||
);
|
);
|
||||||
const [player, row, col] = playCmd.params as [PlayerType, number, number];
|
|
||||||
|
|
||||||
placePiece(game, row, col, turnPlayer);
|
placePiece(game, row, col, turnPlayer);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue