summaryrefslogtreecommitdiff
path: root/src/engine/liberty.js
blob: a006c625b18cd25e33b0893f2ed2c4b2a963e8d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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.y][z.x] == undefined ?
	    true :
	    board.stones[z.y][z.x].team == team
	)
}