From cec1da14708e782801d0cc27fb13ff7586c0e96c Mon Sep 17 00:00:00 2001 From: nil Date: Fri, 13 Dec 2024 16:42:57 +0100 Subject: liberty check demo --- src/engine/liberty.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 src/engine/liberty.js (limited to 'src/engine/liberty.js') 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 + ) +} -- cgit v1.2.3