init Game.returnToMove(x) where x < 0 rewinds x number of moves
This commit is contained in:
parent
bdeb9c9d86
commit
d51e3f72f4
2 changed files with 96 additions and 79 deletions
|
@ -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 = {
|
||||
|
|
|
@ -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();
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue