add logic for legal check that prevents moves at points with no liberties

This commit is contained in:
Sorrel Bri 2020-04-24 00:16:31 -07:00
parent 089783c82d
commit 4a90b933a7
2 changed files with 22 additions and 6 deletions

View file

@ -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;
}

View file

@ -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();
})
});