init Game.returnToMove(x) where x < 0 rewinds x number of moves

This commit is contained in:
Sorrel Bri 2020-05-02 21:25:54 -07:00
parent bdeb9c9d86
commit d51e3f72f4
2 changed files with 96 additions and 79 deletions

View file

@ -163,7 +163,12 @@ const initBoard = (game) => {
}
// returns Game object
const Game = ({gameData = {}, gameRecord = []} = {}) => ({
const Game = ({gameData = {}, gameRecord = []} = {}) => {
if (gameRecord.length) {
// play through all the moves
return gameRecord.reduce((game, move) => game.makeMove(move), Game({gameData}).initGame())
}
return {
winner: gameData.winner ||null,
turn: gameData.turn || 0, // turn logic depends on handicap stones
pass: gameData.pass || 0, // -1 represents state in which resignation has been submitted, not confirmed
@ -242,7 +247,8 @@ const Game = ({gameData = {}, gameRecord = []} = {}) => ({
})
}
}
});
}
};
const Point = ({x, y, boardSize = 19}) => {
let point = {

View file

@ -422,16 +422,27 @@ describe('Game history functionality', () => {
done();
});
it('Game.returnToMove returns new Game with gameRecord', done => {
Game().initGame()
const rewoundGame = () => Game().initGame()
.makeMove(firstMove)
.makeMove(secondMove)
.makeMove({ player: 'black', pos: { x: 16, y: 4 } })
.returnToMove(-1)
.returnToMove(-1);
it('Game.returnToMove returns new Game with gameRecord', done => {
rewoundGame()
.gameRecord.should.eql([ firstMove, secondMove ])
done();
})
// .boardState['16-4'].stone.should.eql(0)
it('Game.returnToMove returns new Game with new board state', done => {
rewoundGame()
.boardState['16-4'].stone.should.eql(0);
rewoundGame()
.boardState['4-4'].stone.should.eql(1);
rewoundGame()
.boardState['16-16'].stone.should.eql(-1);
done();
})
})