add game rules
This commit is contained in:
parent
39138354b7
commit
d511a3d16c
17
src/common/game_rules/GRLastStoneInBank.ts
Normal file
17
src/common/game_rules/GRLastStoneInBank.ts
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
34
src/common/game_rules/GRLastStoneInEmptyPit.ts
Normal file
34
src/common/game_rules/GRLastStoneInEmptyPit.ts
Normal 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
7
src/core/GameRule.ts
Normal 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;
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user