add GameStep
This commit is contained in:
parent
ebd2fef54c
commit
8abecca909
@ -1,16 +1,34 @@
|
|||||||
export class HistoryItem {
|
export class HistoryItem {
|
||||||
boardSnapshot: number[];
|
boardSnapshot: number[];
|
||||||
constructor(boardSnapshot: number[]) {
|
gameSteps: GameStep[];
|
||||||
|
constructor(boardSnapshot: number[], gameSteps: GameStep[]) {
|
||||||
this.boardSnapshot = boardSnapshot;
|
this.boardSnapshot = boardSnapshot;
|
||||||
|
this.gameSteps = gameSteps;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class MoveHistoryItem extends HistoryItem {
|
export class MoveHistoryItem extends HistoryItem {
|
||||||
playerId: string;
|
playerId: string;
|
||||||
moveIndex: number;
|
moveIndex: number;
|
||||||
constructor(playerId: string, moveIndex: number, boardSnapshot: number[]) {
|
constructor(
|
||||||
super(boardSnapshot);
|
playerId: string,
|
||||||
|
moveIndex: number,
|
||||||
|
boardSnapshot: number[],
|
||||||
|
gameSteps: GameStep[]
|
||||||
|
) {
|
||||||
|
super(boardSnapshot, gameSteps);
|
||||||
this.playerId = playerId;
|
this.playerId = playerId;
|
||||||
this.moveIndex = moveIndex;
|
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 { Board, PitType } from './Board';
|
||||||
import { GameRule } from './GameRule';
|
import { GameRule } from './GameRule';
|
||||||
import { HistoryItem, MoveHistoryItem } from './HistoryItem';
|
import { HistoryItem, MoveHistoryItem, GameStep } from './HistoryItem';
|
||||||
|
|
||||||
export type GameState = 'initial' | 'playing' | 'ended';
|
export type GameState = 'initial' | 'playing' | 'ended';
|
||||||
|
|
||||||
|
export const GAME_STEP_GAME_MOVE = 'GAME_STEP_GAME_MOVE';
|
||||||
|
|
||||||
export class MancalaGame {
|
export class MancalaGame {
|
||||||
id: string;
|
id: string;
|
||||||
board: Board;
|
board: Board;
|
||||||
@ -13,6 +15,7 @@ export class MancalaGame {
|
|||||||
state: GameState;
|
state: GameState;
|
||||||
gameRules: GameRule[];
|
gameRules: GameRule[];
|
||||||
history: HistoryItem[];
|
history: HistoryItem[];
|
||||||
|
currentHistoryItem: HistoryItem | null = null;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
id: string,
|
id: string,
|
||||||
@ -42,6 +45,7 @@ export class MancalaGame {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
this.board.onGameMove = (index: number) => {
|
this.board.onGameMove = (index: number) => {
|
||||||
|
this.addGameStep(new GameStep(index, GAME_STEP_GAME_MOVE));
|
||||||
this.gameRules.forEach((gameRule) => {
|
this.gameRules.forEach((gameRule) => {
|
||||||
gameRule.onGameMove(this, index);
|
gameRule.onGameMove(this, index);
|
||||||
});
|
});
|
||||||
@ -131,10 +135,15 @@ export class MancalaGame {
|
|||||||
}
|
}
|
||||||
if (this.canPlayerMove(playerId, pitIndex)) {
|
if (this.canPlayerMove(playerId, pitIndex)) {
|
||||||
const moveIndex = this.getBoardIndexByPlayerId(playerId, pitIndex);
|
const moveIndex = this.getBoardIndexByPlayerId(playerId, pitIndex);
|
||||||
this.board.move(moveIndex);
|
this.currentHistoryItem = new MoveHistoryItem(
|
||||||
this.history.push(
|
playerId,
|
||||||
new MoveHistoryItem(playerId, moveIndex, this.board.getStoneArray())
|
moveIndex,
|
||||||
|
[],
|
||||||
|
[]
|
||||||
);
|
);
|
||||||
|
this.board.move(moveIndex);
|
||||||
|
this.currentHistoryItem.boardSnapshot = this.board.getStoneArray();
|
||||||
|
this.history.push(this.currentHistoryItem);
|
||||||
if (this.checkGameIsEnded()) {
|
if (this.checkGameIsEnded()) {
|
||||||
this.state = 'ended';
|
this.state = 'ended';
|
||||||
}
|
}
|
||||||
@ -218,4 +227,12 @@ export class MancalaGame {
|
|||||||
mancalaGame.state
|
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(player2Id, 0);
|
||||||
game.moveByPlayerPit(player1Id, 1);
|
game.moveByPlayerPit(player1Id, 1);
|
||||||
game.moveByPlayerPit(player2Id, 1);
|
game.moveByPlayerPit(player2Id, 1);
|
||||||
expect(game.history).toStrictEqual([
|
const historyItem1 = new MoveHistoryItem(
|
||||||
new MoveHistoryItem(
|
player1Id,
|
||||||
player1Id,
|
0,
|
||||||
0,
|
[1, 5, 5, 5, 4, 4, 0, 4, 4, 4, 4, 4, 4, 0],
|
||||||
[1, 5, 5, 5, 4, 4, 0, 4, 4, 4, 4, 4, 4, 0]
|
[
|
||||||
),
|
new GameStep(0, GAME_STEP_GAME_MOVE),
|
||||||
new MoveHistoryItem(
|
new GameStep(1, GAME_STEP_GAME_MOVE),
|
||||||
player2Id,
|
new GameStep(2, GAME_STEP_GAME_MOVE),
|
||||||
7,
|
new GameStep(3, GAME_STEP_GAME_MOVE)
|
||||||
[1, 5, 5, 5, 4, 4, 0, 1, 5, 5, 5, 4, 4, 0]
|
]
|
||||||
),
|
);
|
||||||
new MoveHistoryItem(
|
const historyItem2 = new MoveHistoryItem(
|
||||||
player1Id,
|
player2Id,
|
||||||
1,
|
7,
|
||||||
[1, 1, 6, 6, 5, 5, 0, 1, 5, 5, 5, 4, 4, 0]
|
[1, 5, 5, 5, 4, 4, 0, 1, 5, 5, 5, 4, 4, 0],
|
||||||
),
|
[
|
||||||
new MoveHistoryItem(
|
new GameStep(7, GAME_STEP_GAME_MOVE),
|
||||||
player2Id,
|
new GameStep(8, GAME_STEP_GAME_MOVE),
|
||||||
8,
|
new GameStep(9, GAME_STEP_GAME_MOVE),
|
||||||
[1, 1, 6, 6, 5, 5, 0, 1, 1, 6, 6, 5, 5, 0]
|
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