| 1 | import { ArrayFun } from "@/utils/array"; |
| 2 | import { randInt } from "@/utils/alea"; |
| 3 | import { ChessRules, PiPo, Move } from "@/base_rules"; |
| 4 | |
| 5 | export class EightpiecesRules extends ChessRules { |
| 6 | static get JAILER() { |
| 7 | return "j"; |
| 8 | } |
| 9 | static get SENTRY() { |
| 10 | return "s"; |
| 11 | } |
| 12 | static get LANCER() { |
| 13 | return "l"; |
| 14 | } |
| 15 | |
| 16 | static get IMAGE_EXTENSION() { |
| 17 | // Temporarily, for the time SVG pieces are being designed: |
| 18 | return ".png"; |
| 19 | } |
| 20 | |
| 21 | // Lancer directions *from white perspective* |
| 22 | static get LANCER_DIRS() { |
| 23 | return { |
| 24 | 'c': [-1, 0], //north |
| 25 | 'd': [-1, 1], //N-E |
| 26 | 'e': [0, 1], //east |
| 27 | 'f': [1, 1], //S-E |
| 28 | 'g': [1, 0], //south |
| 29 | 'h': [1, -1], //S-W |
| 30 | 'm': [0, -1], //west |
| 31 | 'o': [-1, -1] //N-W |
| 32 | }; |
| 33 | } |
| 34 | |
| 35 | static get PIECES() { |
| 36 | return ChessRules.PIECES |
| 37 | .concat([V.JAILER, V.SENTRY]) |
| 38 | .concat(Object.keys(V.LANCER_DIRS)); |
| 39 | } |
| 40 | |
| 41 | getPiece(i, j) { |
| 42 | const piece = this.board[i][j].charAt(1); |
| 43 | // Special lancer case: 8 possible orientations |
| 44 | if (Object.keys(V.LANCER_DIRS).includes(piece)) return V.LANCER; |
| 45 | return piece; |
| 46 | } |
| 47 | |
| 48 | getPpath(b, color, score, orientation) { |
| 49 | if ([V.JAILER, V.SENTRY].includes(b[1])) return "Eightpieces/tmp_png/" + b; |
| 50 | if (Object.keys(V.LANCER_DIRS).includes(b[1])) { |
| 51 | if (orientation == 'w') return "Eightpieces/tmp_png/" + b; |
| 52 | // Find opposite direction for adequate display: |
| 53 | let oppDir = ''; |
| 54 | switch (b[1]) { |
| 55 | case 'c': |
| 56 | oppDir = 'g'; |
| 57 | break; |
| 58 | case 'g': |
| 59 | oppDir = 'c'; |
| 60 | break; |
| 61 | case 'd': |
| 62 | oppDir = 'h'; |
| 63 | break; |
| 64 | case 'h': |
| 65 | oppDir = 'd'; |
| 66 | break; |
| 67 | case 'e': |
| 68 | oppDir = 'm'; |
| 69 | break; |
| 70 | case 'm': |
| 71 | oppDir = 'e'; |
| 72 | break; |
| 73 | case 'f': |
| 74 | oppDir = 'o'; |
| 75 | break; |
| 76 | case 'o': |
| 77 | oppDir = 'f'; |
| 78 | break; |
| 79 | } |
| 80 | return "Eightpieces/tmp_png/" + b[0] + oppDir; |
| 81 | } |
| 82 | // TODO: after we have SVG pieces, remove the folder and next prefix: |
| 83 | return "Eightpieces/tmp_png/" + b; |
| 84 | } |
| 85 | |
| 86 | getPPpath(m, orientation) { |
| 87 | return ( |
| 88 | this.getPpath( |
| 89 | m.appear[0].c + m.appear[0].p, |
| 90 | null, |
| 91 | null, |
| 92 | orientation |
| 93 | ) |
| 94 | ); |
| 95 | } |
| 96 | |
| 97 | static ParseFen(fen) { |
| 98 | const fenParts = fen.split(" "); |
| 99 | return Object.assign( |
| 100 | ChessRules.ParseFen(fen), |
| 101 | { sentrypush: fenParts[5] } |
| 102 | ); |
| 103 | } |
| 104 | |
| 105 | static IsGoodFen(fen) { |
| 106 | if (!ChessRules.IsGoodFen(fen)) return false; |
| 107 | const fenParsed = V.ParseFen(fen); |
| 108 | // 5) Check sentry push (if any) |
| 109 | if ( |
| 110 | fenParsed.sentrypush != "-" && |
| 111 | !fenParsed.sentrypush.match(/^([a-h][1-8]){2,2}$/) |
| 112 | ) { |
| 113 | return false; |
| 114 | } |
| 115 | return true; |
| 116 | } |
| 117 | |
| 118 | getFen() { |
| 119 | return super.getFen() + " " + this.getSentrypushFen(); |
| 120 | } |
| 121 | |
| 122 | getFenForRepeat() { |
| 123 | return super.getFenForRepeat() + "_" + this.getSentrypushFen(); |
| 124 | } |
| 125 | |
| 126 | getSentrypushFen() { |
| 127 | const L = this.sentryPush.length; |
| 128 | if (!this.sentryPush[L-1]) return "-"; |
| 129 | let res = ""; |
| 130 | const spL = this.sentryPush[L-1].length; |
| 131 | // Condensate path: just need initial and final squares: |
| 132 | return [0, spL - 1] |
| 133 | .map(i => V.CoordsToSquare(this.sentryPush[L-1][i])) |
| 134 | .join(""); |
| 135 | } |
| 136 | |
| 137 | setOtherVariables(fen) { |
| 138 | super.setOtherVariables(fen); |
| 139 | // subTurn == 2 only when a sentry moved, and is about to push something |
| 140 | this.subTurn = 1; |
| 141 | // Sentry position just after a "capture" (subTurn from 1 to 2) |
| 142 | this.sentryPos = null; |
| 143 | // Stack pieces' forbidden squares after a sentry move at each turn |
| 144 | const parsedFen = V.ParseFen(fen); |
| 145 | if (parsedFen.sentrypush == "-") this.sentryPush = [null]; |
| 146 | else { |
| 147 | // Expand init + dest squares into a full path: |
| 148 | const init = V.SquareToCoords(parsedFen.sentrypush.substr(0, 2)), |
| 149 | dest = V.SquareToCoords(parsedFen.sentrypush.substr(2)); |
| 150 | let newPath = [init]; |
| 151 | const delta = ['x', 'y'].map(i => Math.abs(dest[i] - init[i])); |
| 152 | // Check that it's not a knight movement: |
| 153 | if (delta[0] == 0 || delta[1] == 0 || delta[0] == delta[1]) { |
| 154 | const step = ['x', 'y'].map((i, idx) => { |
| 155 | return (dest[i] - init[i]) / delta[idx] || 0 |
| 156 | }); |
| 157 | let x = init.x + step[0], |
| 158 | y = init.y + step[1]; |
| 159 | while (x != dest.x || y != dest.y) { |
| 160 | newPath.push({ x: x, y: y }); |
| 161 | x += step[0]; |
| 162 | y += step[1]; |
| 163 | } |
| 164 | } |
| 165 | newPath.push(dest); |
| 166 | this.sentryPush = [newPath]; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | static GenRandInitFen(randomness) { |
| 171 | if (randomness == 0) |
| 172 | // Deterministic: |
| 173 | return "jfsqkbnr/pppppppp/8/8/8/8/PPPPPPPP/JDSQKBNR w 0 ahah - -"; |
| 174 | |
| 175 | let pieces = { w: new Array(8), b: new Array(8) }; |
| 176 | let flags = ""; |
| 177 | // Shuffle pieces on first (and last rank if randomness == 2) |
| 178 | for (let c of ["w", "b"]) { |
| 179 | if (c == 'b' && randomness == 1) { |
| 180 | const lancerIdx = pieces['w'].findIndex(p => { |
| 181 | return Object.keys(V.LANCER_DIRS).includes(p); |
| 182 | }); |
| 183 | pieces['b'] = |
| 184 | pieces['w'].slice(0, lancerIdx) |
| 185 | .concat(['g']) |
| 186 | .concat(pieces['w'].slice(lancerIdx + 1)); |
| 187 | flags += flags; |
| 188 | break; |
| 189 | } |
| 190 | |
| 191 | let positions = ArrayFun.range(8); |
| 192 | |
| 193 | // Get random squares for bishop and sentry |
| 194 | let randIndex = 2 * randInt(4); |
| 195 | let bishopPos = positions[randIndex]; |
| 196 | // The sentry must be on a square of different color |
| 197 | let randIndex_tmp = 2 * randInt(4) + 1; |
| 198 | let sentryPos = positions[randIndex_tmp]; |
| 199 | if (c == 'b') { |
| 200 | // Check if white sentry is on the same color as ours. |
| 201 | // If yes: swap bishop and sentry positions. |
| 202 | // NOTE: test % 2 == 1 because there are 7 slashes. |
| 203 | if ((pieces['w'].indexOf('s') - sentryPos) % 2 == 1) |
| 204 | [bishopPos, sentryPos] = [sentryPos, bishopPos]; |
| 205 | } |
| 206 | positions.splice(Math.max(randIndex, randIndex_tmp), 1); |
| 207 | positions.splice(Math.min(randIndex, randIndex_tmp), 1); |
| 208 | |
| 209 | // Get random squares for knight and lancer |
| 210 | randIndex = randInt(6); |
| 211 | const knightPos = positions[randIndex]; |
| 212 | positions.splice(randIndex, 1); |
| 213 | randIndex = randInt(5); |
| 214 | const lancerPos = positions[randIndex]; |
| 215 | positions.splice(randIndex, 1); |
| 216 | |
| 217 | // Get random square for queen |
| 218 | randIndex = randInt(4); |
| 219 | const queenPos = positions[randIndex]; |
| 220 | positions.splice(randIndex, 1); |
| 221 | |
| 222 | // Rook, jailer and king positions are now almost fixed, |
| 223 | // only the ordering rook->jailer or jailer->rook must be decided. |
| 224 | let rookPos = positions[0]; |
| 225 | let jailerPos = positions[2]; |
| 226 | const kingPos = positions[1]; |
| 227 | flags += V.CoordToColumn(rookPos) + V.CoordToColumn(jailerPos); |
| 228 | if (Math.random() < 0.5) [rookPos, jailerPos] = [jailerPos, rookPos]; |
| 229 | |
| 230 | pieces[c][rookPos] = "r"; |
| 231 | pieces[c][knightPos] = "n"; |
| 232 | pieces[c][bishopPos] = "b"; |
| 233 | pieces[c][queenPos] = "q"; |
| 234 | pieces[c][kingPos] = "k"; |
| 235 | pieces[c][sentryPos] = "s"; |
| 236 | // Lancer faces north for white, and south for black: |
| 237 | pieces[c][lancerPos] = c == 'w' ? 'c' : 'g'; |
| 238 | pieces[c][jailerPos] = "j"; |
| 239 | } |
| 240 | return ( |
| 241 | pieces["b"].join("") + |
| 242 | "/pppppppp/8/8/8/8/PPPPPPPP/" + |
| 243 | pieces["w"].join("").toUpperCase() + |
| 244 | " w 0 " + flags + " - -" |
| 245 | ); |
| 246 | } |
| 247 | |
| 248 | canTake([x1, y1], [x2, y2]) { |
| 249 | if (this.subTurn == 2) |
| 250 | // Only self captures on this subturn: |
| 251 | return this.getColor(x1, y1) == this.getColor(x2, y2); |
| 252 | return super.canTake([x1, y1], [x2, y2]); |
| 253 | } |
| 254 | |
| 255 | // Is piece on square (x,y) immobilized? |
| 256 | isImmobilized([x, y]) { |
| 257 | const color = this.getColor(x, y); |
| 258 | const oppCol = V.GetOppCol(color); |
| 259 | for (let step of V.steps[V.ROOK]) { |
| 260 | const [i, j] = [x + step[0], y + step[1]]; |
| 261 | if ( |
| 262 | V.OnBoard(i, j) && |
| 263 | this.board[i][j] != V.EMPTY && |
| 264 | this.getColor(i, j) == oppCol |
| 265 | ) { |
| 266 | if (this.getPiece(i, j) == V.JAILER) return [i, j]; |
| 267 | } |
| 268 | } |
| 269 | return null; |
| 270 | } |
| 271 | |
| 272 | // Because of the lancers, getPiece() could be wrong: |
| 273 | // use board[x][y][1] instead (always valid). |
| 274 | getBasicMove([sx, sy], [ex, ey], tr) { |
| 275 | const initColor = this.getColor(sx, sy); |
| 276 | const initPiece = this.board[sx][sy].charAt(1); |
| 277 | let mv = new Move({ |
| 278 | appear: [ |
| 279 | new PiPo({ |
| 280 | x: ex, |
| 281 | y: ey, |
| 282 | c: tr ? tr.c : initColor, |
| 283 | p: tr ? tr.p : initPiece |
| 284 | }) |
| 285 | ], |
| 286 | vanish: [ |
| 287 | new PiPo({ |
| 288 | x: sx, |
| 289 | y: sy, |
| 290 | c: initColor, |
| 291 | p: initPiece |
| 292 | }) |
| 293 | ] |
| 294 | }); |
| 295 | |
| 296 | // The opponent piece disappears if we take it |
| 297 | if (this.board[ex][ey] != V.EMPTY) { |
| 298 | mv.vanish.push( |
| 299 | new PiPo({ |
| 300 | x: ex, |
| 301 | y: ey, |
| 302 | c: this.getColor(ex, ey), |
| 303 | p: this.board[ex][ey].charAt(1) |
| 304 | }) |
| 305 | ); |
| 306 | } |
| 307 | |
| 308 | return mv; |
| 309 | } |
| 310 | |
| 311 | canIplay(side, [x, y]) { |
| 312 | return ( |
| 313 | (this.subTurn == 1 && this.turn == side && this.getColor(x, y) == side) |
| 314 | || |
| 315 | (this.subTurn == 2 && x == this.sentryPos.x && y == this.sentryPos.y) |
| 316 | ); |
| 317 | } |
| 318 | |
| 319 | getPotentialMovesFrom([x, y]) { |
| 320 | const piece = this.getPiece(x, y); |
| 321 | const L = this.sentryPush.length; |
| 322 | // At subTurn == 2, jailers aren't effective (Jeff K) |
| 323 | if (this.subTurn == 1) { |
| 324 | const jsq = this.isImmobilized([x, y]); |
| 325 | if (!!jsq) { |
| 326 | let moves = []; |
| 327 | // Special pass move if king: |
| 328 | if (piece == V.KING) { |
| 329 | moves.push( |
| 330 | new Move({ |
| 331 | appear: [], |
| 332 | vanish: [], |
| 333 | start: { x: x, y: y }, |
| 334 | end: { x: jsq[0], y: jsq[1] } |
| 335 | }) |
| 336 | ); |
| 337 | } |
| 338 | else if (piece == V.LANCER && !!this.sentryPush[L-1]) { |
| 339 | // A pushed lancer next to the jailer: reorient |
| 340 | const color = this.getColor(x, y); |
| 341 | const curDir = this.board[x][y].charAt(1); |
| 342 | Object.keys(V.LANCER_DIRS).forEach(k => { |
| 343 | moves.push( |
| 344 | new Move({ |
| 345 | appear: [{ x: x, y: y, c: color, p: k }], |
| 346 | vanish: [{ x: x, y: y, c: color, p: curDir }], |
| 347 | start: { x: x, y: y }, |
| 348 | end: { x: jsq[0], y: jsq[1] } |
| 349 | }) |
| 350 | ); |
| 351 | }); |
| 352 | } |
| 353 | return moves; |
| 354 | } |
| 355 | } |
| 356 | let moves = []; |
| 357 | switch (piece) { |
| 358 | case V.JAILER: |
| 359 | moves = this.getPotentialJailerMoves([x, y]); |
| 360 | break; |
| 361 | case V.SENTRY: |
| 362 | moves = this.getPotentialSentryMoves([x, y]); |
| 363 | break; |
| 364 | case V.LANCER: |
| 365 | moves = this.getPotentialLancerMoves([x, y]); |
| 366 | break; |
| 367 | default: |
| 368 | moves = super.getPotentialMovesFrom([x, y]); |
| 369 | break; |
| 370 | } |
| 371 | if (!!this.sentryPush[L-1]) { |
| 372 | // Delete moves walking back on sentry push path, |
| 373 | // only if not a pawn, and the piece is the pushed one. |
| 374 | const pl = this.sentryPush[L-1].length; |
| 375 | const finalPushedSq = this.sentryPush[L-1][pl-1]; |
| 376 | moves = moves.filter(m => { |
| 377 | if ( |
| 378 | m.vanish[0].p != V.PAWN && |
| 379 | m.start.x == finalPushedSq.x && m.start.y == finalPushedSq.y && |
| 380 | this.sentryPush[L-1].some(sq => sq.x == m.end.x && sq.y == m.end.y) |
| 381 | ) { |
| 382 | return false; |
| 383 | } |
| 384 | return true; |
| 385 | }); |
| 386 | } else if (this.subTurn == 2) { |
| 387 | // Put back the sentinel on board: |
| 388 | const color = this.turn; |
| 389 | moves.forEach(m => { |
| 390 | m.appear.push({x: x, y: y, p: V.SENTRY, c: color}); |
| 391 | }); |
| 392 | } |
| 393 | return moves; |
| 394 | } |
| 395 | |
| 396 | getPotentialPawnMoves([x, y]) { |
| 397 | const color = this.getColor(x, y); |
| 398 | let moves = []; |
| 399 | const [sizeX, sizeY] = [V.size.x, V.size.y]; |
| 400 | let shiftX = (color == "w" ? -1 : 1); |
| 401 | if (this.subTurn == 2) shiftX *= -1; |
| 402 | const firstRank = color == "w" ? sizeX - 1 : 0; |
| 403 | const startRank = color == "w" ? sizeX - 2 : 1; |
| 404 | const lastRank = color == "w" ? 0 : sizeX - 1; |
| 405 | |
| 406 | // Pawns might be pushed on 1st rank and attempt to move again: |
| 407 | if (!V.OnBoard(x + shiftX, y)) return []; |
| 408 | |
| 409 | const finalPieces = |
| 410 | // A push cannot put a pawn on last rank (it goes backward) |
| 411 | x + shiftX == lastRank |
| 412 | ? Object.keys(V.LANCER_DIRS).concat( |
| 413 | [V.ROOK, V.KNIGHT, V.BISHOP, V.QUEEN, V.SENTRY, V.JAILER]) |
| 414 | : [V.PAWN]; |
| 415 | if (this.board[x + shiftX][y] == V.EMPTY) { |
| 416 | // One square forward |
| 417 | for (let piece of finalPieces) { |
| 418 | moves.push( |
| 419 | this.getBasicMove([x, y], [x + shiftX, y], { |
| 420 | c: color, |
| 421 | p: piece |
| 422 | }) |
| 423 | ); |
| 424 | } |
| 425 | if ( |
| 426 | // 2-squares jumps forbidden if pawn push |
| 427 | this.subTurn == 1 && |
| 428 | [startRank, firstRank].includes(x) && |
| 429 | this.board[x + 2 * shiftX][y] == V.EMPTY |
| 430 | ) { |
| 431 | // Two squares jump |
| 432 | moves.push(this.getBasicMove([x, y], [x + 2 * shiftX, y])); |
| 433 | } |
| 434 | } |
| 435 | // Captures |
| 436 | for (let shiftY of [-1, 1]) { |
| 437 | if ( |
| 438 | y + shiftY >= 0 && |
| 439 | y + shiftY < sizeY && |
| 440 | this.board[x + shiftX][y + shiftY] != V.EMPTY && |
| 441 | this.canTake([x, y], [x + shiftX, y + shiftY]) |
| 442 | ) { |
| 443 | for (let piece of finalPieces) { |
| 444 | moves.push( |
| 445 | this.getBasicMove([x, y], [x + shiftX, y + shiftY], { |
| 446 | c: color, |
| 447 | p: piece |
| 448 | }) |
| 449 | ); |
| 450 | } |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | // En passant: only on subTurn == 1 |
| 455 | const Lep = this.epSquares.length; |
| 456 | const epSquare = this.epSquares[Lep - 1]; |
| 457 | if ( |
| 458 | this.subTurn == 1 && |
| 459 | !!epSquare && |
| 460 | epSquare.x == x + shiftX && |
| 461 | Math.abs(epSquare.y - y) == 1 |
| 462 | ) { |
| 463 | let enpassantMove = this.getBasicMove([x, y], [epSquare.x, epSquare.y]); |
| 464 | enpassantMove.vanish.push({ |
| 465 | x: x, |
| 466 | y: epSquare.y, |
| 467 | p: "p", |
| 468 | c: this.getColor(x, epSquare.y) |
| 469 | }); |
| 470 | moves.push(enpassantMove); |
| 471 | } |
| 472 | |
| 473 | return moves; |
| 474 | } |
| 475 | |
| 476 | doClick(square) { |
| 477 | if (isNaN(square[0])) return null; |
| 478 | const L = this.sentryPush.length; |
| 479 | const [x, y] = [square[0], square[1]]; |
| 480 | const color = this.turn; |
| 481 | if ( |
| 482 | this.subTurn == 2 || |
| 483 | this.board[x][y] == V.EMPTY || |
| 484 | this.getPiece(x, y) != V.LANCER || |
| 485 | this.getColor(x, y) != color || |
| 486 | !!this.sentryPush[L-1] |
| 487 | ) { |
| 488 | return null; |
| 489 | } |
| 490 | // Stuck lancer? |
| 491 | const orientation = this.board[x][y][1]; |
| 492 | const step = V.LANCER_DIRS[orientation]; |
| 493 | if (!V.OnBoard(x + step[0], y + step[1])) { |
| 494 | let choices = []; |
| 495 | Object.keys(V.LANCER_DIRS).forEach(k => { |
| 496 | const dir = V.LANCER_DIRS[k]; |
| 497 | if ( |
| 498 | (dir[0] != step[0] || dir[1] != step[1]) && |
| 499 | V.OnBoard(x + dir[0], y + dir[1]) |
| 500 | ) { |
| 501 | choices.push( |
| 502 | new Move({ |
| 503 | vanish: [ |
| 504 | new PiPo({ |
| 505 | x: x, |
| 506 | y: y, |
| 507 | c: color, |
| 508 | p: orientation |
| 509 | }) |
| 510 | ], |
| 511 | appear: [ |
| 512 | new PiPo({ |
| 513 | x: x, |
| 514 | y: y, |
| 515 | c: color, |
| 516 | p: k |
| 517 | }) |
| 518 | ], |
| 519 | start: { x: x, y : y }, |
| 520 | end: { x: -1, y: -1 } |
| 521 | }) |
| 522 | ); |
| 523 | } |
| 524 | }); |
| 525 | return choices; |
| 526 | } |
| 527 | return null; |
| 528 | } |
| 529 | |
| 530 | // Obtain all lancer moves in "step" direction |
| 531 | getPotentialLancerMoves_aux([x, y], step, tr) { |
| 532 | let moves = []; |
| 533 | // Add all moves to vacant squares until opponent is met: |
| 534 | const color = this.getColor(x, y); |
| 535 | const oppCol = |
| 536 | this.subTurn == 1 |
| 537 | ? V.GetOppCol(color) |
| 538 | // at subTurn == 2, consider own pieces as opponent |
| 539 | : color; |
| 540 | let sq = [x + step[0], y + step[1]]; |
| 541 | while (V.OnBoard(sq[0], sq[1]) && this.getColor(sq[0], sq[1]) != oppCol) { |
| 542 | if (this.board[sq[0]][sq[1]] == V.EMPTY) |
| 543 | moves.push(this.getBasicMove([x, y], sq, tr)); |
| 544 | sq[0] += step[0]; |
| 545 | sq[1] += step[1]; |
| 546 | } |
| 547 | if (V.OnBoard(sq[0], sq[1])) |
| 548 | // Add capturing move |
| 549 | moves.push(this.getBasicMove([x, y], sq, tr)); |
| 550 | return moves; |
| 551 | } |
| 552 | |
| 553 | getPotentialLancerMoves([x, y]) { |
| 554 | let moves = []; |
| 555 | // Add all lancer possible orientations, similar to pawn promotions. |
| 556 | // Except if just after a push: allow all movements from init square then |
| 557 | const L = this.sentryPush.length; |
| 558 | const color = this.getColor(x, y); |
| 559 | const dirCode = this.board[x][y][1]; |
| 560 | const curDir = V.LANCER_DIRS[dirCode]; |
| 561 | if (!!this.sentryPush[L-1]) { |
| 562 | // Maybe I was pushed |
| 563 | const pl = this.sentryPush[L-1].length; |
| 564 | if ( |
| 565 | this.sentryPush[L-1][pl-1].x == x && |
| 566 | this.sentryPush[L-1][pl-1].y == y |
| 567 | ) { |
| 568 | // I was pushed: allow all directions (for this move only), but |
| 569 | // do not change direction after moving, *except* if I keep the |
| 570 | // same orientation in which I was pushed. |
| 571 | // Also allow simple reorientation ("capturing king"): |
| 572 | if (!V.OnBoard(x + curDir[0], y + curDir[1])) { |
| 573 | const kp = this.kingPos[color]; |
| 574 | let reorientMoves = []; |
| 575 | Object.keys(V.LANCER_DIRS).forEach(k => { |
| 576 | const dir = V.LANCER_DIRS[k]; |
| 577 | if ( |
| 578 | (dir[0] != curDir[0] || dir[1] != curDir[1]) && |
| 579 | V.OnBoard(x + dir[0], y + dir[1]) |
| 580 | ) { |
| 581 | reorientMoves.push( |
| 582 | new Move({ |
| 583 | vanish: [ |
| 584 | new PiPo({ |
| 585 | x: x, |
| 586 | y: y, |
| 587 | c: color, |
| 588 | p: dirCode |
| 589 | }) |
| 590 | ], |
| 591 | appear: [ |
| 592 | new PiPo({ |
| 593 | x: x, |
| 594 | y: y, |
| 595 | c: color, |
| 596 | p: k |
| 597 | }) |
| 598 | ], |
| 599 | start: { x: x, y : y }, |
| 600 | end: { x: kp[0], y: kp[1] } |
| 601 | }) |
| 602 | ); |
| 603 | } |
| 604 | }); |
| 605 | Array.prototype.push.apply(moves, reorientMoves); |
| 606 | } |
| 607 | Object.values(V.LANCER_DIRS).forEach(step => { |
| 608 | const dirCode = Object.keys(V.LANCER_DIRS).find(k => { |
| 609 | return ( |
| 610 | V.LANCER_DIRS[k][0] == step[0] && |
| 611 | V.LANCER_DIRS[k][1] == step[1] |
| 612 | ); |
| 613 | }); |
| 614 | const dirMoves = |
| 615 | this.getPotentialLancerMoves_aux( |
| 616 | [x, y], |
| 617 | step, |
| 618 | { p: dirCode, c: color } |
| 619 | ); |
| 620 | if (curDir[0] == step[0] && curDir[1] == step[1]) { |
| 621 | // Keeping same orientation: can choose after |
| 622 | let chooseMoves = []; |
| 623 | dirMoves.forEach(m => { |
| 624 | Object.keys(V.LANCER_DIRS).forEach(k => { |
| 625 | let mk = JSON.parse(JSON.stringify(m)); |
| 626 | mk.appear[0].p = k; |
| 627 | moves.push(mk); |
| 628 | }); |
| 629 | }); |
| 630 | Array.prototype.push.apply(moves, chooseMoves); |
| 631 | } else Array.prototype.push.apply(moves, dirMoves); |
| 632 | }); |
| 633 | return moves; |
| 634 | } |
| 635 | } |
| 636 | // I wasn't pushed: standard lancer move |
| 637 | const monodirMoves = |
| 638 | this.getPotentialLancerMoves_aux([x, y], V.LANCER_DIRS[dirCode]); |
| 639 | // Add all possible orientations aftermove except if I'm being pushed |
| 640 | if (this.subTurn == 1) { |
| 641 | monodirMoves.forEach(m => { |
| 642 | Object.keys(V.LANCER_DIRS).forEach(k => { |
| 643 | let mk = JSON.parse(JSON.stringify(m)); |
| 644 | mk.appear[0].p = k; |
| 645 | moves.push(mk); |
| 646 | }); |
| 647 | }); |
| 648 | return moves; |
| 649 | } else { |
| 650 | // I'm pushed: add potential nudges, except for current orientation |
| 651 | let potentialNudges = []; |
| 652 | for (let step of V.steps[V.ROOK].concat(V.steps[V.BISHOP])) { |
| 653 | if ( |
| 654 | (step[0] != curDir[0] || step[1] != curDir[1]) && |
| 655 | V.OnBoard(x + step[0], y + step[1]) && |
| 656 | this.board[x + step[0]][y + step[1]] == V.EMPTY |
| 657 | ) { |
| 658 | const newDirCode = Object.keys(V.LANCER_DIRS).find(k => { |
| 659 | const codeStep = V.LANCER_DIRS[k]; |
| 660 | return (codeStep[0] == step[0] && codeStep[1] == step[1]); |
| 661 | }); |
| 662 | potentialNudges.push( |
| 663 | this.getBasicMove( |
| 664 | [x, y], |
| 665 | [x + step[0], y + step[1]], |
| 666 | { c: color, p: newDirCode } |
| 667 | ) |
| 668 | ); |
| 669 | } |
| 670 | } |
| 671 | return monodirMoves.concat(potentialNudges); |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | getPotentialSentryMoves([x, y]) { |
| 676 | // The sentry moves a priori like a bishop: |
| 677 | let moves = super.getPotentialBishopMoves([x, y]); |
| 678 | // ...but captures are replaced by special move, if and only if |
| 679 | // "captured" piece can move now, considered as the capturer unit. |
| 680 | // --> except is subTurn == 2, in this case I don't push anything. |
| 681 | if (this.subTurn == 2) return moves.filter(m => m.vanish.length == 1); |
| 682 | moves.forEach(m => { |
| 683 | if (m.vanish.length == 2) { |
| 684 | // Temporarily cancel the sentry capture: |
| 685 | m.appear.pop(); |
| 686 | m.vanish.pop(); |
| 687 | } |
| 688 | }); |
| 689 | const color = this.getColor(x, y); |
| 690 | const fMoves = moves.filter(m => { |
| 691 | // Can the pushed unit make any move? ...resulting in a non-self-check? |
| 692 | if (m.appear.length == 0) { |
| 693 | let res = false; |
| 694 | this.play(m); |
| 695 | let moves2 = this.getPotentialMovesFrom([m.end.x, m.end.y]); |
| 696 | for (let m2 of moves2) { |
| 697 | this.play(m2); |
| 698 | res = !this.underCheck(color); |
| 699 | this.undo(m2); |
| 700 | if (res) break; |
| 701 | } |
| 702 | this.undo(m); |
| 703 | return res; |
| 704 | } |
| 705 | return true; |
| 706 | }); |
| 707 | return fMoves; |
| 708 | } |
| 709 | |
| 710 | getPotentialJailerMoves([x, y]) { |
| 711 | return super.getPotentialRookMoves([x, y]).filter(m => { |
| 712 | // Remove jailer captures |
| 713 | return m.vanish[0].p != V.JAILER || m.vanish.length == 1; |
| 714 | }); |
| 715 | } |
| 716 | |
| 717 | getPotentialKingMoves(sq) { |
| 718 | const moves = this.getSlideNJumpMoves( |
| 719 | sq, |
| 720 | V.steps[V.ROOK].concat(V.steps[V.BISHOP]), |
| 721 | "oneStep" |
| 722 | ); |
| 723 | return ( |
| 724 | this.subTurn == 1 |
| 725 | ? moves.concat(this.getCastleMoves(sq)) |
| 726 | : moves |
| 727 | ); |
| 728 | } |
| 729 | |
| 730 | atLeastOneMove() { |
| 731 | // If in second-half of a move, we already know that a move is possible |
| 732 | if (this.subTurn == 2) return true; |
| 733 | return super.atLeastOneMove(); |
| 734 | } |
| 735 | |
| 736 | filterValid(moves) { |
| 737 | if (moves.length == 0) return []; |
| 738 | const basicFilter = (m, c) => { |
| 739 | this.play(m); |
| 740 | const res = !this.underCheck(c); |
| 741 | this.undo(m); |
| 742 | return res; |
| 743 | }; |
| 744 | // Disable check tests for sentry pushes, |
| 745 | // because in this case the move isn't finished |
| 746 | let movesWithoutSentryPushes = []; |
| 747 | let movesWithSentryPushes = []; |
| 748 | moves.forEach(m => { |
| 749 | // Second condition below for special king "pass" moves |
| 750 | if (m.appear.length > 0 || m.vanish.length == 0) |
| 751 | movesWithoutSentryPushes.push(m); |
| 752 | else movesWithSentryPushes.push(m); |
| 753 | }); |
| 754 | const color = this.turn; |
| 755 | const oppCol = V.GetOppCol(color); |
| 756 | const filteredMoves = |
| 757 | movesWithoutSentryPushes.filter(m => basicFilter(m, color)); |
| 758 | // If at least one full move made, everything is allowed. |
| 759 | // Else: forbid checks and captures. |
| 760 | return ( |
| 761 | this.movesCount >= 2 |
| 762 | ? filteredMoves |
| 763 | : filteredMoves.filter(m => { |
| 764 | return (m.vanish.length <= 1 && basicFilter(m, oppCol)); |
| 765 | }) |
| 766 | ).concat(movesWithSentryPushes); |
| 767 | } |
| 768 | |
| 769 | getAllValidMoves() { |
| 770 | if (this.subTurn == 1) return super.getAllValidMoves(); |
| 771 | // Sentry push: |
| 772 | const sentrySq = [this.sentryPos.x, this.sentryPos.y]; |
| 773 | return this.filterValid(this.getPotentialMovesFrom(sentrySq)); |
| 774 | } |
| 775 | |
| 776 | isAttacked(sq, color) { |
| 777 | return ( |
| 778 | super.isAttacked(sq, color) || |
| 779 | this.isAttackedByLancer(sq, color) || |
| 780 | this.isAttackedBySentry(sq, color) |
| 781 | // The jailer doesn't capture. |
| 782 | ); |
| 783 | } |
| 784 | |
| 785 | isAttackedBySlideNJump([x, y], color, piece, steps, oneStep) { |
| 786 | for (let step of steps) { |
| 787 | let rx = x + step[0], |
| 788 | ry = y + step[1]; |
| 789 | while (V.OnBoard(rx, ry) && this.board[rx][ry] == V.EMPTY && !oneStep) { |
| 790 | rx += step[0]; |
| 791 | ry += step[1]; |
| 792 | } |
| 793 | if ( |
| 794 | V.OnBoard(rx, ry) && |
| 795 | this.getPiece(rx, ry) == piece && |
| 796 | this.getColor(rx, ry) == color && |
| 797 | !this.isImmobilized([rx, ry]) |
| 798 | ) { |
| 799 | return true; |
| 800 | } |
| 801 | } |
| 802 | return false; |
| 803 | } |
| 804 | |
| 805 | isAttackedByPawn([x, y], color) { |
| 806 | const pawnShift = (color == "w" ? 1 : -1); |
| 807 | if (x + pawnShift >= 0 && x + pawnShift < V.size.x) { |
| 808 | for (let i of [-1, 1]) { |
| 809 | if ( |
| 810 | y + i >= 0 && |
| 811 | y + i < V.size.y && |
| 812 | this.getPiece(x + pawnShift, y + i) == V.PAWN && |
| 813 | this.getColor(x + pawnShift, y + i) == color && |
| 814 | !this.isImmobilized([x + pawnShift, y + i]) |
| 815 | ) { |
| 816 | return true; |
| 817 | } |
| 818 | } |
| 819 | } |
| 820 | return false; |
| 821 | } |
| 822 | |
| 823 | isAttackedByLancer([x, y], color) { |
| 824 | for (let step of V.steps[V.ROOK].concat(V.steps[V.BISHOP])) { |
| 825 | // If in this direction there are only enemy pieces and empty squares, |
| 826 | // and we meet a lancer: can he reach us? |
| 827 | // NOTE: do not stop at first lancer, there might be several! |
| 828 | let coord = { x: x + step[0], y: y + step[1] }; |
| 829 | let lancerPos = []; |
| 830 | while ( |
| 831 | V.OnBoard(coord.x, coord.y) && |
| 832 | ( |
| 833 | this.board[coord.x][coord.y] == V.EMPTY || |
| 834 | this.getColor(coord.x, coord.y) == color |
| 835 | ) |
| 836 | ) { |
| 837 | if ( |
| 838 | this.getPiece(coord.x, coord.y) == V.LANCER && |
| 839 | !this.isImmobilized([coord.x, coord.y]) |
| 840 | ) { |
| 841 | lancerPos.push({x: coord.x, y: coord.y}); |
| 842 | } |
| 843 | coord.x += step[0]; |
| 844 | coord.y += step[1]; |
| 845 | } |
| 846 | const L = this.sentryPush.length; |
| 847 | const pl = (!!this.sentryPush[L-1] ? this.sentryPush[L-1].length : 0); |
| 848 | for (let xy of lancerPos) { |
| 849 | const dir = V.LANCER_DIRS[this.board[xy.x][xy.y].charAt(1)]; |
| 850 | if ( |
| 851 | (dir[0] == -step[0] && dir[1] == -step[1]) || |
| 852 | // If the lancer was just pushed, this is an attack too: |
| 853 | ( |
| 854 | !!this.sentryPush[L-1] && |
| 855 | this.sentryPush[L-1][pl-1].x == xy.x && |
| 856 | this.sentryPush[L-1][pl-1].y == xy.y |
| 857 | ) |
| 858 | ) { |
| 859 | return true; |
| 860 | } |
| 861 | } |
| 862 | } |
| 863 | return false; |
| 864 | } |
| 865 | |
| 866 | // Helper to check sentries attacks: |
| 867 | selfAttack([x1, y1], [x2, y2]) { |
| 868 | const color = this.getColor(x1, y1); |
| 869 | const oppCol = V.GetOppCol(color); |
| 870 | const sliderAttack = (allowedSteps, lancer) => { |
| 871 | const deltaX = x2 - x1, |
| 872 | absDeltaX = Math.abs(deltaX); |
| 873 | const deltaY = y2 - y1, |
| 874 | absDeltaY = Math.abs(deltaY); |
| 875 | const step = [ deltaX / absDeltaX || 0, deltaY / absDeltaY || 0 ]; |
| 876 | if ( |
| 877 | // Check that the step is a priori valid: |
| 878 | (absDeltaX != absDeltaY && deltaX != 0 && deltaY != 0) || |
| 879 | allowedSteps.every(st => st[0] != step[0] || st[1] != step[1]) |
| 880 | ) { |
| 881 | return false; |
| 882 | } |
| 883 | let sq = [ x1 + step[0], y1 + step[1] ]; |
| 884 | while (sq[0] != x2 || sq[1] != y2) { |
| 885 | if ( |
| 886 | // NOTE: no need to check OnBoard in this special case |
| 887 | (!lancer && this.board[sq[0]][sq[1]] != V.EMPTY) || |
| 888 | (!!lancer && this.getColor(sq[0], sq[1]) == oppCol) |
| 889 | ) { |
| 890 | return false; |
| 891 | } |
| 892 | sq[0] += step[0]; |
| 893 | sq[1] += step[1]; |
| 894 | } |
| 895 | return true; |
| 896 | }; |
| 897 | switch (this.getPiece(x1, y1)) { |
| 898 | case V.PAWN: { |
| 899 | // Pushed pawns move as enemy pawns |
| 900 | const shift = (color == 'w' ? 1 : -1); |
| 901 | return (x1 + shift == x2 && Math.abs(y1 - y2) == 1); |
| 902 | } |
| 903 | case V.KNIGHT: { |
| 904 | const deltaX = Math.abs(x1 - x2); |
| 905 | const deltaY = Math.abs(y1 - y2); |
| 906 | return ( |
| 907 | deltaX + deltaY == 3 && |
| 908 | [1, 2].includes(deltaX) && |
| 909 | [1, 2].includes(deltaY) |
| 910 | ); |
| 911 | } |
| 912 | case V.ROOK: |
| 913 | return sliderAttack(V.steps[V.ROOK]); |
| 914 | case V.BISHOP: |
| 915 | return sliderAttack(V.steps[V.BISHOP]); |
| 916 | case V.QUEEN: |
| 917 | return sliderAttack(V.steps[V.ROOK].concat(V.steps[V.BISHOP])); |
| 918 | case V.LANCER: { |
| 919 | // Special case: as long as no enemy units stands in-between, |
| 920 | // it attacks (if it points toward the king). |
| 921 | const allowedStep = V.LANCER_DIRS[this.board[x1][y1].charAt(1)]; |
| 922 | return sliderAttack([allowedStep], "lancer"); |
| 923 | } |
| 924 | // No sentries or jailer tests: they cannot self-capture |
| 925 | } |
| 926 | return false; |
| 927 | } |
| 928 | |
| 929 | isAttackedBySentry([x, y], color) { |
| 930 | // Attacked by sentry means it can self-take our king. |
| 931 | // Just check diagonals of enemy sentry(ies), and if it reaches |
| 932 | // one of our pieces: can I self-take? |
| 933 | const myColor = V.GetOppCol(color); |
| 934 | let candidates = []; |
| 935 | for (let i=0; i<V.size.x; i++) { |
| 936 | for (let j=0; j<V.size.y; j++) { |
| 937 | if ( |
| 938 | this.getPiece(i,j) == V.SENTRY && |
| 939 | this.getColor(i,j) == color && |
| 940 | !this.isImmobilized([i, j]) |
| 941 | ) { |
| 942 | for (let step of V.steps[V.BISHOP]) { |
| 943 | let sq = [ i + step[0], j + step[1] ]; |
| 944 | while ( |
| 945 | V.OnBoard(sq[0], sq[1]) && |
| 946 | this.board[sq[0]][sq[1]] == V.EMPTY |
| 947 | ) { |
| 948 | sq[0] += step[0]; |
| 949 | sq[1] += step[1]; |
| 950 | } |
| 951 | if ( |
| 952 | V.OnBoard(sq[0], sq[1]) && |
| 953 | this.getColor(sq[0], sq[1]) == myColor |
| 954 | ) { |
| 955 | candidates.push([ sq[0], sq[1] ]); |
| 956 | } |
| 957 | } |
| 958 | } |
| 959 | } |
| 960 | } |
| 961 | for (let c of candidates) |
| 962 | if (this.selfAttack(c, [x, y])) return true; |
| 963 | return false; |
| 964 | } |
| 965 | |
| 966 | // Jailer doesn't capture or give check |
| 967 | |
| 968 | prePlay(move) { |
| 969 | if (move.appear.length == 0 && move.vanish.length == 1) |
| 970 | // The sentry is about to push a piece: subTurn goes from 1 to 2 |
| 971 | this.sentryPos = { x: move.end.x, y: move.end.y }; |
| 972 | if (this.subTurn == 2 && move.vanish[0].p != V.PAWN) { |
| 973 | // A piece is pushed: forbid array of squares between start and end |
| 974 | // of move, included (except if it's a pawn) |
| 975 | let squares = []; |
| 976 | if ([V.KNIGHT,V.KING].includes(move.vanish[0].p)) |
| 977 | // short-range pieces: just forbid initial square |
| 978 | squares.push({ x: move.start.x, y: move.start.y }); |
| 979 | else { |
| 980 | const deltaX = move.end.x - move.start.x; |
| 981 | const deltaY = move.end.y - move.start.y; |
| 982 | const step = [ |
| 983 | deltaX / Math.abs(deltaX) || 0, |
| 984 | deltaY / Math.abs(deltaY) || 0 |
| 985 | ]; |
| 986 | for ( |
| 987 | let sq = {x: move.start.x, y: move.start.y}; |
| 988 | sq.x != move.end.x || sq.y != move.end.y; |
| 989 | sq.x += step[0], sq.y += step[1] |
| 990 | ) { |
| 991 | squares.push({ x: sq.x, y: sq.y }); |
| 992 | } |
| 993 | } |
| 994 | // Add end square as well, to know if I was pushed (useful for lancers) |
| 995 | squares.push({ x: move.end.x, y: move.end.y }); |
| 996 | this.sentryPush.push(squares); |
| 997 | } else this.sentryPush.push(null); |
| 998 | } |
| 999 | |
| 1000 | play(move) { |
| 1001 | this.prePlay(move); |
| 1002 | move.flags = JSON.stringify(this.aggregateFlags()); |
| 1003 | this.epSquares.push(this.getEpSquare(move)); |
| 1004 | V.PlayOnBoard(this.board, move); |
| 1005 | // Is it a sentry push? (useful for undo) |
| 1006 | move.sentryPush = (this.subTurn == 2); |
| 1007 | if (this.subTurn == 1) this.movesCount++; |
| 1008 | if (move.appear.length == 0 && move.vanish.length == 1) this.subTurn = 2; |
| 1009 | else { |
| 1010 | // Turn changes only if not a sentry "pre-push" |
| 1011 | this.turn = V.GetOppCol(this.turn); |
| 1012 | this.subTurn = 1; |
| 1013 | } |
| 1014 | this.postPlay(move); |
| 1015 | } |
| 1016 | |
| 1017 | postPlay(move) { |
| 1018 | if (move.vanish.length == 0 || this.subTurn == 2) |
| 1019 | // Special pass move of the king, or sentry pre-push: nothing to update |
| 1020 | return; |
| 1021 | const c = move.vanish[0].c; |
| 1022 | const piece = move.vanish[0].p; |
| 1023 | const firstRank = c == "w" ? V.size.x - 1 : 0; |
| 1024 | |
| 1025 | if (piece == V.KING) { |
| 1026 | this.kingPos[c][0] = move.appear[0].x; |
| 1027 | this.kingPos[c][1] = move.appear[0].y; |
| 1028 | this.castleFlags[c] = [V.size.y, V.size.y]; |
| 1029 | return; |
| 1030 | } |
| 1031 | // Update castling flags if rooks are moved |
| 1032 | const oppCol = V.GetOppCol(c); |
| 1033 | const oppFirstRank = V.size.x - 1 - firstRank; |
| 1034 | if ( |
| 1035 | move.start.x == firstRank && //our rook moves? |
| 1036 | this.castleFlags[c].includes(move.start.y) |
| 1037 | ) { |
| 1038 | const flagIdx = (move.start.y == this.castleFlags[c][0] ? 0 : 1); |
| 1039 | this.castleFlags[c][flagIdx] = V.size.y; |
| 1040 | } else if ( |
| 1041 | move.end.x == oppFirstRank && //we took opponent rook? |
| 1042 | this.castleFlags[oppCol].includes(move.end.y) |
| 1043 | ) { |
| 1044 | const flagIdx = (move.end.y == this.castleFlags[oppCol][0] ? 0 : 1); |
| 1045 | this.castleFlags[oppCol][flagIdx] = V.size.y; |
| 1046 | } |
| 1047 | } |
| 1048 | |
| 1049 | undo(move) { |
| 1050 | this.epSquares.pop(); |
| 1051 | this.disaggregateFlags(JSON.parse(move.flags)); |
| 1052 | V.UndoOnBoard(this.board, move); |
| 1053 | // Decrement movesCount except if the move is a sentry push |
| 1054 | if (!move.sentryPush) this.movesCount--; |
| 1055 | if (this.subTurn == 2) this.subTurn = 1; |
| 1056 | else { |
| 1057 | this.turn = V.GetOppCol(this.turn); |
| 1058 | if (move.sentryPush) this.subTurn = 2; |
| 1059 | } |
| 1060 | this.postUndo(move); |
| 1061 | } |
| 1062 | |
| 1063 | postUndo(move) { |
| 1064 | super.postUndo(move); |
| 1065 | this.sentryPush.pop(); |
| 1066 | } |
| 1067 | |
| 1068 | static get VALUES() { |
| 1069 | return Object.assign( |
| 1070 | { l: 4.8, s: 2.8, j: 3.8 }, //Jeff K. estimations |
| 1071 | ChessRules.VALUES |
| 1072 | ); |
| 1073 | } |
| 1074 | |
| 1075 | getComputerMove() { |
| 1076 | const maxeval = V.INFINITY; |
| 1077 | const color = this.turn; |
| 1078 | let moves1 = this.getAllValidMoves(); |
| 1079 | |
| 1080 | if (moves1.length == 0) |
| 1081 | // TODO: this situation should not happen |
| 1082 | return null; |
| 1083 | |
| 1084 | const setEval = (move, next) => { |
| 1085 | const score = this.getCurrentScore(); |
| 1086 | const curEval = move.eval; |
| 1087 | if (score != "*") { |
| 1088 | move.eval = |
| 1089 | score == "1/2" |
| 1090 | ? 0 |
| 1091 | : (score == "1-0" ? 1 : -1) * maxeval; |
| 1092 | } else move.eval = this.evalPosition(); |
| 1093 | if ( |
| 1094 | // "next" is defined after sentry pushes |
| 1095 | !!next && ( |
| 1096 | !curEval || |
| 1097 | color == 'w' && move.eval > curEval || |
| 1098 | color == 'b' && move.eval < curEval |
| 1099 | ) |
| 1100 | ) { |
| 1101 | move.second = next; |
| 1102 | } |
| 1103 | }; |
| 1104 | |
| 1105 | // Just search_depth == 1 (because of sentries. TODO: can do better...) |
| 1106 | moves1.forEach(m1 => { |
| 1107 | this.play(m1); |
| 1108 | if (this.subTurn == 1) setEval(m1); |
| 1109 | else { |
| 1110 | // Need to play every pushes and count: |
| 1111 | const moves2 = this.getAllValidMoves(); |
| 1112 | moves2.forEach(m2 => { |
| 1113 | this.play(m2); |
| 1114 | setEval(m1, m2); |
| 1115 | this.undo(m2); |
| 1116 | }); |
| 1117 | } |
| 1118 | this.undo(m1); |
| 1119 | }); |
| 1120 | |
| 1121 | moves1.sort((a, b) => { |
| 1122 | return (color == "w" ? 1 : -1) * (b.eval - a.eval); |
| 1123 | }); |
| 1124 | let candidates = [0]; |
| 1125 | for (let j = 1; j < moves1.length && moves1[j].eval == moves1[0].eval; j++) |
| 1126 | candidates.push(j); |
| 1127 | const choice = moves1[candidates[randInt(candidates.length)]]; |
| 1128 | return (!choice.second ? choice : [choice, choice.second]); |
| 1129 | } |
| 1130 | |
| 1131 | // For moves notation: |
| 1132 | static get LANCER_DIRNAMES() { |
| 1133 | return { |
| 1134 | 'c': "N", |
| 1135 | 'd': "NE", |
| 1136 | 'e': "E", |
| 1137 | 'f': "SE", |
| 1138 | 'g': "S", |
| 1139 | 'h': "SW", |
| 1140 | 'm': "W", |
| 1141 | 'o': "NW" |
| 1142 | }; |
| 1143 | } |
| 1144 | |
| 1145 | getNotation(move) { |
| 1146 | // Special case "king takes jailer" is a pass move |
| 1147 | if (move.appear.length == 0 && move.vanish.length == 0) return "pass"; |
| 1148 | let notation = undefined; |
| 1149 | if (this.subTurn == 2) { |
| 1150 | // Do not consider appear[1] (sentry) for sentry pushes |
| 1151 | const simpleMove = { |
| 1152 | appear: [move.appear[0]], |
| 1153 | vanish: move.vanish, |
| 1154 | start: move.start, |
| 1155 | end: move.end |
| 1156 | }; |
| 1157 | notation = super.getNotation(simpleMove); |
| 1158 | } |
| 1159 | else if ( |
| 1160 | move.appear.length > 0 && |
| 1161 | move.vanish[0].x == move.appear[0].x && |
| 1162 | move.vanish[0].y == move.appear[0].y |
| 1163 | ) { |
| 1164 | // Lancer in-place reorientation: |
| 1165 | notation = "L" + V.CoordsToSquare(move.start) + ":R"; |
| 1166 | } |
| 1167 | else notation = super.getNotation(move); |
| 1168 | if (Object.keys(V.LANCER_DIRNAMES).includes(move.vanish[0].p)) |
| 1169 | // Lancer: add direction info |
| 1170 | notation += "=" + V.LANCER_DIRNAMES[move.appear[0].p]; |
| 1171 | else if ( |
| 1172 | move.vanish[0].p == V.PAWN && |
| 1173 | Object.keys(V.LANCER_DIRNAMES).includes(move.appear[0].p) |
| 1174 | ) { |
| 1175 | // Fix promotions in lancer: |
| 1176 | notation = notation.slice(0, -1) + |
| 1177 | "L:" + V.LANCER_DIRNAMES[move.appear[0].p]; |
| 1178 | } |
| 1179 | return notation; |
| 1180 | } |
| 1181 | }; |