| 1 | // (Orthodox) Chess rules are defined in ChessRules class. |
| 2 | // Variants generally inherit from it, and modify some parts. |
| 3 | |
| 4 | import { ArrayFun } from "@/utils/array"; |
| 5 | import { randInt, shuffle } from "@/utils/alea"; |
| 6 | |
| 7 | // class "PiPo": Piece + Position |
| 8 | export const PiPo = class PiPo { |
| 9 | // o: {piece[p], color[c], posX[x], posY[y]} |
| 10 | constructor(o) { |
| 11 | this.p = o.p; |
| 12 | this.c = o.c; |
| 13 | this.x = o.x; |
| 14 | this.y = o.y; |
| 15 | } |
| 16 | }; |
| 17 | |
| 18 | export const Move = class Move { |
| 19 | // o: {appear, vanish, [start,] [end,]} |
| 20 | // appear,vanish = arrays of PiPo |
| 21 | // start,end = coordinates to apply to trigger move visually (think castle) |
| 22 | constructor(o) { |
| 23 | this.appear = o.appear; |
| 24 | this.vanish = o.vanish; |
| 25 | this.start = o.start ? o.start : { x: o.vanish[0].x, y: o.vanish[0].y }; |
| 26 | this.end = o.end ? o.end : { x: o.appear[0].x, y: o.appear[0].y }; |
| 27 | } |
| 28 | }; |
| 29 | |
| 30 | // NOTE: x coords = top to bottom; y = left to right (from white player perspective) |
| 31 | export const ChessRules = class ChessRules { |
| 32 | ////////////// |
| 33 | // MISC UTILS |
| 34 | |
| 35 | // Some variants don't have flags: |
| 36 | static get HasFlags() { |
| 37 | return true; |
| 38 | } |
| 39 | |
| 40 | // Some variants don't have en-passant |
| 41 | static get HasEnpassant() { |
| 42 | return true; |
| 43 | } |
| 44 | |
| 45 | // Some variants cannot have analyse mode |
| 46 | static get CanAnalyze() { |
| 47 | return true; |
| 48 | } |
| 49 | |
| 50 | // Some variants show incomplete information, |
| 51 | // and thus show only a partial moves list or no list at all. |
| 52 | static get ShowMoves() { |
| 53 | return "all"; |
| 54 | } |
| 55 | |
| 56 | // Turn "wb" into "B" (for FEN) |
| 57 | static board2fen(b) { |
| 58 | return b[0] == "w" ? b[1].toUpperCase() : b[1]; |
| 59 | } |
| 60 | |
| 61 | // Turn "p" into "bp" (for board) |
| 62 | static fen2board(f) { |
| 63 | return f.charCodeAt() <= 90 ? "w" + f.toLowerCase() : "b" + f; |
| 64 | } |
| 65 | |
| 66 | // Check if FEN describe a board situation correctly |
| 67 | static IsGoodFen(fen) { |
| 68 | const fenParsed = V.ParseFen(fen); |
| 69 | // 1) Check position |
| 70 | if (!V.IsGoodPosition(fenParsed.position)) return false; |
| 71 | // 2) Check turn |
| 72 | if (!fenParsed.turn || !V.IsGoodTurn(fenParsed.turn)) return false; |
| 73 | // 3) Check moves count |
| 74 | if (!fenParsed.movesCount || !(parseInt(fenParsed.movesCount) >= 0)) |
| 75 | return false; |
| 76 | // 4) Check flags |
| 77 | if (V.HasFlags && (!fenParsed.flags || !V.IsGoodFlags(fenParsed.flags))) |
| 78 | return false; |
| 79 | // 5) Check enpassant |
| 80 | if ( |
| 81 | V.HasEnpassant && |
| 82 | (!fenParsed.enpassant || !V.IsGoodEnpassant(fenParsed.enpassant)) |
| 83 | ) { |
| 84 | return false; |
| 85 | } |
| 86 | return true; |
| 87 | } |
| 88 | |
| 89 | // Is position part of the FEN a priori correct? |
| 90 | static IsGoodPosition(position) { |
| 91 | if (position.length == 0) return false; |
| 92 | const rows = position.split("/"); |
| 93 | if (rows.length != V.size.x) return false; |
| 94 | let kings = {}; |
| 95 | for (let row of rows) { |
| 96 | let sumElts = 0; |
| 97 | for (let i = 0; i < row.length; i++) { |
| 98 | if (['K','k'].includes(row[i])) |
| 99 | kings[row[i]] = true; |
| 100 | if (V.PIECES.includes(row[i].toLowerCase())) sumElts++; |
| 101 | else { |
| 102 | const num = parseInt(row[i]); |
| 103 | if (isNaN(num)) return false; |
| 104 | sumElts += num; |
| 105 | } |
| 106 | } |
| 107 | if (sumElts != V.size.y) return false; |
| 108 | } |
| 109 | // Both kings should be on board: |
| 110 | if (Object.keys(kings).length != 2) |
| 111 | return false; |
| 112 | return true; |
| 113 | } |
| 114 | |
| 115 | // For FEN checking |
| 116 | static IsGoodTurn(turn) { |
| 117 | return ["w", "b"].includes(turn); |
| 118 | } |
| 119 | |
| 120 | // For FEN checking |
| 121 | static IsGoodFlags(flags) { |
| 122 | return !!flags.match(/^[01]{4,4}$/); |
| 123 | } |
| 124 | |
| 125 | static IsGoodEnpassant(enpassant) { |
| 126 | if (enpassant != "-") { |
| 127 | const ep = V.SquareToCoords(enpassant); |
| 128 | if (isNaN(ep.x) || !V.OnBoard(ep)) return false; |
| 129 | } |
| 130 | return true; |
| 131 | } |
| 132 | |
| 133 | // 3 --> d (column number to letter) |
| 134 | static CoordToColumn(colnum) { |
| 135 | return String.fromCharCode(97 + colnum); |
| 136 | } |
| 137 | |
| 138 | // d --> 3 (column letter to number) |
| 139 | static ColumnToCoord(column) { |
| 140 | return column.charCodeAt(0) - 97; |
| 141 | } |
| 142 | |
| 143 | // a4 --> {x:3,y:0} |
| 144 | static SquareToCoords(sq) { |
| 145 | return { |
| 146 | // NOTE: column is always one char => max 26 columns |
| 147 | // row is counted from black side => subtraction |
| 148 | x: V.size.x - parseInt(sq.substr(1)), |
| 149 | y: sq[0].charCodeAt() - 97 |
| 150 | }; |
| 151 | } |
| 152 | |
| 153 | // {x:0,y:4} --> e8 |
| 154 | static CoordsToSquare(coords) { |
| 155 | return V.CoordToColumn(coords.y) + (V.size.x - coords.x); |
| 156 | } |
| 157 | |
| 158 | // Path to pieces |
| 159 | getPpath(b) { |
| 160 | return b; //usual pieces in pieces/ folder |
| 161 | } |
| 162 | |
| 163 | // Aggregates flags into one object |
| 164 | aggregateFlags() { |
| 165 | return this.castleFlags; |
| 166 | } |
| 167 | |
| 168 | // Reverse operation |
| 169 | disaggregateFlags(flags) { |
| 170 | this.castleFlags = flags; |
| 171 | } |
| 172 | |
| 173 | // En-passant square, if any |
| 174 | getEpSquare(moveOrSquare) { |
| 175 | if (!moveOrSquare) return undefined; |
| 176 | if (typeof moveOrSquare === "string") { |
| 177 | const square = moveOrSquare; |
| 178 | if (square == "-") return undefined; |
| 179 | return V.SquareToCoords(square); |
| 180 | } |
| 181 | // Argument is a move: |
| 182 | const move = moveOrSquare; |
| 183 | const [sx, sy, ex] = [move.start.x, move.start.y, move.end.x]; |
| 184 | // NOTE: next conditions are first for Atomic, and last for Checkered |
| 185 | if ( |
| 186 | move.appear.length > 0 && |
| 187 | Math.abs(sx - ex) == 2 && |
| 188 | move.appear[0].p == V.PAWN && |
| 189 | ["w", "b"].includes(move.appear[0].c) |
| 190 | ) { |
| 191 | return { |
| 192 | x: (sx + ex) / 2, |
| 193 | y: sy |
| 194 | }; |
| 195 | } |
| 196 | return undefined; //default |
| 197 | } |
| 198 | |
| 199 | // Can thing on square1 take thing on square2 |
| 200 | canTake([x1, y1], [x2, y2]) { |
| 201 | return this.getColor(x1, y1) !== this.getColor(x2, y2); |
| 202 | } |
| 203 | |
| 204 | // Is (x,y) on the chessboard? |
| 205 | static OnBoard(x, y) { |
| 206 | return x >= 0 && x < V.size.x && y >= 0 && y < V.size.y; |
| 207 | } |
| 208 | |
| 209 | // Used in interface: 'side' arg == player color |
| 210 | canIplay(side, [x, y]) { |
| 211 | return this.turn == side && this.getColor(x, y) == side; |
| 212 | } |
| 213 | |
| 214 | // On which squares is color under check ? (for interface) |
| 215 | getCheckSquares(color) { |
| 216 | return this.isAttacked(this.kingPos[color], [V.GetOppCol(color)]) |
| 217 | ? [JSON.parse(JSON.stringify(this.kingPos[color]))] //need to duplicate! |
| 218 | : []; |
| 219 | } |
| 220 | |
| 221 | ///////////// |
| 222 | // FEN UTILS |
| 223 | |
| 224 | // Setup the initial random (assymetric) position |
| 225 | static GenRandInitFen() { |
| 226 | let pieces = { w: new Array(8), b: new Array(8) }; |
| 227 | // Shuffle pieces on first and last rank |
| 228 | for (let c of ["w", "b"]) { |
| 229 | let positions = ArrayFun.range(8); |
| 230 | |
| 231 | // Get random squares for bishops |
| 232 | let randIndex = 2 * randInt(4); |
| 233 | const bishop1Pos = positions[randIndex]; |
| 234 | // The second bishop must be on a square of different color |
| 235 | let randIndex_tmp = 2 * randInt(4) + 1; |
| 236 | const bishop2Pos = positions[randIndex_tmp]; |
| 237 | // Remove chosen squares |
| 238 | positions.splice(Math.max(randIndex, randIndex_tmp), 1); |
| 239 | positions.splice(Math.min(randIndex, randIndex_tmp), 1); |
| 240 | |
| 241 | // Get random squares for knights |
| 242 | randIndex = randInt(6); |
| 243 | const knight1Pos = positions[randIndex]; |
| 244 | positions.splice(randIndex, 1); |
| 245 | randIndex = randInt(5); |
| 246 | const knight2Pos = positions[randIndex]; |
| 247 | positions.splice(randIndex, 1); |
| 248 | |
| 249 | // Get random square for queen |
| 250 | randIndex = randInt(4); |
| 251 | const queenPos = positions[randIndex]; |
| 252 | positions.splice(randIndex, 1); |
| 253 | |
| 254 | // Rooks and king positions are now fixed, |
| 255 | // because of the ordering rook-king-rook |
| 256 | const rook1Pos = positions[0]; |
| 257 | const kingPos = positions[1]; |
| 258 | const rook2Pos = positions[2]; |
| 259 | |
| 260 | // Finally put the shuffled pieces in the board array |
| 261 | pieces[c][rook1Pos] = "r"; |
| 262 | pieces[c][knight1Pos] = "n"; |
| 263 | pieces[c][bishop1Pos] = "b"; |
| 264 | pieces[c][queenPos] = "q"; |
| 265 | pieces[c][kingPos] = "k"; |
| 266 | pieces[c][bishop2Pos] = "b"; |
| 267 | pieces[c][knight2Pos] = "n"; |
| 268 | pieces[c][rook2Pos] = "r"; |
| 269 | } |
| 270 | return ( |
| 271 | pieces["b"].join("") + |
| 272 | "/pppppppp/8/8/8/8/PPPPPPPP/" + |
| 273 | pieces["w"].join("").toUpperCase() + |
| 274 | " w 0 1111 -" |
| 275 | ); //add turn + flags + enpassant |
| 276 | } |
| 277 | |
| 278 | // "Parse" FEN: just return untransformed string data |
| 279 | static ParseFen(fen) { |
| 280 | const fenParts = fen.split(" "); |
| 281 | let res = { |
| 282 | position: fenParts[0], |
| 283 | turn: fenParts[1], |
| 284 | movesCount: fenParts[2] |
| 285 | }; |
| 286 | let nextIdx = 3; |
| 287 | if (V.HasFlags) Object.assign(res, { flags: fenParts[nextIdx++] }); |
| 288 | if (V.HasEnpassant) Object.assign(res, { enpassant: fenParts[nextIdx] }); |
| 289 | return res; |
| 290 | } |
| 291 | |
| 292 | // Return current fen (game state) |
| 293 | getFen() { |
| 294 | return ( |
| 295 | this.getBaseFen() + |
| 296 | " " + |
| 297 | this.getTurnFen() + |
| 298 | " " + |
| 299 | this.movesCount + |
| 300 | (V.HasFlags ? " " + this.getFlagsFen() : "") + |
| 301 | (V.HasEnpassant ? " " + this.getEnpassantFen() : "") |
| 302 | ); |
| 303 | } |
| 304 | |
| 305 | // Position part of the FEN string |
| 306 | getBaseFen() { |
| 307 | let position = ""; |
| 308 | for (let i = 0; i < V.size.x; i++) { |
| 309 | let emptyCount = 0; |
| 310 | for (let j = 0; j < V.size.y; j++) { |
| 311 | if (this.board[i][j] == V.EMPTY) emptyCount++; |
| 312 | else { |
| 313 | if (emptyCount > 0) { |
| 314 | // Add empty squares in-between |
| 315 | position += emptyCount; |
| 316 | emptyCount = 0; |
| 317 | } |
| 318 | position += V.board2fen(this.board[i][j]); |
| 319 | } |
| 320 | } |
| 321 | if (emptyCount > 0) { |
| 322 | // "Flush remainder" |
| 323 | position += emptyCount; |
| 324 | } |
| 325 | if (i < V.size.x - 1) position += "/"; //separate rows |
| 326 | } |
| 327 | return position; |
| 328 | } |
| 329 | |
| 330 | getTurnFen() { |
| 331 | return this.turn; |
| 332 | } |
| 333 | |
| 334 | // Flags part of the FEN string |
| 335 | getFlagsFen() { |
| 336 | let flags = ""; |
| 337 | // Add castling flags |
| 338 | for (let i of ["w", "b"]) { |
| 339 | for (let j = 0; j < 2; j++) flags += this.castleFlags[i][j] ? "1" : "0"; |
| 340 | } |
| 341 | return flags; |
| 342 | } |
| 343 | |
| 344 | // Enpassant part of the FEN string |
| 345 | getEnpassantFen() { |
| 346 | const L = this.epSquares.length; |
| 347 | if (!this.epSquares[L - 1]) return "-"; //no en-passant |
| 348 | return V.CoordsToSquare(this.epSquares[L - 1]); |
| 349 | } |
| 350 | |
| 351 | // Turn position fen into double array ["wb","wp","bk",...] |
| 352 | static GetBoard(position) { |
| 353 | const rows = position.split("/"); |
| 354 | let board = ArrayFun.init(V.size.x, V.size.y, ""); |
| 355 | for (let i = 0; i < rows.length; i++) { |
| 356 | let j = 0; |
| 357 | for (let indexInRow = 0; indexInRow < rows[i].length; indexInRow++) { |
| 358 | const character = rows[i][indexInRow]; |
| 359 | const num = parseInt(character); |
| 360 | // If num is a number, just shift j: |
| 361 | if (!isNaN(num)) j += num; |
| 362 | // Else: something at position i,j |
| 363 | else board[i][j++] = V.fen2board(character); |
| 364 | } |
| 365 | } |
| 366 | return board; |
| 367 | } |
| 368 | |
| 369 | // Extract (relevant) flags from fen |
| 370 | setFlags(fenflags) { |
| 371 | // white a-castle, h-castle, black a-castle, h-castle |
| 372 | this.castleFlags = { w: [true, true], b: [true, true] }; |
| 373 | if (!fenflags) return; |
| 374 | for (let i = 0; i < 4; i++) |
| 375 | this.castleFlags[i < 2 ? "w" : "b"][i % 2] = fenflags.charAt(i) == "1"; |
| 376 | } |
| 377 | |
| 378 | ////////////////// |
| 379 | // INITIALIZATION |
| 380 | |
| 381 | constructor(fen) { |
| 382 | // In printDiagram() fen isn't supply because only getPpath() is used |
| 383 | if (fen) |
| 384 | this.re_init(fen); |
| 385 | } |
| 386 | |
| 387 | // Fen string fully describes the game state |
| 388 | re_init(fen) { |
| 389 | const fenParsed = V.ParseFen(fen); |
| 390 | this.board = V.GetBoard(fenParsed.position); |
| 391 | this.turn = fenParsed.turn[0]; //[0] to work with MarseilleRules |
| 392 | this.movesCount = parseInt(fenParsed.movesCount); |
| 393 | this.setOtherVariables(fen); |
| 394 | } |
| 395 | |
| 396 | // Scan board for kings and rooks positions |
| 397 | scanKingsRooks(fen) { |
| 398 | this.INIT_COL_KING = { w: -1, b: -1 }; |
| 399 | this.INIT_COL_ROOK = { w: [-1, -1], b: [-1, -1] }; |
| 400 | this.kingPos = { w: [-1, -1], b: [-1, -1] }; //squares of white and black king |
| 401 | const fenRows = V.ParseFen(fen).position.split("/"); |
| 402 | for (let i = 0; i < fenRows.length; i++) { |
| 403 | let k = 0; //column index on board |
| 404 | for (let j = 0; j < fenRows[i].length; j++) { |
| 405 | switch (fenRows[i].charAt(j)) { |
| 406 | case "k": |
| 407 | this.kingPos["b"] = [i, k]; |
| 408 | this.INIT_COL_KING["b"] = k; |
| 409 | break; |
| 410 | case "K": |
| 411 | this.kingPos["w"] = [i, k]; |
| 412 | this.INIT_COL_KING["w"] = k; |
| 413 | break; |
| 414 | case "r": |
| 415 | if (this.INIT_COL_ROOK["b"][0] < 0) this.INIT_COL_ROOK["b"][0] = k; |
| 416 | else this.INIT_COL_ROOK["b"][1] = k; |
| 417 | break; |
| 418 | case "R": |
| 419 | if (this.INIT_COL_ROOK["w"][0] < 0) this.INIT_COL_ROOK["w"][0] = k; |
| 420 | else this.INIT_COL_ROOK["w"][1] = k; |
| 421 | break; |
| 422 | default: { |
| 423 | const num = parseInt(fenRows[i].charAt(j)); |
| 424 | if (!isNaN(num)) k += num - 1; |
| 425 | } |
| 426 | } |
| 427 | k++; |
| 428 | } |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | // Some additional variables from FEN (variant dependant) |
| 433 | setOtherVariables(fen) { |
| 434 | // Set flags and enpassant: |
| 435 | const parsedFen = V.ParseFen(fen); |
| 436 | if (V.HasFlags) this.setFlags(parsedFen.flags); |
| 437 | if (V.HasEnpassant) { |
| 438 | const epSq = |
| 439 | parsedFen.enpassant != "-" |
| 440 | ? this.getEpSquare(parsedFen.enpassant) |
| 441 | : undefined; |
| 442 | this.epSquares = [epSq]; |
| 443 | } |
| 444 | // Search for king and rooks positions: |
| 445 | this.scanKingsRooks(fen); |
| 446 | } |
| 447 | |
| 448 | ///////////////////// |
| 449 | // GETTERS & SETTERS |
| 450 | |
| 451 | static get size() { |
| 452 | return { x: 8, y: 8 }; |
| 453 | } |
| 454 | |
| 455 | // Color of thing on suqare (i,j). 'undefined' if square is empty |
| 456 | getColor(i, j) { |
| 457 | return this.board[i][j].charAt(0); |
| 458 | } |
| 459 | |
| 460 | // Piece type on square (i,j). 'undefined' if square is empty |
| 461 | getPiece(i, j) { |
| 462 | return this.board[i][j].charAt(1); |
| 463 | } |
| 464 | |
| 465 | // Get opponent color |
| 466 | static GetOppCol(color) { |
| 467 | return color == "w" ? "b" : "w"; |
| 468 | } |
| 469 | |
| 470 | // Pieces codes (for a clearer code) |
| 471 | static get PAWN() { |
| 472 | return "p"; |
| 473 | } |
| 474 | static get ROOK() { |
| 475 | return "r"; |
| 476 | } |
| 477 | static get KNIGHT() { |
| 478 | return "n"; |
| 479 | } |
| 480 | static get BISHOP() { |
| 481 | return "b"; |
| 482 | } |
| 483 | static get QUEEN() { |
| 484 | return "q"; |
| 485 | } |
| 486 | static get KING() { |
| 487 | return "k"; |
| 488 | } |
| 489 | |
| 490 | // For FEN checking: |
| 491 | static get PIECES() { |
| 492 | return [V.PAWN, V.ROOK, V.KNIGHT, V.BISHOP, V.QUEEN, V.KING]; |
| 493 | } |
| 494 | |
| 495 | // Empty square |
| 496 | static get EMPTY() { |
| 497 | return ""; |
| 498 | } |
| 499 | |
| 500 | // Some pieces movements |
| 501 | static get steps() { |
| 502 | return { |
| 503 | r: [ |
| 504 | [-1, 0], |
| 505 | [1, 0], |
| 506 | [0, -1], |
| 507 | [0, 1] |
| 508 | ], |
| 509 | n: [ |
| 510 | [-1, -2], |
| 511 | [-1, 2], |
| 512 | [1, -2], |
| 513 | [1, 2], |
| 514 | [-2, -1], |
| 515 | [-2, 1], |
| 516 | [2, -1], |
| 517 | [2, 1] |
| 518 | ], |
| 519 | b: [ |
| 520 | [-1, -1], |
| 521 | [-1, 1], |
| 522 | [1, -1], |
| 523 | [1, 1] |
| 524 | ] |
| 525 | }; |
| 526 | } |
| 527 | |
| 528 | //////////////////// |
| 529 | // MOVES GENERATION |
| 530 | |
| 531 | // All possible moves from selected square (assumption: color is OK) |
| 532 | getPotentialMovesFrom([x, y]) { |
| 533 | switch (this.getPiece(x, y)) { |
| 534 | case V.PAWN: |
| 535 | return this.getPotentialPawnMoves([x, y]); |
| 536 | case V.ROOK: |
| 537 | return this.getPotentialRookMoves([x, y]); |
| 538 | case V.KNIGHT: |
| 539 | return this.getPotentialKnightMoves([x, y]); |
| 540 | case V.BISHOP: |
| 541 | return this.getPotentialBishopMoves([x, y]); |
| 542 | case V.QUEEN: |
| 543 | return this.getPotentialQueenMoves([x, y]); |
| 544 | case V.KING: |
| 545 | return this.getPotentialKingMoves([x, y]); |
| 546 | } |
| 547 | return []; //never reached |
| 548 | } |
| 549 | |
| 550 | // Build a regular move from its initial and destination squares. |
| 551 | // tr: transformation |
| 552 | getBasicMove([sx, sy], [ex, ey], tr) { |
| 553 | let mv = new Move({ |
| 554 | appear: [ |
| 555 | new PiPo({ |
| 556 | x: ex, |
| 557 | y: ey, |
| 558 | c: tr ? tr.c : this.getColor(sx, sy), |
| 559 | p: tr ? tr.p : this.getPiece(sx, sy) |
| 560 | }) |
| 561 | ], |
| 562 | vanish: [ |
| 563 | new PiPo({ |
| 564 | x: sx, |
| 565 | y: sy, |
| 566 | c: this.getColor(sx, sy), |
| 567 | p: this.getPiece(sx, sy) |
| 568 | }) |
| 569 | ] |
| 570 | }); |
| 571 | |
| 572 | // The opponent piece disappears if we take it |
| 573 | if (this.board[ex][ey] != V.EMPTY) { |
| 574 | mv.vanish.push( |
| 575 | new PiPo({ |
| 576 | x: ex, |
| 577 | y: ey, |
| 578 | c: this.getColor(ex, ey), |
| 579 | p: this.getPiece(ex, ey) |
| 580 | }) |
| 581 | ); |
| 582 | } |
| 583 | return mv; |
| 584 | } |
| 585 | |
| 586 | // Generic method to find possible moves of non-pawn pieces: |
| 587 | // "sliding or jumping" |
| 588 | getSlideNJumpMoves([x, y], steps, oneStep) { |
| 589 | let moves = []; |
| 590 | outerLoop: for (let step of steps) { |
| 591 | let i = x + step[0]; |
| 592 | let j = y + step[1]; |
| 593 | while (V.OnBoard(i, j) && this.board[i][j] == V.EMPTY) { |
| 594 | moves.push(this.getBasicMove([x, y], [i, j])); |
| 595 | if (oneStep !== undefined) continue outerLoop; |
| 596 | i += step[0]; |
| 597 | j += step[1]; |
| 598 | } |
| 599 | if (V.OnBoard(i, j) && this.canTake([x, y], [i, j])) |
| 600 | moves.push(this.getBasicMove([x, y], [i, j])); |
| 601 | } |
| 602 | return moves; |
| 603 | } |
| 604 | |
| 605 | // What are the pawn moves from square x,y ? |
| 606 | getPotentialPawnMoves([x, y]) { |
| 607 | const color = this.turn; |
| 608 | let moves = []; |
| 609 | const [sizeX, sizeY] = [V.size.x, V.size.y]; |
| 610 | const shiftX = color == "w" ? -1 : 1; |
| 611 | const firstRank = color == "w" ? sizeX - 1 : 0; |
| 612 | const startRank = color == "w" ? sizeX - 2 : 1; |
| 613 | const lastRank = color == "w" ? 0 : sizeX - 1; |
| 614 | const pawnColor = this.getColor(x, y); //can be different for checkered |
| 615 | |
| 616 | // NOTE: next condition is generally true (no pawn on last rank) |
| 617 | if (x + shiftX >= 0 && x + shiftX < sizeX) { |
| 618 | const finalPieces = |
| 619 | x + shiftX == lastRank |
| 620 | ? [V.ROOK, V.KNIGHT, V.BISHOP, V.QUEEN] |
| 621 | : [V.PAWN]; |
| 622 | // One square forward |
| 623 | if (this.board[x + shiftX][y] == V.EMPTY) { |
| 624 | for (let piece of finalPieces) { |
| 625 | moves.push( |
| 626 | this.getBasicMove([x, y], [x + shiftX, y], { |
| 627 | c: pawnColor, |
| 628 | p: piece |
| 629 | }) |
| 630 | ); |
| 631 | } |
| 632 | // Next condition because pawns on 1st rank can generally jump |
| 633 | if ( |
| 634 | [startRank, firstRank].includes(x) && |
| 635 | this.board[x + 2 * shiftX][y] == V.EMPTY |
| 636 | ) { |
| 637 | // Two squares jump |
| 638 | moves.push(this.getBasicMove([x, y], [x + 2 * shiftX, y])); |
| 639 | } |
| 640 | } |
| 641 | // Captures |
| 642 | for (let shiftY of [-1, 1]) { |
| 643 | if ( |
| 644 | y + shiftY >= 0 && |
| 645 | y + shiftY < sizeY && |
| 646 | this.board[x + shiftX][y + shiftY] != V.EMPTY && |
| 647 | this.canTake([x, y], [x + shiftX, y + shiftY]) |
| 648 | ) { |
| 649 | for (let piece of finalPieces) { |
| 650 | moves.push( |
| 651 | this.getBasicMove([x, y], [x + shiftX, y + shiftY], { |
| 652 | c: pawnColor, |
| 653 | p: piece |
| 654 | }) |
| 655 | ); |
| 656 | } |
| 657 | } |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | if (V.HasEnpassant) { |
| 662 | // En passant |
| 663 | const Lep = this.epSquares.length; |
| 664 | const epSquare = this.epSquares[Lep - 1]; //always at least one element |
| 665 | if ( |
| 666 | !!epSquare && |
| 667 | epSquare.x == x + shiftX && |
| 668 | Math.abs(epSquare.y - y) == 1 |
| 669 | ) { |
| 670 | let enpassantMove = this.getBasicMove([x, y], [epSquare.x, epSquare.y]); |
| 671 | enpassantMove.vanish.push({ |
| 672 | x: x, |
| 673 | y: epSquare.y, |
| 674 | p: "p", |
| 675 | c: this.getColor(x, epSquare.y) |
| 676 | }); |
| 677 | moves.push(enpassantMove); |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | return moves; |
| 682 | } |
| 683 | |
| 684 | // What are the rook moves from square x,y ? |
| 685 | getPotentialRookMoves(sq) { |
| 686 | return this.getSlideNJumpMoves(sq, V.steps[V.ROOK]); |
| 687 | } |
| 688 | |
| 689 | // What are the knight moves from square x,y ? |
| 690 | getPotentialKnightMoves(sq) { |
| 691 | return this.getSlideNJumpMoves(sq, V.steps[V.KNIGHT], "oneStep"); |
| 692 | } |
| 693 | |
| 694 | // What are the bishop moves from square x,y ? |
| 695 | getPotentialBishopMoves(sq) { |
| 696 | return this.getSlideNJumpMoves(sq, V.steps[V.BISHOP]); |
| 697 | } |
| 698 | |
| 699 | // What are the queen moves from square x,y ? |
| 700 | getPotentialQueenMoves(sq) { |
| 701 | return this.getSlideNJumpMoves( |
| 702 | sq, |
| 703 | V.steps[V.ROOK].concat(V.steps[V.BISHOP]) |
| 704 | ); |
| 705 | } |
| 706 | |
| 707 | // What are the king moves from square x,y ? |
| 708 | getPotentialKingMoves(sq) { |
| 709 | // Initialize with normal moves |
| 710 | let moves = this.getSlideNJumpMoves( |
| 711 | sq, |
| 712 | V.steps[V.ROOK].concat(V.steps[V.BISHOP]), |
| 713 | "oneStep" |
| 714 | ); |
| 715 | return moves.concat(this.getCastleMoves(sq)); |
| 716 | } |
| 717 | |
| 718 | getCastleMoves([x, y]) { |
| 719 | const c = this.getColor(x, y); |
| 720 | if (x != (c == "w" ? V.size.x - 1 : 0) || y != this.INIT_COL_KING[c]) |
| 721 | return []; //x isn't first rank, or king has moved (shortcut) |
| 722 | |
| 723 | // Castling ? |
| 724 | const oppCol = V.GetOppCol(c); |
| 725 | let moves = []; |
| 726 | let i = 0; |
| 727 | // King, then rook: |
| 728 | const finalSquares = [ |
| 729 | [2, 3], |
| 730 | [V.size.y - 2, V.size.y - 3] |
| 731 | ]; |
| 732 | castlingCheck: for ( |
| 733 | let castleSide = 0; |
| 734 | castleSide < 2; |
| 735 | castleSide++ //large, then small |
| 736 | ) { |
| 737 | if (!this.castleFlags[c][castleSide]) continue; |
| 738 | // If this code is reached, rooks and king are on initial position |
| 739 | |
| 740 | // Nothing on the path of the king ? (and no checks) |
| 741 | const finDist = finalSquares[castleSide][0] - y; |
| 742 | let step = finDist / Math.max(1, Math.abs(finDist)); |
| 743 | i = y; |
| 744 | do { |
| 745 | if ( |
| 746 | this.isAttacked([x, i], [oppCol]) || |
| 747 | (this.board[x][i] != V.EMPTY && |
| 748 | // NOTE: next check is enough, because of chessboard constraints |
| 749 | (this.getColor(x, i) != c || |
| 750 | ![V.KING, V.ROOK].includes(this.getPiece(x, i)))) |
| 751 | ) { |
| 752 | continue castlingCheck; |
| 753 | } |
| 754 | i += step; |
| 755 | } while (i != finalSquares[castleSide][0]); |
| 756 | |
| 757 | // Nothing on the path to the rook? |
| 758 | step = castleSide == 0 ? -1 : 1; |
| 759 | for (i = y + step; i != this.INIT_COL_ROOK[c][castleSide]; i += step) { |
| 760 | if (this.board[x][i] != V.EMPTY) continue castlingCheck; |
| 761 | } |
| 762 | const rookPos = this.INIT_COL_ROOK[c][castleSide]; |
| 763 | |
| 764 | // Nothing on final squares, except maybe king and castling rook? |
| 765 | for (i = 0; i < 2; i++) { |
| 766 | if ( |
| 767 | this.board[x][finalSquares[castleSide][i]] != V.EMPTY && |
| 768 | this.getPiece(x, finalSquares[castleSide][i]) != V.KING && |
| 769 | finalSquares[castleSide][i] != rookPos |
| 770 | ) { |
| 771 | continue castlingCheck; |
| 772 | } |
| 773 | } |
| 774 | |
| 775 | // If this code is reached, castle is valid |
| 776 | moves.push( |
| 777 | new Move({ |
| 778 | appear: [ |
| 779 | new PiPo({ x: x, y: finalSquares[castleSide][0], p: V.KING, c: c }), |
| 780 | new PiPo({ x: x, y: finalSquares[castleSide][1], p: V.ROOK, c: c }) |
| 781 | ], |
| 782 | vanish: [ |
| 783 | new PiPo({ x: x, y: y, p: V.KING, c: c }), |
| 784 | new PiPo({ x: x, y: rookPos, p: V.ROOK, c: c }) |
| 785 | ], |
| 786 | end: |
| 787 | Math.abs(y - rookPos) <= 2 |
| 788 | ? { x: x, y: rookPos } |
| 789 | : { x: x, y: y + 2 * (castleSide == 0 ? -1 : 1) } |
| 790 | }) |
| 791 | ); |
| 792 | } |
| 793 | |
| 794 | return moves; |
| 795 | } |
| 796 | |
| 797 | //////////////////// |
| 798 | // MOVES VALIDATION |
| 799 | |
| 800 | // For the interface: possible moves for the current turn from square sq |
| 801 | getPossibleMovesFrom(sq) { |
| 802 | return this.filterValid(this.getPotentialMovesFrom(sq)); |
| 803 | } |
| 804 | |
| 805 | // TODO: promotions (into R,B,N,Q) should be filtered only once |
| 806 | filterValid(moves) { |
| 807 | if (moves.length == 0) return []; |
| 808 | const color = this.turn; |
| 809 | return moves.filter(m => { |
| 810 | this.play(m); |
| 811 | const res = !this.underCheck(color); |
| 812 | this.undo(m); |
| 813 | return res; |
| 814 | }); |
| 815 | } |
| 816 | |
| 817 | // Search for all valid moves considering current turn |
| 818 | // (for engine and game end) |
| 819 | getAllValidMoves() { |
| 820 | const color = this.turn; |
| 821 | const oppCol = V.GetOppCol(color); |
| 822 | let potentialMoves = []; |
| 823 | for (let i = 0; i < V.size.x; i++) { |
| 824 | for (let j = 0; j < V.size.y; j++) { |
| 825 | // Next condition "!= oppCol" to work with checkered variant |
| 826 | if (this.board[i][j] != V.EMPTY && this.getColor(i, j) != oppCol) { |
| 827 | Array.prototype.push.apply( |
| 828 | potentialMoves, |
| 829 | this.getPotentialMovesFrom([i, j]) |
| 830 | ); |
| 831 | } |
| 832 | } |
| 833 | } |
| 834 | return this.filterValid(potentialMoves); |
| 835 | } |
| 836 | |
| 837 | // Stop at the first move found |
| 838 | atLeastOneMove() { |
| 839 | const color = this.turn; |
| 840 | const oppCol = V.GetOppCol(color); |
| 841 | for (let i = 0; i < V.size.x; i++) { |
| 842 | for (let j = 0; j < V.size.y; j++) { |
| 843 | if (this.board[i][j] != V.EMPTY && this.getColor(i, j) != oppCol) { |
| 844 | const moves = this.getPotentialMovesFrom([i, j]); |
| 845 | if (moves.length > 0) { |
| 846 | for (let k = 0; k < moves.length; k++) { |
| 847 | if (this.filterValid([moves[k]]).length > 0) return true; |
| 848 | } |
| 849 | } |
| 850 | } |
| 851 | } |
| 852 | } |
| 853 | return false; |
| 854 | } |
| 855 | |
| 856 | // Check if pieces of color in 'colors' are attacking (king) on square x,y |
| 857 | isAttacked(sq, colors) { |
| 858 | return ( |
| 859 | this.isAttackedByPawn(sq, colors) || |
| 860 | this.isAttackedByRook(sq, colors) || |
| 861 | this.isAttackedByKnight(sq, colors) || |
| 862 | this.isAttackedByBishop(sq, colors) || |
| 863 | this.isAttackedByQueen(sq, colors) || |
| 864 | this.isAttackedByKing(sq, colors) |
| 865 | ); |
| 866 | } |
| 867 | |
| 868 | // Is square x,y attacked by 'colors' pawns ? |
| 869 | isAttackedByPawn([x, y], colors) { |
| 870 | for (let c of colors) { |
| 871 | let pawnShift = c == "w" ? 1 : -1; |
| 872 | if (x + pawnShift >= 0 && x + pawnShift < V.size.x) { |
| 873 | for (let i of [-1, 1]) { |
| 874 | if ( |
| 875 | y + i >= 0 && |
| 876 | y + i < V.size.y && |
| 877 | this.getPiece(x + pawnShift, y + i) == V.PAWN && |
| 878 | this.getColor(x + pawnShift, y + i) == c |
| 879 | ) { |
| 880 | return true; |
| 881 | } |
| 882 | } |
| 883 | } |
| 884 | } |
| 885 | return false; |
| 886 | } |
| 887 | |
| 888 | // Is square x,y attacked by 'colors' rooks ? |
| 889 | isAttackedByRook(sq, colors) { |
| 890 | return this.isAttackedBySlideNJump(sq, colors, V.ROOK, V.steps[V.ROOK]); |
| 891 | } |
| 892 | |
| 893 | // Is square x,y attacked by 'colors' knights ? |
| 894 | isAttackedByKnight(sq, colors) { |
| 895 | return this.isAttackedBySlideNJump( |
| 896 | sq, |
| 897 | colors, |
| 898 | V.KNIGHT, |
| 899 | V.steps[V.KNIGHT], |
| 900 | "oneStep" |
| 901 | ); |
| 902 | } |
| 903 | |
| 904 | // Is square x,y attacked by 'colors' bishops ? |
| 905 | isAttackedByBishop(sq, colors) { |
| 906 | return this.isAttackedBySlideNJump(sq, colors, V.BISHOP, V.steps[V.BISHOP]); |
| 907 | } |
| 908 | |
| 909 | // Is square x,y attacked by 'colors' queens ? |
| 910 | isAttackedByQueen(sq, colors) { |
| 911 | return this.isAttackedBySlideNJump( |
| 912 | sq, |
| 913 | colors, |
| 914 | V.QUEEN, |
| 915 | V.steps[V.ROOK].concat(V.steps[V.BISHOP]) |
| 916 | ); |
| 917 | } |
| 918 | |
| 919 | // Is square x,y attacked by 'colors' king(s) ? |
| 920 | isAttackedByKing(sq, colors) { |
| 921 | return this.isAttackedBySlideNJump( |
| 922 | sq, |
| 923 | colors, |
| 924 | V.KING, |
| 925 | V.steps[V.ROOK].concat(V.steps[V.BISHOP]), |
| 926 | "oneStep" |
| 927 | ); |
| 928 | } |
| 929 | |
| 930 | // Generic method for non-pawn pieces ("sliding or jumping"): |
| 931 | // is x,y attacked by a piece of color in array 'colors' ? |
| 932 | isAttackedBySlideNJump([x, y], colors, piece, steps, oneStep) { |
| 933 | for (let step of steps) { |
| 934 | let rx = x + step[0], |
| 935 | ry = y + step[1]; |
| 936 | while (V.OnBoard(rx, ry) && this.board[rx][ry] == V.EMPTY && !oneStep) { |
| 937 | rx += step[0]; |
| 938 | ry += step[1]; |
| 939 | } |
| 940 | if ( |
| 941 | V.OnBoard(rx, ry) && |
| 942 | this.getPiece(rx, ry) === piece && |
| 943 | colors.includes(this.getColor(rx, ry)) |
| 944 | ) { |
| 945 | return true; |
| 946 | } |
| 947 | } |
| 948 | return false; |
| 949 | } |
| 950 | |
| 951 | // Is color under check after his move ? |
| 952 | underCheck(color) { |
| 953 | return this.isAttacked(this.kingPos[color], [V.GetOppCol(color)]); |
| 954 | } |
| 955 | |
| 956 | ///////////////// |
| 957 | // MOVES PLAYING |
| 958 | |
| 959 | // Apply a move on board |
| 960 | static PlayOnBoard(board, move) { |
| 961 | for (let psq of move.vanish) board[psq.x][psq.y] = V.EMPTY; |
| 962 | for (let psq of move.appear) board[psq.x][psq.y] = psq.c + psq.p; |
| 963 | } |
| 964 | // Un-apply the played move |
| 965 | static UndoOnBoard(board, move) { |
| 966 | for (let psq of move.appear) board[psq.x][psq.y] = V.EMPTY; |
| 967 | for (let psq of move.vanish) board[psq.x][psq.y] = psq.c + psq.p; |
| 968 | } |
| 969 | |
| 970 | // After move is played, update variables + flags |
| 971 | updateVariables(move) { |
| 972 | let piece = undefined; |
| 973 | // TODO: update variables before move is played, and just use this.turn ? |
| 974 | // (doesn't work in general, think MarseilleChess) |
| 975 | let c = undefined; |
| 976 | if (move.vanish.length >= 1) { |
| 977 | // Usual case, something is moved |
| 978 | piece = move.vanish[0].p; |
| 979 | c = move.vanish[0].c; |
| 980 | } else { |
| 981 | // Crazyhouse-like variants |
| 982 | piece = move.appear[0].p; |
| 983 | c = move.appear[0].c; |
| 984 | } |
| 985 | if (!['w','b'].includes(c)) { |
| 986 | // Checkered, for example |
| 987 | c = V.GetOppCol(this.turn); |
| 988 | } |
| 989 | const firstRank = c == "w" ? V.size.x - 1 : 0; |
| 990 | |
| 991 | // Update king position + flags |
| 992 | if (piece == V.KING && move.appear.length > 0) { |
| 993 | this.kingPos[c][0] = move.appear[0].x; |
| 994 | this.kingPos[c][1] = move.appear[0].y; |
| 995 | if (V.HasFlags) this.castleFlags[c] = [false, false]; |
| 996 | return; |
| 997 | } |
| 998 | if (V.HasFlags) { |
| 999 | // Update castling flags if rooks are moved |
| 1000 | const oppCol = V.GetOppCol(c); |
| 1001 | const oppFirstRank = V.size.x - 1 - firstRank; |
| 1002 | if ( |
| 1003 | move.start.x == firstRank && //our rook moves? |
| 1004 | this.INIT_COL_ROOK[c].includes(move.start.y) |
| 1005 | ) { |
| 1006 | const flagIdx = move.start.y == this.INIT_COL_ROOK[c][0] ? 0 : 1; |
| 1007 | this.castleFlags[c][flagIdx] = false; |
| 1008 | } else if ( |
| 1009 | move.end.x == oppFirstRank && //we took opponent rook? |
| 1010 | this.INIT_COL_ROOK[oppCol].includes(move.end.y) |
| 1011 | ) { |
| 1012 | const flagIdx = move.end.y == this.INIT_COL_ROOK[oppCol][0] ? 0 : 1; |
| 1013 | this.castleFlags[oppCol][flagIdx] = false; |
| 1014 | } |
| 1015 | } |
| 1016 | } |
| 1017 | |
| 1018 | // After move is undo-ed *and flags resetted*, un-update other variables |
| 1019 | // TODO: more symmetry, by storing flags increment in move (?!) |
| 1020 | unupdateVariables(move) { |
| 1021 | // (Potentially) Reset king position |
| 1022 | const c = this.getColor(move.start.x, move.start.y); |
| 1023 | if (this.getPiece(move.start.x, move.start.y) == V.KING) |
| 1024 | this.kingPos[c] = [move.start.x, move.start.y]; |
| 1025 | } |
| 1026 | |
| 1027 | play(move) { |
| 1028 | // DEBUG: |
| 1029 | // if (!this.states) this.states = []; |
| 1030 | // const stateFen = this.getBaseFen() + this.getTurnFen() + this.getFlagsFen(); |
| 1031 | // this.states.push(stateFen); |
| 1032 | |
| 1033 | if (V.HasFlags) move.flags = JSON.stringify(this.aggregateFlags()); //save flags (for undo) |
| 1034 | if (V.HasEnpassant) this.epSquares.push(this.getEpSquare(move)); |
| 1035 | V.PlayOnBoard(this.board, move); |
| 1036 | this.turn = V.GetOppCol(this.turn); |
| 1037 | this.movesCount++; |
| 1038 | this.updateVariables(move); |
| 1039 | } |
| 1040 | |
| 1041 | undo(move) { |
| 1042 | if (V.HasEnpassant) this.epSquares.pop(); |
| 1043 | if (V.HasFlags) this.disaggregateFlags(JSON.parse(move.flags)); |
| 1044 | V.UndoOnBoard(this.board, move); |
| 1045 | this.turn = V.GetOppCol(this.turn); |
| 1046 | this.movesCount--; |
| 1047 | this.unupdateVariables(move); |
| 1048 | |
| 1049 | // DEBUG: |
| 1050 | // const stateFen = this.getBaseFen() + this.getTurnFen() + this.getFlagsFen(); |
| 1051 | // if (stateFen != this.states[this.states.length-1]) debugger; |
| 1052 | // this.states.pop(); |
| 1053 | } |
| 1054 | |
| 1055 | /////////////// |
| 1056 | // END OF GAME |
| 1057 | |
| 1058 | // What is the score ? (Interesting if game is over) |
| 1059 | getCurrentScore() { |
| 1060 | if (this.atLeastOneMove()) |
| 1061 | return "*"; |
| 1062 | |
| 1063 | // Game over |
| 1064 | const color = this.turn; |
| 1065 | // No valid move: stalemate or checkmate? |
| 1066 | if (!this.isAttacked(this.kingPos[color], [V.GetOppCol(color)])) |
| 1067 | return "1/2"; |
| 1068 | // OK, checkmate |
| 1069 | return color == "w" ? "0-1" : "1-0"; |
| 1070 | } |
| 1071 | |
| 1072 | /////////////// |
| 1073 | // ENGINE PLAY |
| 1074 | |
| 1075 | // Pieces values |
| 1076 | static get VALUES() { |
| 1077 | return { |
| 1078 | p: 1, |
| 1079 | r: 5, |
| 1080 | n: 3, |
| 1081 | b: 3, |
| 1082 | q: 9, |
| 1083 | k: 1000 |
| 1084 | }; |
| 1085 | } |
| 1086 | |
| 1087 | // "Checkmate" (unreachable eval) |
| 1088 | static get INFINITY() { |
| 1089 | return 9999; |
| 1090 | } |
| 1091 | |
| 1092 | // At this value or above, the game is over |
| 1093 | static get THRESHOLD_MATE() { |
| 1094 | return V.INFINITY; |
| 1095 | } |
| 1096 | |
| 1097 | // Search depth: 2 for high branching factor, 4 for small (Loser chess, eg.) |
| 1098 | static get SEARCH_DEPTH() { |
| 1099 | return 3; |
| 1100 | } |
| 1101 | |
| 1102 | // NOTE: works also for extinction chess because depth is 3... |
| 1103 | getComputerMove() { |
| 1104 | const maxeval = V.INFINITY; |
| 1105 | const color = this.turn; |
| 1106 | // Some variants may show a bigger moves list to the human (Switching), |
| 1107 | // thus the argument "computer" below (which is generally ignored) |
| 1108 | let moves1 = this.getAllValidMoves("computer"); |
| 1109 | if (moves1.length == 0) |
| 1110 | //TODO: this situation should not happen |
| 1111 | return null; |
| 1112 | |
| 1113 | // Can I mate in 1 ? (for Magnetic & Extinction) |
| 1114 | for (let i of shuffle(ArrayFun.range(moves1.length))) { |
| 1115 | this.play(moves1[i]); |
| 1116 | let finish = Math.abs(this.evalPosition()) >= V.THRESHOLD_MATE; |
| 1117 | if (!finish) { |
| 1118 | const score = this.getCurrentScore(); |
| 1119 | if (["1-0", "0-1"].includes(score)) finish = true; |
| 1120 | } |
| 1121 | this.undo(moves1[i]); |
| 1122 | if (finish) return moves1[i]; |
| 1123 | } |
| 1124 | |
| 1125 | // Rank moves using a min-max at depth 2 |
| 1126 | for (let i = 0; i < moves1.length; i++) { |
| 1127 | // Initial self evaluation is very low: "I'm checkmated" |
| 1128 | moves1[i].eval = (color == "w" ? -1 : 1) * maxeval; |
| 1129 | this.play(moves1[i]); |
| 1130 | const score1 = this.getCurrentScore(); |
| 1131 | let eval2 = undefined; |
| 1132 | if (score1 == "*") { |
| 1133 | // Initial enemy evaluation is very low too, for him |
| 1134 | eval2 = (color == "w" ? 1 : -1) * maxeval; |
| 1135 | // Second half-move: |
| 1136 | let moves2 = this.getAllValidMoves("computer"); |
| 1137 | for (let j = 0; j < moves2.length; j++) { |
| 1138 | this.play(moves2[j]); |
| 1139 | const score2 = this.getCurrentScore(); |
| 1140 | let evalPos = 0; //1/2 value |
| 1141 | switch (score2) { |
| 1142 | case "*": |
| 1143 | evalPos = this.evalPosition(); |
| 1144 | break; |
| 1145 | case "1-0": |
| 1146 | evalPos = maxeval; |
| 1147 | break; |
| 1148 | case "0-1": |
| 1149 | evalPos = -maxeval; |
| 1150 | break; |
| 1151 | } |
| 1152 | if ( |
| 1153 | (color == "w" && evalPos < eval2) || |
| 1154 | (color == "b" && evalPos > eval2) |
| 1155 | ) { |
| 1156 | eval2 = evalPos; |
| 1157 | } |
| 1158 | this.undo(moves2[j]); |
| 1159 | } |
| 1160 | } else eval2 = score1 == "1/2" ? 0 : (score1 == "1-0" ? 1 : -1) * maxeval; |
| 1161 | if ( |
| 1162 | (color == "w" && eval2 > moves1[i].eval) || |
| 1163 | (color == "b" && eval2 < moves1[i].eval) |
| 1164 | ) { |
| 1165 | moves1[i].eval = eval2; |
| 1166 | } |
| 1167 | this.undo(moves1[i]); |
| 1168 | } |
| 1169 | moves1.sort((a, b) => { |
| 1170 | return (color == "w" ? 1 : -1) * (b.eval - a.eval); |
| 1171 | }); |
| 1172 | |
| 1173 | let candidates = [0]; //indices of candidates moves |
| 1174 | for (let j = 1; j < moves1.length && moves1[j].eval == moves1[0].eval; j++) |
| 1175 | candidates.push(j); |
| 1176 | let currentBest = moves1[candidates[randInt(candidates.length)]]; |
| 1177 | |
| 1178 | // Skip depth 3+ if we found a checkmate (or if we are checkmated in 1...) |
| 1179 | if (V.SEARCH_DEPTH >= 3 && Math.abs(moves1[0].eval) < V.THRESHOLD_MATE) { |
| 1180 | // From here, depth >= 3: may take a while, so we control time |
| 1181 | const timeStart = Date.now(); |
| 1182 | for (let i = 0; i < moves1.length; i++) { |
| 1183 | if (Date.now() - timeStart >= 5000) |
| 1184 | //more than 5 seconds |
| 1185 | return currentBest; //depth 2 at least |
| 1186 | this.play(moves1[i]); |
| 1187 | // 0.1 * oldEval : heuristic to avoid some bad moves (not all...) |
| 1188 | moves1[i].eval = |
| 1189 | 0.1 * moves1[i].eval + |
| 1190 | this.alphabeta(V.SEARCH_DEPTH - 1, -maxeval, maxeval); |
| 1191 | this.undo(moves1[i]); |
| 1192 | } |
| 1193 | moves1.sort((a, b) => { |
| 1194 | return (color == "w" ? 1 : -1) * (b.eval - a.eval); |
| 1195 | }); |
| 1196 | } else return currentBest; |
| 1197 | // console.log(moves1.map(m => { return [this.getNotation(m), m.eval]; })); |
| 1198 | |
| 1199 | candidates = [0]; |
| 1200 | for (let j = 1; j < moves1.length && moves1[j].eval == moves1[0].eval; j++) |
| 1201 | candidates.push(j); |
| 1202 | return moves1[candidates[randInt(candidates.length)]]; |
| 1203 | } |
| 1204 | |
| 1205 | alphabeta(depth, alpha, beta) { |
| 1206 | const maxeval = V.INFINITY; |
| 1207 | const color = this.turn; |
| 1208 | const score = this.getCurrentScore(); |
| 1209 | if (score != "*") |
| 1210 | return score == "1/2" ? 0 : (score == "1-0" ? 1 : -1) * maxeval; |
| 1211 | if (depth == 0) return this.evalPosition(); |
| 1212 | const moves = this.getAllValidMoves("computer"); |
| 1213 | let v = color == "w" ? -maxeval : maxeval; |
| 1214 | if (color == "w") { |
| 1215 | for (let i = 0; i < moves.length; i++) { |
| 1216 | this.play(moves[i]); |
| 1217 | v = Math.max(v, this.alphabeta(depth - 1, alpha, beta)); |
| 1218 | this.undo(moves[i]); |
| 1219 | alpha = Math.max(alpha, v); |
| 1220 | if (alpha >= beta) break; //beta cutoff |
| 1221 | } |
| 1222 | } //color=="b" |
| 1223 | else { |
| 1224 | for (let i = 0; i < moves.length; i++) { |
| 1225 | this.play(moves[i]); |
| 1226 | v = Math.min(v, this.alphabeta(depth - 1, alpha, beta)); |
| 1227 | this.undo(moves[i]); |
| 1228 | beta = Math.min(beta, v); |
| 1229 | if (alpha >= beta) break; //alpha cutoff |
| 1230 | } |
| 1231 | } |
| 1232 | return v; |
| 1233 | } |
| 1234 | |
| 1235 | evalPosition() { |
| 1236 | let evaluation = 0; |
| 1237 | // Just count material for now |
| 1238 | for (let i = 0; i < V.size.x; i++) { |
| 1239 | for (let j = 0; j < V.size.y; j++) { |
| 1240 | if (this.board[i][j] != V.EMPTY) { |
| 1241 | const sign = this.getColor(i, j) == "w" ? 1 : -1; |
| 1242 | evaluation += sign * V.VALUES[this.getPiece(i, j)]; |
| 1243 | } |
| 1244 | } |
| 1245 | } |
| 1246 | return evaluation; |
| 1247 | } |
| 1248 | |
| 1249 | ///////////////////////// |
| 1250 | // MOVES + GAME NOTATION |
| 1251 | ///////////////////////// |
| 1252 | |
| 1253 | // Context: just before move is played, turn hasn't changed |
| 1254 | // TODO: un-ambiguous notation (switch on piece type, check directions...) |
| 1255 | getNotation(move) { |
| 1256 | if (move.appear.length == 2 && move.appear[0].p == V.KING) |
| 1257 | //castle |
| 1258 | return move.end.y < move.start.y ? "0-0-0" : "0-0"; |
| 1259 | |
| 1260 | // Translate final square |
| 1261 | const finalSquare = V.CoordsToSquare(move.end); |
| 1262 | |
| 1263 | const piece = this.getPiece(move.start.x, move.start.y); |
| 1264 | if (piece == V.PAWN) { |
| 1265 | // Pawn move |
| 1266 | let notation = ""; |
| 1267 | if (move.vanish.length > move.appear.length) { |
| 1268 | // Capture |
| 1269 | const startColumn = V.CoordToColumn(move.start.y); |
| 1270 | notation = startColumn + "x" + finalSquare; |
| 1271 | } |
| 1272 | else notation = finalSquare; |
| 1273 | if (move.appear.length > 0 && move.appear[0].p != V.PAWN) |
| 1274 | // Promotion |
| 1275 | notation += "=" + move.appear[0].p.toUpperCase(); |
| 1276 | return notation; |
| 1277 | } |
| 1278 | // Piece movement |
| 1279 | return ( |
| 1280 | piece.toUpperCase() + |
| 1281 | (move.vanish.length > move.appear.length ? "x" : "") + |
| 1282 | finalSquare |
| 1283 | ); |
| 1284 | } |
| 1285 | }; |