diff --git a/packages/server/services/Game.v2.js b/packages/server/services/Game.v2.js index 15e3362..9416c10 100644 --- a/packages/server/services/Game.v2.js +++ b/packages/server/services/Game.v2.js @@ -99,18 +99,11 @@ const checkLegal = ({ point, Game }) => { return { ...point, isInLiveGroup }; } - // if move would capture opposing group - // set capturing object and return true - // const isNotTurnStone = neighbor => neighbor.stone === Game.turn * -1; - // const isInGroupWithLastLiberty = neighbor => getGroupLiberties(neighbor).filter(isNotSamePoint) - // // .length === 0; - // const isCapturing = Object.values(point.neighbors).filter(isNotTurnStone).filter(isInGroupWithLastLiberty) - // // .length; - - // if (isCapturing) { - // point.legal = true; - // return { ...point, isCapturing }; - // } + // if move would capture opposing group return true + if (point.capturing[Game.turn].length) { + point.legal = true; + return point; + } point.legal = false; return { ...point, adj: isEmptyAdjacent, isInLiveGroup }; @@ -225,7 +218,10 @@ const Point = ({x, y, boardSize = 19}) => ({ stone: 0, // can be 1, -1, 0, or 'k' for ko legal: true, territory: 0, - capturing: [], + capturing: { + '1': [], + '-1': [] + }, group: null, neighbors: { top: x > 1 ? `${ x - 1 }-${ y }` : null, @@ -237,6 +233,9 @@ const Point = ({x, y, boardSize = 19}) => ({ makeMove: function(game) { this.stone = game.turn; this.legal = false; + if (this.capturing[this.stone].length) { + this.makeCaptures(game); + } this.joinGroup({ point: this, game }); return this.checkCaptures(game); }, @@ -282,18 +281,36 @@ const Point = ({x, y, boardSize = 19}) => ({ const liberties = game.groups[this.group].liberties; if (liberties.size === 1) { const lastLiberty = getSingleItemFromSet(liberties); - lastLiberty.capturing = this; + lastLiberty.capturing[this.stone * -1].push(this.group); } // if neighbors have one liberty - const neighbors = Object.values(this.neighbors); - neighbors.filter(neighbor => neighbor.stone).forEach( neighbor => { - const liberties = game.groups[neighbor.group].liberties; - if (liberties.size === 1) { + const neighbors = Object.values(this.neighbors).filter(neighbor => neighbor.stone === -1 * this.stone) + neighbors.forEach( neighbor => { + const liberties = game.groups[neighbor.group] && game.groups[neighbor.group].liberties; + if (liberties && liberties.size === 1) { const lastLiberty = getSingleItemFromSet(liberties); - lastLiberty.capturing = neighbor; + lastLiberty.capturing[neighbor.stone * -1].push(neighbor.group); } }) + }, + + makeCaptures: function(game) { + // for each group + this.capturing[this.stone].forEach(captureGroup => { + const capturesSet = game.groups[captureGroup].stones; + for (let [capture] of capturesSet.entries()) { + capture.removeStone(game); + } + }) + }, + + removeStone: function(game) { + // reset point + this.stone = 0; + this.group = null; + this.capturing[game.turn] = []; + // add captures } }); diff --git a/packages/server/test/Game.v2.spec.js b/packages/server/test/Game.v2.spec.js index 72f5260..e26ea4c 100644 --- a/packages/server/test/Game.v2.spec.js +++ b/packages/server/test/Game.v2.spec.js @@ -212,7 +212,7 @@ describe('makeMove group join and capture logic', () => { }); it('group with only remaining liberty at point to be played returns success: false', done => { - Game({ gameData: { handicap: 2 } }).initGame() + const point = Game({ gameData: { handicap: 2 } }).initGame() .makeMove({ player: 'white', pos: { x: 4, y: 15 } }) // 3 4 5 6 .makeMove({ player: 'black', pos: { x: 4, y: 4 } }) // 15 -1 -1 .makeMove({ player: 'white', pos: { x: 5, y: 15 } }) // 16 -1 1h 0 -1 @@ -224,6 +224,8 @@ describe('makeMove group join and capture logic', () => { .makeMove({ player: 'white', pos: { x: 4, y: 17 } }) .makeMove({ player: 'black', pos: { x: 10, y: 16 } }) .makeMove({ player: 'white', pos: { x: 5, y: 17 } }) + console.log(point.boardState['5-16']); + point .makeMove({ player: 'black', pos: { x: 5, y: 16 } }) .success.should.eql(false); done(); @@ -244,15 +246,15 @@ describe('makeMove group join and capture logic', () => { }); it('makeMove assesses captures', done => { - (!!captureGame.boardState['4-17'].capturing).should.eql(true); + captureGame.boardState['4-17'].capturing[-1].length.should.eql(1); done(); }) - // it('makeMove capture removes captured stone', done => { - // captureGame.makeMove({ player: 'white', pos: { x: 4, y: 17 } }) - // .boardState['4-16'].stone.should.eql(0); - // done(); - // }); + it('makeMove capture removes captured stone', done => { + captureGame.makeMove({ player: 'white', pos: { x: 4, y: 17 } }) + .boardState['4-16'].stone.should.eql(0); + done(); + }); // it('makeMove capture increases capturing players captures', done => { // captureGame.makeMove({ player: 'white', pos: { x: 4, y: 17 } })