From d511a3d16c7c5d4b075a195afd0c083dcfce9415 Mon Sep 17 00:00:00 2001 From: Halit Aksoy Date: Mon, 2 May 2022 00:42:38 +0300 Subject: [PATCH] add game rules --- src/common/game_rules/GRLastStoneInBank.ts | 17 ++++++++++ .../game_rules/GRLastStoneInEmptyPit.ts | 34 +++++++++++++++++++ src/core/GameRule.ts | 7 ++++ 3 files changed, 58 insertions(+) create mode 100644 src/common/game_rules/GRLastStoneInBank.ts create mode 100644 src/common/game_rules/GRLastStoneInEmptyPit.ts create mode 100644 src/core/GameRule.ts diff --git a/src/common/game_rules/GRLastStoneInBank.ts b/src/common/game_rules/GRLastStoneInBank.ts new file mode 100644 index 0000000..de64711 --- /dev/null +++ b/src/common/game_rules/GRLastStoneInBank.ts @@ -0,0 +1,17 @@ +import { GameRule } from '../../core/GameRule'; +import { MancalaGame } from '../../core/MancalaGame'; + +export class GRLastStoneInBank implements GameRule { + onGameMoveStart(game: MancalaGame, index: number): void {} + onGameMove(game: MancalaGame, index: number): void {} + onGameMoveEnd(game: MancalaGame, index: number): void { + const pitType = game.board.getPitTypeByIndex(index); + if ( + (pitType === 'player1Bank' && game.isTurnPlayer1()) || + (pitType === 'player2Bank' && game.isTurnPlayer2()) + ) { + } else { + game.changePlayerTurn(); + } + } +} diff --git a/src/common/game_rules/GRLastStoneInEmptyPit.ts b/src/common/game_rules/GRLastStoneInEmptyPit.ts new file mode 100644 index 0000000..61dbc79 --- /dev/null +++ b/src/common/game_rules/GRLastStoneInEmptyPit.ts @@ -0,0 +1,34 @@ +import { GameRule } from '../../core/GameRule'; +import { MancalaGame } from '../../core/MancalaGame'; + +export class GRLastStoneInEmptyPit implements GameRule { + onGameMoveStart(game: MancalaGame, index: number): void {} + onGameMove(game: MancalaGame, index: number): void {} + onGameMoveEnd(game: MancalaGame, index: number): void { + 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; + } 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; + } + } + } + + getOppositePitIndex(game: MancalaGame, index: number) { + return game.board.getPitIndexCircularly( + index + game.board.totalPitCount() / 2 + ); + } +} diff --git a/src/core/GameRule.ts b/src/core/GameRule.ts new file mode 100644 index 0000000..be214fc --- /dev/null +++ b/src/core/GameRule.ts @@ -0,0 +1,7 @@ +import { MancalaGame } from './MancalaGame'; + +export interface GameRule { + onGameMoveStart(game: MancalaGame, index: number): void; + onGameMove(game: MancalaGame, index: number): void; + onGameMoveEnd(game: MancalaGame, index: number): void; +}