summaryrefslogtreecommitdiff
path: root/src/engine/liberty.js
blob: 7c51ae2948620da45652df17880ba274e786b57b (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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import { board } from "../board.js"
import { Vector2 } from "../engine/vector2.js"
import { PLAYER } from "../constants.js"

const SURROUNDING = [
    new Vector2(-1, 0),
    new Vector2(0, 1),
    new Vector2(0, -1),
    new Vector2(1, 0),
]

export function hasLiberties({
    position,
    team
}) {
    if (getPiece(position) != undefined) return false
    let groups = getSurroundingGroups(position, team)
    groups.push(position)

    let liberties = getGroupLiberties(groups)
    liberties = removeIfInArray(position, liberties)

    if (liberties.length == 0) {
        let enemies = getGroupSurroundingEnemies(groups, team)
        let enemyLiberties = getGroupLiberties(enemies)
        enemyLiberties = removeIfInArray(position, enemyLiberties)
        return (enemyLiberties.length == 0)
    }

    return (liberties.length > 0)
}

function removeIfInArray(value, array) {
    return array.filter(x => !Vector2.equals(value, x))
}

function getLiberties(position) {
    return getSurroundingSquares(position)
        .filter(p => getPiece(p) == undefined)
}

function getGroupLiberties(group) {
    let checkedPositions = []
    group
        .map(p => getLiberties(p))
        .forEach(g => g
            .forEach(p => pushIfNotInArray(p, checkedPositions))
        )

    return checkedPositions
}

function getGroupSurroundingEnemies(group, playerTeam) {
    if (group.length == 0) return []

    let team = playerTeam == PLAYER.BLACK ?
        PLAYER.WHITE :
        PLAYER.BLACK

    let surroundingEnemies = group
        .map(p => getSurroundingGroups(p, team))

    if (surroundingEnemies.length == 0) return []

    return surroundingEnemies
        .reduce((partialArray = [], newGroup) => {
            if (newGroup.length == 0) {
                return partialArray
            }
            if (isInArray(newGroup[0], partialArray)) {
                return partialArray
            }

            return partialArray.concat(newGroup)
        })
}

function getSurroundingGroups(position, team) {
    let surroundingGroups = getSurroundingSquares(position)
        .filter(p => getPiece(p) != undefined)
        .filter(p => getPiece(p).team == team)
        .map(p => getGroup({position: p}))

    if (surroundingGroups.length == 0) {
        return []
    }

    return surroundingGroups
        .reduce((partialArray = [], newGroup) => {
            if (newGroup.length == 0) {
                return partialArray
            }
            if (isInArray(newGroup[0], partialArray)) {
                return partialArray
            }

            return partialArray.concat(newGroup)
        })
}

function getGroup({position, checkedPositions = []}) {
    if (isInArray(position, checkedPositions)) {
        return
    }

    checkedPositions.push(position)
    let team = getPiece(position).team

    getSurroundingSquares(position)
        .filter(p => getPiece(p) != undefined)
        .filter(p => getPiece(p).team == team)
        .forEach(p => {
            getGroup({
                position: p,
                checkedPositions: checkedPositions
            })
        })

    return checkedPositions
}

function getPiece(position) {
    return board.stones[position.y][position.x]
}

function isInArray(position, array) {
    return Array.from(array)
        .some(p => Vector2.equals(p, position))
}

function pushIfNotInArray(position, array) {
    if (!isInArray(position, array)) {
        array.push(position)
    }
}

function getSurroundingSquares(position) {
    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
        )
}