add clearKo to Game called after makeMove is verified as legal

This commit is contained in:
Sorrel Bri 2020-05-02 20:30:28 -07:00
parent 2237e344c1
commit 88e51fdbf9
2 changed files with 30 additions and 17 deletions

View file

@ -196,6 +196,13 @@ const Game = ({gameData = {}, gameRecord = []} = {}) => ({
// does not affect game object // does not affect game object
return { winner: this.winner, turn: this.turn, pass: this.pass, playerState: this.playerState, gameRecord: this.gameRecord } return { winner: this.winner, turn: this.turn, pass: this.pass, playerState: this.playerState, gameRecord: this.gameRecord }
}, },
clearKo: function() {
this.kos.forEach(ko => {
this.boardState[ko] = { ...this.boardState[ko], legal: true, ko: false };
})
this.kos = [];
},
makeMove: function({ player, pos: {x, y}}) { makeMove: function({ player, pos: {x, y}}) {
let game = this; let game = this;
@ -205,6 +212,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) {
if (this.kos.length) this.clearKo();
point.makeMove(game); point.makeMove(game);
game.turn *= -1; game.turn *= -1;
success = true; success = true;
@ -362,18 +370,4 @@ const Point = ({x, y, boardSize = 19}) => {
module.exports = { module.exports = {
Game, Game,
Point Point
} }
Game().initGame()
.makeMove({ player: 'black', pos: { x: 4, y: 4 } }) // 3 4 5 6 7
.makeMove({ player: 'white', pos: { x: 5, y: 4 } }) // 4 1 1 -1
.makeMove({ player: 'black', pos: { x: 5, y: 6 } }) // 5 1 -1 -1 1 -1
.makeMove({ player: 'white', pos: { x: 5, y: 7 } }) // 6 1 1 -1
.makeMove({ player: 'black', pos: { x: 4, y: 5 } }) // (13) at {5,6}
.makeMove({ player: 'white', pos: { x: 4, y: 6 } })
.makeMove({ player: 'black', pos: { x: 5, y: 3 } })
.makeMove({ player: 'white', pos: { x: 6, y: 6 } })
.makeMove({ player: 'black', pos: { x: 6, y: 5 } })
.makeMove({ player: 'white', pos: { x: 16, y: 16 } })
.makeMove({ player: 'black', pos: { x: 6, y: 4 } })
.makeMove({ player: 'white', pos: { x: 5, y: 5 } });

View file

@ -382,8 +382,27 @@ describe('capture logic: snapback, ko and playing in eyes', () => {
koGame().legalMoves['5-5'].should.eql('k'); koGame().legalMoves['5-5'].should.eql('k');
done(); done();
}) })
// ko cleared on Point after move
// ko cleared on Game after move it('ko cleared on Point after move', done => {
koGame().makeMove({ player: 'black', pos: { x: 16, y: 16 } })
.makeMove({ player: 'white', pos: { x: 4, y: 16 } })
.boardState['5-5'].ko.should.eql(false);
done();
});
it('ko cleared on Game after move', done => {
koGame().makeMove({ player: 'black', pos: { x: 16, y: 16 } })
.makeMove({ player: 'white', pos: { x: 4, y: 16 } })
.kos.should.eql([])
done();
});
it('ko cleared on legalMoves after move', done => {
koGame().makeMove({ player: 'black', pos: { x: 16, y: 16 } })
.makeMove({ player: 'white', pos: { x: 4, y: 16 } })
.legalMoves['5-5'].should.eql('l');
done();
})
}) })