| 1 | import { ChessRules, PiPo, Move } from "@/base_rules"; |
| 2 | import { randInt } from "@/utils/alea"; |
| 3 | |
| 4 | export class PacosakoRules extends ChessRules { |
| 5 | |
| 6 | static get IMAGE_EXTENSION() { |
| 7 | return ".png"; |
| 8 | } |
| 9 | |
| 10 | // Unions (left = white if upperCase, black otherwise) |
| 11 | static get UNIONS() { |
| 12 | return { |
| 13 | a: ['p', 'p'], |
| 14 | c: ['p', 'r'], |
| 15 | d: ['p', 'n'], |
| 16 | e: ['p', 'b'], |
| 17 | f: ['p', 'q'], |
| 18 | g: ['p', 'k'], |
| 19 | h: ['r', 'r'], |
| 20 | i: ['r', 'n'], |
| 21 | j: ['r', 'b'], |
| 22 | l: ['r', 'q'], |
| 23 | m: ['r', 'k'], |
| 24 | o: ['n', 'n'], |
| 25 | s: ['n', 'b'], |
| 26 | t: ['n', 'q'], |
| 27 | u: ['n', 'k'], |
| 28 | v: ['b', 'b'], |
| 29 | w: ['b', 'q'], |
| 30 | x: ['b', 'k'], |
| 31 | y: ['q', 'q'], |
| 32 | z: ['q', 'k'] |
| 33 | }; |
| 34 | } |
| 35 | |
| 36 | static IsGoodPosition(position) { |
| 37 | if (position.length == 0) return false; |
| 38 | const rows = position.split("/"); |
| 39 | if (rows.length != V.size.x) return false; |
| 40 | let kingSymb = ['k', 'g', 'm', 'u', 'x']; |
| 41 | let kings = { 'k': 0, 'K': 0 }; |
| 42 | for (let row of rows) { |
| 43 | let sumElts = 0; |
| 44 | for (let i = 0; i < row.length; i++) { |
| 45 | const lowR = row[i].toLowerCase |
| 46 | if (!!(row[i].toLowerCase().match(/[a-z]/))) { |
| 47 | sumElts++; |
| 48 | if (kingSymb.includes(row[i])) kings['k']++; |
| 49 | else if (kingSymb.some(s => row[i] == s.toUpperCase())) kings['K']++; |
| 50 | } |
| 51 | else { |
| 52 | const num = parseInt(row[i], 10); |
| 53 | if (isNaN(num) || num <= 0) return false; |
| 54 | sumElts += num; |
| 55 | } |
| 56 | } |
| 57 | if (sumElts != V.size.y) return false; |
| 58 | } |
| 59 | // Both kings should be on board. Exactly one per color. |
| 60 | if (Object.values(kings).some(v => v != 1)) return false; |
| 61 | return true; |
| 62 | } |
| 63 | |
| 64 | getPpath(b) { |
| 65 | return "Pacosako/" + b; |
| 66 | } |
| 67 | |
| 68 | getPPpath(m) { |
| 69 | if (ChessRules.PIECES.includes(m.appear[0].p)) return super.getPPpath(m); |
| 70 | // For an union, show only relevant piece: |
| 71 | // The color must be deduced from the move: reaching final rank of who? |
| 72 | const color = (m.appear[0].x == 0 ? 'w' : 'b'); |
| 73 | const up = this.getUnionPieces(m.appear[0].c, m.appear[0].p); |
| 74 | return "Pacosako/" + color + up[color]; |
| 75 | } |
| 76 | |
| 77 | canTake([x1, y1], [x2, y2]) { |
| 78 | const p1 = this.board[x1][y1].charAt(1); |
| 79 | if (!(ChessRules.PIECES.includes(p1))) return false; |
| 80 | const p2 = this.board[x2][y2].charAt(1); |
| 81 | if (!(ChessRules.PIECES.includes(p2))) return true; |
| 82 | const c1 = this.board[x1][y1].charAt(0); |
| 83 | const c2 = this.board[x2][y2].charAt(0); |
| 84 | return (c1 != c2); |
| 85 | } |
| 86 | |
| 87 | canIplay(side, [x, y]) { |
| 88 | return ( |
| 89 | this.turn == side && |
| 90 | ( |
| 91 | !(ChessRules.PIECES.includes(this.board[x][y].charAt(1))) || |
| 92 | this.board[x][y].charAt(0) == side |
| 93 | ) |
| 94 | ); |
| 95 | } |
| 96 | |
| 97 | scanKings(fen) { |
| 98 | this.kingPos = { w: [-1, -1], b: [-1, -1] }; |
| 99 | const fenRows = V.ParseFen(fen).position.split("/"); |
| 100 | const startRow = { 'w': V.size.x - 1, 'b': 0 }; |
| 101 | const kingSymb = ['k', 'g', 'm', 'u', 'x']; |
| 102 | for (let i = 0; i < fenRows.length; i++) { |
| 103 | let k = 0; |
| 104 | for (let j = 0; j < fenRows[i].length; j++) { |
| 105 | const c = fenRows[i].charAt(j); |
| 106 | if (kingSymb.includes(c)) |
| 107 | this.kingPos["b"] = [i, k]; |
| 108 | else if (kingSymb.some(s => c == s.toUpperCase())) |
| 109 | this.kingPos["w"] = [i, k]; |
| 110 | else { |
| 111 | const num = parseInt(fenRows[i].charAt(j), 10); |
| 112 | if (!isNaN(num)) k += num - 1; |
| 113 | } |
| 114 | k++; |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | setOtherVariables(fen) { |
| 120 | super.setOtherVariables(fen); |
| 121 | // Stack of "last move" only for intermediate chaining |
| 122 | this.lastMoveEnd = [null]; |
| 123 | // Local stack of non-capturing union moves: |
| 124 | this.umoves = []; |
| 125 | const umove = V.ParseFen(fen).umove; |
| 126 | if (umove == "-") this.umoves.push(null); |
| 127 | else { |
| 128 | this.umoves.push({ |
| 129 | start: ChessRules.SquareToCoords(umove.substr(0, 2)), |
| 130 | end: ChessRules.SquareToCoords(umove.substr(2)) |
| 131 | }); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | static IsGoodFen(fen) { |
| 136 | if (!ChessRules.IsGoodFen(fen)) return false; |
| 137 | const fenParts = fen.split(" "); |
| 138 | if (fenParts.length != 6) return false; |
| 139 | if (fenParts[5] != "-" && !fenParts[5].match(/^([a-h][1-8]){2}$/)) |
| 140 | return false; |
| 141 | return true; |
| 142 | } |
| 143 | |
| 144 | getUmove(move) { |
| 145 | if ( |
| 146 | move.vanish.length == 1 && |
| 147 | !(ChessRules.PIECES.includes(move.appear[0].p)) |
| 148 | ) { |
| 149 | // An union moving |
| 150 | return { start: move.start, end: move.end }; |
| 151 | } |
| 152 | return null; |
| 153 | } |
| 154 | |
| 155 | static ParseFen(fen) { |
| 156 | const fenParts = fen.split(" "); |
| 157 | return Object.assign( |
| 158 | ChessRules.ParseFen(fen), |
| 159 | { umove: fenParts[5] } |
| 160 | ); |
| 161 | } |
| 162 | |
| 163 | static GenRandInitFen(randomness) { |
| 164 | // Add empty umove |
| 165 | return ChessRules.GenRandInitFen(randomness) + " -"; |
| 166 | } |
| 167 | |
| 168 | getUmoveFen() { |
| 169 | const L = this.umoves.length; |
| 170 | return ( |
| 171 | !this.umoves[L - 1] |
| 172 | ? "-" |
| 173 | : ChessRules.CoordsToSquare(this.umoves[L - 1].start) + |
| 174 | ChessRules.CoordsToSquare(this.umoves[L - 1].end) |
| 175 | ); |
| 176 | } |
| 177 | |
| 178 | getFen() { |
| 179 | return super.getFen() + " " + this.getUmoveFen(); |
| 180 | } |
| 181 | |
| 182 | getFenForRepeat() { |
| 183 | return super.getFenForRepeat() + "_" + this.getUmoveFen(); |
| 184 | } |
| 185 | |
| 186 | getColor(i, j) { |
| 187 | const p = this.board[i][j].charAt(1); |
| 188 | if (ChessRules.PIECES.includes(p)) return super.getColor(i, j); |
| 189 | return this.turn; //union: I can use it, so it's "my" color... |
| 190 | } |
| 191 | |
| 192 | getPiece(i, j, color) { |
| 193 | const p = this.board[i][j].charAt(1); |
| 194 | if (ChessRules.PIECES.includes(p)) return p; |
| 195 | const c = this.board[i][j].charAt(0); |
| 196 | // NOTE: this.turn == HACK, but should work... |
| 197 | color = color || this.turn; |
| 198 | return V.UNIONS[p][c == color ? 0 : 1]; |
| 199 | } |
| 200 | |
| 201 | getUnionPieces(color, code) { |
| 202 | const pieces = V.UNIONS[code]; |
| 203 | return { |
| 204 | w: pieces[color == 'w' ? 0 : 1], |
| 205 | b: pieces[color == 'b' ? 0 : 1] |
| 206 | }; |
| 207 | } |
| 208 | |
| 209 | // p1: white piece, p2: black piece |
| 210 | getUnionCode(p1, p2) { |
| 211 | let uIdx = ( |
| 212 | Object.values(V.UNIONS).findIndex(v => v[0] == p1 && v[1] == p2) |
| 213 | ); |
| 214 | const c = (uIdx >= 0 ? 'w' : 'b'); |
| 215 | if (uIdx == -1) { |
| 216 | uIdx = ( |
| 217 | Object.values(V.UNIONS).findIndex(v => v[0] == p2 && v[1] == p1) |
| 218 | ); |
| 219 | } |
| 220 | return { c: c, p: Object.keys(V.UNIONS)[uIdx] }; |
| 221 | } |
| 222 | |
| 223 | getBasicMove([sx, sy], [ex, ey], tr) { |
| 224 | const L = this.lastMoveEnd.length; |
| 225 | const lm = this.lastMoveEnd[L-1]; |
| 226 | const piece = (!!lm ? lm.p : null); |
| 227 | const initColor = (!!piece ? this.turn : this.board[sx][sy].charAt(0)); |
| 228 | const initPiece = (piece || this.board[sx][sy].charAt(1)); |
| 229 | const c = this.turn; |
| 230 | const oppCol = V.GetOppCol(c); |
| 231 | if (!!tr && !(ChessRules.PIECES.includes(initPiece))) { |
| 232 | // Transformation computed without taking union into account |
| 233 | const up = this.getUnionPieces(initColor, initPiece); |
| 234 | let args = [tr.p, up[oppCol]]; |
| 235 | if (c == 'b') args = args.reverse(); |
| 236 | const cp = this.getUnionCode(args[0], args[1]); |
| 237 | tr.c = cp.c; |
| 238 | tr.p = cp.p; |
| 239 | } |
| 240 | // 4 cases : moving |
| 241 | // - union to free square (other cases are illegal: return null) |
| 242 | // - normal piece to free square, |
| 243 | // to enemy normal piece, or |
| 244 | // to union (releasing our piece) |
| 245 | let mv = new Move({ |
| 246 | start: { x: sx, y: sy }, |
| 247 | end: { x: ex, y: ey }, |
| 248 | vanish: [] |
| 249 | }); |
| 250 | if (!piece) { |
| 251 | mv.vanish = [ |
| 252 | new PiPo({ |
| 253 | x: sx, |
| 254 | y: sy, |
| 255 | c: initColor, |
| 256 | p: initPiece |
| 257 | }) |
| 258 | ]; |
| 259 | } |
| 260 | // Treat free square cases first: |
| 261 | if (this.board[ex][ey] == V.EMPTY) { |
| 262 | mv.appear = [ |
| 263 | new PiPo({ |
| 264 | x: ex, |
| 265 | y: ey, |
| 266 | c: !!tr ? tr.c : initColor, |
| 267 | p: !!tr ? tr.p : initPiece |
| 268 | }) |
| 269 | ]; |
| 270 | return mv; |
| 271 | } |
| 272 | // Now the two cases with union / release: |
| 273 | const destColor = this.board[ex][ey].charAt(0); |
| 274 | const destPiece = this.board[ex][ey].charAt(1); |
| 275 | mv.vanish.push( |
| 276 | new PiPo({ |
| 277 | x: ex, |
| 278 | y: ey, |
| 279 | c: destColor, |
| 280 | p: destPiece |
| 281 | }) |
| 282 | ); |
| 283 | if (ChessRules.PIECES.includes(destPiece)) { |
| 284 | // Normal piece: just create union |
| 285 | let args = [!!tr ? tr.p : initPiece, destPiece]; |
| 286 | if (c == 'b') args = args.reverse(); |
| 287 | const cp = this.getUnionCode(args[0], args[1]); |
| 288 | mv.appear = [ |
| 289 | new PiPo({ |
| 290 | x: ex, |
| 291 | y: ey, |
| 292 | c: cp.c, |
| 293 | p: cp.p |
| 294 | }) |
| 295 | ]; |
| 296 | return mv; |
| 297 | } |
| 298 | // Releasing a piece in an union: keep track of released piece |
| 299 | const up = this.getUnionPieces(destColor, destPiece); |
| 300 | let args = [!!tr ? tr.p : initPiece, up[oppCol]]; |
| 301 | if (c == 'b') args = args.reverse(); |
| 302 | const cp = this.getUnionCode(args[0], args[1]); |
| 303 | mv.appear = [ |
| 304 | new PiPo({ |
| 305 | x: ex, |
| 306 | y: ey, |
| 307 | c: cp.c, |
| 308 | p: cp.p |
| 309 | }) |
| 310 | ]; |
| 311 | mv.released = up[c]; |
| 312 | return mv; |
| 313 | } |
| 314 | |
| 315 | getPotentialMovesFrom([x, y]) { |
| 316 | const L = this.lastMoveEnd.length; |
| 317 | const lm = this.lastMoveEnd[L-1]; |
| 318 | if (!!lm && (x != lm.x || y != lm.y)) return []; |
| 319 | const piece = (!!lm ? lm.p : this.getPiece(x, y)); |
| 320 | if (!!lm) { |
| 321 | var saveSquare = this.board[x][y]; |
| 322 | this.board[x][y] = this.turn + piece; |
| 323 | } |
| 324 | let baseMoves = []; |
| 325 | switch (piece || this.getPiece(x, y)) { |
| 326 | case V.PAWN: |
| 327 | baseMoves = this.getPotentialPawnMoves([x, y]); |
| 328 | break; |
| 329 | case V.ROOK: |
| 330 | baseMoves = this.getPotentialRookMoves([x, y]); |
| 331 | break; |
| 332 | case V.KNIGHT: |
| 333 | baseMoves = this.getPotentialKnightMoves([x, y]); |
| 334 | break; |
| 335 | case V.BISHOP: |
| 336 | baseMoves = this.getPotentialBishopMoves([x, y]); |
| 337 | break; |
| 338 | case V.QUEEN: |
| 339 | baseMoves = this.getPotentialQueenMoves([x, y]); |
| 340 | break; |
| 341 | case V.KING: |
| 342 | baseMoves = this.getPotentialKingMoves([x, y]); |
| 343 | break; |
| 344 | } |
| 345 | // When a pawn in an union reaches final rank with a non-standard |
| 346 | // promotion move: apply promotion anyway |
| 347 | let moves = []; |
| 348 | const c = this.turn; |
| 349 | const oppCol = V.GetOppCol(c); |
| 350 | const oppLastRank = (c == 'w' ? 7 : 0); |
| 351 | baseMoves.forEach(m => { |
| 352 | if ( |
| 353 | m.end.x == oppLastRank && |
| 354 | ['c', 'd', 'e', 'f', 'g'].includes(m.appear[0].p) |
| 355 | ) { |
| 356 | // Move to first rank, which is last rank for opponent's pawn. |
| 357 | // => Show promotion choices. |
| 358 | // Find our piece in union (not a pawn) |
| 359 | const up = this.getUnionPieces(m.appear[0].c, m.appear[0].p); |
| 360 | // merge with all potential promotion pieces + push (loop) |
| 361 | for (let promotionPiece of [V.ROOK, V.KNIGHT, V.BISHOP, V.QUEEN]) { |
| 362 | let args = [up[c], promotionPiece]; |
| 363 | if (c == 'b') args = args.reverse(); |
| 364 | const cp = this.getUnionCode(args[0], args[1]); |
| 365 | let cpMove = JSON.parse(JSON.stringify(m)); |
| 366 | cpMove.appear[0].c = cp.c; |
| 367 | cpMove.appear[0].p = cp.p; |
| 368 | moves.push(cpMove); |
| 369 | } |
| 370 | } |
| 371 | else { |
| 372 | if ( |
| 373 | m.vanish.length > 0 && |
| 374 | m.vanish[0].p == V.PAWN && |
| 375 | m.start.y != m.end.y && |
| 376 | this.board[m.end.x][m.end.y] == V.EMPTY |
| 377 | ) { |
| 378 | if (!!lm) |
| 379 | // No en-passant inside a chaining |
| 380 | return; |
| 381 | // Fix en-passant capture: union type, maybe released piece too |
| 382 | const cs = [m.end.x + (c == 'w' ? 1 : -1), m.end.y]; |
| 383 | const color = this.board[cs[0]][cs[1]].charAt(0); |
| 384 | const code = this.board[cs[0]][cs[1]].charAt(1); |
| 385 | if (code == V.PAWN) { |
| 386 | // Simple en-passant capture (usual: just form union) |
| 387 | m.appear[0].c = 'w'; |
| 388 | m.appear[0].p = 'a'; |
| 389 | } |
| 390 | else { |
| 391 | // An union pawn + something juste moved two squares |
| 392 | const up = this.getUnionPieces(color, code); |
| 393 | m.released = up[c]; |
| 394 | let args = [V.PAWN, up[oppCol]]; |
| 395 | if (c == 'b') args = args.reverse(); |
| 396 | const cp = this.getUnionCode(args[0], args[1]); |
| 397 | m.appear[0].c = cp.c; |
| 398 | m.appear[0].p = cp.p; |
| 399 | } |
| 400 | } |
| 401 | moves.push(m); |
| 402 | } |
| 403 | }); |
| 404 | if (!!lm) this.board[x][y] = saveSquare; |
| 405 | return moves; |
| 406 | } |
| 407 | |
| 408 | getPotentialKingMoves(sq) { |
| 409 | let moves = this.getSlideNJumpMoves( |
| 410 | sq, |
| 411 | V.steps[V.ROOK].concat(V.steps[V.BISHOP]), |
| 412 | "oneStep" |
| 413 | ); |
| 414 | const c = this.turn; |
| 415 | const oppCol = V.GetOppCol(c); |
| 416 | if ( |
| 417 | !this.isAttacked(this.kingPos[c], oppCol) && |
| 418 | this.castleFlags[c].some(v => v < V.size.y) |
| 419 | ) { |
| 420 | moves = moves.concat(super.getCastleMoves(sq, null, true)); |
| 421 | } |
| 422 | return moves; |
| 423 | } |
| 424 | |
| 425 | getEpSquare(moveOrSquare) { |
| 426 | if (typeof moveOrSquare === "string") { |
| 427 | const square = moveOrSquare; |
| 428 | if (square == "-") return undefined; |
| 429 | return V.SquareToCoords(square); |
| 430 | } |
| 431 | const move = moveOrSquare; |
| 432 | const s = move.start, |
| 433 | e = move.end; |
| 434 | const oppCol = V.GetOppCol(this.turn); |
| 435 | if ( |
| 436 | s.y == e.y && |
| 437 | Math.abs(s.x - e.x) == 2 && |
| 438 | this.getPiece(s.x, s.y, oppCol) == V.PAWN |
| 439 | ) { |
| 440 | return { |
| 441 | x: (s.x + e.x) / 2, |
| 442 | y: s.y |
| 443 | }; |
| 444 | } |
| 445 | return undefined; |
| 446 | } |
| 447 | |
| 448 | // Does m2 un-do m1 ? (to disallow undoing union moves) |
| 449 | oppositeMoves(m1, m2) { |
| 450 | return ( |
| 451 | !!m1 && |
| 452 | !(ChessRules.PIECES.includes(m2.appear[0].p)) && |
| 453 | m2.vanish.length == 1 && |
| 454 | m1.start.x == m2.end.x && |
| 455 | m1.end.x == m2.start.x && |
| 456 | m1.start.y == m2.end.y && |
| 457 | m1.end.y == m2.start.y |
| 458 | ); |
| 459 | } |
| 460 | |
| 461 | // Do not consider checks for now (TODO) |
| 462 | underCheck() { |
| 463 | return false; |
| 464 | } |
| 465 | getCheckSquares() { |
| 466 | return []; |
| 467 | } |
| 468 | filterValid(moves) { |
| 469 | if (moves.length == 0) return []; |
| 470 | const L = this.umoves.length; //at least 1: init from FEN |
| 471 | return moves.filter(m => !this.oppositeMoves(this.umoves[L - 1], m)); |
| 472 | } |
| 473 | |
| 474 | play(move) { |
| 475 | move.flags = JSON.stringify(this.aggregateFlags()); |
| 476 | this.epSquares.push(this.getEpSquare(move)); |
| 477 | // Check if the move is the last of the turn: all cases except releases |
| 478 | if (!move.released) { |
| 479 | // No more union releases available |
| 480 | this.turn = V.GetOppCol(this.turn); |
| 481 | this.movesCount++; |
| 482 | this.lastMoveEnd.push(null); |
| 483 | } |
| 484 | else this.lastMoveEnd.push(Object.assign({ p: move.released }, move.end)); |
| 485 | V.PlayOnBoard(this.board, move); |
| 486 | this.umoves.push(this.getUmove(move)); |
| 487 | this.postPlay(move); |
| 488 | } |
| 489 | |
| 490 | postPlay(move) { |
| 491 | if (move.vanish.length == 0) |
| 492 | // A piece released just moved. Cannot be the king. |
| 493 | return; |
| 494 | const c = move.vanish[0].c; |
| 495 | const piece = move.vanish[0].p; |
| 496 | if (piece == V.KING) |
| 497 | this.kingPos[c] = [move.appear[0].x, move.appear[0].y]; |
| 498 | this.updateCastleFlags(move, piece); |
| 499 | } |
| 500 | |
| 501 | undo(move) { |
| 502 | this.epSquares.pop(); |
| 503 | this.disaggregateFlags(JSON.parse(move.flags)); |
| 504 | V.UndoOnBoard(this.board, move); |
| 505 | this.lastMoveEnd.pop(); |
| 506 | if (!move.released) { |
| 507 | this.turn = V.GetOppCol(this.turn); |
| 508 | this.movesCount--; |
| 509 | } |
| 510 | this.umoves.pop(); |
| 511 | this.postUndo(move); |
| 512 | } |
| 513 | |
| 514 | postUndo(move) { |
| 515 | if (this.getPiece(move.start.x, move.start.y) == V.KING) |
| 516 | this.kingPos[this.turn] = [move.start.x, move.start.y]; |
| 517 | } |
| 518 | |
| 519 | getCurrentScore() { |
| 520 | // Check kings: if one is dancing, the side lost |
| 521 | // But, if both dancing, let's say it's a draw :-) |
| 522 | const [kpW, kpB] = [this.kingPos['w'], this.kingPos['b']]; |
| 523 | const atKingPlace = [ |
| 524 | this.board[kpW[0]][kpW[1]].charAt(1), |
| 525 | this.board[kpB[0]][kpB[1]].charAt(1) |
| 526 | ]; |
| 527 | if (!atKingPlace.includes('k')) return "1/2"; |
| 528 | if (atKingPlace[0] != 'k') return "0-1"; |
| 529 | if (atKingPlace[1] != 'k') return "1-0"; |
| 530 | return "*"; |
| 531 | } |
| 532 | |
| 533 | getComputerMove() { |
| 534 | let initMoves = this.getAllValidMoves(); |
| 535 | if (initMoves.length == 0) return null; |
| 536 | // Loop until valid move is found (no blocked pawn released...) |
| 537 | while (true) { |
| 538 | let moves = JSON.parse(JSON.stringify(initMoves)); |
| 539 | let mvArray = []; |
| 540 | let mv = null; |
| 541 | // Just play random moves (for now at least. TODO?) |
| 542 | while (moves.length > 0) { |
| 543 | mv = moves[randInt(moves.length)]; |
| 544 | mvArray.push(mv); |
| 545 | this.play(mv); |
| 546 | if (!!mv.released) |
| 547 | // A piece was just released from an union |
| 548 | moves = this.getPotentialMovesFrom([mv.end.x, mv.end.y]); |
| 549 | else break; |
| 550 | } |
| 551 | for (let i = mvArray.length - 1; i >= 0; i--) this.undo(mvArray[i]); |
| 552 | if (!mv.released) return (mvArray.length > 1 ? mvArray : mvArray[0]); |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | // NOTE: evalPosition() is wrong, but unused since bot plays at random |
| 557 | |
| 558 | getNotation(move) { |
| 559 | if (move.appear.length == 2 && move.appear[0].p == V.KING) |
| 560 | return (move.end.y < move.start.y ? "0-0-0" : "0-0"); |
| 561 | |
| 562 | const c = this.turn; |
| 563 | const L = this.lastMoveEnd.length; |
| 564 | const lm = this.lastMoveEnd[L-1]; |
| 565 | let piece = null; |
| 566 | if (!lm && move.vanish.length == 0) |
| 567 | // When importing a game, the info move.released is lost |
| 568 | piece = move.appear[0].p; |
| 569 | else piece = (!!lm ? lm.p : move.vanish[0].p); |
| 570 | if (!(ChessRules.PIECES.includes(piece))) { |
| 571 | // Decode (moving) union |
| 572 | const up = this.getUnionPieces( |
| 573 | move.vanish.length > 0 ? move.vanish[0].c : move.appear[0].c, piece); |
| 574 | piece = up[c] |
| 575 | } |
| 576 | |
| 577 | // Basic move notation: |
| 578 | let notation = piece.toUpperCase(); |
| 579 | if ( |
| 580 | this.board[move.end.x][move.end.y] != V.EMPTY || |
| 581 | (piece == V.PAWN && move.start.y != move.end.y) |
| 582 | ) { |
| 583 | notation += "x"; |
| 584 | } |
| 585 | const finalSquare = V.CoordsToSquare(move.end); |
| 586 | notation += finalSquare; |
| 587 | |
| 588 | // Add potential promotion indications: |
| 589 | const firstLastRank = (c == 'w' ? [7, 0] : [0, 7]); |
| 590 | if (move.end.x == firstLastRank[1] && piece == V.PAWN) { |
| 591 | const up = this.getUnionPieces(move.appear[0].c, move.appear[0].p); |
| 592 | notation += "=" + up[c].toUpperCase(); |
| 593 | } |
| 594 | else if ( |
| 595 | move.end.x == firstLastRank[0] && |
| 596 | move.vanish.length > 0 && |
| 597 | ['c', 'd', 'e', 'f', 'g'].includes(move.vanish[0].p) |
| 598 | ) { |
| 599 | // We promoted an opponent's pawn |
| 600 | const oppCol = V.GetOppCol(c); |
| 601 | const up = this.getUnionPieces(move.appear[0].c, move.appear[0].p); |
| 602 | notation += "=" + up[oppCol].toUpperCase(); |
| 603 | } |
| 604 | |
| 605 | return notation; |
| 606 | } |
| 607 | |
| 608 | }; |