mancala/src/common/game_rules/GRClearBoardAtEnd.ts

53 lines
1.8 KiB
TypeScript
Raw Normal View History

2022-05-08 16:59:58 +03:00
import { Board } from '../../core/Board';
2022-05-04 15:04:01 +03:00
import { GameRule } from '../../core/GameRule';
2022-05-08 16:59:58 +03:00
import { GameStep } from '../../core/HistoryItem';
2022-05-04 15:04:01 +03:00
import { MancalaGame } from '../../core/MancalaGame';
2022-05-08 16:59:58 +03:00
export const GAME_STEP_BOARD_CLEARED = 'GAME_STEP_BOARD_CLEARED';
export type ClearBoardAtEndData = { pitIndexesThatHasStone: number[] };
2022-05-04 15:04:01 +03:00
export class GRClearBoardAtEnd implements GameRule {
onGameMoveStart(game: MancalaGame, index: number): void {}
onGameMove(game: MancalaGame, index: number): void {}
onGameMoveEnd(game: MancalaGame, index: number): void {
if (game.getPlayer1StoneCountInPits() === 0) {
2022-05-08 16:59:58 +03:00
const clearBoardAtEndData = {
pitIndexesThatHasStone: this.getPitIndexesThatHasStone(game.board)
};
2022-05-04 15:04:01 +03:00
game.board.player1Bank.stoneCount += game.getPlayer2StoneCountInPits();
game.board.clearPlayer2Pits();
2022-05-08 16:59:58 +03:00
this.addGameStep(game, index, clearBoardAtEndData);
} else if (game.getPlayer2StoneCountInPits() === 0) {
const clearBoardAtEndData = {
pitIndexesThatHasStone: this.getPitIndexesThatHasStone(game.board)
};
2022-05-04 15:04:01 +03:00
game.board.player2Bank.stoneCount += game.getPlayer1StoneCountInPits();
game.board.clearPlayer1Pits();
2022-05-08 16:59:58 +03:00
this.addGameStep(game, index, clearBoardAtEndData);
}
}
private getPitIndexesThatHasStone(board: Board): number[] {
let index = 0;
const indexList = [];
for (const stoneCount of board.getStoneArray()) {
if (stoneCount > 0 && board.checkPitTypeIsNormalPitByIndex(index)) {
indexList.push(index);
}
index++;
2022-05-04 15:04:01 +03:00
}
2022-05-08 16:59:58 +03:00
return indexList;
}
private addGameStep(
game: MancalaGame,
index: number,
clearBoardAtEndData: ClearBoardAtEndData
) {
game.addGameStep(
new GameStep(index, GAME_STEP_BOARD_CLEARED, clearBoardAtEndData)
);
2022-05-04 15:04:01 +03:00
}
}