summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornil <niljo@airmail.cc>2024-12-13 20:00:50 +0100
committernil <niljo@airmail.cc>2024-12-13 20:00:50 +0100
commit1b6275b4fbc4ab7bcd876938f807aad9ce1900a6 (patch)
treecd1c577a726f67afc2c276eb81ae1628edcdcd96
parent99c5056ffc4adec03f29730869158e5f80bc4304 (diff)
add liberties
-rw-r--r--src/engine/liberty.js63
-rw-r--r--src/engine/moves.js4
-rw-r--r--src/engine/vector2.js19
-rw-r--r--src/engine/visual/placeOnBoard.js28
-rwxr-xr-xsrc/index.js1
-rw-r--r--src/link.js1
6 files changed, 85 insertions, 31 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
}
diff --git a/src/engine/moves.js b/src/engine/moves.js
index 26158a0..694a482 100644
--- a/src/engine/moves.js
+++ b/src/engine/moves.js
@@ -6,11 +6,11 @@ export function isMoveLegal({
team = "none"
}) {
if (board.stones[position.y][position.x] != undefined) {
- return false
+ return false
}
if (!hasLiberties({position: position, team: team})) {
- return false
+ return false
}
return true
diff --git a/src/engine/vector2.js b/src/engine/vector2.js
index 6c2a2a2..8b10e28 100644
--- a/src/engine/vector2.js
+++ b/src/engine/vector2.js
@@ -5,9 +5,20 @@ export class Vector2 {
}
static sum(a, b) {
- return new Vector2(
- a.x + b.x,
- a.y + b.y,
- )
+ return new Vector2(
+ a.x + b.x,
+ a.y + b.y,
+ )
+ }
+
+ static equals(a, b) {
+ if (
+ a.x == b.x &&
+ a.y == b.y
+ ) {
+ return true
+ }
+
+ return false
}
}
diff --git a/src/engine/visual/placeOnBoard.js b/src/engine/visual/placeOnBoard.js
index 4eea0d6..6e04e0d 100644
--- a/src/engine/visual/placeOnBoard.js
+++ b/src/engine/visual/placeOnBoard.js
@@ -7,22 +7,22 @@ const MEDIA_LISTENER = window.matchMedia("(max-width: 600px)")
MEDIA_LISTENER.addEventListener("change", function() {
Array.from(board.stones)
- .forEach(x => {
- Array.from(x)
- .filter((x) => x !== undefined)
- .map(y => {
- y.update(MEDIA_LISTENER)
- })
- })
+ .forEach(x => {
+ Array.from(x)
+ .filter((x) => x !== undefined)
+ .map(y => {
+ y.update(MEDIA_LISTENER)
+ })
+ })
Array.from(board.links)
- .forEach(x => {
- Array.from(x)
- .filter((x) => x !== undefined)
- .map(y => {
- y.update(MEDIA_LISTENER)
- })
- })
+ .forEach(x => {
+ Array.from(x)
+ .filter((x) => x !== undefined)
+ .map(y => {
+ y.update(MEDIA_LISTENER)
+ })
+ })
});
const PLACE_VALUES = {
diff --git a/src/index.js b/src/index.js
index c05662c..5a376ac 100755
--- a/src/index.js
+++ b/src/index.js
@@ -3,7 +3,6 @@ import { nextTurn } from "./engine/turns.js"
function start() {
- console.log("start")
prepareBoard()
nextTurn()
}
diff --git a/src/link.js b/src/link.js
index 192f1e9..051527c 100644
--- a/src/link.js
+++ b/src/link.js
@@ -17,7 +17,6 @@ export class Link {
board.links[position.y][position.x] = this
this.span.addEventListener("click", _ => {
- console.log("link position", this.position)
new Stone({
position: position,
team: this.team