stub endGame function, add pass counter on Game

This commit is contained in:
Sorrel Bri 2020-05-15 22:00:08 -07:00
parent 9b419a416b
commit 3eabf4191a
2 changed files with 45 additions and 0 deletions

View file

@ -232,6 +232,7 @@ const Game = ({gameData = {}, gameRecord = []} = {}) => {
|| ( game.turn === -1 && player === 'white' ); || ( game.turn === -1 && player === 'white' );
if (isTurn) { if (isTurn) {
if (point.legal) { if (point.legal) {
game.pass = 0;
game.addToRecord({ player, pos: { x, y } }); game.addToRecord({ player, pos: { x, y } });
if (this.kos.length) helper.clearKo.call(this); if (this.kos.length) helper.clearKo.call(this);
point.makeMove(game); point.makeMove(game);
@ -269,6 +270,10 @@ const Game = ({gameData = {}, gameRecord = []} = {}) => {
if (player !== 'black' && player !== 'white') { if (player !== 'black' && player !== 'white') {
return { ...this, success: false }; return { ...this, success: false };
} }
if (this.pass > 0) {
return this.endGame();
}
this.pass = 1;
this.addToRecord({ player, pos: { x: null, y: null } }); this.addToRecord({ player, pos: { x: null, y: null } });
if (this.kos.length) helper.clearKo.call(this); if (this.kos.length) helper.clearKo.call(this);
this.turn = player === 'black' ? -1 : 1; this.turn = player === 'black' ? -1 : 1;
@ -281,6 +286,12 @@ const Game = ({gameData = {}, gameRecord = []} = {}) => {
if (player === 'white') this.winner = 1; if (player === 'white') this.winner = 1;
this.turn = 0; this.turn = 0;
return this; return this;
},
endGame: function() {
// TODO manage territory counting
this.turn = 0;
return this;
} }
} }
}; };

View file

@ -509,6 +509,40 @@ describe('Game end logic', () => {
Game().initGame().submitPass('black').getMeta() Game().initGame().submitPass('black').getMeta()
.gameRecord.should.eql([ { player: 'black', pos: { x: null, y: null } } ]) .gameRecord.should.eql([ { player: 'black', pos: { x: null, y: null } } ])
done(); done();
});
it('move after makes normal change in game state', done => {
Game().initGame()
.makeMove({ player: 'black', pos: { x: 4, y: 4 } })
.makeMove({ player: 'white', pos: { x: 4, y: 5 } })
.makeMove({ player: 'black', pos: { x: 5, y: 3 } })
.submitPass('white')
.makeMove({ player: 'black', pos: { x: 16, y: 16 } })
.legalMoves.should.eql({...emptyBoard, '4-4': 1, '4-5': -1, '5-3': 1, '16-16': 1})
done();
});
it('two nonconsecutive passes continue game', done => {
Game().initGame()
.makeMove({ player: 'black', pos: { x: 4, y: 4 } })
.makeMove({ player: 'white', pos: { x: 4, y: 5 } })
.makeMove({ player: 'black', pos: { x: 5, y: 3 } })
.submitPass('white')
.makeMove({ player: 'black', pos: { x: 16, y: 16 } })
.submitPass('white')
.getMeta().should.eql({...initialMeta, pass: 1, turn: 1, gameRecord: [ { player: 'black', pos: { x: 4, y: 4 } }, { player: 'white', pos: { x: 4, y: 5 } }, { player: 'black', pos: { x: 5, y: 3 } }, { player: 'white', pos: { x: null, y: null } }, { player: 'black', pos: { x: 16, y: 16 } }, { player: 'white', pos: { x: null, y: null } } ]})
done();
});
it('consecutive passes end game', done => {
Game().initGame()
.makeMove({ player: 'black', pos: { x: 4, y: 4 } })
.makeMove({ player: 'white', pos: { x: 4, y: 5 } })
.makeMove({ player: 'black', pos: { x: 5, y: 3 } })
.submitPass('white')
.submitPass('black')
.turn.should.eql(0);
done();
}) })
}) })