| 1 | class AliceRules extends ChessRules |
| 2 | { |
| 3 | static get ALICE_PIECES() |
| 4 | { |
| 5 | return { |
| 6 | 's': 'p', |
| 7 | 't': 'q', |
| 8 | 'u': 'r', |
| 9 | 'c': 'b', |
| 10 | 'o': 'n', |
| 11 | 'l': 'k', |
| 12 | }; |
| 13 | } |
| 14 | static get ALICE_CODES() |
| 15 | { |
| 16 | return { |
| 17 | 'p': 's', |
| 18 | 'q': 't', |
| 19 | 'r': 'u', |
| 20 | 'b': 'c', |
| 21 | 'n': 'o', |
| 22 | 'k': 'l', |
| 23 | }; |
| 24 | } |
| 25 | |
| 26 | static getPpath(b) |
| 27 | { |
| 28 | return (Object.keys(this.ALICE_PIECES).includes(b[1]) ? "Alice/" : "") + b; |
| 29 | } |
| 30 | |
| 31 | initVariables(fen) |
| 32 | { |
| 33 | super.initVariables(fen); |
| 34 | const fenParts = fen.split(" "); |
| 35 | const position = fenParts[0].split("/"); |
| 36 | if (this.kingPos["w"][0] < 0 || this.kingPos["b"][0] < 0) |
| 37 | { |
| 38 | // INIT_COL_XXX won't be used, so no need to set them for Alice kings |
| 39 | for (let i=0; i<position.length; i++) |
| 40 | { |
| 41 | let k = 0; //column index on board |
| 42 | for (let j=0; j<position[i].length; j++) |
| 43 | { |
| 44 | switch (position[i].charAt(j)) |
| 45 | { |
| 46 | case 'l': |
| 47 | this.kingPos['b'] = [i,k]; |
| 48 | break; |
| 49 | case 'L': |
| 50 | this.kingPos['w'] = [i,k]; |
| 51 | break; |
| 52 | default: |
| 53 | let num = parseInt(position[i].charAt(j)); |
| 54 | if (!isNaN(num)) |
| 55 | k += (num-1); |
| 56 | } |
| 57 | k++; |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | // Build board of the given (mirror)side |
| 64 | getSideBoard(mirrorSide) |
| 65 | { |
| 66 | const V = VariantRules; |
| 67 | // Build corresponding board from complete board |
| 68 | const [sizeX,sizeY] = V.size; |
| 69 | let sideBoard = doubleArray(sizeX, sizeY, ""); |
| 70 | for (let i=0; i<sizeX; i++) |
| 71 | { |
| 72 | for (let j=0; j<sizeY; j++) |
| 73 | { |
| 74 | const piece = this.getPiece(i,j); |
| 75 | if (mirrorSide==1 && Object.keys(V.ALICE_CODES).includes(piece)) |
| 76 | sideBoard[i][j] = this.board[i][j]; |
| 77 | else if (mirrorSide==2 && Object.keys(V.ALICE_PIECES).includes(piece)) |
| 78 | sideBoard[i][j] = this.getColor(i,j) + V.ALICE_PIECES[piece]; |
| 79 | } |
| 80 | } |
| 81 | return sideBoard; |
| 82 | } |
| 83 | |
| 84 | // NOTE: castle & enPassant https://www.chessvariants.com/other.dir/alice.html |
| 85 | // --> Should be OK as is. |
| 86 | getPotentialMovesFrom([x,y], sideBoard) |
| 87 | { |
| 88 | const pieces = Object.keys(VariantRules.ALICE_CODES); |
| 89 | const codes = Object.keys(VariantRules.ALICE_PIECES); |
| 90 | const mirrorSide = (pieces.includes(this.getPiece(x,y)) ? 1 : 2); |
| 91 | |
| 92 | // Search valid moves on sideBoard |
| 93 | let saveBoard = this.board; |
| 94 | this.board = sideBoard || this.getSideBoard(mirrorSide); |
| 95 | let moves = super.getPotentialMovesFrom([x,y]); |
| 96 | this.board = saveBoard; |
| 97 | |
| 98 | // Finally filter impossible moves |
| 99 | let res = moves.filter(m => { |
| 100 | if (m.appear.length == 2) //castle |
| 101 | { |
| 102 | // If appear[i] not in vanish array, then must be empty square on other board |
| 103 | m.appear.forEach(psq => { |
| 104 | if (this.board[psq.x][psq.y] != VariantRules.EMPTY && |
| 105 | ![m.vanish[0].y,m.vanish[1].y].includes(psq.y)) |
| 106 | { |
| 107 | return false; |
| 108 | } |
| 109 | }); |
| 110 | } |
| 111 | else if (this.board[m.end.x][m.end.y] != VariantRules.EMPTY) |
| 112 | { |
| 113 | // Attempt to capture |
| 114 | const piece = this.getPiece(m.end.x,m.end.y); |
| 115 | if ((mirrorSide==1 && codes.includes(piece)) |
| 116 | || (mirrorSide==2 && pieces.includes(piece))) |
| 117 | { |
| 118 | return false; |
| 119 | } |
| 120 | } |
| 121 | // If the move is computed on board1, m.appear change for Alice pieces. |
| 122 | if (mirrorSide==1) |
| 123 | { |
| 124 | m.appear.forEach(psq => { //forEach: castling taken into account |
| 125 | psq.p = VariantRules.ALICE_CODES[psq.p]; //goto board2 |
| 126 | }); |
| 127 | } |
| 128 | else //move on board2: mark vanishing pieces as Alice |
| 129 | { |
| 130 | m.vanish.forEach(psq => { |
| 131 | psq.p = VariantRules.ALICE_CODES[psq.p]; |
| 132 | }); |
| 133 | } |
| 134 | // Fix en-passant captures |
| 135 | if (m.vanish.length == 2 && this.board[m.end.x][m.end.y] == VariantRules.EMPTY) |
| 136 | { |
| 137 | m.vanish[1].c = this.getOppCol(this.getColor(x,y)); |
| 138 | // In the special case of en-passant, if |
| 139 | // - board1 takes board2 : vanish[1] --> Alice |
| 140 | // - board2 takes board1 : vanish[1] --> normal |
| 141 | let van = m.vanish[1]; |
| 142 | if (mirrorSide==1 && codes.includes(this.getPiece(van.x,van.y))) |
| 143 | van.p = VariantRules.ALICE_CODES[van.p]; |
| 144 | else if (mirrorSide==2 && pieces.includes(this.getPiece(van.x,van.y))) |
| 145 | van.p = VariantRules.ALICE_PIECES[van.p]; |
| 146 | } |
| 147 | return true; |
| 148 | }); |
| 149 | return res; |
| 150 | } |
| 151 | |
| 152 | // NOTE: alternative implementation, recompute sideBoard's in this function |
| 153 | filterValid(moves, sideBoard) |
| 154 | { |
| 155 | if (moves.length == 0) |
| 156 | return []; |
| 157 | const pieces = Object.keys(VariantRules.ALICE_CODES); |
| 158 | return moves.filter(m => { |
| 159 | // WARNING: for underCheck(), we need the sideBoard of the arrival world ! |
| 160 | const mirrorSide = (pieces.includes(this.getPiece(m.start.x,m.start.y)) ? 2 : 1); |
| 161 | return !this.underCheck(m, !!sideBoard ? sideBoard[mirrorSide-1] : null); |
| 162 | }); |
| 163 | } |
| 164 | |
| 165 | getAllValidMoves() |
| 166 | { |
| 167 | const color = this.turn; |
| 168 | const oppCol = this.getOppCol(color); |
| 169 | var potentialMoves = []; |
| 170 | let [sizeX,sizeY] = VariantRules.size; |
| 171 | let sideBoard = [this.getSideBoard(1), this.getSideBoard(2)]; |
| 172 | for (var i=0; i<sizeX; i++) |
| 173 | { |
| 174 | for (var j=0; j<sizeY; j++) |
| 175 | { |
| 176 | if (this.board[i][j] != VariantRules.EMPTY && this.getColor(i,j) == color) |
| 177 | { |
| 178 | const mirrorSide = |
| 179 | (Object.keys(VariantRules.ALICE_CODES).includes(this.getPiece(i,j)) ? 1 : 2); |
| 180 | Array.prototype.push.apply(potentialMoves, |
| 181 | this.getPotentialMovesFrom([i,j], sideBoard[mirrorSide-1])); |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | return this.filterValid(potentialMoves, sideBoard); |
| 186 | } |
| 187 | |
| 188 | underCheck(move, sideBoard) |
| 189 | { |
| 190 | const color = this.turn; |
| 191 | this.play(move); |
| 192 | const pieces = Object.keys(VariantRules.ALICE_CODES); |
| 193 | const kp = this.kingPos[color]; |
| 194 | const mirrorSide = (pieces.includes(this.getPiece(kp[0],kp[1])) ? 1 : 2); |
| 195 | let saveBoard = this.board; |
| 196 | this.board = sideBoard || this.getSideBoard(mirrorSide); |
| 197 | let res = this.isAttacked(this.kingPos[color], this.getOppCol(color)); |
| 198 | this.board = saveBoard; |
| 199 | this.undo(move); |
| 200 | return res; |
| 201 | } |
| 202 | |
| 203 | getCheckSquares(move) |
| 204 | { |
| 205 | this.play(move); |
| 206 | const color = this.turn; //opponent |
| 207 | const pieces = Object.keys(VariantRules.ALICE_CODES); |
| 208 | const kp = this.kingPos[color]; |
| 209 | const mirrorSide = (pieces.includes(this.getPiece(kp[0],kp[1])) ? 1 : 2); |
| 210 | let sideBoard = this.getSideBoard(mirrorSide); |
| 211 | let saveBoard = this.board; |
| 212 | this.board = sideBoard; |
| 213 | let res = this.isAttacked(this.kingPos[color], this.getOppCol(color)) |
| 214 | ? [ JSON.parse(JSON.stringify(this.kingPos[color])) ] |
| 215 | : [ ]; |
| 216 | this.board = saveBoard; |
| 217 | this.undo(move); |
| 218 | return res; |
| 219 | } |
| 220 | |
| 221 | updateVariables(move) |
| 222 | { |
| 223 | super.updateVariables(move); //standard king |
| 224 | const piece = this.getPiece(move.start.x,move.start.y); |
| 225 | const c = this.getColor(move.start.x,move.start.y); |
| 226 | // "l" = Alice king |
| 227 | if (piece == "l") |
| 228 | { |
| 229 | this.kingPos[c][0] = move.appear[0].x; |
| 230 | this.kingPos[c][1] = move.appear[0].y; |
| 231 | this.castleFlags[c] = [false,false]; |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | unupdateVariables(move) |
| 236 | { |
| 237 | super.unupdateVariables(move); |
| 238 | const c = this.getColor(move.start.x,move.start.y); |
| 239 | if (this.getPiece(move.start.x,move.start.y) == "l") |
| 240 | this.kingPos[c] = [move.start.x, move.start.y]; |
| 241 | } |
| 242 | |
| 243 | checkGameEnd() |
| 244 | { |
| 245 | const pieces = Object.keys(VariantRules.ALICE_CODES); |
| 246 | const color = this.turn; |
| 247 | const kp = this.kingPos[color]; |
| 248 | const mirrorSide = (pieces.includes(this.getPiece(kp[0],kp[1])) ? 1 : 2); |
| 249 | let sideBoard = this.getSideBoard(mirrorSide); |
| 250 | let saveBoard = this.board; |
| 251 | this.board = sideBoard; |
| 252 | let res = "*"; |
| 253 | if (!this.isAttacked(this.kingPos[color], this.getOppCol(color))) |
| 254 | res = "1/2"; |
| 255 | else |
| 256 | res = (color == "w" ? "0-1" : "1-0"); |
| 257 | this.board = saveBoard; |
| 258 | return res; |
| 259 | } |
| 260 | |
| 261 | static get VALUES() { |
| 262 | return { |
| 263 | 'p': 1, |
| 264 | 's': 1, |
| 265 | 'r': 5, |
| 266 | 'u': 5, |
| 267 | 'n': 3, |
| 268 | 'o': 3, |
| 269 | 'b': 3, |
| 270 | 'c': 3, |
| 271 | 'q': 9, |
| 272 | 't': 9, |
| 273 | 'k': 1000, |
| 274 | 'l': 1000 |
| 275 | }; |
| 276 | } |
| 277 | |
| 278 | getNotation(move) |
| 279 | { |
| 280 | if (move.appear.length == 2 && move.appear[0].p == VariantRules.KING) |
| 281 | { |
| 282 | if (move.end.y < move.start.y) |
| 283 | return "0-0-0"; |
| 284 | else |
| 285 | return "0-0"; |
| 286 | } |
| 287 | |
| 288 | const finalSquare = |
| 289 | String.fromCharCode(97 + move.end.y) + (VariantRules.size[0]-move.end.x); |
| 290 | const piece = this.getPiece(move.start.x, move.start.y); |
| 291 | |
| 292 | const captureMark = (move.vanish.length > move.appear.length ? "x" : ""); |
| 293 | let pawnMark = ""; |
| 294 | if (["p","s"].includes(piece) && captureMark.length == 1) |
| 295 | pawnMark = String.fromCharCode(97 + move.start.y); //start column |
| 296 | |
| 297 | // Piece or pawn movement |
| 298 | let notation = piece.toUpperCase() + pawnMark + captureMark + finalSquare; |
| 299 | if (['s','p'].includes(piece) && !['s','p'].includes(move.appear[0].p)) |
| 300 | { |
| 301 | // Promotion |
| 302 | notation += "=" + move.appear[0].p.toUpperCase(); |
| 303 | } |
| 304 | return notation; |
| 305 | } |
| 306 | } |