| 1 | import { ChessRules, Move, PiPo } from "@/base_rules"; |
| 2 | import { randInt } from "@/utils/alea"; |
| 3 | |
| 4 | export class DynamoRules extends ChessRules { |
| 5 | // TODO: later, allow to push out pawns on a and h files |
| 6 | static get HasEnpassant() { |
| 7 | return false; |
| 8 | } |
| 9 | |
| 10 | canIplay(side, [x, y]) { |
| 11 | // Sometimes opponent's pieces can be moved directly |
| 12 | return this.turn == side; |
| 13 | } |
| 14 | |
| 15 | setOtherVariables(fen) { |
| 16 | super.setOtherVariables(fen); |
| 17 | this.subTurn = 1; |
| 18 | // Local stack of "action moves" |
| 19 | this.amoves = []; |
| 20 | const amove = V.ParseFen(fen).amove; |
| 21 | if (amove != "-") { |
| 22 | const amoveParts = amove.split("/"); |
| 23 | let move = { |
| 24 | // No need for start & end |
| 25 | appear: [], |
| 26 | vanish: [] |
| 27 | }; |
| 28 | [0, 1].map(i => { |
| 29 | if (amoveParts[i] != "-") { |
| 30 | amoveParts[i].split(".").forEach(av => { |
| 31 | // Format is "bpe3" |
| 32 | const xy = V.SquareToCoords(av.substr(2)); |
| 33 | move[i == 0 ? "appear" : "vanish"].push( |
| 34 | new PiPo({ |
| 35 | x: xy.x, |
| 36 | y: xy.y, |
| 37 | c: av[0], |
| 38 | p: av[1] |
| 39 | }) |
| 40 | ); |
| 41 | }); |
| 42 | } |
| 43 | }); |
| 44 | this.amoves.push(move); |
| 45 | } |
| 46 | this.subTurn = 1; |
| 47 | // Stack "first moves" (on subTurn 1) to merge and check opposite moves |
| 48 | this.firstMove = []; |
| 49 | } |
| 50 | |
| 51 | static ParseFen(fen) { |
| 52 | return Object.assign( |
| 53 | ChessRules.ParseFen(fen), |
| 54 | { amove: fen.split(" ")[4] } |
| 55 | ); |
| 56 | } |
| 57 | |
| 58 | static IsGoodFen(fen) { |
| 59 | if (!ChessRules.IsGoodFen(fen)) return false; |
| 60 | const fenParts = fen.split(" "); |
| 61 | if (fenParts.length != 5) return false; |
| 62 | if (fenParts[4] != "-") { |
| 63 | // TODO: a single regexp instead. |
| 64 | // Format is [bpa2[.wpd3]] || '-'/[bbc3[.wrd5]] || '-' |
| 65 | const amoveParts = fenParts[4].split("/"); |
| 66 | if (amoveParts.length != 2) return false; |
| 67 | for (let part of amoveParts) { |
| 68 | if (part != "-") { |
| 69 | for (let psq of part.split(".")) |
| 70 | if (!psq.match(/^[a-r]{3}[1-8]$/)) return false; |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | return true; |
| 75 | } |
| 76 | |
| 77 | getFen() { |
| 78 | return super.getFen() + " " + this.getAmoveFen(); |
| 79 | } |
| 80 | |
| 81 | getFenForRepeat() { |
| 82 | return super.getFenForRepeat() + "_" + this.getAmoveFen(); |
| 83 | } |
| 84 | |
| 85 | getAmoveFen() { |
| 86 | const L = this.amoves.length; |
| 87 | if (L == 0) return "-"; |
| 88 | return ( |
| 89 | ["appear","vanish"].map( |
| 90 | mpart => { |
| 91 | if (this.amoves[L-1][mpart].length == 0) return "-"; |
| 92 | return ( |
| 93 | this.amoves[L-1][mpart].map( |
| 94 | av => { |
| 95 | const square = V.CoordsToSquare({ x: av.x, y: av.y }); |
| 96 | return av.c + av.p + square; |
| 97 | } |
| 98 | ).join(".") |
| 99 | ); |
| 100 | } |
| 101 | ).join("/") |
| 102 | ); |
| 103 | } |
| 104 | |
| 105 | canTake() { |
| 106 | // Captures don't occur (only pulls & pushes) |
| 107 | return false; |
| 108 | } |
| 109 | |
| 110 | // Step is right, just add (push/pull) moves in this direction |
| 111 | // Direction is assumed normalized. |
| 112 | getMovesInDirection([x, y], [dx, dy], nbSteps) { |
| 113 | nbSteps = nbSteps || 8; //max 8 steps anyway |
| 114 | let [i, j] = [x + dx, y + dy]; |
| 115 | let moves = []; |
| 116 | const color = this.getColor(x, y); |
| 117 | const piece = this.getPiece(x, y); |
| 118 | const lastRank = (color == 'w' ? 0 : 7); |
| 119 | let counter = 1; |
| 120 | while (V.OnBoard(i, j) && this.board[i][j] == V.EMPTY) { |
| 121 | if (i == lastRank && piece == V.PAWN) { |
| 122 | // Promotion by push or pull |
| 123 | V.PawnSpecs.promotions.forEach(p => { |
| 124 | let move = super.getBasicMove([x, y], [i, j], { c: color, p: p }); |
| 125 | moves.push(move); |
| 126 | }); |
| 127 | } |
| 128 | else moves.push(super.getBasicMove([x, y], [i, j])); |
| 129 | if (++counter > nbSteps) break; |
| 130 | i += dx; |
| 131 | j += dy; |
| 132 | } |
| 133 | if (!V.OnBoard(i, j) && piece != V.KING) { |
| 134 | // Add special "exit" move, by "taking king" |
| 135 | moves.push( |
| 136 | new Move({ |
| 137 | start: { x: x, y: y }, |
| 138 | end: { x: this.kingPos[color][0], y: this.kingPos[color][1] }, |
| 139 | appear: [], |
| 140 | vanish: [{ x: x, y: y, c: color, p: piece }] |
| 141 | }) |
| 142 | ); |
| 143 | } |
| 144 | return moves; |
| 145 | } |
| 146 | |
| 147 | // Normalize direction to know the step |
| 148 | getNormalizedDirection([dx, dy]) { |
| 149 | const absDir = [Math.abs(dx), Math.abs(dy)]; |
| 150 | let divisor = 0; |
| 151 | if (absDir[0] != 0 && absDir[1] != 0 && absDir[0] != absDir[1]) |
| 152 | // Knight |
| 153 | divisor = Math.min(absDir[0], absDir[1]); |
| 154 | else |
| 155 | // Standard slider (or maybe a pawn or king: same) |
| 156 | divisor = Math.max(absDir[0], absDir[1]); |
| 157 | return [dx / divisor, dy / divisor]; |
| 158 | } |
| 159 | |
| 160 | // There was something on x2,y2, maybe our color, pushed/pulled. |
| 161 | // Also, the pushed/pulled piece must exit the board. |
| 162 | isAprioriValidExit([x1, y1], [x2, y2], color2) { |
| 163 | const color1 = this.getColor(x1, y1); |
| 164 | const pawnShift = (color1 == 'w' ? -1 : 1); |
| 165 | const lastRank = (color1 == 'w' ? 0 : 7); |
| 166 | const deltaX = Math.abs(x1 - x2); |
| 167 | const deltaY = Math.abs(y1 - y2); |
| 168 | const checkSlider = () => { |
| 169 | const dir = this.getNormalizedDirection([x2 - x1, y2 - y1]); |
| 170 | let [i, j] = [x1 + dir[0], y1 + dir[1]]; |
| 171 | while (V.OnBoard(i, j) && this.board[i][j] == V.EMPTY) { |
| 172 | i += dir[0]; |
| 173 | j += dir[1]; |
| 174 | } |
| 175 | return !V.OnBoard(i, j); |
| 176 | }; |
| 177 | switch (this.getPiece(x1, y1)) { |
| 178 | case V.PAWN: |
| 179 | return ( |
| 180 | x1 + pawnShift == x2 && |
| 181 | ( |
| 182 | (color1 == color2 && x2 == lastRank && y1 == y2) || |
| 183 | (color1 != color2 && deltaY == 1 && !V.OnBoard(x2, 2 * y2 - y1)) |
| 184 | ) |
| 185 | ); |
| 186 | case V.ROOK: |
| 187 | if (x1 != x2 && y1 != y2) return false; |
| 188 | return checkSlider(); |
| 189 | case V.KNIGHT: |
| 190 | return ( |
| 191 | deltaX + deltaY == 3 && |
| 192 | (deltaX == 1 || deltaY == 1) && |
| 193 | !V.OnBoard(2 * x2 - x1, 2 * y2 - y1) |
| 194 | ); |
| 195 | case V.BISHOP: |
| 196 | if (deltaX != deltaY) return false; |
| 197 | return checkSlider(); |
| 198 | case V.QUEEN: |
| 199 | if (deltaX != 0 && deltaY != 0 && deltaX != deltaY) return false; |
| 200 | return checkSlider(); |
| 201 | case V.KING: |
| 202 | return ( |
| 203 | deltaX <= 1 && |
| 204 | deltaY <= 1 && |
| 205 | !V.OnBoard(2 * x2 - x1, 2 * y2 - y1) |
| 206 | ); |
| 207 | } |
| 208 | return false; |
| 209 | } |
| 210 | |
| 211 | // NOTE: for pushes, play the pushed piece first. |
| 212 | // for pulls: play the piece doing the action first |
| 213 | // NOTE: to push a piece out of the board, make it slide until its king |
| 214 | getPotentialMovesFrom([x, y]) { |
| 215 | const color = this.turn; |
| 216 | if (this.subTurn == 1) { |
| 217 | const getMoveHash = (m) => { |
| 218 | return V.CoordsToSquare(m.start) + V.CoordsToSquare(m.end); |
| 219 | }; |
| 220 | const addMoves = (dir, nbSteps) => { |
| 221 | const newMoves = |
| 222 | this.getMovesInDirection([x, y], [-dir[0], -dir[1]], nbSteps) |
| 223 | .filter(m => !movesHash[getMoveHash(m)]); |
| 224 | newMoves.forEach(m => { movesHash[getMoveHash(m)] = true; }); |
| 225 | Array.prototype.push.apply(moves, newMoves); |
| 226 | }; |
| 227 | // Free to play any move (if piece of my color): |
| 228 | const moves = |
| 229 | this.getColor(x, y) == color |
| 230 | ? super.getPotentialMovesFrom([x, y]) |
| 231 | : []; |
| 232 | const pawnShift = (color == 'w' ? -1 : 1); |
| 233 | const pawnStartRank = (color == 'w' ? 6 : 1); |
| 234 | // Structure to avoid adding moves twice (can be action & move) |
| 235 | let movesHash = {}; |
| 236 | moves.forEach(m => { movesHash[getMoveHash(m)] = true; }); |
| 237 | // [x, y] is pushed by 'color' |
| 238 | for (let step of V.steps[V.KNIGHT]) { |
| 239 | const [i, j] = [x + step[0], y + step[1]]; |
| 240 | if ( |
| 241 | V.OnBoard(i, j) && |
| 242 | this.board[i][j] != V.EMPTY && |
| 243 | this.getColor(i, j) == color && |
| 244 | this.getPiece(i, j) == V.KNIGHT |
| 245 | ) { |
| 246 | addMoves(step, 1); |
| 247 | } |
| 248 | } |
| 249 | for (let step of V.steps[V.ROOK].concat(V.steps[V.BISHOP])) { |
| 250 | let [i, j] = [x + step[0], y + step[1]]; |
| 251 | while (V.OnBoard(i, j) && this.board[i][j] == V.EMPTY) { |
| 252 | i += step[0]; |
| 253 | j += step[1]; |
| 254 | } |
| 255 | if ( |
| 256 | V.OnBoard(i, j) && |
| 257 | this.board[i][j] != V.EMPTY && |
| 258 | this.getColor(i, j) == color |
| 259 | ) { |
| 260 | const deltaX = Math.abs(i - x); |
| 261 | const deltaY = Math.abs(j - y); |
| 262 | // Can a priori go both ways, except with pawns |
| 263 | switch (this.getPiece(i, j)) { |
| 264 | case V.PAWN: |
| 265 | if ( |
| 266 | (x - i) / deltaX == pawnShift && |
| 267 | deltaX <= 2 && |
| 268 | deltaY <= 1 |
| 269 | ) { |
| 270 | const pColor = this.getColor(x, y); |
| 271 | if (pColor == color && deltaY == 0) { |
| 272 | // Pushed forward |
| 273 | const maxSteps = (i == pawnStartRank && deltaX == 1 ? 2 : 1); |
| 274 | addMoves(step, maxSteps); |
| 275 | } |
| 276 | else if (pColor != color && deltaY == 1 && deltaX == 1) |
| 277 | // Pushed diagonally |
| 278 | addMoves(step, 1); |
| 279 | } |
| 280 | break; |
| 281 | case V.ROOK: |
| 282 | if (deltaX == 0 || deltaY == 0) addMoves(step); |
| 283 | break; |
| 284 | case V.BISHOP: |
| 285 | if (deltaX == deltaY) addMoves(step); |
| 286 | break; |
| 287 | case V.QUEEN: |
| 288 | if (deltaX == 0 || deltaY == 0 || deltaX == deltaY) |
| 289 | addMoves(step); |
| 290 | break; |
| 291 | case V.KING: |
| 292 | if (deltaX <= 1 && deltaY <= 1) addMoves(step, 1); |
| 293 | break; |
| 294 | } |
| 295 | } |
| 296 | } |
| 297 | return moves; |
| 298 | } |
| 299 | // If subTurn == 2 then we should have a first move, |
| 300 | // which restrict what we can play now: only in the first move direction |
| 301 | // NOTE: no need for knight or pawn checks, because the move will be |
| 302 | // naturally limited in those cases. |
| 303 | const L = this.firstMove.length; |
| 304 | const fm = this.firstMove[L-1]; |
| 305 | if (fm.appear.length == 2 && fm.vanish.length == 2) |
| 306 | // Castle: no real move playable then. |
| 307 | return []; |
| 308 | if (fm.appear.length == 0) { |
| 309 | // Piece at subTurn 1 just exited the board. |
| 310 | // Can I be a piece which caused the exit? |
| 311 | if ( |
| 312 | this.isAprioriValidExit( |
| 313 | [x, y], |
| 314 | [fm.start.x, fm.start.y], |
| 315 | fm.vanish[0].c |
| 316 | ) |
| 317 | ) { |
| 318 | // Seems so: |
| 319 | const dir = this.getNormalizedDirection( |
| 320 | [fm.start.x - x, fm.start.y - y]); |
| 321 | return this.getMovesInDirection([x, y], dir); |
| 322 | } |
| 323 | } |
| 324 | else { |
| 325 | const dirM = this.getNormalizedDirection( |
| 326 | [fm.end.x - fm.start.x, fm.end.y - fm.start.y]); |
| 327 | const dir = this.getNormalizedDirection( |
| 328 | [fm.start.x - x, fm.start.y - y]); |
| 329 | // Normalized directions should match |
| 330 | if (dir[0] == dirM[0] && dir[1] == dirM[1]) { |
| 331 | // And nothing should stand between [x, y] and the square fm.start |
| 332 | let [i, j] = [x + dir[0], y + dir[1]]; |
| 333 | while ( |
| 334 | (i != fm.start.x || j != fm.start.y) && |
| 335 | this.board[i][j] == V.EMPTY |
| 336 | ) { |
| 337 | i += dir[0]; |
| 338 | j += dir[1]; |
| 339 | } |
| 340 | if (i == fm.start.x && j == fm.start.y) |
| 341 | return this.getMovesInDirection([x, y], dir); |
| 342 | } |
| 343 | } |
| 344 | return []; |
| 345 | } |
| 346 | |
| 347 | // Does m2 un-do m1 ? (to disallow undoing actions) |
| 348 | oppositeMoves(m1, m2) { |
| 349 | const isEqual = (av1, av2) => { |
| 350 | // Precondition: av1 and av2 length = 2 |
| 351 | for (let av of av1) { |
| 352 | const avInAv2 = av2.find(elt => { |
| 353 | return ( |
| 354 | elt.x == av.x && |
| 355 | elt.y == av.y && |
| 356 | elt.c == av.c && |
| 357 | elt.p == av.p |
| 358 | ); |
| 359 | }); |
| 360 | if (!avInAv2) return false; |
| 361 | } |
| 362 | return true; |
| 363 | }; |
| 364 | return ( |
| 365 | m1.appear.length == 2 && |
| 366 | m2.appear.length == 2 && |
| 367 | m1.vanish.length == 2 && |
| 368 | m2.vanish.length == 2 && |
| 369 | isEqual(m1.appear, m2.vanish) && |
| 370 | isEqual(m1.vanish, m2.appear) |
| 371 | ); |
| 372 | } |
| 373 | |
| 374 | getAmove(move1, move2) { |
| 375 | // Just merge (one is action one is move, one may be empty) |
| 376 | return { |
| 377 | appear: move1.appear.concat(move2.appear), |
| 378 | vanish: move1.vanish.concat(move2.vanish) |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | filterValid(moves) { |
| 383 | const color = this.turn; |
| 384 | if (this.subTurn == 1) { |
| 385 | return moves.filter(m => { |
| 386 | // A move is valid either if it doesn't result in a check, |
| 387 | // or if a second move is possible to counter the check |
| 388 | // (not undoing a potential move + action of the opponent) |
| 389 | this.play(m); |
| 390 | let res = this.underCheck(color); |
| 391 | if (res) { |
| 392 | const moves2 = this.getAllPotentialMoves(); |
| 393 | for (let m2 of moves2) { |
| 394 | this.play(m2); |
| 395 | const res2 = this.underCheck(color); |
| 396 | this.undo(m2); |
| 397 | if (!res2) { |
| 398 | res = false; |
| 399 | break; |
| 400 | } |
| 401 | } |
| 402 | } |
| 403 | this.undo(m); |
| 404 | return !res; |
| 405 | }); |
| 406 | } |
| 407 | const Lf = this.firstMove.length; |
| 408 | const La = this.amoves.length; |
| 409 | if (La == 0) return super.filterValid(moves); |
| 410 | return ( |
| 411 | super.filterValid( |
| 412 | moves.filter(m => { |
| 413 | // Move shouldn't undo another: |
| 414 | const amove = this.getAmove(this.firstMove[Lf-1], m); |
| 415 | return !this.oppositeMoves(this.amoves[La-1], amove); |
| 416 | }) |
| 417 | ) |
| 418 | ); |
| 419 | } |
| 420 | |
| 421 | isAttackedBySlideNJump([x, y], color, piece, steps, oneStep) { |
| 422 | for (let step of steps) { |
| 423 | let rx = x + step[0], |
| 424 | ry = y + step[1]; |
| 425 | while (V.OnBoard(rx, ry) && this.board[rx][ry] == V.EMPTY && !oneStep) { |
| 426 | rx += step[0]; |
| 427 | ry += step[1]; |
| 428 | } |
| 429 | if ( |
| 430 | V.OnBoard(rx, ry) && |
| 431 | this.getPiece(rx, ry) == piece && |
| 432 | this.getColor(rx, ry) == color |
| 433 | ) { |
| 434 | // Continue some steps in the same direction (pull) |
| 435 | rx += step[0]; |
| 436 | ry += step[1]; |
| 437 | while ( |
| 438 | V.OnBoard(rx, ry) && |
| 439 | this.board[rx][ry] == V.EMPTY && |
| 440 | !oneStep |
| 441 | ) { |
| 442 | rx += step[0]; |
| 443 | ry += step[1]; |
| 444 | } |
| 445 | if (!V.OnBoard(rx, ry)) return true; |
| 446 | // Step in the other direction (push) |
| 447 | rx = x - step[0]; |
| 448 | ry = y - step[1]; |
| 449 | while ( |
| 450 | V.OnBoard(rx, ry) && |
| 451 | this.board[rx][ry] == V.EMPTY && |
| 452 | !oneStep |
| 453 | ) { |
| 454 | rx -= step[0]; |
| 455 | ry -= step[1]; |
| 456 | } |
| 457 | if (!V.OnBoard(rx, ry)) return true; |
| 458 | } |
| 459 | } |
| 460 | return false; |
| 461 | } |
| 462 | |
| 463 | isAttackedByPawn([x, y], color) { |
| 464 | const lastRank = (color == 'w' ? 0 : 7); |
| 465 | if (x != lastRank) |
| 466 | // The king can be pushed out by a pawn only on last rank |
| 467 | return false; |
| 468 | const pawnShift = (color == "w" ? 1 : -1); |
| 469 | for (let i of [-1, 1]) { |
| 470 | if ( |
| 471 | y + i >= 0 && |
| 472 | y + i < V.size.y && |
| 473 | this.getPiece(x + pawnShift, y + i) == V.PAWN && |
| 474 | this.getColor(x + pawnShift, y + i) == color |
| 475 | ) { |
| 476 | return true; |
| 477 | } |
| 478 | } |
| 479 | return false; |
| 480 | } |
| 481 | |
| 482 | // No consideration of color: all pieces could be played |
| 483 | getAllPotentialMoves() { |
| 484 | let potentialMoves = []; |
| 485 | for (let i = 0; i < V.size.x; i++) { |
| 486 | for (let j = 0; j < V.size.y; j++) { |
| 487 | if (this.board[i][j] != V.EMPTY) { |
| 488 | Array.prototype.push.apply( |
| 489 | potentialMoves, |
| 490 | this.getPotentialMovesFrom([i, j]) |
| 491 | ); |
| 492 | } |
| 493 | } |
| 494 | } |
| 495 | return potentialMoves; |
| 496 | } |
| 497 | |
| 498 | getCurrentScore() { |
| 499 | if (this.subTurn == 2) |
| 500 | // Move not over |
| 501 | return "*"; |
| 502 | return super.getCurrentScore(); |
| 503 | } |
| 504 | |
| 505 | doClick(square) { |
| 506 | // If subTurn == 2 && square is empty && !underCheck, |
| 507 | // then return an empty move, allowing to "pass" subTurn2 |
| 508 | if ( |
| 509 | this.subTurn == 2 && |
| 510 | this.board[square[0]][square[1]] == V.EMPTY && |
| 511 | !this.underCheck(this.turn) |
| 512 | ) { |
| 513 | return { |
| 514 | start: { x: -1, y: -1 }, |
| 515 | end: { x: -1, y: -1 }, |
| 516 | appear: [], |
| 517 | vanish: [] |
| 518 | }; |
| 519 | } |
| 520 | return null; |
| 521 | } |
| 522 | |
| 523 | play(move) { |
| 524 | move.flags = JSON.stringify(this.aggregateFlags()); |
| 525 | V.PlayOnBoard(this.board, move); |
| 526 | if (this.subTurn == 2) { |
| 527 | const L = this.firstMove.length; |
| 528 | this.amoves.push(this.getAmove(this.firstMove[L-1], move)); |
| 529 | this.turn = V.GetOppCol(this.turn); |
| 530 | this.movesCount++; |
| 531 | } |
| 532 | else this.firstMove.push(move); |
| 533 | this.subTurn = 3 - this.subTurn; |
| 534 | this.postPlay(move); |
| 535 | } |
| 536 | |
| 537 | postPlay(move) { |
| 538 | if (move.start.x < 0) return; |
| 539 | for (let a of move.appear) |
| 540 | if (a.p == V.KING) this.kingPos[a.c] = [a.x, a.y]; |
| 541 | this.updateCastleFlags(move); |
| 542 | } |
| 543 | |
| 544 | updateCastleFlags(move) { |
| 545 | const firstRank = { 'w': V.size.x - 1, 'b': 0 }; |
| 546 | for (let v of move.vanish) { |
| 547 | if (v.p == V.KING) this.castleFlags[v.c] = [V.size.y, V.size.y]; |
| 548 | else if (v.x == firstRank[v.c] && this.castleFlags[v.c].includes(v.y)) { |
| 549 | const flagIdx = (v.y == this.castleFlags[v.c][0] ? 0 : 1); |
| 550 | this.castleFlags[v.c][flagIdx] = V.size.y; |
| 551 | } |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | undo(move) { |
| 556 | this.disaggregateFlags(JSON.parse(move.flags)); |
| 557 | V.UndoOnBoard(this.board, move); |
| 558 | if (this.subTurn == 1) { |
| 559 | this.turn = V.GetOppCol(this.turn); |
| 560 | this.movesCount--; |
| 561 | } |
| 562 | else this.firstMove.pop(); |
| 563 | this.subTurn = 3 - this.subTurn; |
| 564 | this.postUndo(move); |
| 565 | } |
| 566 | |
| 567 | postUndo(move) { |
| 568 | // (Potentially) Reset king position |
| 569 | for (let v of move.vanish) |
| 570 | if (v.p == V.KING) this.kingPos[v.c] = [v.x, v.y]; |
| 571 | } |
| 572 | |
| 573 | getComputerMove() { |
| 574 | let moves = this.getAllValidMoves(); |
| 575 | if (moves.length == 0) return null; |
| 576 | // "Search" at depth 1 for now |
| 577 | const maxeval = V.INFINITY; |
| 578 | const color = this.turn; |
| 579 | const emptyMove = { |
| 580 | start: { x: -1, y: -1 }, |
| 581 | end: { x: -1, y: -1 }, |
| 582 | appear: [], |
| 583 | vanish: [] |
| 584 | }; |
| 585 | moves.forEach(m => { |
| 586 | this.play(m); |
| 587 | m.eval = (color == "w" ? -1 : 1) * maxeval; |
| 588 | const moves2 = this.getAllValidMoves().concat([emptyMove]); |
| 589 | m.next = moves2[0]; |
| 590 | moves2.forEach(m2 => { |
| 591 | this.play(m2); |
| 592 | const score = this.getCurrentScore(); |
| 593 | let mvEval = 0; |
| 594 | if (score != "1/2") { |
| 595 | if (score != "*") mvEval = (score == "1-0" ? 1 : -1) * maxeval; |
| 596 | else mvEval = this.evalPosition(); |
| 597 | } |
| 598 | if ( |
| 599 | (color == 'w' && mvEval > m.eval) || |
| 600 | (color == 'b' && mvEval < m.eval) |
| 601 | ) { |
| 602 | m.eval = mvEval; |
| 603 | m.next = m2; |
| 604 | } |
| 605 | this.undo(m2); |
| 606 | }); |
| 607 | this.undo(m); |
| 608 | }); |
| 609 | moves.sort((a, b) => { |
| 610 | return (color == "w" ? 1 : -1) * (b.eval - a.eval); |
| 611 | }); |
| 612 | let candidates = [0]; |
| 613 | for (let i = 1; i < moves.length && moves[i].eval == moves[0].eval; i++) |
| 614 | candidates.push(i); |
| 615 | const mIdx = candidates[randInt(candidates.length)]; |
| 616 | const move2 = moves[mIdx].next; |
| 617 | delete moves[mIdx]["next"]; |
| 618 | return [moves[mIdx], move2]; |
| 619 | } |
| 620 | |
| 621 | getNotation(move) { |
| 622 | if (move.start.x < 0) |
| 623 | // A second move is always required, but may be empty |
| 624 | return "-"; |
| 625 | const initialSquare = V.CoordsToSquare(move.start); |
| 626 | const finalSquare = V.CoordsToSquare(move.end); |
| 627 | if (move.appear.length == 0) |
| 628 | // Pushed or pulled out of the board |
| 629 | return initialSquare + "R"; |
| 630 | return move.appear[0].p.toUpperCase() + initialSquare + finalSquare; |
| 631 | } |
| 632 | }; |