From 9902404ca87e33fbbb41214e2446bc20d0103c98 Mon Sep 17 00:00:00 2001 From: Halit Aksoy Date: Wed, 4 May 2022 15:03:41 +0300 Subject: [PATCH] fix game rule issues --- src/common/game_rules/GRLastStoneInBank.ts | 1 + .../game_rules/GRLastStoneInEmptyPit.ts | 33 ++++++++++--------- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/common/game_rules/GRLastStoneInBank.ts b/src/common/game_rules/GRLastStoneInBank.ts index de64711..3969d02 100644 --- a/src/common/game_rules/GRLastStoneInBank.ts +++ b/src/common/game_rules/GRLastStoneInBank.ts @@ -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()) || diff --git a/src/common/game_rules/GRLastStoneInEmptyPit.ts b/src/common/game_rules/GRLastStoneInEmptyPit.ts index 61dbc79..7481e4e 100644 --- a/src/common/game_rules/GRLastStoneInEmptyPit.ts +++ b/src/common/game_rules/GRLastStoneInEmptyPit.ts @@ -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 - ); - } }