summaryrefslogtreecommitdiff
path: root/src/engine/liberty.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/engine/liberty.js')
-rw-r--r--src/engine/liberty.js63
1 files changed, 54 insertions, 9 deletions
diff --git a/src/engine/liberty.js b/src/engine/liberty.js
index a006c62..d5d4448 100644
--- a/src/engine/liberty.js
+++ b/src/engine/liberty.js
@@ -12,13 +12,58 @@ export function hasLiberties({
position,
team
}) {
- return Array.from(SURROUNDING)
- .map(v => Vector2.sum(v, position))
- .filter(z => 0 <= z.x && z.x < board.size &&
- 0 <= z.y && z.y < board.size
- )
- .some(z => board.stones[z.y][z.x] == undefined ?
- true :
- board.stones[z.y][z.x].team == team
- )
+ return checkLiberties({
+ position,
+ team
+ })
+}
+
+function checkLiberties({
+ position,
+ team,
+ checkedStones = [],
+ checkedLiberties = [],
+}) {
+ if (
+ Array.from(checkedStones)
+ .some(p => Vector2.equals(p, position))
+ ) {
+ return false
+ }
+
+ checkedStones.push(position)
+
+ let surroundingSquares = Array.from(SURROUNDING)
+ .map(v => Vector2.sum(v, position))
+ .filter(z => 0 <= z.x && z.x < board.size &&
+ 0 <= z.y && z.y < board.size
+ )
+
+ surroundingSquares
+ .filter(z => board.stones[z.y][z.x] == undefined)
+ .forEach(z => {
+ if (!Array.from(checkedStones)
+ .some(p => Vector2.equals(p, z))
+ ) {
+ checkedLiberties.push(z)
+ }
+ })
+
+ surroundingSquares
+ .filter(z => board.stones[z.y][z.x] != undefined)
+ .filter(z => board.stones[z.y][z.x].team == team)
+ .forEach(z => {
+ checkLiberties({
+ position: z,
+ team: team,
+ checkedStones: checkedStones,
+ checkedLiberties: checkedLiberties,
+ })
+ })
+
+ if (checkedLiberties.length > 0) {
+ return true
+ }
+
+ return false
}