fix game rule issues

This commit is contained in:
Halit Aksoy 2022-05-04 15:03:41 +03:00
parent c970c7c7fa
commit 9902404ca8
2 changed files with 18 additions and 16 deletions

View File

@ -5,6 +5,7 @@ export class GRLastStoneInBank implements GameRule {
onGameMoveStart(game: MancalaGame, index: number): void {}
onGameMove(game: MancalaGame, index: number): void {}
onGameMoveEnd(game: MancalaGame, index: number): void {
index = game.board.getPitIndexCircularly(index);
const pitType = game.board.getPitTypeByIndex(index);
if (
(pitType === 'player1Bank' && game.isTurnPlayer1()) ||

View File

@ -5,30 +5,31 @@ export class GRLastStoneInEmptyPit implements GameRule {
onGameMoveStart(game: MancalaGame, index: number): void {}
onGameMove(game: MancalaGame, index: number): void {}
onGameMoveEnd(game: MancalaGame, index: number): void {
index = game.board.getPitIndexCircularly(index);
const pit = game.board.pits[index];
const pitType = game.board.getPitTypeByIndex(index);
if (pit.stoneCount === 1) {
if (pitType === 'player1Pit' && game.isTurnPlayer1()) {
const oppositePit =
game.board.pits[this.getOppositePitIndex(game, index)];
const player1BankIndex = game.board.pits[game.board.player1BankIndex()];
player1BankIndex.stoneCount += 1 + oppositePit.stoneCount;
oppositePit.stoneCount = 0;
pit.stoneCount = 0;
game.board.pits[game.board.getOppositePitIndex(index)];
if (oppositePit.stoneCount > 0) {
const player1BankIndex =
game.board.pits[game.board.player1BankIndex()];
player1BankIndex.stoneCount += 1 + oppositePit.stoneCount;
oppositePit.stoneCount = 0;
pit.stoneCount = 0;
}
} else if (pitType === 'player2Pit' && game.isTurnPlayer2()) {
const oppositePit =
game.board.pits[this.getOppositePitIndex(game, index)];
const player2BankIndex = game.board.pits[game.board.player2BankIndex()];
player2BankIndex.stoneCount += 1 + oppositePit.stoneCount;
oppositePit.stoneCount = 0;
pit.stoneCount = 0;
game.board.pits[game.board.getOppositePitIndex(index)];
if (oppositePit.stoneCount > 0) {
const player2BankIndex =
game.board.pits[game.board.player2BankIndex()];
player2BankIndex.stoneCount += 1 + oppositePit.stoneCount;
oppositePit.stoneCount = 0;
pit.stoneCount = 0;
}
}
}
}
getOppositePitIndex(game: MancalaGame, index: number) {
return game.board.getPitIndexCircularly(
index + game.board.totalPitCount() / 2
);
}
}