diff options
Diffstat (limited to 'src/engine')
-rw-r--r-- | src/engine/liberty.js | 45 |
1 files changed, 41 insertions, 4 deletions
diff --git a/src/engine/liberty.js b/src/engine/liberty.js index 7ccd77d..9f2049c 100644 --- a/src/engine/liberty.js +++ b/src/engine/liberty.js @@ -12,10 +12,47 @@ export function hasLiberties({ position, team }) { - return checkLiberties({ - position, - team - }) + if (getPiece(position) != undefined) return false + let groups = getSurroundingGroups(position, team) + // if (stones.length > 0) console.log(stones) + // console.log(stones) + return true + // return checkLiberties({ + // position, + // team + // }) +} + +function getSurroundingGroups(position, team) { + return getSurroundingSquares(position) + .filter(p => getPiece(p) != undefined) + .filter(p => getPiece(p).team == team) + .map(p => getGroup({position: p})) +} + +function getGroup({position, checkedPositions = []}) { + if (isInArray(position, checkedPositions)) { + return + } + + checkedPositions.push(position) + let team = getPiece(position).team + + getSurroundingSquares(position) + .filter(p => getPiece(p) != undefined) + .filter(p => getPiece(p).team == team) + .forEach(p => { + getGroup({ + position: p, + checkedPositions: checkedPositions + }) + }) + + return checkedPositions +} + +function getPiece(position) { + return board.stones[position.y][position.x] } function checkLiberties({ |