Merge pull request #2 from jhalitaksoy/feature/basic-history

feature : add game history
This commit is contained in:
Halit Aksoy 2022-05-05 23:52:16 +03:00 committed by GitHub
commit 83d33e3984
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 98 additions and 16 deletions

View File

@ -6,10 +6,18 @@ import { GRLastStoneInEmptyPit } from './game_rules/GRLastStoneInEmptyPit';
export class CommonMancalaGame extends MancalaGame { export class CommonMancalaGame extends MancalaGame {
constructor(id: string, player1Id: string, player2Id: string) { constructor(id: string, player1Id: string, player2Id: string) {
super(id, new CommonBoard(), player1Id, player2Id, player1Id, [ super(
id,
new CommonBoard(),
player1Id,
player2Id,
player1Id,
[
new GRLastStoneInEmptyPit(), new GRLastStoneInEmptyPit(),
new GRLastStoneInBank(), new GRLastStoneInBank(),
new GRClearBoardAtEnd() new GRClearBoardAtEnd()
]); ],
[]
);
} }
} }

16
src/core/HistoryItem.ts Normal file
View File

@ -0,0 +1,16 @@
export class HistoryItem {
boardSnapshot: number[];
constructor(boardSnapshot: number[]) {
this.boardSnapshot = boardSnapshot;
}
}
export class MoveHistoryItem extends HistoryItem {
playerId: string;
moveIndex: number;
constructor(playerId: string, moveIndex: number, boardSnapshot: number[]) {
super(boardSnapshot);
this.playerId = playerId;
this.moveIndex = moveIndex;
}
}

View File

@ -1,5 +1,6 @@
import { Board, PitType } from './Board'; import { Board, PitType } from './Board';
import { GameRule } from './GameRule'; import { GameRule } from './GameRule';
import { HistoryItem, MoveHistoryItem } from './HistoryItem';
export type GameState = 'initial' | 'playing' | 'ended'; export type GameState = 'initial' | 'playing' | 'ended';
@ -11,6 +12,7 @@ export class MancalaGame {
turnPlayerId: string; turnPlayerId: string;
state: GameState; state: GameState;
gameRules: GameRule[]; gameRules: GameRule[];
history: HistoryItem[];
constructor( constructor(
id: string, id: string,
@ -19,6 +21,7 @@ export class MancalaGame {
player2Id: string, player2Id: string,
turnPlayerId: string, turnPlayerId: string,
gameRules: GameRule[], gameRules: GameRule[],
history: HistoryItem[],
state: GameState = 'initial' state: GameState = 'initial'
) { ) {
this.id = id; this.id = id;
@ -28,6 +31,7 @@ export class MancalaGame {
this.turnPlayerId = turnPlayerId; this.turnPlayerId = turnPlayerId;
this.state = state; this.state = state;
this.gameRules = gameRules; this.gameRules = gameRules;
this.history = history;
this.listenBoardMoveEvents(); this.listenBoardMoveEvents();
} }
@ -126,7 +130,11 @@ export class MancalaGame {
this.state = 'playing'; this.state = 'playing';
} }
if (this.canPlayerMove(playerId, pitIndex)) { if (this.canPlayerMove(playerId, pitIndex)) {
this.board.move(this.getBoardIndexByPlayerId(playerId, pitIndex)); const moveIndex = this.getBoardIndexByPlayerId(playerId, pitIndex);
this.board.move(moveIndex);
this.history.push(
new MoveHistoryItem(playerId, moveIndex, this.board.getStoneArray())
);
if (this.checkGameIsEnded()) { if (this.checkGameIsEnded()) {
this.state = 'ended'; this.state = 'ended';
} }
@ -206,6 +214,7 @@ export class MancalaGame {
mancalaGame.player2Id, mancalaGame.player2Id,
mancalaGame.turnPlayerId, mancalaGame.turnPlayerId,
mancalaGame.gameRules, mancalaGame.gameRules,
mancalaGame.history,
mancalaGame.state mancalaGame.state
); );
} }

View File

@ -2,17 +2,26 @@ import { GRClearBoardAtEnd } from '../src/common/game_rules/GRClearBoardAtEnd';
import { GRLastStoneInBank } from '../src/common/game_rules/GRLastStoneInBank'; import { GRLastStoneInBank } from '../src/common/game_rules/GRLastStoneInBank';
import { GRLastStoneInEmptyPit } from '../src/common/game_rules/GRLastStoneInEmptyPit'; import { GRLastStoneInEmptyPit } from '../src/common/game_rules/GRLastStoneInEmptyPit';
import { Board } from '../src/core/Board'; import { Board } from '../src/core/Board';
import { MoveHistoryItem } from '../src/core/HistoryItem';
import { MancalaGame } from '../src/core/MancalaGame'; import { MancalaGame } from '../src/core/MancalaGame';
function createGame(): MancalaGame { function createGame(): MancalaGame {
const board = new Board(6, 4); const board = new Board(6, 4);
const player1Id = '0'; const player1Id = '0';
const player2Id = '1'; const player2Id = '1';
const game = new MancalaGame('0', board, player1Id, player2Id, player1Id, [ const game = new MancalaGame(
'0',
board,
player1Id,
player2Id,
player1Id,
[
new GRLastStoneInEmptyPit(), new GRLastStoneInEmptyPit(),
new GRLastStoneInBank(), new GRLastStoneInBank(),
new GRClearBoardAtEnd() new GRClearBoardAtEnd()
]); ],
[]
);
return game; return game;
} }
@ -107,4 +116,36 @@ describe('Game Test', () => {
0, 0, 4, 4, 4, 4, 5, 4, 4, 4, 4, 0, 4, 0 0, 0, 4, 4, 4, 4, 5, 4, 4, 4, 4, 0, 4, 0
]); ]);
}); });
test('test game history', () => {
const game = createGame();
const player1Id = '0';
const player2Id = '1';
game.moveByPlayerPit(player1Id, 0);
game.moveByPlayerPit(player2Id, 0);
game.moveByPlayerPit(player1Id, 1);
game.moveByPlayerPit(player2Id, 1);
expect(game.history).toStrictEqual([
new MoveHistoryItem(
player1Id,
0,
[1, 5, 5, 5, 4, 4, 0, 4, 4, 4, 4, 4, 4, 0]
),
new MoveHistoryItem(
player2Id,
7,
[1, 5, 5, 5, 4, 4, 0, 1, 5, 5, 5, 4, 4, 0]
),
new MoveHistoryItem(
player1Id,
1,
[1, 1, 6, 6, 5, 5, 0, 1, 5, 5, 5, 4, 4, 0]
),
new MoveHistoryItem(
player2Id,
8,
[1, 1, 6, 6, 5, 5, 0, 1, 1, 6, 6, 5, 5, 0]
)
]);
});
}); });

View File

@ -8,11 +8,19 @@ function createGame(): MancalaGame {
const board = new Board(6, 4); const board = new Board(6, 4);
const player1Id = '0'; const player1Id = '0';
const player2Id = '1'; const player2Id = '1';
const game = new MancalaGame('0', board, player1Id, player2Id, player1Id, [ const game = new MancalaGame(
'0',
board,
player1Id,
player2Id,
player1Id,
[
new GRLastStoneInEmptyPit(), new GRLastStoneInEmptyPit(),
new GRLastStoneInBank(), new GRLastStoneInBank(),
new GRClearBoardAtEnd() new GRClearBoardAtEnd()
]); ],
[]
);
return game; return game;
} }