| 1 | class UltimaRules extends ChessRules |
| 2 | { |
| 3 | static getPpath(b) |
| 4 | { |
| 5 | if (b[1] == "m") //'m' for Immobilizer (I is too similar to 1) |
| 6 | return "Ultima/" + b; |
| 7 | return b; //usual piece |
| 8 | } |
| 9 | |
| 10 | initVariables(fen) |
| 11 | { |
| 12 | this.kingPos = {'w':[-1,-1], 'b':[-1,-1]}; |
| 13 | const fenParts = fen.split(" "); |
| 14 | const position = fenParts[0].split("/"); |
| 15 | for (let i=0; i<position.length; i++) |
| 16 | { |
| 17 | let k = 0; |
| 18 | for (let j=0; j<position[i].length; j++) |
| 19 | { |
| 20 | switch (position[i].charAt(j)) |
| 21 | { |
| 22 | case 'k': |
| 23 | this.kingPos['b'] = [i,k]; |
| 24 | break; |
| 25 | case 'K': |
| 26 | this.kingPos['w'] = [i,k]; |
| 27 | break; |
| 28 | default: |
| 29 | let num = parseInt(position[i].charAt(j)); |
| 30 | if (!isNaN(num)) |
| 31 | k += (num-1); |
| 32 | } |
| 33 | k++; |
| 34 | } |
| 35 | } |
| 36 | this.epSquares = []; //no en-passant here |
| 37 | } |
| 38 | |
| 39 | setFlags(fen) |
| 40 | { |
| 41 | // TODO: for compatibility? |
| 42 | this.castleFlags = {"w":[false,false], "b":[false,false]}; |
| 43 | } |
| 44 | |
| 45 | static get IMMOBILIZER() { return 'm'; } |
| 46 | // Although other pieces keep their names here for coding simplicity, |
| 47 | // keep in mind that: |
| 48 | // - a "rook" is a coordinator, capturing by coordinating with the king |
| 49 | // - a "knight" is a long-leaper, capturing as in draughts |
| 50 | // - a "bishop" is a chameleon, capturing as its prey |
| 51 | // - a "queen" is a withdrawer, capturing by moving away from pieces |
| 52 | |
| 53 | // Is piece on square (x,y) immobilized? |
| 54 | isImmobilized([x,y]) |
| 55 | { |
| 56 | // Final check: is this knight immobilized? |
| 57 | let foundImmobilizer = false; |
| 58 | let neutralized = false; |
| 59 | outerLoop: |
| 60 | for (let step of steps) |
| 61 | { |
| 62 | const [i2,j2] = [i+step[0],j+step[1]]; |
| 63 | if (i2>=0 && i2<sizeX && j2>=0 && j2<sizeY |
| 64 | && this.board[i2][j2] != V.EMPTY |
| 65 | && this.getColor(i2,j2) == oppCol |
| 66 | && this.getPiece(i2,j2) == V.IMMOBILIZER) |
| 67 | { |
| 68 | foundImmobilizer = true; |
| 69 | // Moving is possible only if this immobilizer is neutralized |
| 70 | for (let step2 of steps) |
| 71 | { |
| 72 | const [i3,j3] = [i2+step2[0],j2+step2[1]]; |
| 73 | if (i3>=0 && i3<sizeX && j3>=0 && j3<sizeY |
| 74 | && this.board[i3][j3] != V.EMPTY && this.getColor(i3,j3) == color |
| 75 | && [V.BISHOP,V.IMMOBILIZER].includes(this.getPiece(i3,j3))) |
| 76 | { |
| 77 | neutralized = true; |
| 78 | break outerLoop; |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | if (!foundImmobilizer || neutralized) |
| 84 | return false; |
| 85 | |
| 86 | const piece = this.getPiece(x,y); |
| 87 | const color = this.getColor(x,y); |
| 88 | const oppCol = this.getOppCol(color); |
| 89 | const V = VariantRules; |
| 90 | const adjacentSteps = V.steps[V.ROOK].concat(V.steps[V.BISHOP]); |
| 91 | const [sizeX,sizeY] = V.size; |
| 92 | outerLoop: |
| 93 | for (let step of adjacentSteps) |
| 94 | { |
| 95 | const [i,j] = [x+step[0],y+step[1]]; |
| 96 | if (i>=0 && i<sizeX && j>=0 && j<sizeY && this.board[i][j] != V.EMPTY |
| 97 | && this.getColor(i,j) == oppCol) |
| 98 | { |
| 99 | const oppPiece = this.getPiece(i,j); |
| 100 | if (oppPiece == V.BISHOP && piece == V.IMMOBILIZER) |
| 101 | return []; |
| 102 | if (oppPiece == V.IMMOBILIZER && ![V.BISHOP,V.IMMOBILIZER].includes(piece)) |
| 103 | { |
| 104 | // Moving is impossible only if this immobilizer is not neutralized |
| 105 | for (let step2 of adjacentSteps) |
| 106 | { |
| 107 | const [i2,j2] = [i+step2[0],j+step2[1]]; |
| 108 | if (i2>=0 && i2<sizeX && j2>=0 && j2<sizeY |
| 109 | && this.board[i2][j2] != V.EMPTY && this.getColor(i2,j2) == color) |
| 110 | { |
| 111 | const friendlyPiece = this.getPiece(i2,j2); |
| 112 | if ([V.BISHOP,V.IMMOBILIZER].includes(friendlyPiece)) |
| 113 | break outerLoop; |
| 114 | } |
| 115 | } |
| 116 | return []; //immobilizer isn't neutralized |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | getPotentialMovesFrom([x,y]) |
| 123 | { |
| 124 | // Pre-check: is thing on this square immobilized? |
| 125 | if (this.isImmobilized([x,y])) |
| 126 | return []; |
| 127 | switch (this.getPiece(x,y)) |
| 128 | { |
| 129 | case VariantRules.IMMOBILIZER: |
| 130 | return this.getPotentialImmobilizerMoves([x,y]); |
| 131 | default: |
| 132 | return super.getPotentialMovesFrom([x,y]); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | getSlideNJumpMoves([x,y], steps, oneStep) |
| 137 | { |
| 138 | const color = this.getColor(x,y); |
| 139 | const piece = this.getPiece(x,y); |
| 140 | let moves = []; |
| 141 | const [sizeX,sizeY] = VariantRules.size; |
| 142 | outerLoop: |
| 143 | for (let step of steps) |
| 144 | { |
| 145 | let i = x + step[0]; |
| 146 | let j = y + step[1]; |
| 147 | while (i>=0 && i<sizeX && j>=0 && j<sizeY |
| 148 | && this.board[i][j] == VariantRules.EMPTY) |
| 149 | { |
| 150 | moves.push(this.getBasicMove([x,y], [i,j])); |
| 151 | if (oneStep !== undefined) |
| 152 | continue outerLoop; |
| 153 | i += step[0]; |
| 154 | j += step[1]; |
| 155 | } |
| 156 | // Only king can take on occupied square: |
| 157 | if (piece==VariantRules.KING && i>=0 && i<sizeX && j>=0 |
| 158 | && j<sizeY && this.canTake([x,y], [i,j])) |
| 159 | { |
| 160 | moves.push(this.getBasicMove([x,y], [i,j])); |
| 161 | } |
| 162 | } |
| 163 | return moves; |
| 164 | } |
| 165 | |
| 166 | // Modify capturing moves among listed pawn moves |
| 167 | addPawnCaptures(moves, byChameleon) |
| 168 | { |
| 169 | const steps = VariantRules.steps[VariantRules.ROOK]; |
| 170 | const [sizeX,sizeY] = VariantRules.size; |
| 171 | const color = this.turn; |
| 172 | const oppCol = this.getOppCol(color); |
| 173 | moves.forEach(m => { |
| 174 | if (!!byChameleon && m.start.x!=m.end.x && m.start.y!=m.end.y) |
| 175 | return; //chameleon not moving as pawn |
| 176 | // Try capturing in every direction |
| 177 | for (let step of steps) |
| 178 | { |
| 179 | const sq2 = [m.end.x+2*step[0],m.end.y+2*step[1]]; |
| 180 | if (sq2[0]>=0 && sq2[0]<sizeX && sq2[1]>=0 && sq2[1]<sizeY |
| 181 | && this.board[sq2[0]][sq2[1]] != VariantRules.EMPTY |
| 182 | && this.getColor(sq2[0],sq2[1]) == color) |
| 183 | { |
| 184 | // Potential capture |
| 185 | const sq1 = [m.end.x+step[0],m.end.y+step[1]]; |
| 186 | if (this.board[sq1[0]][sq1[1]] != VariantRules.EMPTY |
| 187 | && this.getColor(sq1[0],sq1[1]) == oppCol) |
| 188 | { |
| 189 | const piece1 = this.getPiece(sq1[0],sq1[1]); |
| 190 | if (!byChameleon || piece1 == VariantRules.PAWN) |
| 191 | { |
| 192 | m.vanish.push(new PiPo({ |
| 193 | x:sq1[0], |
| 194 | y:sq1[1], |
| 195 | c:oppCol, |
| 196 | p:piece1 |
| 197 | })); |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | }); |
| 203 | } |
| 204 | |
| 205 | // "Pincher" |
| 206 | getPotentialPawnMoves([x,y]) |
| 207 | { |
| 208 | let moves = super.getPotentialRookMoves([x,y]); |
| 209 | this.addPawnCaptures(moves); |
| 210 | return moves; |
| 211 | } |
| 212 | |
| 213 | addRookCaptures(moves, byChameleon) |
| 214 | { |
| 215 | const color = this.turn; |
| 216 | const oppCol = this.getOppCol(color); |
| 217 | const kp = this.kingPos[color]; |
| 218 | moves.forEach(m => { |
| 219 | // Check piece-king rectangle (if any) corners for enemy pieces |
| 220 | if (m.end.x == kp[0] || m.end.y == kp[1]) |
| 221 | return; //"flat rectangle" |
| 222 | const corner1 = [Math.max(m.end.x,kp[0]), Math.min(m.end.y,kp[1])]; |
| 223 | const corner2 = [Math.min(m.end.x,kp[0]), Math.max(m.end.y,kp[1])]; |
| 224 | for (let [i,j] of [corner1,corner2]) |
| 225 | { |
| 226 | if (this.board[i][j] != VariantRules.EMPTY && this.getColor(i,j) == oppCol) |
| 227 | { |
| 228 | const piece = this.getPiece(i,j); |
| 229 | if (!byChameleon || piece == VariantRules.ROOK) |
| 230 | { |
| 231 | m.vanish.push( new PiPo({ |
| 232 | x:i, |
| 233 | y:j, |
| 234 | p:piece, |
| 235 | c:oppCol |
| 236 | }) ); |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | }); |
| 241 | } |
| 242 | |
| 243 | // Coordinator |
| 244 | getPotentialRookMoves(sq) |
| 245 | { |
| 246 | let moves = super.getPotentialQueenMoves(sq); |
| 247 | this.addRookCaptures(moves); |
| 248 | return moves; |
| 249 | } |
| 250 | |
| 251 | // Long-leaper |
| 252 | getKnightCaptures(startSquare, byChameleon) |
| 253 | { |
| 254 | // Look in every direction for captures |
| 255 | const V = VariantRules; |
| 256 | const steps = V.steps[V.ROOK].concat(V.steps[V.BISHOP]); |
| 257 | const [sizeX,sizeY] = V.size; |
| 258 | const color = this.turn; |
| 259 | const oppCol = this.getOppCol(color); |
| 260 | let moves = []; |
| 261 | const [x,y] = [startSquare[0],startSquare[1]]; |
| 262 | const piece = this.getPiece(x,y); //might be a chameleon! |
| 263 | outerLoop: |
| 264 | for (let step of steps) |
| 265 | { |
| 266 | let [i,j] = [x+step[0], y+step[1]]; |
| 267 | while (i>=0 && i<sizeX && j>=0 && j<sizeY && this.board[i][j]==V.EMPTY) |
| 268 | { |
| 269 | i += step[0]; |
| 270 | j += step[1]; |
| 271 | } |
| 272 | if (i<0 || i>=sizeX || j<0 || j>=sizeY || this.getColor(i,j)==color |
| 273 | || (!!byChameleon && this.getPiece(i,j)!=V.KNIGHT)) |
| 274 | { |
| 275 | continue; |
| 276 | } |
| 277 | // last(thing), cur(thing) : stop if "cur" is our color, or beyond board limits, |
| 278 | // or if "last" isn't empty and cur neither. Otherwise, if cur is empty then |
| 279 | // add move until cur square; if cur is occupied then stop if !!byChameleon and |
| 280 | // the square not occupied by a leaper. |
| 281 | let last = [i,j]; |
| 282 | let cur = [i+step[0],j+step[1]]; |
| 283 | let vanished = [ new PiPo({x:x,y:y,c:color,p:piece}) ]; |
| 284 | while (cur[0]>=0 && cur[0]<sizeX && cur[1]>=0 && cur[1]<sizeY) |
| 285 | { |
| 286 | if (this.board[last[0]][last[1]] != V.EMPTY) |
| 287 | { |
| 288 | const oppPiece = this.getPiece(last[0],last[1]); |
| 289 | if (!!byChameleon && oppPiece != V.KNIGHT) |
| 290 | continue outerLoop; |
| 291 | // Something to eat: |
| 292 | vanished.push( new PiPo({x:last[0],y:last[1],c:oppCol,p:oppPiece}) ); |
| 293 | } |
| 294 | if (this.board[cur[0]][cur[1]] != V.EMPTY) |
| 295 | { |
| 296 | if (this.getColor(cur[0],cur[1]) == color |
| 297 | || this.board[last[0]][last[1]] != V.EMPTY) //TODO: redundant test |
| 298 | { |
| 299 | continue outerLoop; |
| 300 | } |
| 301 | } |
| 302 | else |
| 303 | { |
| 304 | moves.push(new Move({ |
| 305 | appear: [ new PiPo({x:cur[0],y:cur[1],c:color,p:piece}) ], |
| 306 | vanish: JSON.parse(JSON.stringify(vanished)), //TODO: required? |
| 307 | start: {x:x,y:y}, |
| 308 | end: {x:cur[0],y:cur[1]} |
| 309 | })); |
| 310 | } |
| 311 | last = [last[0]+step[0],last[1]+step[1]]; |
| 312 | cur = [cur[0]+step[0],cur[1]+step[1]]; |
| 313 | } |
| 314 | } |
| 315 | return moves; |
| 316 | } |
| 317 | |
| 318 | // Long-leaper |
| 319 | getPotentialKnightMoves(sq) |
| 320 | { |
| 321 | return super.getPotentialQueenMoves(sq).concat(this.getKnightCaptures(sq)); |
| 322 | } |
| 323 | |
| 324 | getPotentialBishopMoves([x,y]) |
| 325 | { |
| 326 | let moves = super.getPotentialQueenMoves([x,y]) |
| 327 | .concat(this.getKnightCaptures([x,y],"asChameleon")); |
| 328 | // No "king capture" because king cannot remain under check |
| 329 | this.addPawnCaptures(moves, "asChameleon"); |
| 330 | this.addRookCaptures(moves, "asChameleon"); |
| 331 | this.addQueenCaptures(moves, "asChameleon"); |
| 332 | // Post-processing: merge similar moves, concatenating vanish arrays |
| 333 | let mergedMoves = {}; |
| 334 | const [sizeX,sizeY] = VariantRules.size; |
| 335 | moves.forEach(m => { |
| 336 | const key = m.end.x + sizeX * m.end.y; |
| 337 | if (!mergedMoves[key]) |
| 338 | mergedMoves[key] = m; |
| 339 | else |
| 340 | { |
| 341 | for (let i=1; i<m.vanish.length; i++) |
| 342 | mergedMoves[key].vanish.push(m.vanish[i]); |
| 343 | } |
| 344 | }); |
| 345 | // Finally return an array |
| 346 | moves = []; |
| 347 | Object.keys(mergedMoves).forEach(k => { moves.push(mergedMoves[k]); }); |
| 348 | return moves; |
| 349 | } |
| 350 | |
| 351 | // Withdrawer |
| 352 | addQueenCaptures(moves, byChameleon) |
| 353 | { |
| 354 | if (moves.length == 0) |
| 355 | return; |
| 356 | const [x,y] = [moves[0].start.x,moves[0].start.y]; |
| 357 | const V = VariantRules; |
| 358 | const adjacentSteps = V.steps[V.ROOK].concat(V.steps[V.BISHOP]); |
| 359 | let capturingDirections = []; |
| 360 | const color = this.turn; |
| 361 | const oppCol = this.getOppCol(color); |
| 362 | const [sizeX,sizeY] = V.size; |
| 363 | adjacentSteps.forEach(step => { |
| 364 | const [i,j] = [x+step[0],y+step[1]]; |
| 365 | if (i>=0 && i<sizeX && j>=0 && j<sizeY |
| 366 | && this.board[i][j] != V.EMPTY && this.getColor(i,j) == oppCol |
| 367 | && (!byChameleon || this.getPiece(i,j) == V.QUEEN)) |
| 368 | { |
| 369 | capturingDirections.push(step); |
| 370 | } |
| 371 | }); |
| 372 | moves.forEach(m => { |
| 373 | const step = [ |
| 374 | m.end.x!=x ? (m.end.x-x)/Math.abs(m.end.x-x) : 0, |
| 375 | m.end.y!=y ? (m.end.y-y)/Math.abs(m.end.y-y) : 0 |
| 376 | ]; |
| 377 | // NOTE: includes() and even _.isEqual() functions fail... |
| 378 | // TODO: this test should be done only once per direction |
| 379 | if (capturingDirections.some(dir => |
| 380 | { return (dir[0]==-step[0] && dir[1]==-step[1]); })) |
| 381 | { |
| 382 | const [i,j] = [x-step[0],y-step[1]]; |
| 383 | m.vanish.push(new PiPo({ |
| 384 | x:i, |
| 385 | y:j, |
| 386 | p:this.getPiece(i,j), |
| 387 | c:oppCol |
| 388 | })); |
| 389 | } |
| 390 | }); |
| 391 | } |
| 392 | |
| 393 | getPotentialQueenMoves(sq) |
| 394 | { |
| 395 | let moves = super.getPotentialQueenMoves(sq); |
| 396 | this.addQueenCaptures(moves); |
| 397 | return moves; |
| 398 | } |
| 399 | |
| 400 | getPotentialImmobilizerMoves(sq) |
| 401 | { |
| 402 | // Immobilizer doesn't capture |
| 403 | return super.getPotentialQueenMoves(sq); |
| 404 | } |
| 405 | |
| 406 | getPotentialKingMoves(sq) |
| 407 | { |
| 408 | const V = VariantRules; |
| 409 | return this.getSlideNJumpMoves(sq, |
| 410 | V.steps[V.ROOK].concat(V.steps[V.BISHOP]), "oneStep"); |
| 411 | } |
| 412 | |
| 413 | // isAttacked() is OK because the immobilizer doesn't take |
| 414 | |
| 415 | // TODO: check if any pawn can reach capturing square + !immobilized |
| 416 | isAttackedByPawn([x,y], colors) |
| 417 | { |
| 418 | // Square (x,y) must be surrounded by two enemy pieces, |
| 419 | // and one of them at least should be a pawn. |
| 420 | const dirs = [ [1,0],[0,1],[1,1],[-1,1] ]; |
| 421 | const [sizeX,sizeY] = VariantRules.size; |
| 422 | for (let dir of dirs) |
| 423 | { |
| 424 | const [i1,j1] = [x-dir[0],y-dir[1]]; //"before" |
| 425 | const [i2,j2] = [x+dir[0],y+dir[1]]; //"after" |
| 426 | if (i1>=0 && i1<sizeX && i2>=0 && i2<sizeX |
| 427 | && j1>=0 && j1<sizeY && j2>=0 && j2<sizeY |
| 428 | && this.board[i1][j1]!=VariantRules.EMPTY |
| 429 | && this.board[i2][j2]!=VariantRules.EMPTY |
| 430 | && colors.includes(this.getColor(i1,j1)) |
| 431 | && colors.includes(this.getColor(i2,j2)) |
| 432 | && [this.getPiece(i1,j1),this.getPiece(i2,j2)].includes(VariantRules.PAWN)) |
| 433 | { |
| 434 | return true; |
| 435 | } |
| 436 | } |
| 437 | return false; |
| 438 | } |
| 439 | |
| 440 | // TODO: check if enemy's rook can reach capturing squares + !immobilized |
| 441 | isAttackedByRook([x,y], colors) |
| 442 | { |
| 443 | const [sizeX,sizeY] = VariantRules.size; |
| 444 | // King must be on same column and a rook on same row (or reverse) |
| 445 | if (x == this.kingPos[colors[0]][0]) //using colors[0], only element in this case |
| 446 | { |
| 447 | // Look for enemy rook on this column |
| 448 | for (let i=0; i<sizeY; i++) |
| 449 | { |
| 450 | if (this.board[x][i] != VariantRules.EMPTY |
| 451 | && colors.includes(this.getColor(x,i)) |
| 452 | && this.getPiece(x,i) == VariantRules.ROOK) |
| 453 | { |
| 454 | return true; |
| 455 | } |
| 456 | } |
| 457 | } |
| 458 | else if (y == this.kingPos[colors[0]][1]) |
| 459 | { |
| 460 | // Look for enemy rook on this row |
| 461 | for (let i=0; i<sizeX; i++) |
| 462 | { |
| 463 | if (this.board[i][y] != VariantRules.EMPTY |
| 464 | && colors.includes(this.getColor(i,y)) |
| 465 | && this.getPiece(i,y) == VariantRules.ROOK) |
| 466 | { |
| 467 | return true; |
| 468 | } |
| 469 | } |
| 470 | } |
| 471 | return false; |
| 472 | } |
| 473 | |
| 474 | isAttackedByKnight([x,y], colors) |
| 475 | { |
| 476 | // Square (x,y) must be on same line as a knight, |
| 477 | // and there must be empty square(s) behind. |
| 478 | const V = VariantRules; |
| 479 | const steps = V.steps[V.ROOK].concat(V.steps[V.BISHOP]); |
| 480 | const [sizeX,sizeY] = V.size; |
| 481 | outerLoop: |
| 482 | for (let step of steps) |
| 483 | { |
| 484 | const [i0,j0] = [x+step[0],y+step[1]]; |
| 485 | if (i0>=0 && i0<sizeX && j0>=0 && j0<sizeY && this.board[i0][j0] == V.EMPTY) |
| 486 | { |
| 487 | // Try in opposite direction: |
| 488 | let [i,j] = [x-step[0],y-step[1]]; |
| 489 | while (i>=0 && i<sizeX && j>=0 && j<sizeY && this.board[i][j] == V.EMPTY) |
| 490 | { |
| 491 | i -= step[0]; |
| 492 | j -= step[1]; |
| 493 | } |
| 494 | if (i>=0 && i<sizeX && j>=0 && j<sizeY && colors.includes(this.getColor(i,j)) |
| 495 | && this.getPiece(i,j) == V.KNIGHT) |
| 496 | { |
| 497 | if (!this.isImmobilized([i,j])) |
| 498 | return true; |
| 499 | } |
| 500 | } |
| 501 | } |
| 502 | return false; |
| 503 | } |
| 504 | |
| 505 | isAttackedByBishop([x,y], colors) |
| 506 | { |
| 507 | // We cheat a little here: since this function is used exclusively for king, |
| 508 | // it's enough to check the immediate surrounding of the square. |
| 509 | const V = VariantRules; |
| 510 | const adjacentSteps = V.steps[V.ROOK].concat(V.steps[V.BISHOP]); |
| 511 | const [sizeX,sizeY] = V.size; |
| 512 | for (let step of adjacentSteps) |
| 513 | { |
| 514 | const [i,j] = [x+step[0],y+step[1]]; |
| 515 | if (i>=0 && i<sizeX && j>=0 && j<sizeY && this.board[i][j]!=V.EMPTY |
| 516 | && colors.includes(this.getColor(i,j)) && this.getPiece(i,j) == V.BISHOP) |
| 517 | { |
| 518 | return true; //bishops are never immobilized |
| 519 | } |
| 520 | } |
| 521 | return false; |
| 522 | } |
| 523 | |
| 524 | isAttackedByQueen([x,y], colors) |
| 525 | { |
| 526 | // Square (x,y) must be adjacent to a queen, and the queen must have |
| 527 | // some free space in the opposite direction from (x,y) |
| 528 | const V = VariantRules; |
| 529 | const adjacentSteps = V.steps[V.ROOK].concat(V.steps[V.BISHOP]); |
| 530 | const [sizeX,sizeY] = V.size; |
| 531 | for (let step of adjacentSteps) |
| 532 | { |
| 533 | const sq2 = [x+2*step[0],y+2*step[1]]; |
| 534 | if (sq2[0]>=0 && sq2[0]<sizeX && sq2[1]>=0 && sq2[1]<sizeY |
| 535 | && this.board[sq2[0]][sq2[1]] == V.EMPTY) |
| 536 | { |
| 537 | const sq1 = [x+step[0],y+step[1]]; |
| 538 | if (this.board[sq1[0]][sq1[1]] != V.EMPTY |
| 539 | && colors.includes(this.getColor(sq1[0],sq1[1])) |
| 540 | && this.getPiece(sq1[0],sq1[1]) == V.QUEEN |
| 541 | && !this.isImmobilized(sq1)) |
| 542 | { |
| 543 | return true; |
| 544 | } |
| 545 | } |
| 546 | } |
| 547 | return false; |
| 548 | } |
| 549 | |
| 550 | updateVariables(move) |
| 551 | { |
| 552 | // Just update king(s) position(s) |
| 553 | const piece = this.getPiece(move.start.x,move.start.y); |
| 554 | const c = this.getColor(move.start.x,move.start.y); |
| 555 | if (piece == VariantRules.KING && move.appear.length > 0) |
| 556 | { |
| 557 | this.kingPos[c][0] = move.appear[0].x; |
| 558 | this.kingPos[c][1] = move.appear[0].y; |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | static get VALUES() { //TODO: totally experimental! |
| 563 | return { |
| 564 | 'p': 1, |
| 565 | 'r': 2, |
| 566 | 'n': 5, |
| 567 | 'b': 3, |
| 568 | 'q': 3, |
| 569 | 'm': 5, |
| 570 | 'k': 1000 |
| 571 | }; |
| 572 | } |
| 573 | |
| 574 | static get SEARCH_DEPTH() { return 2; } //TODO? |
| 575 | |
| 576 | static GenRandInitFen() |
| 577 | { |
| 578 | let pieces = { "w": new Array(8), "b": new Array(8) }; |
| 579 | // Shuffle pieces on first and last rank |
| 580 | for (let c of ["w","b"]) |
| 581 | { |
| 582 | let positions = _.range(8); |
| 583 | // Get random squares for every piece, totally freely |
| 584 | |
| 585 | let randIndex = _.random(7); |
| 586 | const bishop1Pos = positions[randIndex]; |
| 587 | positions.splice(randIndex, 1); |
| 588 | |
| 589 | randIndex = _.random(6); |
| 590 | const bishop2Pos = positions[randIndex]; |
| 591 | positions.splice(randIndex, 1); |
| 592 | |
| 593 | randIndex = _.random(5); |
| 594 | const knight1Pos = positions[randIndex]; |
| 595 | positions.splice(randIndex, 1); |
| 596 | |
| 597 | randIndex = _.random(4); |
| 598 | const knight2Pos = positions[randIndex]; |
| 599 | positions.splice(randIndex, 1); |
| 600 | |
| 601 | randIndex = _.random(3); |
| 602 | const queenPos = positions[randIndex]; |
| 603 | positions.splice(randIndex, 1); |
| 604 | |
| 605 | randIndex = _.random(2); |
| 606 | const kingPos = positions[randIndex]; |
| 607 | positions.splice(randIndex, 1); |
| 608 | |
| 609 | randIndex = _.random(1); |
| 610 | const rookPos = positions[randIndex]; |
| 611 | positions.splice(randIndex, 1); |
| 612 | const immobilizerPos = positions[0]; |
| 613 | |
| 614 | pieces[c][bishop1Pos] = 'b'; |
| 615 | pieces[c][bishop2Pos] = 'b'; |
| 616 | pieces[c][knight1Pos] = 'n'; |
| 617 | pieces[c][knight2Pos] = 'n'; |
| 618 | pieces[c][queenPos] = 'q'; |
| 619 | pieces[c][kingPos] = 'k'; |
| 620 | pieces[c][rookPos] = 'r'; |
| 621 | pieces[c][immobilizerPos] = 'm'; |
| 622 | } |
| 623 | return pieces["b"].join("") + |
| 624 | "/pppppppp/8/8/8/8/PPPPPPPP/" + |
| 625 | pieces["w"].join("").toUpperCase() + |
| 626 | " 0000"; //TODO: flags?! |
| 627 | } |
| 628 | |
| 629 | getFlagsFen() |
| 630 | { |
| 631 | return "0000"; //TODO: or "-" ? |
| 632 | } |
| 633 | } |