diff --git a/src/samples/tic-tac-toe.ts b/src/samples/tic-tac-toe.ts index 7526af9..ee4fab7 100644 --- a/src/samples/tic-tac-toe.ts +++ b/src/samples/tic-tac-toe.ts @@ -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 ', (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);