add GameStep for game rules

This commit is contained in:
Halit Aksoy 2022-05-08 16:59:58 +03:00
parent 8abecca909
commit 5a4caca9a4
5 changed files with 97 additions and 6 deletions

View File

@ -1,17 +1,52 @@
import { Board } from '../../core/Board';
import { GameRule } from '../../core/GameRule'; import { GameRule } from '../../core/GameRule';
import { GameStep } from '../../core/HistoryItem';
import { MancalaGame } from '../../core/MancalaGame'; import { MancalaGame } from '../../core/MancalaGame';
export const GAME_STEP_BOARD_CLEARED = 'GAME_STEP_BOARD_CLEARED';
export type ClearBoardAtEndData = { pitIndexesThatHasStone: number[] };
export class GRClearBoardAtEnd implements GameRule { export class GRClearBoardAtEnd implements GameRule {
onGameMoveStart(game: MancalaGame, index: number): void {} onGameMoveStart(game: MancalaGame, index: number): void {}
onGameMove(game: MancalaGame, index: number): void {} onGameMove(game: MancalaGame, index: number): void {}
onGameMoveEnd(game: MancalaGame, index: number): void { onGameMoveEnd(game: MancalaGame, index: number): void {
if (game.getPlayer1StoneCountInPits() === 0) { if (game.getPlayer1StoneCountInPits() === 0) {
const clearBoardAtEndData = {
pitIndexesThatHasStone: this.getPitIndexesThatHasStone(game.board)
};
game.board.player1Bank.stoneCount += game.getPlayer2StoneCountInPits(); game.board.player1Bank.stoneCount += game.getPlayer2StoneCountInPits();
game.board.clearPlayer2Pits(); game.board.clearPlayer2Pits();
} this.addGameStep(game, index, clearBoardAtEndData);
if (game.getPlayer2StoneCountInPits() === 0) { } else if (game.getPlayer2StoneCountInPits() === 0) {
const clearBoardAtEndData = {
pitIndexesThatHasStone: this.getPitIndexesThatHasStone(game.board)
};
game.board.player2Bank.stoneCount += game.getPlayer1StoneCountInPits(); game.board.player2Bank.stoneCount += game.getPlayer1StoneCountInPits();
game.board.clearPlayer1Pits(); game.board.clearPlayer1Pits();
this.addGameStep(game, index, clearBoardAtEndData);
} }
} }
private getPitIndexesThatHasStone(board: Board): number[] {
let index = 0;
const indexList = [];
for (const stoneCount of board.getStoneArray()) {
if (stoneCount > 0 && board.checkPitTypeIsNormalPitByIndex(index)) {
indexList.push(index);
}
index++;
}
return indexList;
}
private addGameStep(
game: MancalaGame,
index: number,
clearBoardAtEndData: ClearBoardAtEndData
) {
game.addGameStep(
new GameStep(index, GAME_STEP_BOARD_CLEARED, clearBoardAtEndData)
);
}
} }

View File

