summaryrefslogtreecommitdiff
path: root/src/engine/liberty.js
blob: 372057e23394af656e40584d9e02d6cf41564431 (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.x][z.y] == undefined ?
	    true :
	    board.stones[z.x][z.y].team == team
	)
}