Game.returnToMove(x) where x >= 0 returns game state at move x

This commit is contained in:
Sorrel Bri 2020-05-02 22:04:22 -07:00
parent d51e3f72f4
commit a99426e03f
2 changed files with 47 additions and 14 deletions

View file

@ -238,14 +238,20 @@ const Game = ({gameData = {}, gameRecord = []} = {}) => {
return { game: this, group }; return { game: this, group };
}, },
returnToMove: function(idx) { returnToMove: function(lastMove) {
if (idx < 0) {
const { komi, handicap, boardSize } = this; const { komi, handicap, boardSize } = this;
if (lastMove === 0) {
return Game({
gameData: { komi, handicap, boardSize }
}).initGame();
}
const length = this.gameRecord.length;
const index = lastMove < 0 ? length + lastMove : lastMove;
if (lastMove >= length && lastMove > 0) return this;
return Game({ return Game({
gameData: { komi, handicap, boardSize }, gameData: { komi, handicap, boardSize },
gameRecord: [...this.gameRecord.slice(0, this.gameRecord.length + idx)] gameRecord: [...this.gameRecord.slice(0, index)]
}) });
}
} }
} }
}; };

View file

@ -407,6 +407,12 @@ describe('capture logic: snapback, ko and playing in eyes', () => {
describe('Game history functionality', () => { describe('Game history functionality', () => {
const firstMove = { player: 'black', pos: { x: 4, y: 4 }}; const firstMove = { player: 'black', pos: { x: 4, y: 4 }};
const secondMove = { player: 'white', pos: { x: 16, y: 16 }}; const secondMove = { player: 'white', pos: { x: 16, y: 16 }};
const thirdMove = { player: 'black', pos: { x: 16, y: 4 } };
const fourthMove = { player: 'white', pos: { x: 4, y: 16 }};
const fifthMove = { player: 'black', pos: { x: 10, y: 4 } };
const sixthMove = { player: 'white', pos: { x: 4, y: 10 }};
const seventhMove = { player: 'black', pos: { x: 10, y: 16 } };
const eighthMove = { player: 'white', pos: { x: 16, y: 10 }};
it('makeMove creates gameRecord item', done => { it('makeMove creates gameRecord item', done => {
Game().initGame() Game().initGame()
@ -425,14 +431,14 @@ describe('Game history functionality', () => {
const rewoundGame = () => Game().initGame() const rewoundGame = () => Game().initGame()
.makeMove(firstMove) .makeMove(firstMove)
.makeMove(secondMove) .makeMove(secondMove)
.makeMove({ player: 'black', pos: { x: 16, y: 4 } }) .makeMove(thirdMove)
.returnToMove(-1); .returnToMove(-1);
it('Game.returnToMove returns new Game with gameRecord', done => { it('Game.returnToMove returns new Game with gameRecord', done => {
rewoundGame() rewoundGame()
.gameRecord.should.eql([ firstMove, secondMove ]) .gameRecord.should.eql([ firstMove, secondMove ])
done(); done();
}) });
it('Game.returnToMove returns new Game with new board state', done => { it('Game.returnToMove returns new Game with new board state', done => {
rewoundGame() rewoundGame()
@ -442,6 +448,27 @@ describe('Game history functionality', () => {
rewoundGame() rewoundGame()
.boardState['16-16'].stone.should.eql(-1); .boardState['16-16'].stone.should.eql(-1);
done(); done();
});
const resetGame = () => [
firstMove, secondMove, thirdMove, fourthMove, fifthMove, sixthMove, seventhMove, eighthMove
].reduce((game, move) => game.makeMove(move), Game().initGame());
it('Game.returnToMove(0) returns to init board state', done => {
const erasedGame = resetGame()
.returnToMove(0)
erasedGame.gameRecord.should.eql([])
erasedGame.boardState['4-4'].stone.should.eql(0)
done();
});
it('Game.returnToMove(5) returns to state after 5th move', done => {
const fifthMoveGame = resetGame()
.returnToMove(5);
fifthMoveGame.gameRecord.should.eql([firstMove, secondMove, thirdMove, fourthMove, fifthMove]);
fifthMoveGame.boardState['10-4'].stone.should.eql(1)
fifthMoveGame.boardState['4-10'].stone.should.eql(0)
done();
}) })
}) })