Skip to content

Commit

Permalink
Fix game over check
Browse files Browse the repository at this point in the history
  • Loading branch information
vehlwn committed Jun 23, 2024
1 parent 7c4aa26 commit 464792b
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions front/src/Game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,6 @@ export class Game extends EventTarget {
new LogMessageEvent("captured " + last_move.captured)
);
}
this.check_game_over();
}

private highlight_move(m: Move) {
Expand Down Expand Up @@ -207,6 +206,7 @@ export class Game extends EventTarget {
this.enable_user_input();
}
this.update_view(last_move);
this.check_game_over();
this.highlight_move(last_move);
})
.catch((e) => {
Expand Down Expand Up @@ -242,19 +242,21 @@ export class Game extends EventTarget {
}
}

private check_game_over() {
private check_game_over(): boolean {
if (this.chess.isGameOver()) {
let msg = "";
if (this.chess.isCheckmate()) {
msg = "checkmate";
} else if (this.chess.isStalemate()) {
msg = "stalemate";
} else {
return;
return false;
}
this.board.disableMoveInput();
this.dispatchEvent(new GameOverEvent(msg));
return true;
}
return false;
}

private enable_user_input() {
Expand Down Expand Up @@ -314,7 +316,9 @@ export class Game extends EventTarget {
event.chessboard.state.moveInputProcess.then(() => {
// update position, maybe castled
this.update_view(move_result);
this.do_computer_move();
if (!this.check_game_over()) {
this.do_computer_move();
}
});
return true;
} catch (er) {
Expand Down Expand Up @@ -342,7 +346,9 @@ export class Game extends EventTarget {
promotion: selected.piece.charAt(1)
});
this.update_view(move_result);
this.do_computer_move();
if (!this.check_game_over()) {
this.do_computer_move();
}
} else {
// promotion canceled
this.enable_user_input();
Expand Down

0 comments on commit 464792b

Please sign in to comment.