+ getComputerMove() {
+ const moves = super.getAllValidMoves();
+ if (moves.length == 0) return null;
+ const color = this.turn;
+ const oppCol = V.GetOppCol(color);
+ // Capture available? If yes, play it
+ const captures = moves.filter(m => m.vanish.length == 2);
+ if (captures.length >= 1) {
+ const m1 = captures[randInt(captures.length)];
+ this.play(m1);
+ const moves2 = super.getAllValidMoves();
+ // Remove a stone which was about to capture one of ours, if possible
+ let candidates = [];
+ for (let m2 of moves2) {
+ const [x, y] = [m2.start.x, m2.start.y];
+ for (let s of V.steps[V.ROOK]) {
+ const [i, j] = [x + 2*s[0], y + 2*s[1]];
+ if (
+ V.OnBoard(i, j) &&
+ this.board[i][j] == V.EMPTY &&
+ this.board[i - s[0], j - s[1]] != V.EMPTY &&
+ this.getColor(i - s[0], j - s[1]) == color
+ ) {
+ candidates.push(m2);
+ break;
+ }
+ }
+ }
+ this.undo(m1);
+ if (candidates.length >= 1)
+ return [m1, candidates[randInt(candidates.length)]];
+ return [m1, moves2[randInt(moves2.length)]];
+ }
+ // Just play a random move, which if possible do not let a capture
+ let candidates = [];
+ for (let m of moves) {
+ this.play(m);
+ const moves2 = super.getAllValidMoves();
+ if (moves2.every(m2 => m2.vanish.length <= 1))
+ candidates.push(m);
+ this.undo(m);
+ }
+ if (candidates.length >= 1) return candidates[randInt(candidates.length)];
+ return moves[randInt(moves.length)];