add game rules

This commit is contained in:
Halit Aksoy 2022-05-02 00:42:38 +03:00
parent 39138354b7
commit d511a3d16c
3 changed files with 58 additions and 0 deletions

View File

@ -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();
}
}
}

View File

@ -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
);
}
}

7
src/core/GameRule.ts Normal file
View File

@ -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;
}