summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorniliara-edu <nil.jimeno@estudiant.fjaverianas.com>2024-12-17 23:33:22 +0100
committerniliara-edu <nil.jimeno@estudiant.fjaverianas.com>2024-12-17 23:33:22 +0100
commitcd65b9535168e2bf610fd8feb2024e866d3b449d (patch)
tree1bc17222bfc1e4016ce3ef5f12071f31d135937e
parentef94d922448ff5f0cac8d6bd1e60e546e4bee6ed (diff)
liberty rewritten - get groups
-rw-r--r--notes2
-rw-r--r--src/engine/liberty.js45
2 files changed, 43 insertions, 4 deletions
diff --git a/notes b/notes
new file mode 100644
index 0000000..053cba7
--- /dev/null
+++ b/notes
@@ -0,0 +1,2 @@
+get enemies
+- get values such as you'd do with the others
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({