From 8abecca9092ebe71b7edef73d6a4deb480af8047 Mon Sep 17 00:00:00 2001 From: Halit Aksoy Date: Sun, 8 May 2022 16:59:35 +0300 Subject: [PATCH] add GameStep --- src/core/HistoryItem.ts | 24 +++++++++++-- src/core/MancalaGame.ts | 25 +++++++++++--- tests/MancalaGame.test.ts | 72 +++++++++++++++++++++++++++------------ 3 files changed, 92 insertions(+), 29 deletions(-) diff --git a/src/core/HistoryItem.ts b/src/core/HistoryItem.ts index 107b3ec..f0cb36a 100644 --- a/src/core/HistoryItem.ts +++ b/src/core/HistoryItem.ts @@ -1,16 +1,34 @@ export class HistoryItem { boardSnapshot: number[]; - constructor(boardSnapshot: number[]) { + gameSteps: GameStep[]; + constructor(boardSnapshot: number[], gameSteps: GameStep[]) { this.boardSnapshot = boardSnapshot; + this.gameSteps = gameSteps; } } export class MoveHistoryItem extends HistoryItem { playerId: string; moveIndex: number; - constructor(playerId: string, moveIndex: number, boardSnapshot: number[]) { - super(boardSnapshot); + constructor( + playerId: string, + moveIndex: number, + boardSnapshot: number[], + gameSteps: GameStep[] + ) { + super(boardSnapshot, gameSteps); this.playerId = playerId; this.moveIndex = moveIndex; } } + +export class GameStep { + index: number; + type: string; + data: any | null; + constructor(index: number, type: string, data: any = null) { + this.index = index; + this.type = type; + this.data = data; + } +} diff --git a/src/core/MancalaGame.ts b/src/core/MancalaGame.ts index afa6038..d613bf2 100644 --- a/src/core/MancalaGame.ts +++ b/src/core/MancalaGame.ts @@ -1,9 +1,11 @@ import { Board, PitType } from './Board'; import { GameRule } from './GameRule'; -import { HistoryItem, MoveHistoryItem } from './HistoryItem'; +import { HistoryItem, MoveHistoryItem, GameStep } from './HistoryItem'; export type GameState = 'initial' | 'playing' | 'ended'; +export const GAME_STEP_GAME_MOVE = 'GAME_STEP_GAME_MOVE'; + export class MancalaGame { id: string; board: Board; @@ -13,6 +15,7 @@ export class MancalaGame { state: GameState; gameRules: GameRule[]; history: HistoryItem[]; + currentHistoryItem: HistoryItem | null = null; constructor( id: string, @@ -42,6 +45,7 @@ export class MancalaGame { }); }; this.board.onGameMove = (index: number) => { + this.addGameStep(new GameStep(index, GAME_STEP_GAME_MOVE)); this.gameRules.forEach((gameRule) => { gameRule.onGameMove(this, index); }); @@ -131,10 +135,15 @@ export class MancalaGame { } if (this.canPlayerMove(playerId, pitIndex)) { const moveIndex = this.getBoardIndexByPlayerId(playerId, pitIndex); - this.board.move(moveIndex); - this.history.push( - new MoveHistoryItem(playerId, moveIndex, this.board.getStoneArray()) + this.currentHistoryItem = new MoveHistoryItem( + playerId, + moveIndex, + [], + [] ); + this.board.move(moveIndex); + this.currentHistoryItem.boardSnapshot = this.board.getStoneArray(); + this.history.push(this.currentHistoryItem); if (this.checkGameIsEnded()) { this.state = 'ended'; } @@ -218,4 +227,12 @@ export class MancalaGame { mancalaGame.state ); } + + public getCurrentHistoryItem(): HistoryItem | null { + return this.currentHistoryItem; + } + + public addGameStep(gameStep: GameStep) { + this.getCurrentHistoryItem()?.gameSteps.push(gameStep); + } } diff --git a/tests/MancalaGame.test.ts b/tests/MancalaGame.test.ts index 27597a8..e8c39d9 100644 --- a/tests/MancalaGame.test.ts +++ b/tests/MancalaGame.test.ts @@ -102,27 +102,55 @@ describe('Game Test', () => { 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] - ) - ]); + const historyItem1 = new MoveHistoryItem( + player1Id, + 0, + [1, 5, 5, 5, 4, 4, 0, 4, 4, 4, 4, 4, 4, 0], + [ + new GameStep(0, GAME_STEP_GAME_MOVE), + new GameStep(1, GAME_STEP_GAME_MOVE), + new GameStep(2, GAME_STEP_GAME_MOVE), + new GameStep(3, GAME_STEP_GAME_MOVE) + ] + ); + const historyItem2 = new MoveHistoryItem( + player2Id, + 7, + [1, 5, 5, 5, 4, 4, 0, 1, 5, 5, 5, 4, 4, 0], + [ + new GameStep(7, GAME_STEP_GAME_MOVE), + new GameStep(8, GAME_STEP_GAME_MOVE), + new GameStep(9, GAME_STEP_GAME_MOVE), + new GameStep(10, GAME_STEP_GAME_MOVE) + ] + ); + const historyItem3 = new MoveHistoryItem( + player1Id, + 1, + [1, 1, 6, 6, 5, 5, 0, 1, 5, 5, 5, 4, 4, 0], + [ + new GameStep(1, GAME_STEP_GAME_MOVE), + new GameStep(2, GAME_STEP_GAME_MOVE), + new GameStep(3, GAME_STEP_GAME_MOVE), + new GameStep(4, GAME_STEP_GAME_MOVE), + new GameStep(5, GAME_STEP_GAME_MOVE) + ] + ); + const historyItem4 = new MoveHistoryItem( + player2Id, + 8, + [1, 1, 6, 6, 5, 5, 0, 1, 1, 6, 6, 5, 5, 0], + [ + new GameStep(8, GAME_STEP_GAME_MOVE), + new GameStep(9, GAME_STEP_GAME_MOVE), + new GameStep(10, GAME_STEP_GAME_MOVE), + new GameStep(11, GAME_STEP_GAME_MOVE), + new GameStep(12, GAME_STEP_GAME_MOVE) + ] + ); + expect(game.history[0]).toStrictEqual(historyItem1); + expect(game.history[1]).toStrictEqual(historyItem2); + expect(game.history[2]).toStrictEqual(historyItem3); + expect(game.history[3]).toStrictEqual(historyItem4); }); });