refactor: just throw Error if fail
This commit is contained in:
@@ -90,7 +90,7 @@ const boopCommand = registry.register('boop <row:number> <col:number> <type>', b
|
||||
/**
|
||||
* 检查是否有玩家获胜(三个猫连线)
|
||||
*/
|
||||
async function checkWin(game: BoopGame) {
|
||||
async function checkWin(game: BoopGame): Promise<WinnerType | null> {
|
||||
for(const line of getLineCandidates()){
|
||||
let whites = 0;
|
||||
let blacks = 0;
|
||||
@@ -151,10 +151,9 @@ async function setup(game: BoopGame) {
|
||||
while (true) {
|
||||
const currentPlayer = game.value.currentPlayer;
|
||||
const turnOutput = await turnCommand(game, currentPlayer);
|
||||
if (!turnOutput.success) throw new Error(turnOutput.error);
|
||||
|
||||
await game.produceAsync(state => {
|
||||
state.winner = turnOutput.result.winner;
|
||||
state.winner = turnOutput.winner;
|
||||
if (!state.winner) {
|
||||
state.currentPlayer = state.currentPlayer === 'white' ? 'black' : 'white';
|
||||
}
|
||||
@@ -233,7 +232,7 @@ async function turn(game: BoopGame, turnPlayer: PlayerType) {
|
||||
await placeCommand(game, row, col, turnPlayer, pieceType);
|
||||
await boopCommand(game, row, col, pieceType);
|
||||
const winner = await checkWinCommand(game);
|
||||
if(winner.success) return { winner: winner.result as WinnerType };
|
||||
if(winner) return { winner: winner };
|
||||
|
||||
await checkGraduatesCommand(game);
|
||||
await checkFullBoard(game, turnPlayer);
|
||||
|
||||
@@ -42,10 +42,9 @@ async function setup(game: TicTacToeGame) {
|
||||
const currentPlayer = game.value.currentPlayer;
|
||||
const turnNumber = game.value.turn + 1;
|
||||
const turnOutput = await turnCommand(game, currentPlayer, turnNumber);
|
||||
if (!turnOutput.success) throw new Error(turnOutput.error);
|
||||
|
||||
game.produce(state => {
|
||||
state.winner = turnOutput.result.winner;
|
||||
state.winner = turnOutput.winner;
|
||||
if (!state.winner) {
|
||||
state.currentPlayer = state.currentPlayer === 'X' ? 'O' : 'X';
|
||||
state.turn = turnNumber;
|
||||
|
||||
Reference in New Issue
Block a user