summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornil <niljo@airmail.cc>2024-12-14 21:38:07 +0100
committernil <niljo@airmail.cc>2024-12-14 21:38:07 +0100
commitadd682d6aaa50b2c1561731cdb1696bf7665cba3 (patch)
tree81bbb91e40d3aa537f41ab884ff4f7dd13d113c9
parent1b6275b4fbc4ab7bcd876938f807aad9ce1900a6 (diff)
liberty fix and tidying code
-rw-r--r--src/engine/liberty.js63
-rw-r--r--src/engine/placeLinks.js38
-rw-r--r--src/engine/turns.js37
3 files changed, 84 insertions, 54 deletions
diff --git a/src/engine/liberty.js b/src/engine/liberty.js
index d5d4448..6d1bbda 100644
--- a/src/engine/liberty.js
+++ b/src/engine/liberty.js
@@ -24,46 +24,73 @@ function checkLiberties({
checkedStones = [],
checkedLiberties = [],
}) {
- if (
- Array.from(checkedStones)
- .some(p => Vector2.equals(p, position))
- ) {
+ if (isInArray(position, checkedStones)) {
return false
}
+
+ if (checkedLiberties.length > 0) {
+ console.log(team, checkedLiberties)
+ }
checkedStones.push(position)
- let surroundingSquares = Array.from(SURROUNDING)
+ let surroundingSquares = getSurroundingSquares(position)
+
+ findEmptySquares(
+ surroundingSquares,
+ checkedLiberties
+ )
+
+ findFriendlyStones(
+ surroundingSquares,
+ {team, checkedStones, checkedLiberties}
+ )
+
+ if (checkedLiberties.length == 1 && checkedStones.length < 2) {
+ return true
+ }
+
+ if (checkedLiberties.length > 1) {
+ return true
+ }
+
+ return false
+}
+
+function isInArray(position, array) {
+ return Array.from(array)
+ .some(p => Vector2.equals(p, position))
+}
+
+function getSurroundingSquares(position) {
+ 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
)
+}
+function findEmptySquares(surroundingSquares, checkedLiberties) {
surroundingSquares
.filter(z => board.stones[z.y][z.x] == undefined)
.forEach(z => {
- if (!Array.from(checkedStones)
- .some(p => Vector2.equals(p, z))
- ) {
+ if (!isInArray(z, checkedLiberties)) {
checkedLiberties.push(z)
}
})
+}
+function findFriendlyStones(surroundingSquares, data) {
surroundingSquares
.filter(z => board.stones[z.y][z.x] != undefined)
- .filter(z => board.stones[z.y][z.x].team == team)
+ .filter(z => board.stones[z.y][z.x].team == data.team)
.forEach(z => {
checkLiberties({
position: z,
- team: team,
- checkedStones: checkedStones,
- checkedLiberties: checkedLiberties,
+ team: data.team,
+ checkedStones: data.checkedStones,
+ checkedLiberties: data.checkedLiberties,
})
})
-
- if (checkedLiberties.length > 0) {
- return true
- }
-
- return false
}
+
diff --git a/src/engine/placeLinks.js b/src/engine/placeLinks.js
new file mode 100644
index 0000000..07d67d3
--- /dev/null
+++ b/src/engine/placeLinks.js
@@ -0,0 +1,38 @@
+import { board } from "../board.js"
+import { Vector2 } from "../engine/vector2.js"
+import { Link } from "../link.js"
+import { isMoveLegal } from "../engine/moves.js"
+import { playerTurn } from "../engine/turns.js"
+
+export function placeLinks() {
+ resetLink()
+ board.links = [...Array(board.size)].map(_ => Array(board.size))
+ for (let row = 0; row < board.size; row++) {
+ for (let col = 0; col < board.size; col++) {
+ tryLink(new Vector2(row, col))
+ }
+ }
+}
+
+function resetLink() {
+ Array.from(board.links).forEach(x => {
+ Array.from(x)
+ .filter((x) => x !== undefined)
+ .map(y => {
+ y.removeLink()
+ })
+ })
+}
+
+function tryLink(position) {
+ if (!isMoveLegal({
+ position: position,
+ team: playerTurn
+ })) {
+ return
+ }
+
+ new Link({
+ position: position,
+ })
+}
diff --git a/src/engine/turns.js b/src/engine/turns.js
index ee5b9e2..0018417 100644
--- a/src/engine/turns.js
+++ b/src/engine/turns.js
@@ -1,8 +1,5 @@
-import { board } from "../board.js"
-import { Vector2 } from "../engine/vector2.js"
-import { Link } from "../link.js"
import { PLAYER } from "../constants.js"
-import { isMoveLegal } from "../engine/moves.js"
+import { placeLinks } from "../engine/placeLinks.js"
export let playerTurn = ""
@@ -15,35 +12,3 @@ function changeTurn() {
playerTurn = playerTurn == PLAYER.BLACK ? PLAYER.WHITE : PLAYER.BLACK
}
-function placeLinks() {
- resetLink()
- board.links = [...Array(board.size)].map(_ => Array(board.size))
- for (let row = 0; row < board.size; row++) {
- for (let col = 0; col < board.size; col++) {
- tryLink(new Vector2(row, col))
- }
- }
-}
-
-function resetLink() {
- Array.from(board.links).forEach(x => {
- Array.from(x)
- .filter((x) => x !== undefined)
- .map(y => {
- y.removeLink()
- })
- })
-}
-
-function tryLink(position) {
- if (!isMoveLegal({
- position: position,
- team: playerTurn
- })) {
- return
- }
-
- new Link({
- position: position,
- })
-}