From 4a90b933a7af5d1f5ddf6e8241285ba8209b815c Mon Sep 17 00:00:00 2001 From: Sorrel Bri Date: Fri, 24 Apr 2020 00:16:31 -0700 Subject: [PATCH] add logic for legal check that prevents moves at points with no liberties --- packages/server/services/Game.v2.js | 19 +++++++++++++++++-- packages/server/test/Game.v2.spec.js | 9 +++++---- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/packages/server/services/Game.v2.js b/packages/server/services/Game.v2.js index 70ba96d..1b67d39 100644 --- a/packages/server/services/Game.v2.js +++ b/packages/server/services/Game.v2.js @@ -71,13 +71,23 @@ const checkLegal = ({ point, Game }) => { // if stone (includes ko) return false let legal = false; if (point.stone) { - return { ...point, legal }; + point.legal = legal; + return point; + } + const isEmptyAdjacent = Object.values(point.neighbors).filter(pt => pt.stone === 0)[0] + // .length; + + // if empty point adjacent return true + if (!isEmptyAdjacent) { + point.legal = legal; + return { ...point, adj: isEmptyAdjacent }; + // TODO change to positive check with legal: true after remaining logic has been added } - // if liberties return true // if group has liberties return true // if move would capture opposing group // set capturing object and return true point.legal = !point.stone ? true : false; + point.adj = isEmptyAdjacent; return point; } @@ -93,6 +103,11 @@ const getNeighbors = boardSize => (point, i, boardState) => { point.neighbors.btm = btm ? boardState[i + boardSize][1] : btm; point.neighbors.lft = lft ? boardState[i - 1][1] : lft; point.neighbors.rgt = rgt ? boardState[i + 1][1] : rgt; + for (let [direction, neighbor] of Object.entries(point.neighbors)) { + if (!neighbor) { + delete point.neighbors[direction]; + } + } return point; } diff --git a/packages/server/test/Game.v2.spec.js b/packages/server/test/Game.v2.spec.js index 6b99665..c7cd973 100644 --- a/packages/server/test/Game.v2.spec.js +++ b/packages/server/test/Game.v2.spec.js @@ -188,12 +188,13 @@ describe('Game.makeMove({ player: str, pos: { x: int, y: int } })', () => { }) it('makeMove returns success: false when move is made in point with no liberties', done => { - Game({ gameData: { handicap: 2 } }).initGame() - .makeMove({ player: 'white', pos: { x: 4, y: 4 } }).makeMove({ player: 'black', pos: { x: 6, y: 16 } }) - .makeMove({ player: 'white', pos: { x: 16, y: 16 }}).makeMove({ player: 'black', pos: { x: 5, y: 15 } }) - .makeMove({ player: 'white', pos: { x: 16, y: 10 }}).makeMove({ player: 'black', pos: { x: 5, y: 17 } }) + const point = Game({ gameData: { handicap: 2 } }).initGame() // 15 16 17 + .makeMove({ player: 'white', pos: { x: 4, y: 4 } }).makeMove({ player: 'black', pos: { x: 6, y: 16 } }) // 4 1 + .makeMove({ player: 'white', pos: { x: 16, y: 16 }}).makeMove({ player: 'black', pos: { x: 5, y: 15 } }) // 5 1 x 1 + .makeMove({ player: 'white', pos: { x: 16, y: 10 }}).makeMove({ player: 'black', pos: { x: 5, y: 17 } }) // 6 1 .makeMove({ player: 'white', pos: { x: 5, y: 16 }}) .success.should.eql(false); + done(); }) });