summaryrefslogtreecommitdiff
path: root/qml/javascript/goban_util.js
blob: dfaa70c557a30e0d5a409caca2d9cef7ed7e887c (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
207
208
209
210
211
212
.pragma library

/**
 * Check if the case on the grid belongs to the first column.
 */
function isFirstCol(index, cols) {
    return index % cols == 0;
}

/**
 * Check if the case on the grid belongs to the last column.
 */
function isLastCol(index, cols) {
    return  index % cols == cols - 1;
}

/**
 * Check if the case on the grid belongs to the first row
 */
function isFirstRow(index, cols) {
    return index < cols;
}

/**
 * Check if the case on the grid belongs to the last row.
 */
function isLastRow(index, cols, rows) {
    return cols * (rows - 1) <= index;
}

/**
 * Get all the neighbors for a given position.
 */
function getNeighbors(index, cols, rows) {

    var neighbors = [];
    if (!isFirstCol(index, cols)) {
        neighbors.push(index - 1)
    }

    if (!isLastCol(index, cols)) {
        neighbors.push(index + 1)
    }

    if (!isFirstRow(index, cols)) {
        neighbors.push(index - cols)
    }

    if (!isLastRow(index, cols, rows)) {
        neighbors.push(index + cols)
    }

    return neighbors;
}

function getChainToRemove(index, grid, filter) {

    var piecesToCheck = [];
    var piecesToRemove = [];

    /*
     * filter wich keep only free places.
     */
    function freePlaces(x) {
        return grid.getElementAtIndex(x).getType() === "";
    }

    var piece = index;
    while (piece !== undefined) {

        /* if the case has already been marked, do not check it again.
         */
        if (!grid.getElementAtIndex(piece).mark) {
            grid.getElementAtIndex(piece).mark = true;
            piecesToRemove.push(piece);

            var neighbors = getNeighbors(piece, grid.columns, grid.rows);

            if (neighbors.length !== 0) {
                /*
                 * If the place has liberty, return empty list.
                 */
                if (neighbors.some(freePlaces)) {
                    return [];
                }

                /*
                 * Now update the check list.
                 */
                neighbors.filter(filter).forEach(function(x) {
                    piecesToCheck.push(x)
                });

            }
        } else {
            /*
             * The piece may have been marked outside of this call.
             * (We try to check chain in each direction, and return as soon as
             * we find an empty place).
             * If the piece is marked, but does not belongs to the piecesToRemove,
             * we assume the piece is connected to a living chain, and
             * subsequently this chain too.
             */
            if (! piecesToRemove.some(function(x) { return x === piece})) {
                return [];
            }
        }

        piece = piecesToCheck.pop();
    }
    return piecesToRemove;

}

/**
 * Add a new stone on the goban.
 *
 * Check if there are dead chained and remove them from the goban.
 *
 * index(int):          the index where put the stone.
 * grid(object):        the grid where to put the stone:
 *  - grid.rows:        number of rows in the grid
 *  - grid.columns:     number of columes in the grid
 *  - grid.getElementAtIndex(index) should return the stone a the given index
 * currentPlayer(bool): player color
 * animation(bool):     should we add animation on the goban
 * allowSuicide(bool):  if suicide an autorized action
 *
 * return true if the movement has been allowed.
 */
function addPiece(index, grid, currentPlayer, animation, allowSuicide, allowOveride) {

    var point = grid.getElementAtIndex(index);
    var elementType = point.getType();

    if (!allowOveride && elementType !== "") {
        return false;
    }

    var neighbors = getNeighbors(index, grid.columns, grid.rows);

    function isPlayer(x) {
        return grid.getElementAtIndex(x).getType() === (currentPlayer ? "white" : "black");
    }

    function isOponnent(x) {
        return grid.getElementAtIndex(x).getType() === (currentPlayer ? "black" : "white");
    }

    function freeOrChain(x) {
        var pointType = grid.getElementAtIndex(x).getType();
        return pointType === "" || pointType === (currentPlayer ? "white" : "black");
    }

    point.put(currentPlayer, animation);

    if (neighbors.length === 0) {
        return true;
    }

    var somethingToRemove = false;
    var movementAutorized = true;

    /*
     * Check for pieces to remove.
     */
    neighbors.filter(isOponnent).forEach(function(neighbor) {

        var piecesToRemove = getChainToRemove(neighbor, grid, isOponnent);
        if (piecesToRemove.length !== 0) {
            somethingToRemove = true;
        }
        piecesToRemove.forEach(function(x) {
            grid.getElementAtIndex(x).remove(animation);
        })
    });

    /*
     * Check for suicide.
     */
    if (!somethingToRemove) {
        var suicides = getChainToRemove(index, grid, isPlayer);
        if (suicides.length !== 0) {
            if (allowSuicide) {
                suicides.forEach(function(x) {
                    grid.getElementAtIndex(x).remove(animation);
                });
            } else {
                point.remove(false);
                movementAutorized = false;
            }
        }

    }

    /*
     * Remove the marks in the cases.
     *
     * The call to getChainToRemove add marks on the cases in order to
     * prevent infinite looping. We need to clean the cases before any new
     * click.
     *
     * We do not need to remove them before as we are not filtering the
     * same pieces.
     */
    for (var i = 0; i < grid.columns * grid.rows; i++) {
        grid.getElementAtIndex(i).mark = false;
    }

    return movementAutorized;

}