Add takeback button

This commit is contained in:
sepia 2025-07-22 19:06:12 -05:00
parent 3093754bd4
commit 1a221bf680
6 changed files with 217 additions and 26 deletions

View file

@ -7,7 +7,7 @@ export class GomokuGame {
public currentPlayerColor: null | PlayerColor;
public status: GameStatus;
public winnerColor: null | PlayerColor | 'draw';
public history: { row: number, col: number }[];
public history: { row: number; col: number }[];
private readonly boardSize = 15;
private moveCount = 0;
@ -82,6 +82,24 @@ export class GomokuGame {
this.currentPlayerColor = null;
}
public undoMove() {
if (this.history.length === 0) {
return;
}
const lastMove = this.history.pop();
if (lastMove) {
this.board[lastMove.row][lastMove.col] = null;
this.moveCount--;
this.currentPlayerColor =
this.currentPlayerColor === 'black' ? 'white' : 'black';
if (this.status === 'finished') {
this.status = 'playing';
this.winnerColor = null;
}
}
}
private checkWin(row: number, col: number, color: PlayerColor): boolean {
const directions = [
[1, 0], // vertical