summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornil <niljo@airmail.cc>2024-12-13 16:42:57 +0100
committernil <niljo@airmail.cc>2024-12-13 16:42:57 +0100
commitcec1da14708e782801d0cc27fb13ff7586c0e96c (patch)
treeccd80c58e0aba22facb937c88ec5c9957055e4a1
parent16e02376e2273eeb25a6461f84641e96a485f652 (diff)
liberty check demo
-rw-r--r--src/engine/liberty.js24
-rw-r--r--src/engine/moves.js8
-rw-r--r--src/engine/turns.js2
-rw-r--r--src/engine/vector2.js (renamed from src/engine/visual/vector2.js)7
-rw-r--r--src/link.js3
-rw-r--r--src/stone.js2
6 files changed, 42 insertions, 4 deletions
diff --git a/src/engine/liberty.js b/src/engine/liberty.js
new file mode 100644
index 0000000..372057e
--- /dev/null
+++ b/src/engine/liberty.js
@@ -0,0 +1,24 @@
+import { board } from "../board.js"
+import { Vector2 } from "../engine/vector2.js"
+
+const SURROUNDING = [
+ new Vector2(-1, 0),
+ new Vector2(0, 1),
+ new Vector2(0, -1),
+ new Vector2(1, 0),
+]
+
+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.x][z.y] == undefined ?
+ true :
+ board.stones[z.x][z.y].team == team
+ )
+}
diff --git a/src/engine/moves.js b/src/engine/moves.js
index f820f97..26158a0 100644
--- a/src/engine/moves.js
+++ b/src/engine/moves.js
@@ -1,11 +1,17 @@
import { board } from "../board.js"
+import { hasLiberties } from "./liberty.js"
export function isMoveLegal({
position,
- turn = "none"
+ team = "none"
}) {
if (board.stones[position.y][position.x] != undefined) {
return false
}
+
+ if (!hasLiberties({position: position, team: team})) {
+ return false
+ }
+
return true
}
diff --git a/src/engine/turns.js b/src/engine/turns.js
index 6a5e6fb..ee5b9e2 100644
--- a/src/engine/turns.js
+++ b/src/engine/turns.js
@@ -1,5 +1,5 @@
import { board } from "../board.js"
-import { Vector2 } from "../engine/visual/vector2.js"
+import { Vector2 } from "../engine/vector2.js"
import { Link } from "../link.js"
import { PLAYER } from "../constants.js"
import { isMoveLegal } from "../engine/moves.js"
diff --git a/src/engine/visual/vector2.js b/src/engine/vector2.js
index a3a1bc2..6c2a2a2 100644
--- a/src/engine/visual/vector2.js
+++ b/src/engine/vector2.js
@@ -3,4 +3,11 @@ export class Vector2 {
this.x = x
this.y = y
}
+
+ static sum(a, b) {
+ return new Vector2(
+ a.x + b.x,
+ a.y + b.y,
+ )
+ }
}
diff --git a/src/link.js b/src/link.js
index e0c8266..192f1e9 100644
--- a/src/link.js
+++ b/src/link.js
@@ -1,5 +1,5 @@
import { placeOnBoard } from "./engine/visual/placeOnBoard.js"
-import { Vector2 } from "./engine/visual/vector2.js"
+import { Vector2 } from "./engine/vector2.js"
import { board } from "./board.js"
import { ASSETS_BLACK, ASSETS_WHITE, PLAYER } from "./constants.js"
import { Stone } from "./stone.js"
@@ -17,6 +17,7 @@ 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
diff --git a/src/stone.js b/src/stone.js
index 0fc8607..15035f0 100644
--- a/src/stone.js
+++ b/src/stone.js
@@ -1,5 +1,5 @@
import { placeOnBoard } from "./engine/visual/placeOnBoard.js"
-import { Vector2 } from "./engine/visual/vector2.js"
+import { Vector2 } from "./engine/vector2.js"
import { board } from "./board.js"
import { ASSETS_BLACK, ASSETS_WHITE, PLAYER } from "./constants.js"