| 1 | class DarkRules extends ChessRules |
| 2 | { |
| 3 | // Standard rules, in the shadow |
| 4 | setOtherVariables(fen) |
| 5 | { |
| 6 | super.setOtherVariables(fen); |
| 7 | const [sizeX,sizeY] = [V.size.x,V.size.y]; |
| 8 | this.enlightened = { |
| 9 | "w": doubleArray(sizeX,sizeY), |
| 10 | "b": doubleArray(sizeX,sizeY) |
| 11 | }; |
| 12 | // Setup enlightened: squares reachable by each side |
| 13 | // (TODO: one side would be enough ?) |
| 14 | this.updateEnlightened(); |
| 15 | } |
| 16 | |
| 17 | updateEnlightened() |
| 18 | { |
| 19 | // Initialize with pieces positions (which are seen) |
| 20 | for (let i=0; i<V.size.x; i++) |
| 21 | { |
| 22 | for (let j=0; j<V.size.y; j++) |
| 23 | { |
| 24 | this.enlightened["w"][i][j] = false; |
| 25 | this.enlightened["b"][i][j] = false; |
| 26 | if (this.board[i][j] != V.EMPTY) |
| 27 | this.enlightened[this.getColor(i,j)][i][j] = true; |
| 28 | } |
| 29 | } |
| 30 | const currentTurn = this.turn; |
| 31 | this.turn = "w"; |
| 32 | const movesWhite = this.getAllValidMoves(); |
| 33 | this.turn = "b"; |
| 34 | const movesBlack = this.getAllValidMoves(); |
| 35 | this.turn = currentTurn; |
| 36 | for (let move of movesWhite) |
| 37 | this.enlightened["w"][move.end.x][move.end.y] = true; |
| 38 | for (let move of movesBlack) |
| 39 | this.enlightened["b"][move.end.x][move.end.y] = true; |
| 40 | } |
| 41 | |
| 42 | // Has to be redefined to avoid an infinite loop |
| 43 | getAllValidMoves() |
| 44 | { |
| 45 | const color = this.turn; |
| 46 | const oppCol = this.getOppCol(color); |
| 47 | let potentialMoves = []; |
| 48 | for (let i=0; i<V.size.x; i++) |
| 49 | { |
| 50 | for (let j=0; j<V.size.y; j++) |
| 51 | { |
| 52 | if (this.board[i][j] != V.EMPTY && this.getColor(i,j) == color) |
| 53 | Array.prototype.push.apply(potentialMoves, this.getPotentialMovesFrom([i,j])); |
| 54 | } |
| 55 | } |
| 56 | return potentialMoves; //because there are no checks |
| 57 | } |
| 58 | |
| 59 | atLeastOneMove() |
| 60 | { |
| 61 | if (this.kingPos[this.turn][0] < 0) |
| 62 | return false; |
| 63 | return true; //TODO: is it right? |
| 64 | } |
| 65 | |
| 66 | underCheck(color) |
| 67 | { |
| 68 | return false; //there is no check |
| 69 | } |
| 70 | |
| 71 | getCheckSquares(color) |
| 72 | { |
| 73 | return []; |
| 74 | } |
| 75 | |
| 76 | updateVariables(move) |
| 77 | { |
| 78 | // Update kings positions |
| 79 | const piece = move.vanish[0].p; |
| 80 | const c = move.vanish[0].c; |
| 81 | if (piece == V.KING && move.appear.length > 0) |
| 82 | { |
| 83 | this.kingPos[c][0] = move.appear[0].x; |
| 84 | this.kingPos[c][1] = move.appear[0].y; |
| 85 | } |
| 86 | if (move.vanish.length >= 2 && move.vanish[1].p == V.KING) |
| 87 | { |
| 88 | // We took opponent king ! |
| 89 | const oppCol = this.getOppCol(c); |
| 90 | this.kingPos[oppCol] = [-1,-1]; |
| 91 | } |
| 92 | |
| 93 | // Update moves for both colors: |
| 94 | this.updateEnlightened(); |
| 95 | } |
| 96 | |
| 97 | unupdateVariables(move) |
| 98 | { |
| 99 | super.unupdateVariables(move); |
| 100 | const c = move.vanish[0].c; |
| 101 | const oppCol = this.getOppCol(c); |
| 102 | if (this.kingPos[oppCol][0] < 0) |
| 103 | { |
| 104 | // Last move took opponent's king |
| 105 | for (let psq of move.vanish) |
| 106 | { |
| 107 | if (psq.p == 'k') |
| 108 | { |
| 109 | this.kingPos[oppCol] = [psq.x, psq.y]; |
| 110 | break; |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | // Update moves for both colors: |
| 116 | this.updateEnlightened(); |
| 117 | } |
| 118 | |
| 119 | checkGameEnd() |
| 120 | { |
| 121 | // No valid move: our king disappeared |
| 122 | return this.turn == "w" ? "0-1" : "1-0"; |
| 123 | } |
| 124 | |
| 125 | static get THRESHOLD_MATE() |
| 126 | { |
| 127 | return 500; //checkmates evals may be slightly below 1000 |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | const VariantRules = DarkRules; |