summaryrefslogtreecommitdiff
path: root/src/engine/liberty.js
blob: 3af21e273da37080a37020e797ccdc00e16ca6b5 (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
import { Vector2 } from "../engine/vector2.js"
import { getPiece } from "../engine/getPiece.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 = Vector2.removeIfInArray(position, liberties)

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

    return (liberties.length > 0)
}

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 => Vector2.pushIfNotInArray(p, checkedPositions))
        )

    return checkedPositions
}

function getGroup({position, checkedPositions = []}) {
    if (Vector2.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 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 (Vector2.isInArray(newGroup[0], partialArray)) {
                return partialArray
            }

            return partialArray.concat(newGroup)
        })
}

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

    let team = PLAYER.OPPOSITE(playerTeam)

    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 (Vector2.isInArray(newGroup[0], partialArray)) {
                return partialArray
            }

            return partialArray.concat(newGroup)
        })
}

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