summaryrefslogtreecommitdiff
path: root/src/engine/liberty.js
blob: 9f2049cc282904d79d9a8d82b695ed112b34ad40 (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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
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
}) {
    if (getPiece(position) != undefined) return false
    let groups = getSurroundingGroups(position, team)
    // if (stones.length > 0) console.log(stones)
    // console.log(stones)
    return true
    // return checkLiberties({
    //     position,
    //     team
    // })
}

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

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