Commit | Line | Data |
---|---|---|
0c3fe8a6 BA |
1 | import { ChessRules } from "@/base_rules"; |
2 | ||
6808d7a1 | 3 | export const VariantRules = class BerolinaRules extends ChessRules { |
dac39588 | 4 | // En-passant after 2-sq jump |
6808d7a1 BA |
5 | getEpSquare(moveOrSquare) { |
6 | if (!moveOrSquare) return undefined; | |
7 | if (typeof moveOrSquare === "string") { | |
dac39588 | 8 | const square = moveOrSquare; |
6808d7a1 | 9 | if (square == "-") return undefined; |
dac39588 BA |
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; | |
6808d7a1 BA |
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) { | |
8d1fcc37 BA |
21 | return [ |
22 | { | |
23 | x: (ex + sx) / 2, | |
24 | y: (move.end.y + sy) / 2 | |
25 | }, | |
26 | // The arrival column must be remembered, because | |
27 | // potentially two pawns could be candidates to be captured: | |
28 | // one on our left, and one on our right. | |
29 | move.end.y | |
30 | ]; | |
dac39588 BA |
31 | } |
32 | return undefined; //default | |
33 | } | |
375ecdd1 | 34 | |
8d1fcc37 BA |
35 | static IsGoodEnpassant(enpassant) { |
36 | if (enpassant != "-") { | |
37 | const epParts = enpassant.split(","); | |
38 | const epSq = V.SquareToCoords(epParts[0]); | |
39 | if (isNaN(epSq.x) || isNaN(epSq.y) || !V.OnBoard(epSq)) return false; | |
40 | const arrCol = V.ColumnToCoord(epParts[1]); | |
41 | if (isNaN(arrCol) || arrCol < 0 || arrCol >= V.size.y) return false; | |
42 | } | |
43 | return true; | |
44 | } | |
45 | ||
46 | getEnpassantFen() { | |
47 | const L = this.epSquares.length; | |
48 | if (!this.epSquares[L - 1]) return "-"; //no en-passant | |
49 | return ( | |
50 | V.CoordsToSquare(this.epSquares[L - 1][0]) + | |
51 | "," + | |
52 | V.CoordToColumn(this.epSquares[L - 1][1]) | |
53 | ); | |
54 | } | |
55 | ||
dac39588 | 56 | // Special pawns movements |
6808d7a1 | 57 | getPotentialPawnMoves([x, y]) { |
dac39588 BA |
58 | const color = this.turn; |
59 | let moves = []; | |
6808d7a1 BA |
60 | const [sizeX, sizeY] = [V.size.x, V.size.y]; |
61 | const shiftX = color == "w" ? -1 : 1; | |
62 | const startRank = color == "w" ? sizeX - 2 : 1; | |
63 | const lastRank = color == "w" ? 0 : sizeX - 1; | |
64 | const finalPieces = | |
65 | x + shiftX == lastRank ? [V.ROOK, V.KNIGHT, V.BISHOP, V.QUEEN] : [V.PAWN]; | |
375ecdd1 | 66 | |
dac39588 | 67 | // One square diagonally |
6808d7a1 BA |
68 | for (let shiftY of [-1, 1]) { |
69 | if (this.board[x + shiftX][y + shiftY] == V.EMPTY) { | |
70 | for (let piece of finalPieces) { | |
71 | moves.push( | |
72 | this.getBasicMove([x, y], [x + shiftX, y + shiftY], { | |
73 | c: color, | |
74 | p: piece | |
75 | }) | |
76 | ); | |
dac39588 | 77 | } |
6808d7a1 BA |
78 | if ( |
79 | x == startRank && | |
80 | y + 2 * shiftY >= 0 && | |
81 | y + 2 * shiftY < sizeY && | |
82 | this.board[x + 2 * shiftX][y + 2 * shiftY] == V.EMPTY | |
83 | ) { | |
dac39588 | 84 | // Two squares jump |
6808d7a1 BA |
85 | moves.push( |
86 | this.getBasicMove([x, y], [x + 2 * shiftX, y + 2 * shiftY]) | |
87 | ); | |
dac39588 BA |
88 | } |
89 | } | |
90 | } | |
91 | // Capture | |
6808d7a1 BA |
92 | if ( |
93 | this.board[x + shiftX][y] != V.EMPTY && | |
94 | this.canTake([x, y], [x + shiftX, y]) | |
95 | ) { | |
dac39588 | 96 | for (let piece of finalPieces) |
6808d7a1 BA |
97 | moves.push( |
98 | this.getBasicMove([x, y], [x + shiftX, y], { c: color, p: piece }) | |
99 | ); | |
dac39588 | 100 | } |
375ecdd1 | 101 | |
dac39588 BA |
102 | // En passant |
103 | const Lep = this.epSquares.length; | |
6808d7a1 BA |
104 | const epSquare = this.epSquares[Lep - 1]; //always at least one element |
105 | if ( | |
106 | !!epSquare && | |
107 | epSquare[0].x == x + shiftX && | |
8d1fcc37 | 108 | epSquare[0].y == y |
6808d7a1 BA |
109 | ) { |
110 | let enpassantMove = this.getBasicMove([x, y], [x + shiftX, y]); | |
dac39588 BA |
111 | enpassantMove.vanish.push({ |
112 | x: x, | |
113 | y: epSquare[1], | |
6808d7a1 BA |
114 | p: "p", |
115 | c: this.getColor(x, epSquare[1]) | |
dac39588 BA |
116 | }); |
117 | moves.push(enpassantMove); | |
118 | } | |
375ecdd1 | 119 | |
dac39588 BA |
120 | return moves; |
121 | } | |
f6dbe8e3 | 122 | |
6808d7a1 BA |
123 | isAttackedByPawn([x, y], colors) { |
124 | for (let c of colors) { | |
125 | let pawnShift = c == "w" ? 1 : -1; | |
126 | if (x + pawnShift >= 0 && x + pawnShift < V.size.x) { | |
127 | if ( | |
128 | this.getPiece(x + pawnShift, y) == V.PAWN && | |
129 | this.getColor(x + pawnShift, y) == c | |
130 | ) { | |
dac39588 BA |
131 | return true; |
132 | } | |
133 | } | |
134 | } | |
135 | return false; | |
136 | } | |
26c1e3bd | 137 | |
6808d7a1 | 138 | getNotation(move) { |
dac39588 | 139 | const piece = this.getPiece(move.start.x, move.start.y); |
6808d7a1 | 140 | if (piece == V.PAWN) { |
dac39588 BA |
141 | // Pawn move |
142 | const finalSquare = V.CoordsToSquare(move.end); | |
143 | let notation = ""; | |
6808d7a1 BA |
144 | if (move.vanish.length == 2) |
145 | //capture | |
dac39588 | 146 | notation = "Px" + finalSquare; |
6808d7a1 | 147 | else { |
dac39588 BA |
148 | // No capture: indicate the initial square for potential ambiguity |
149 | const startSquare = V.CoordsToSquare(move.start); | |
150 | notation = startSquare + finalSquare; | |
151 | } | |
6808d7a1 BA |
152 | if (move.appear[0].p != V.PAWN) |
153 | //promotion | |
dac39588 BA |
154 | notation += "=" + move.appear[0].p.toUpperCase(); |
155 | return notation; | |
156 | } | |
157 | return super.getNotation(move); //all other pieces are orthodox | |
158 | } | |
6808d7a1 | 159 | }; |