summaryrefslogtreecommitdiff
path: root/src/engine/liberty.js
blob: 7ccd77d02ac416fd28425b57c1a08014404aab75 (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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
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 checkLiberties({
        position,
        team
    })
}

function checkLiberties({
    position,
    team,
    checkedStones = [],
    checkedLiberties = [],
    checkedEnemies = [],
    isAttacking = false,
    isRoot = true,
}) {
    if (isInArray(position, checkedStones)) {
        return
    }

    checkedStones.push(position)

    let surroundingSquares = getSurroundingSquares(position)

    surroundingSquares.every((z) => {
        let s = board.stones[z.y][z.x]
        switch (true) {
            case s == undefined:
                pushIfNotInArray(z, checkedLiberties)
                break

            case s.team == team:
                handleFriendlyStone({
                    position: z,
                    team: team,
                    checkedStones: checkedStones,
                    checkedLiberties: checkedLiberties,
                    checkedEnemies: checkedEnemies,
                    isAttacking: isAttacking,
                })
                break

            case s.team != team:
                if (isAttackingEnemyStone({
                    position: z,
                    team: s.team,
                    checkedEnemies: checkedEnemies,
                    isAttacking: isAttacking,
                })) {
                    isAttacking = true
                    return false
                }
                break
        }

        return true
    })

    if (isRoot) {
        let liberties = checkedLiberties
            .filter(x => !Vector2.equals(x, position))
            .length

        if (liberties > 0 || isAttacking) {
            return true
        }

        return false
    }
}

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
        )
}

function handleFriendlyStone(data) {
    checkLiberties({
        position: data.position,
        team: data.team,
        checkedStones: data.checkedStones,
        checkedLiberties: data.checkedLiberties,
        checkedEnemies: data.checkedEnemies,
        isAttacking: data.isAttacking,
        isRoot: false,
    })
}

function isAttackingEnemyStone({
    position,
    isAttacking,
    checkedEnemies,
}) {
    if (isAttacking) {
        return
    }

    let liberties = getEnemyLiberties({
        position: position,
        team: board.stones[position.y][position.x].team,
        checkedEnemies: checkedEnemies,
    })

    if (liberties == 1) {
        return true
    }

    return false
}

function getEnemyLiberties({
    position,
    team,
    checkedEnemies,
    checkedEnemyLiberties = [],
}) {
    if (isInArray(position, checkedEnemies)) {
        return
    }

    checkedEnemies.push(position)

    getSurroundingSquares(position)
        .forEach(z => {
            let s = board.stones[z.y][z.x]
            switch (true) {
                case s == undefined:
                    pushIfNotInArray(z, checkedEnemyLiberties)
                    break
                case s.team == team:
                    getEnemyLiberties({
                        position: z,
                        team: team,
                        checkedEnemies: checkedEnemies,
                        checkedEnemyLiberties: checkedEnemyLiberties,
                    })
                    break
            }
        })

    return checkedEnemyLiberties.length
}