| 1 | import { ChessRules } from "@/base_rules"; |
| 2 | |
| 3 | export class IsardamRules extends ChessRules { |
| 4 | |
| 5 | isImmobilized(sq) { |
| 6 | const oppCol = V.GetOppCol(this.getColor(sq[0], sq[1])); |
| 7 | const piece = this.getPiece(sq[0], sq[1]); |
| 8 | let steps = []; |
| 9 | switch (piece) { |
| 10 | // NOTE: cannot use super.isAttackedByXXX |
| 11 | // because it would call the redefined isAttackedBySlideNJump |
| 12 | // => Infinite recursive calls. |
| 13 | case V.PAWN: { |
| 14 | const forward = (oppCol == 'w' ? 1 : -1); |
| 15 | steps = [[forward, 1], [forward, -1]]; |
| 16 | break; |
| 17 | } |
| 18 | case V.ROOK: |
| 19 | case V.BISHOP: |
| 20 | case V.KNIGHT: |
| 21 | steps = V.steps[piece]; |
| 22 | break; |
| 23 | case V.KING: |
| 24 | case V.QUEEN: |
| 25 | steps = V.steps[V.ROOK].concat(V.steps[V.BISHOP]); |
| 26 | break; |
| 27 | } |
| 28 | return super.isAttackedBySlideNJump( |
| 29 | sq, oppCol, piece, steps, [V.KING, V.PAWN, V.KNIGHT].includes(piece)) |
| 30 | } |
| 31 | |
| 32 | getPotentialMovesFrom([x, y]) { |
| 33 | return ( |
| 34 | super.getPotentialMovesFrom([x, y]).filter(m => { |
| 35 | let res = true; |
| 36 | this.play(m); |
| 37 | if (this.isImmobilized([m.end.x, m.end.y])) res = false; |
| 38 | else { |
| 39 | // Check discovered attacks |
| 40 | for (let step of V.steps[V.BISHOP].concat(V.steps[V.ROOK])) { |
| 41 | let allowedPieces = [V.QUEEN]; |
| 42 | if (step[0] == 0 || step[1] == 0) allowedPieces.push(V.ROOK); |
| 43 | else allowedPieces.push(V.BISHOP); |
| 44 | let [i, j] = [m.start.x + step[0], m.start.y + step[1]]; |
| 45 | while (V.OnBoard(i, j) && this.board[i][j] == V.EMPTY) { |
| 46 | i += step[0]; |
| 47 | j += step[1]; |
| 48 | } |
| 49 | if (V.OnBoard(i, j)) { |
| 50 | const meet = { c: this.getColor(i, j), p: this.getPiece(i, j) }; |
| 51 | if (allowedPieces.includes(meet.p)) { |
| 52 | // Search in the other direction |
| 53 | [i, j] = [m.start.x - step[0], m.start.y - step[1]]; |
| 54 | while (V.OnBoard(i, j) && this.board[i][j] == V.EMPTY) { |
| 55 | i -= step[0]; |
| 56 | j -= step[1]; |
| 57 | } |
| 58 | if ( |
| 59 | V.OnBoard(i, j) && |
| 60 | this.getPiece(i, j) == meet.p && |
| 61 | this.getColor(i, j) != meet.c |
| 62 | ) { |
| 63 | res = false; |
| 64 | break; |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | this.undo(m); |
| 71 | return res; |
| 72 | }) |
| 73 | ); |
| 74 | } |
| 75 | |
| 76 | static get SEARCH_DEPTH() { |
| 77 | return 2; |
| 78 | } |
| 79 | |
| 80 | }; |