@ -1,6 +1,9 @@
import { GameRule } from '../../core/GameRule'; import { GameRule } from '../../core/GameRule';
import { GameStep } from '../../core/HistoryItem';
import { MancalaGame } from '../../core/MancalaGame'; import { MancalaGame } from '../../core/MancalaGame';
export const GAME_STEP_LAST_STONE_IN_BANK = 'GAME_STEP_LAST_STONE_IN_BANK';
export class GRLastStoneInBank implements GameRule { export class GRLastStoneInBank implements GameRule {
onGameMoveStart(game: MancalaGame, index: number): void {} onGameMoveStart(game: MancalaGame, index: number): void {}
onGameMove(game: MancalaGame, index: number): void {} onGameMove(game: MancalaGame, index: number): void {}
@ -11,6 +14,7 @@ export class GRLastStoneInBank implements GameRule {
(pitType === 'player1Bank' && game.isTurnPlayer1()) || (pitType === 'player1Bank' && game.isTurnPlayer1()) ||
(pitType === 'player2Bank' && game.isTurnPlayer2()) (pitType === 'player2Bank' && game.isTurnPlayer2())
) { ) {
game.addGameStep(new GameStep(index, GAME_STEP_LAST_STONE_IN_BANK));
} else { } else {
game.changePlayerTurn(); game.changePlayerTurn();
} }

View File

@ -1,6 +1,12 @@
import { GameRule } from '../../core/GameRule'; import { GameRule } from '../../core/GameRule';
import { GameStep } from '../../core/HistoryItem';
import { MancalaGame } from '../../core/MancalaGame'; import { MancalaGame } from '../../core/MancalaGame';
export const GAME_STEP_LAST_STONE_IN_EMPTY_PIT =
'GAME_STEP_LAST_STONE_IN_EMPTY_PIT';
export type LastStoneInEmptyPitData = { oppositeIndex: number };
export class GRLastStoneInEmptyPit implements GameRule { export class GRLastStoneInEmptyPit implements GameRule {
onGameMoveStart(game: MancalaGame, index: number): void {} onGameMoveStart(game: MancalaGame, index: number): void {}
onGameMove(game: MancalaGame, index: number): void {} onGameMove(game: MancalaGame, index: number): void {}
@ -10,26 +16,38 @@ export class GRLastStoneInEmptyPit implements GameRule {
const pitType = game.board.getPitTypeByIndex(index); const pitType = game.board.getPitTypeByIndex(index);
if (pit.stoneCount === 1) { if (pit.stoneCount === 1) {
if (pitType === 'player1Pit' && game.isTurnPlayer1()) { if (pitType === 'player1Pit' && game.isTurnPlayer1()) {
const oppositePit = const oppositeIndex = game.board.getOppositePitIndex(index);
game.board.pits[game.board.getOppositePitIndex(index)]; const oppositePit = game.board.pits[oppositeIndex];
if (oppositePit.stoneCount > 0) { if (oppositePit.stoneCount > 0) {
const player1BankIndex = const player1BankIndex =
game.board.pits[game.board.player1BankIndex()]; game.board.pits[game.board.player1BankIndex()];
player1BankIndex.stoneCount += 1 + oppositePit.stoneCount; player1BankIndex.stoneCount += 1 + oppositePit.stoneCount;
oppositePit.stoneCount = 0; oppositePit.stoneCount = 0;
pit.stoneCount = 0; pit.stoneCount = 0;
this.addGameStep(game, index, { oppositeIndex });
} }
} else if (pitType === 'player2Pit' && game.isTurnPlayer2()) { } else if (pitType === 'player2Pit' && game.isTurnPlayer2()) {
const oppositePit = const oppositeIndex = game.board.getOppositePitIndex(index);
game.board.pits[game.board.getOppositePitIndex(index)]; const oppositePit = game.board.pits[oppositeIndex];
if (oppositePit.stoneCount > 0) { if (oppositePit.stoneCount > 0) {
const player2BankIndex = const player2BankIndex =
game.board.pits[game.board.player2BankIndex()]; game.board.pits[game.board.player2BankIndex()];
player2BankIndex.stoneCount += 1 + oppositePit.stoneCount; player2BankIndex.stoneCount += 1 + oppositePit.stoneCount;
oppositePit.stoneCount = 0; oppositePit.stoneCount = 0;
pit.stoneCount = 0; pit.stoneCount = 0;
this.addGameStep(game, index, { oppositeIndex });
} }
} }
} }
} }
private addGameStep(
game: MancalaGame,
index: number,
data: LastStoneInEmptyPitData
) {
game.addGameStep(
new GameStep(index, GAME_STEP_LAST_STONE_IN_EMPTY_PIT, data)
);
}
} }

View File

@ -35,5 +35,12 @@ describe('GRClearBoardAtEnd Test', () => {
.map((pit) => pit.stoneCount) .map((pit) => pit.stoneCount)
.reduce((sum, stoneCount) => sum + stoneCount, 0) .reduce((sum, stoneCount) => sum + stoneCount, 0)
).toBe(0); ).toBe(0);
expect(game.history[0].gameSteps).toStrictEqual([
new GameStep(6, GAME_STEP_GAME_MOVE),
new GameStep(6, GAME_STEP_LAST_STONE_IN_BANK),
new GameStep(6, GAME_STEP_BOARD_CLEARED, {
pitIndexesThatHasStone: [12]
})
]);
}); });
}); });

View File

@ -0,0 +1,27 @@
import { GAME_STEP_GAME_MOVE } from '../../src/core/MancalaGame';
import { GameStep } from '../../src/core/HistoryItem';
import { createGame } from '../TestUtil';
import { GAME_STEP_LAST_STONE_IN_EMPTY_PIT } from '../../src/common/game_rules/GRLastStoneInEmptyPit';
describe('GRClearBoardAtEnd Test', () => {
test('test GRClearBoardAtEnd 1', () => {
const game = createGame();
const board = game.board;
const initialBoard = [4, 4, 4, 4, 4, 4, 0, 4, 4, 4, 4, 4, 4, 0];
expect(board.getStoneArray()).toStrictEqual(initialBoard);
game.board.player1Pits[0].stoneCount = 1;
game.board.player1Pits[1].stoneCount = 0;
expect(board.getStoneArray()).toStrictEqual([
1, 0, 4, 4, 4, 4, 0, 4, 4, 4, 4, 4, 4, 0
]);
game.moveByPlayerPit('0', 0);
expect(board.getStoneArray()).toStrictEqual([
0, 0, 4, 4, 4, 4, 5, 4, 4, 4, 4, 0, 4, 0
]);
expect(game.history[0].gameSteps).toStrictEqual([
new GameStep(1, GAME_STEP_GAME_MOVE),
new GameStep(1, GAME_STEP_LAST_STONE_IN_EMPTY_PIT, { oppositeIndex: 11 })
]);
});
});