From cd65b9535168e2bf610fd8feb2024e866d3b449d Mon Sep 17 00:00:00 2001 From: niliara-edu Date: Tue, 17 Dec 2024 23:33:22 +0100 Subject: liberty rewritten - get groups --- src/engine/liberty.js | 45 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 4 deletions(-) (limited to 'src/engine') 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({ -- cgit v1.2.3