| 1 | import { ChessRules } from "@/base_rules"; |
| 2 | |
| 3 | export const VariantRules = class BerolinaRules extends ChessRules { |
| 4 | // En-passant after 2-sq jump |
| 5 | getEpSquare(moveOrSquare) { |
| 6 | if (!moveOrSquare) return undefined; |
| 7 | if (typeof moveOrSquare === "string") { |
| 8 | const square = moveOrSquare; |
| 9 | if (square == "-") return undefined; |
| 10 | // Enemy pawn initial column must be given too: |
| 11 | let res = []; |
| 12 | const epParts = square.split(","); |
| 13 | res.push(V.SquareToCoords(epParts[0])); |
| 14 | res.push(V.ColumnToCoord(epParts[1])); |
| 15 | return res; |
| 16 | } |
| 17 | // Argument is a move: |
| 18 | const move = moveOrSquare; |
| 19 | const [sx, ex, sy] = [move.start.x, move.end.x, move.start.y]; |
| 20 | if (this.getPiece(sx, sy) == V.PAWN && Math.abs(sx - ex) == 2) { |
| 21 | return [ |
| 22 | { |
| 23 | x: (ex + sx) / 2, |
| 24 | y: (move.end.y + sy) / 2 |
| 25 | }, |
| 26 | move.end.y |
| 27 | ]; |
| 28 | } |
| 29 | return undefined; //default |
| 30 | } |
| 31 | |
| 32 | // Special pawns movements |
| 33 | getPotentialPawnMoves([x, y]) { |
| 34 | const color = this.turn; |
| 35 | let moves = []; |
| 36 | const [sizeX, sizeY] = [V.size.x, V.size.y]; |
| 37 | const shiftX = color == "w" ? -1 : 1; |
| 38 | const startRank = color == "w" ? sizeX - 2 : 1; |
| 39 | const lastRank = color == "w" ? 0 : sizeX - 1; |
| 40 | const finalPieces = |
| 41 | x + shiftX == lastRank ? [V.ROOK, V.KNIGHT, V.BISHOP, V.QUEEN] : [V.PAWN]; |
| 42 | |
| 43 | // One square diagonally |
| 44 | for (let shiftY of [-1, 1]) { |
| 45 | if (this.board[x + shiftX][y + shiftY] == V.EMPTY) { |
| 46 | for (let piece of finalPieces) { |
| 47 | moves.push( |
| 48 | this.getBasicMove([x, y], [x + shiftX, y + shiftY], { |
| 49 | c: color, |
| 50 | p: piece |
| 51 | }) |
| 52 | ); |
| 53 | } |
| 54 | if ( |
| 55 | x == startRank && |
| 56 | y + 2 * shiftY >= 0 && |
| 57 | y + 2 * shiftY < sizeY && |
| 58 | this.board[x + 2 * shiftX][y + 2 * shiftY] == V.EMPTY |
| 59 | ) { |
| 60 | // Two squares jump |
| 61 | moves.push( |
| 62 | this.getBasicMove([x, y], [x + 2 * shiftX, y + 2 * shiftY]) |
| 63 | ); |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | // Capture |
| 68 | if ( |
| 69 | this.board[x + shiftX][y] != V.EMPTY && |
| 70 | this.canTake([x, y], [x + shiftX, y]) |
| 71 | ) { |
| 72 | for (let piece of finalPieces) |
| 73 | moves.push( |
| 74 | this.getBasicMove([x, y], [x + shiftX, y], { c: color, p: piece }) |
| 75 | ); |
| 76 | } |
| 77 | |
| 78 | // En passant |
| 79 | const Lep = this.epSquares.length; |
| 80 | const epSquare = this.epSquares[Lep - 1]; //always at least one element |
| 81 | if ( |
| 82 | !!epSquare && |
| 83 | epSquare[0].x == x + shiftX && |
| 84 | epSquare[0].y == y && |
| 85 | Math.abs(epSquare[1] - y) == 1 |
| 86 | ) { |
| 87 | let enpassantMove = this.getBasicMove([x, y], [x + shiftX, y]); |
| 88 | enpassantMove.vanish.push({ |
| 89 | x: x, |
| 90 | y: epSquare[1], |
| 91 | p: "p", |
| 92 | c: this.getColor(x, epSquare[1]) |
| 93 | }); |
| 94 | moves.push(enpassantMove); |
| 95 | } |
| 96 | |
| 97 | return moves; |
| 98 | } |
| 99 | |
| 100 | isAttackedByPawn([x, y], colors) { |
| 101 | for (let c of colors) { |
| 102 | let pawnShift = c == "w" ? 1 : -1; |
| 103 | if (x + pawnShift >= 0 && x + pawnShift < V.size.x) { |
| 104 | if ( |
| 105 | this.getPiece(x + pawnShift, y) == V.PAWN && |
| 106 | this.getColor(x + pawnShift, y) == c |
| 107 | ) { |
| 108 | return true; |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | return false; |
| 113 | } |
| 114 | |
| 115 | getNotation(move) { |
| 116 | const piece = this.getPiece(move.start.x, move.start.y); |
| 117 | if (piece == V.PAWN) { |
| 118 | // Pawn move |
| 119 | const finalSquare = V.CoordsToSquare(move.end); |
| 120 | let notation = ""; |
| 121 | if (move.vanish.length == 2) |
| 122 | //capture |
| 123 | notation = "Px" + finalSquare; |
| 124 | else { |
| 125 | // No capture: indicate the initial square for potential ambiguity |
| 126 | const startSquare = V.CoordsToSquare(move.start); |
| 127 | notation = startSquare + finalSquare; |
| 128 | } |
| 129 | if (move.appear[0].p != V.PAWN) |
| 130 | //promotion |
| 131 | notation += "=" + move.appear[0].p.toUpperCase(); |
| 132 | return notation; |
| 133 | } |
| 134 | return super.getNotation(move); //all other pieces are orthodox |
| 135 | } |
| 136 | }; |