summaryrefslogtreecommitdiff
path: root/qml/javascript/goban_util.js
blob: f6f48c9968d36bc4344464ac188f4d96c51fb0aa (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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
.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;

}

/*
 * Undo a move on the board.
 */
function undo(grid, step) {

    /*
     * Fill the space with the given color.
     */
    function fillWith(index, color) {

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

        var piece = index;

        var space = [];

        while (piece !== undefined) {

            var point = grid.getElementAtIndex(piece);

            if (point.mark || !point.getType === "") {
                piece = space.pop();
                continue;
            }
            point.mark = true;

            point.put(color, false);

            getNeighbors(piece, grid.columns, grid.rows).filter(freePlaces).forEach(function(x) {
                space.push(x);
            });
            piece = space.pop();
        }
    }

    /*
     * First add each point marked as removed.
     * Only the first point is recorded, so we fill all the other points
     */

    if (step.suicide) {
        fillWith(step.added, step.player);
    } else {
        var removed = step.removed;
        if (removed !== undefined) {
            fillWith(step.removed, !step.player);
        }
        grid.getElementAtIndex(step.added).remove(false);
    }

    clearMarks(grid);
}

/**
 * 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 a step object 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 undefined;
    }

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

    var step = {};
    step.added = index;
    step.player = currentPlayer;

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

    var somethingToRemove = false;
    var movementAutorized = true;

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

        if (!isOponnent(neighbor)) {
            return;
        }

        var piecesToRemove = getChainToRemove(neighbor, grid, isOponnent);
        if (piecesToRemove.length !== 0) {
            step.removed = piecesToRemove[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) {

                step.suicide = true;

                suicides.forEach(function(x) {
                    grid.getElementAtIndex(x).remove(animation);
                });
            } else {
                point.remove(false);
                movementAutorized = false;
            }
        }

    }

    /* We do not need to clear the marks before as we are not filtering the
     * same pieces.
     */
    clearMarks(grid);

    if (movementAutorized) {
        return step;
    } else {
        return undefined;
    }
}


/*
 * Remove the marks in the cases.
 *
 * Some functions add marks on each stone in order to prevent infinite looping.
 * We need to clean the cases before any new action.
 *
 */
function clearMarks(grid) {
    for (var i = 0; i < grid.columns * grid.rows; i++) {
        grid.getElementAtIndex(i).mark = false;
    }
}