| 1 | import { ChessRules } from "@/base_rules"; |
| 2 | |
| 3 | export const VariantRules = class CheckeredRules extends ChessRules { |
| 4 | static board2fen(b) { |
| 5 | const checkered_codes = { |
| 6 | p: "s", |
| 7 | q: "t", |
| 8 | r: "u", |
| 9 | b: "c", |
| 10 | n: "o" |
| 11 | }; |
| 12 | if (b[0] == "c") return checkered_codes[b[1]]; |
| 13 | return ChessRules.board2fen(b); |
| 14 | } |
| 15 | |
| 16 | static fen2board(f) { |
| 17 | // Tolerate upper-case versions of checkered pieces (why not?) |
| 18 | const checkered_pieces = { |
| 19 | s: "p", |
| 20 | S: "p", |
| 21 | t: "q", |
| 22 | T: "q", |
| 23 | u: "r", |
| 24 | U: "r", |
| 25 | c: "b", |
| 26 | C: "b", |
| 27 | o: "n", |
| 28 | O: "n" |
| 29 | }; |
| 30 | if (Object.keys(checkered_pieces).includes(f)) |
| 31 | return "c" + checkered_pieces[f]; |
| 32 | return ChessRules.fen2board(f); |
| 33 | } |
| 34 | |
| 35 | static get PIECES() { |
| 36 | return ChessRules.PIECES.concat(["s", "t", "u", "c", "o"]); |
| 37 | } |
| 38 | |
| 39 | getPpath(b) { |
| 40 | return (b[0] == "c" ? "Checkered/" : "") + b; |
| 41 | } |
| 42 | |
| 43 | setOtherVariables(fen) { |
| 44 | super.setOtherVariables(fen); |
| 45 | // Local stack of non-capturing checkered moves: |
| 46 | this.cmoves = []; |
| 47 | const cmove = V.ParseFen(fen).cmove; |
| 48 | if (cmove == "-") this.cmoves.push(null); |
| 49 | else { |
| 50 | this.cmoves.push({ |
| 51 | start: ChessRules.SquareToCoords(cmove.substr(0, 2)), |
| 52 | end: ChessRules.SquareToCoords(cmove.substr(2)) |
| 53 | }); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | static IsGoodFen(fen) { |
| 58 | if (!ChessRules.IsGoodFen(fen)) return false; |
| 59 | const fenParts = fen.split(" "); |
| 60 | if (fenParts.length != 6) return false; |
| 61 | if (fenParts[5] != "-" && !fenParts[5].match(/^([a-h][1-8]){2}$/)) |
| 62 | return false; |
| 63 | return true; |
| 64 | } |
| 65 | |
| 66 | static IsGoodFlags(flags) { |
| 67 | // 4 for castle + 16 for pawns |
| 68 | return !!flags.match(/^[a-z]{4,4}[01]{16,16}$/); |
| 69 | } |
| 70 | |
| 71 | setFlags(fenflags) { |
| 72 | super.setFlags(fenflags); //castleFlags |
| 73 | this.pawnFlags = { |
| 74 | w: [...Array(8).fill(true)], //pawns can move 2 squares? |
| 75 | b: [...Array(8).fill(true)] |
| 76 | }; |
| 77 | const flags = fenflags.substr(4); //skip first 4 letters, for castle |
| 78 | for (let c of ["w", "b"]) { |
| 79 | for (let i = 0; i < 8; i++) |
| 80 | this.pawnFlags[c][i] = flags.charAt((c == "w" ? 0 : 8) + i) == "1"; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | aggregateFlags() { |
| 85 | return [this.castleFlags, this.pawnFlags]; |
| 86 | } |
| 87 | |
| 88 | disaggregateFlags(flags) { |
| 89 | this.castleFlags = flags[0]; |
| 90 | this.pawnFlags = flags[1]; |
| 91 | } |
| 92 | |
| 93 | getEpSquare(moveOrSquare) { |
| 94 | if (typeof moveOrSquare !== "object" || moveOrSquare.appear[0].c != 'c') |
| 95 | return super.getEpSquare(moveOrSquare); |
| 96 | // Checkered move: no en-passant |
| 97 | return undefined; |
| 98 | } |
| 99 | |
| 100 | getCmove(move) { |
| 101 | if (move.appear[0].c == "c" && move.vanish.length == 1) |
| 102 | return { start: move.start, end: move.end }; |
| 103 | return null; |
| 104 | } |
| 105 | |
| 106 | canTake([x1, y1], [x2, y2]) { |
| 107 | const color1 = this.getColor(x1, y1); |
| 108 | const color2 = this.getColor(x2, y2); |
| 109 | // Checkered aren't captured |
| 110 | return ( |
| 111 | color1 != color2 && |
| 112 | color2 != "c" && |
| 113 | (color1 != "c" || color2 != this.turn) |
| 114 | ); |
| 115 | } |
| 116 | |
| 117 | // Post-processing: apply "checkerization" of standard moves |
| 118 | getPotentialMovesFrom([x, y]) { |
| 119 | let standardMoves = super.getPotentialMovesFrom([x, y]); |
| 120 | const lastRank = this.turn == "w" ? 0 : 7; |
| 121 | // King has to be treated differently (for castles) |
| 122 | if (this.getPiece(x, y) == V.KING) return standardMoves; |
| 123 | let moves = []; |
| 124 | standardMoves.forEach(m => { |
| 125 | if (m.vanish[0].p == V.PAWN) { |
| 126 | if ( |
| 127 | Math.abs(m.end.x - m.start.x) == 2 && |
| 128 | !this.pawnFlags[this.turn][m.start.y] |
| 129 | ) |
| 130 | return; //skip forbidden 2-squares jumps |
| 131 | if ( |
| 132 | this.board[m.end.x][m.end.y] == V.EMPTY && |
| 133 | m.vanish.length == 2 && |
| 134 | this.getColor(m.start.x, m.start.y) == "c" |
| 135 | ) { |
| 136 | return; //checkered pawns cannot take en-passant |
| 137 | } |
| 138 | } |
| 139 | if (m.vanish.length == 1) moves.push(m); |
| 140 | // No capture |
| 141 | else { |
| 142 | // A capture occured (m.vanish.length == 2) |
| 143 | m.appear[0].c = "c"; |
| 144 | moves.push(m); |
| 145 | if ( |
| 146 | m.appear[0].p != m.vanish[1].p && //avoid promotions (already treated): |
| 147 | (m.vanish[0].p != V.PAWN || m.end.x != lastRank) |
| 148 | ) { |
| 149 | // Add transformation into captured piece |
| 150 | let m2 = JSON.parse(JSON.stringify(m)); |
| 151 | m2.appear[0].p = m.vanish[1].p; |
| 152 | moves.push(m2); |
| 153 | } |
| 154 | } |
| 155 | }); |
| 156 | return moves; |
| 157 | } |
| 158 | |
| 159 | getPotentialPawnMoves([x, y]) { |
| 160 | const color = this.turn; |
| 161 | let moves = []; |
| 162 | const [sizeX, sizeY] = [V.size.x, V.size.y]; |
| 163 | const shiftX = color == "w" ? -1 : 1; |
| 164 | const startRank = color == "w" ? sizeX - 2 : 1; |
| 165 | const lastRank = color == "w" ? 0 : sizeX - 1; |
| 166 | const pawnColor = this.getColor(x, y); //can be checkered |
| 167 | |
| 168 | const finalPieces = |
| 169 | x + shiftX == lastRank |
| 170 | ? [V.ROOK, V.KNIGHT, V.BISHOP, V.QUEEN] |
| 171 | : [V.PAWN]; |
| 172 | if (this.board[x + shiftX][y] == V.EMPTY) { |
| 173 | // One square forward |
| 174 | for (let piece of finalPieces) { |
| 175 | moves.push( |
| 176 | this.getBasicMove([x, y], [x + shiftX, y], { |
| 177 | c: pawnColor, |
| 178 | p: piece |
| 179 | }) |
| 180 | ); |
| 181 | } |
| 182 | if ( |
| 183 | x == startRank && |
| 184 | this.board[x + 2 * shiftX][y] == V.EMPTY |
| 185 | ) { |
| 186 | // Two squares jump |
| 187 | moves.push(this.getBasicMove([x, y], [x + 2 * shiftX, y])); |
| 188 | } |
| 189 | } |
| 190 | // Captures |
| 191 | for (let shiftY of [-1, 1]) { |
| 192 | if ( |
| 193 | y + shiftY >= 0 && |
| 194 | y + shiftY < sizeY && |
| 195 | this.board[x + shiftX][y + shiftY] != V.EMPTY && |
| 196 | this.canTake([x, y], [x + shiftX, y + shiftY]) |
| 197 | ) { |
| 198 | for (let piece of finalPieces) { |
| 199 | moves.push( |
| 200 | this.getBasicMove([x, y], [x + shiftX, y + shiftY], { |
| 201 | c: pawnColor, |
| 202 | p: piece |
| 203 | }) |
| 204 | ); |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | // En passant |
| 210 | const Lep = this.epSquares.length; |
| 211 | const epSquare = this.epSquares[Lep - 1]; //always at least one element |
| 212 | if ( |
| 213 | !!epSquare && |
| 214 | epSquare.x == x + shiftX && |
| 215 | Math.abs(epSquare.y - y) == 1 |
| 216 | ) { |
| 217 | let enpassantMove = this.getBasicMove([x, y], [epSquare.x, epSquare.y]); |
| 218 | enpassantMove.vanish.push({ |
| 219 | x: x, |
| 220 | y: epSquare.y, |
| 221 | p: "p", |
| 222 | c: this.getColor(x, epSquare.y) |
| 223 | }); |
| 224 | moves.push(enpassantMove); |
| 225 | } |
| 226 | |
| 227 | return moves; |
| 228 | } |
| 229 | |
| 230 | canIplay(side, [x, y]) { |
| 231 | return side == this.turn && [side, "c"].includes(this.getColor(x, y)); |
| 232 | } |
| 233 | |
| 234 | // Does m2 un-do m1 ? (to disallow undoing checkered moves) |
| 235 | oppositeMoves(m1, m2) { |
| 236 | return ( |
| 237 | m1 && |
| 238 | m2.appear[0].c == "c" && |
| 239 | m2.appear.length == 1 && |
| 240 | m2.vanish.length == 1 && |
| 241 | m1.start.x == m2.end.x && |
| 242 | m1.end.x == m2.start.x && |
| 243 | m1.start.y == m2.end.y && |
| 244 | m1.end.y == m2.start.y |
| 245 | ); |
| 246 | } |
| 247 | |
| 248 | filterValid(moves) { |
| 249 | if (moves.length == 0) return []; |
| 250 | const color = this.turn; |
| 251 | const L = this.cmoves.length; //at least 1: init from FEN |
| 252 | return moves.filter(m => { |
| 253 | if (this.oppositeMoves(this.cmoves[L - 1], m)) return false; |
| 254 | this.play(m); |
| 255 | const res = !this.underCheck(color); |
| 256 | this.undo(m); |
| 257 | return res; |
| 258 | }); |
| 259 | } |
| 260 | |
| 261 | getAllValidMoves() { |
| 262 | const oppCol = V.GetOppCol(this.turn); |
| 263 | let potentialMoves = []; |
| 264 | for (let i = 0; i < V.size.x; i++) { |
| 265 | for (let j = 0; j < V.size.y; j++) { |
| 266 | // NOTE: just testing == color isn't enough because of checkred pieces |
| 267 | if (this.board[i][j] != V.EMPTY && this.getColor(i, j) != oppCol) { |
| 268 | Array.prototype.push.apply( |
| 269 | potentialMoves, |
| 270 | this.getPotentialMovesFrom([i, j]) |
| 271 | ); |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | return this.filterValid(potentialMoves); |
| 276 | } |
| 277 | |
| 278 | atLeastOneMove() { |
| 279 | const oppCol = V.GetOppCol(this.turn); |
| 280 | for (let i = 0; i < V.size.x; i++) { |
| 281 | for (let j = 0; j < V.size.y; j++) { |
| 282 | // NOTE: just testing == color isn't enough because of checkered pieces |
| 283 | if (this.board[i][j] != V.EMPTY && this.getColor(i, j) != oppCol) { |
| 284 | const moves = this.getPotentialMovesFrom([i, j]); |
| 285 | if (moves.length > 0) { |
| 286 | for (let k = 0; k < moves.length; k++) { |
| 287 | if (this.filterValid([moves[k]]).length > 0) return true; |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 | return false; |
| 294 | } |
| 295 | |
| 296 | isAttackedByPawn([x, y], colors) { |
| 297 | for (let c of colors) { |
| 298 | const color = c == "c" ? this.turn : c; |
| 299 | let pawnShift = color == "w" ? 1 : -1; |
| 300 | if (x + pawnShift >= 0 && x + pawnShift < 8) { |
| 301 | for (let i of [-1, 1]) { |
| 302 | if ( |
| 303 | y + i >= 0 && |
| 304 | y + i < 8 && |
| 305 | this.getPiece(x + pawnShift, y + i) == V.PAWN && |
| 306 | this.getColor(x + pawnShift, y + i) == c |
| 307 | ) { |
| 308 | return true; |
| 309 | } |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | return false; |
| 314 | } |
| 315 | |
| 316 | underCheck(color) { |
| 317 | return this.isAttacked(this.kingPos[color], [V.GetOppCol(color), "c"]); |
| 318 | } |
| 319 | |
| 320 | getCheckSquares(color) { |
| 321 | // Artifically change turn, for checkered pawns |
| 322 | this.turn = V.GetOppCol(color); |
| 323 | const kingAttacked = this.isAttacked(this.kingPos[color], [ |
| 324 | V.GetOppCol(color), |
| 325 | "c" |
| 326 | ]); |
| 327 | let res = kingAttacked |
| 328 | ? [JSON.parse(JSON.stringify(this.kingPos[color]))] //need to duplicate! |
| 329 | : []; |
| 330 | this.turn = color; |
| 331 | return res; |
| 332 | } |
| 333 | |
| 334 | postPlay(move) { |
| 335 | super.postPlay(move); |
| 336 | // Does this move turn off a 2-squares pawn flag? |
| 337 | if ([1, 6].includes(move.start.x) && move.vanish[0].p == V.PAWN) |
| 338 | this.pawnFlags[move.start.x == 6 ? "w" : "b"][move.start.y] = false; |
| 339 | this.cmoves.push(this.getCmove(move)); |
| 340 | } |
| 341 | |
| 342 | postUndo(move) { |
| 343 | super.postUndo(move); |
| 344 | this.cmoves.pop(); |
| 345 | } |
| 346 | |
| 347 | getCurrentScore() { |
| 348 | if (this.atLeastOneMove()) |
| 349 | // game not over |
| 350 | return "*"; |
| 351 | |
| 352 | const color = this.turn; |
| 353 | // Artifically change turn, for checkered pawns |
| 354 | this.turn = V.GetOppCol(this.turn); |
| 355 | const res = this.isAttacked(this.kingPos[color], [V.GetOppCol(color), "c"]) |
| 356 | ? color == "w" |
| 357 | ? "0-1" |
| 358 | : "1-0" |
| 359 | : "1/2"; |
| 360 | this.turn = V.GetOppCol(this.turn); |
| 361 | return res; |
| 362 | } |
| 363 | |
| 364 | evalPosition() { |
| 365 | let evaluation = 0; |
| 366 | // Just count material for now, considering checkered neutral (...) |
| 367 | for (let i = 0; i < V.size.x; i++) { |
| 368 | for (let j = 0; j < V.size.y; j++) { |
| 369 | if (this.board[i][j] != V.EMPTY) { |
| 370 | const sqColor = this.getColor(i, j); |
| 371 | if (["w","b"].includes(sqColor)) { |
| 372 | const sign = sqColor == "w" ? 1 : -1; |
| 373 | evaluation += sign * V.VALUES[this.getPiece(i, j)]; |
| 374 | } |
| 375 | } |
| 376 | } |
| 377 | } |
| 378 | return evaluation; |
| 379 | } |
| 380 | |
| 381 | static GenRandInitFen(randomness) { |
| 382 | // Add 16 pawns flags + empty cmove: |
| 383 | return ChessRules.GenRandInitFen(randomness) |
| 384 | .slice(0, -2) + "1111111111111111 - -"; |
| 385 | } |
| 386 | |
| 387 | static ParseFen(fen) { |
| 388 | return Object.assign({}, ChessRules.ParseFen(fen), { |
| 389 | cmove: fen.split(" ")[5] |
| 390 | }); |
| 391 | } |
| 392 | |
| 393 | getFen() { |
| 394 | const L = this.cmoves.length; |
| 395 | const cmoveFen = !this.cmoves[L - 1] |
| 396 | ? "-" |
| 397 | : ChessRules.CoordsToSquare(this.cmoves[L - 1].start) + |
| 398 | ChessRules.CoordsToSquare(this.cmoves[L - 1].end); |
| 399 | return super.getFen() + " " + cmoveFen; |
| 400 | } |
| 401 | |
| 402 | getFlagsFen() { |
| 403 | let fen = super.getFlagsFen(); |
| 404 | // Add pawns flags |
| 405 | for (let c of ["w", "b"]) { |
| 406 | for (let i = 0; i < 8; i++) fen += this.pawnFlags[c][i] ? "1" : "0"; |
| 407 | } |
| 408 | return fen; |
| 409 | } |
| 410 | |
| 411 | static get SEARCH_DEPTH() { |
| 412 | return 2; |
| 413 | } |
| 414 | |
| 415 | getNotation(move) { |
| 416 | if (move.appear.length == 2) { |
| 417 | // Castle |
| 418 | if (move.end.y < move.start.y) return "0-0-0"; |
| 419 | return "0-0"; |
| 420 | } |
| 421 | |
| 422 | // Translate final square |
| 423 | const finalSquare = V.CoordsToSquare(move.end); |
| 424 | |
| 425 | const piece = this.getPiece(move.start.x, move.start.y); |
| 426 | if (piece == V.PAWN) { |
| 427 | // Pawn move |
| 428 | let notation = ""; |
| 429 | if (move.vanish.length > 1) { |
| 430 | // Capture |
| 431 | const startColumn = V.CoordToColumn(move.start.y); |
| 432 | notation = |
| 433 | startColumn + |
| 434 | "x" + |
| 435 | finalSquare + |
| 436 | "=" + |
| 437 | move.appear[0].p.toUpperCase(); |
| 438 | } //no capture |
| 439 | else { |
| 440 | notation = finalSquare; |
| 441 | if (move.appear.length > 0 && piece != move.appear[0].p) |
| 442 | //promotion |
| 443 | notation += "=" + move.appear[0].p.toUpperCase(); |
| 444 | } |
| 445 | return notation; |
| 446 | } |
| 447 | // Piece movement |
| 448 | return ( |
| 449 | piece.toUpperCase() + |
| 450 | (move.vanish.length > 1 ? "x" : "") + |
| 451 | finalSquare + |
| 452 | (move.vanish.length > 1 ? "=" + move.appear[0].p.toUpperCase() : "") |
| 453 | ); |
| 454 | } |
| 455 | }; |