1 import { ChessRules
} from "@/base_rules";
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:
12 const epParts
= square
.split(",");
13 res
.push(V
.SquareToCoords(epParts
[0]));
14 res
.push(V
.ColumnToCoord(epParts
[1]));
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) {
24 y: (move.end
.y
+ sy
) / 2
29 return undefined; //default
32 // Special pawns movements
33 getPotentialPawnMoves([x
, y
]) {
34 const color
= this.turn
;
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;
41 x
+ shiftX
== lastRank
? [V
.ROOK
, V
.KNIGHT
, V
.BISHOP
, V
.QUEEN
] : [V
.PAWN
];
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
) {
48 this.getBasicMove([x
, y
], [x
+ shiftX
, y
+ shiftY
], {
56 y
+ 2 * shiftY
>= 0 &&
57 y
+ 2 * shiftY
< sizeY
&&
58 this.board
[x
+ 2 * shiftX
][y
+ 2 * shiftY
] == V
.EMPTY
62 this.getBasicMove([x
, y
], [x
+ 2 * shiftX
, y
+ 2 * shiftY
])
69 this.board
[x
+ shiftX
][y
] != V
.EMPTY
&&
70 this.canTake([x
, y
], [x
+ shiftX
, y
])
72 for (let piece
of finalPieces
)
74 this.getBasicMove([x
, y
], [x
+ shiftX
, y
], { c: color
, p: piece
})
79 const Lep
= this.epSquares
.length
;
80 const epSquare
= this.epSquares
[Lep
- 1]; //always at least one element
83 epSquare
[0].x
== x
+ shiftX
&&
85 Math
.abs(epSquare
[1] - y
) == 1
87 let enpassantMove
= this.getBasicMove([x
, y
], [x
+ shiftX
, y
]);
88 enpassantMove
.vanish
.push({
92 c: this.getColor(x
, epSquare
[1])
94 moves
.push(enpassantMove
);
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
) {
105 this.getPiece(x
+ pawnShift
, y
) == V
.PAWN
&&
106 this.getColor(x
+ pawnShift
, y
) == c
116 const piece
= this.getPiece(move.start
.x
, move.start
.y
);
117 if (piece
== V
.PAWN
) {
119 const finalSquare
= V
.CoordsToSquare(move.end
);
121 if (move.vanish
.length
== 2)
123 notation
= "Px" + finalSquare
;
125 // No capture: indicate the initial square for potential ambiguity
126 const startSquare
= V
.CoordsToSquare(move.start
);
127 notation
= startSquare
+ finalSquare
;
129 if (move.appear
[0].p
!= V
.PAWN
)
131 notation
+= "=" + move.appear
[0].p
.toUpperCase();
134 return super.getNotation(move); //all other pieces are orthodox