init Game.returnToMove

This commit is contained in:
Sorrel Bri 2020-05-02 21:11:54 -07:00
parent 88e51fdbf9
commit bdeb9c9d86
2 changed files with 46 additions and 2 deletions

View file

@ -191,6 +191,10 @@ const Game = ({gameData = {}, gameRecord = []} = {}) => ({
return this;
},
addToRecord: function(moveObject) {
this.gameRecord.push(moveObject);
},
getMeta: function() {
// cannot be chained
// does not affect game object
@ -212,6 +216,7 @@ const Game = ({gameData = {}, gameRecord = []} = {}) => ({
|| ( game.turn === -1 && player === 'white' );
if (isTurn) {
if (point.legal) {
game.addToRecord({ player, pos: { x, y } });
if (this.kos.length) this.clearKo();
point.makeMove(game);
game.turn *= -1;
@ -226,6 +231,16 @@ const Game = ({gameData = {}, gameRecord = []} = {}) => ({
const group = Symbol(`${point.pos.x}-${point.pos.y}`);
this.groups[group] = { stones: new Set(), liberties: new Set()};
return { game: this, group };
},
returnToMove: function(idx) {
if (idx < 0) {
const { komi, handicap, boardSize } = this;
return Game({
gameData: { komi, handicap, boardSize },
gameRecord: [...this.gameRecord.slice(0, this.gameRecord.length + idx)]
})
}
}
});

View file

@ -348,11 +348,10 @@ describe('capture logic: snapback, ko and playing in eyes', () => {
.makeMove({ player: 'white', pos: { x: 16, y: 16 } })
.makeMove({ player: 'black', pos: { x: 6, y: 4 } })
.makeMove({ player: 'white', pos: { x: 5, y: 5 } })
.makeMove({ player: 'black', pos: { x: 5, y: 6 } });
it('snapback functions properly', done => {
console.log(snapbackGame().boardState['5-6'])
snapbackGame()
.makeMove({ player: 'black', pos: { x: 5, y: 6 } })
.success.should.eql(true);
done();
});
@ -402,7 +401,37 @@ describe('capture logic: snapback, ko and playing in eyes', () => {
.makeMove({ player: 'white', pos: { x: 4, y: 16 } })
.legalMoves['5-5'].should.eql('l');
done();
});
});
describe('Game history functionality', () => {
const firstMove = { player: 'black', pos: { x: 4, y: 4 }};
const secondMove = { player: 'white', pos: { x: 16, y: 16 }};
it('makeMove creates gameRecord item', done => {
Game().initGame()
.makeMove(firstMove).gameRecord[0].should.eql(firstMove);
done();
});
it('makeMove holds history', done => {
const game = Game().initGame()
.makeMove(firstMove).makeMove(secondMove);
game.gameRecord[0].should.eql(firstMove);
game.gameRecord[1].should.eql(secondMove)
done();
});
it('Game.returnToMove returns new Game with gameRecord', done => {
Game().initGame()
.makeMove(firstMove)
.makeMove(secondMove)
.makeMove({ player: 'black', pos: { x: 16, y: 4 } })
.returnToMove(-1)
.gameRecord.should.eql([ firstMove, secondMove ])
done();
})
// .boardState['16-4'].stone.should.eql(0)
})