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 };
},
returnToMove: function(idx) {
if (idx < 0) {
const { komi, handicap, boardSize } = this;
returnToMove: function(lastMove) {
const { komi, handicap, boardSize } = this;
if (lastMove === 0) {
return Game({
gameData: { komi, handicap, boardSize },
gameRecord: [...this.gameRecord.slice(0, this.gameRecord.length + idx)]
})
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({
gameData: { komi, handicap, boardSize },
gameRecord: [...this.gameRecord.slice(0, index)]
});
}
}
};

View file

@ -405,8 +405,14 @@ describe('capture logic: snapback, ko and playing in eyes', () => {
});
describe('Game history functionality', () => {
const firstMove = { player: 'black', pos: { x: 4, y: 4 }};
const secondMove = { player: 'white', pos: { x: 16, y: 16 }};
const firstMove = { player: 'black', pos: { x: 4, y: 4 }};
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 => {
Game().initGame()
@ -423,16 +429,16 @@ describe('Game history functionality', () => {
});
const rewoundGame = () => Game().initGame()
.makeMove(firstMove)
.makeMove(secondMove)
.makeMove({ player: 'black', pos: { x: 16, y: 4 } })
.returnToMove(-1);
.makeMove(firstMove)
.makeMove(secondMove)
.makeMove(thirdMove)
.returnToMove(-1);
it('Game.returnToMove returns new Game with gameRecord', done => {
rewoundGame()
.gameRecord.should.eql([ firstMove, secondMove ])
done();
})
});
it('Game.returnToMove returns new Game with new board state', done => {
rewoundGame()
@ -442,6 +448,27 @@ describe('Game history functionality', () => {
rewoundGame()
.boardState['16-16'].stone.should.eql(-1);
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();
})
})