summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornil <niljo@airmail.cc>2024-12-18 16:14:54 +0100
committernil <niljo@airmail.cc>2024-12-18 16:14:54 +0100
commit7b005bc02b1e73c71f8cb8f4e03b6fe6ff420c67 (patch)
tree92dd185788f0c9d7868e935649a676f13ca9bbf7
parent827d1d8e533e45360ead5cbb5cab5547d2252282 (diff)
order liberties
-rw-r--r--src/engine/liberty.js95
-rw-r--r--src/engine/vector2.js16
2 files changed, 56 insertions, 55 deletions
diff --git a/src/engine/liberty.js b/src/engine/liberty.js
index 7c51ae2..0947ab0 100644
--- a/src/engine/liberty.js
+++ b/src/engine/liberty.js
@@ -18,22 +18,18 @@ export function hasLiberties({
groups.push(position)
let liberties = getGroupLiberties(groups)
- liberties = removeIfInArray(position, liberties)
+ liberties = Vector2.removeIfInArray(position, liberties)
if (liberties.length == 0) {
let enemies = getGroupSurroundingEnemies(groups, team)
let enemyLiberties = getGroupLiberties(enemies)
- enemyLiberties = removeIfInArray(position, enemyLiberties)
+ enemyLiberties = Vector2.removeIfInArray(position, enemyLiberties)
return (enemyLiberties.length == 0)
}
return (liberties.length > 0)
}
-function removeIfInArray(value, array) {
- return array.filter(x => !Vector2.equals(value, x))
-}
-
function getLiberties(position) {
return getSurroundingSquares(position)
.filter(p => getPiece(p) == undefined)
@@ -44,35 +40,31 @@ function getGroupLiberties(group) {
group
.map(p => getLiberties(p))
.forEach(g => g
- .forEach(p => pushIfNotInArray(p, checkedPositions))
+ .forEach(p => Vector2.pushIfNotInArray(p, checkedPositions))
)
return checkedPositions
}
-function getGroupSurroundingEnemies(group, playerTeam) {
- if (group.length == 0) return []
-
- let team = playerTeam == PLAYER.BLACK ?
- PLAYER.WHITE :
- PLAYER.BLACK
-
- let surroundingEnemies = group
- .map(p => getSurroundingGroups(p, team))
-
- if (surroundingEnemies.length == 0) return []
+function getGroup({position, checkedPositions = []}) {
+ if (Vector2.isInArray(position, checkedPositions)) {
+ return
+ }
- return surroundingEnemies
- .reduce((partialArray = [], newGroup) => {
- if (newGroup.length == 0) {
- return partialArray
- }
- if (isInArray(newGroup[0], partialArray)) {
- return partialArray
- }
+ checkedPositions.push(position)
+ let team = getPiece(position).team
- return partialArray.concat(newGroup)
+ getSurroundingSquares(position)
+ .filter(p => getPiece(p) != undefined)
+ .filter(p => getPiece(p).team == team)
+ .forEach(p => {
+ getGroup({
+ position: p,
+ checkedPositions: checkedPositions
+ })
})
+
+ return checkedPositions
}
function getSurroundingGroups(position, team) {
@@ -90,7 +82,7 @@ function getSurroundingGroups(position, team) {
if (newGroup.length == 0) {
return partialArray
}
- if (isInArray(newGroup[0], partialArray)) {
+ if (Vector2.isInArray(newGroup[0], partialArray)) {
return partialArray
}
@@ -98,40 +90,33 @@ function getSurroundingGroups(position, team) {
})
}
-function getGroup({position, checkedPositions = []}) {
- if (isInArray(position, checkedPositions)) {
- return
- }
+function getGroupSurroundingEnemies(group, playerTeam) {
+ if (group.length == 0) return []
- checkedPositions.push(position)
- let team = getPiece(position).team
+ let team = playerTeam == PLAYER.BLACK ?
+ PLAYER.WHITE :
+ PLAYER.BLACK
- getSurroundingSquares(position)
- .filter(p => getPiece(p) != undefined)
- .filter(p => getPiece(p).team == team)
- .forEach(p => {
- getGroup({
- position: p,
- checkedPositions: checkedPositions
- })
- })
+ let surroundingEnemies = group
+ .map(p => getSurroundingGroups(p, team))
- return checkedPositions
-}
+ if (surroundingEnemies.length == 0) return []
-function getPiece(position) {
- return board.stones[position.y][position.x]
-}
+ return surroundingEnemies
+ .reduce((partialArray = [], newGroup) => {
+ if (newGroup.length == 0) {
+ return partialArray
+ }
+ if (Vector2.isInArray(newGroup[0], partialArray)) {
+ return partialArray
+ }
-function isInArray(position, array) {
- return Array.from(array)
- .some(p => Vector2.equals(p, position))
+ return partialArray.concat(newGroup)
+ })
}
-function pushIfNotInArray(position, array) {
- if (!isInArray(position, array)) {
- array.push(position)
- }
+function getPiece(position) {
+ return board.stones[position.y][position.x]
}
function getSurroundingSquares(position) {
diff --git a/src/engine/vector2.js b/src/engine/vector2.js
index 0cf2217..2bd52f7 100644
--- a/src/engine/vector2.js
+++ b/src/engine/vector2.js
@@ -21,4 +21,20 @@ export class Vector2 {
return false
}
+
+
+ static removeIfInArray(value, array) {
+ return array.filter(x => !Vector2.equals(value, x))
+ }
+
+ static isInArray(position, array) {
+ return Array.from(array)
+ .some(p => Vector2.equals(p, position))
+ }
+
+ static pushIfNotInArray(position, array) {
+ if (!Vector2.isInArray(position, array)) {
+ array.push(position)
+ }
+ }
}