add GameStep
This commit is contained in:
parent
ebd2fef54c
commit
8abecca909
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@ -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);
|
||||
});
|
||||
});
|
||||
|
||||
Loading…
Reference in New Issue
Block a user