5f8d9c093d4adb52a68fbcfe41243d2bc2b9a088
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) {
23 y: (move.end
.y
+ sy
) / 2
26 return undefined; //default
29 // Special pawns movements
30 getPotentialPawnMoves([x
, y
]) {
31 const color
= this.turn
;
33 const [sizeX
, sizeY
] = [V
.size
.x
, V
.size
.y
];
34 const shiftX
= color
== "w" ? -1 : 1;
35 const startRank
= color
== "w" ? sizeX
- 2 : 1;
36 const lastRank
= color
== "w" ? 0 : sizeX
- 1;
38 x
+ shiftX
== lastRank
? [V
.ROOK
, V
.KNIGHT
, V
.BISHOP
, V
.QUEEN
] : [V
.PAWN
];
40 // One square diagonally
41 for (let shiftY
of [-1, 1]) {
42 if (this.board
[x
+ shiftX
][y
+ shiftY
] == V
.EMPTY
) {
43 for (let piece
of finalPieces
) {
45 this.getBasicMove([x
, y
], [x
+ shiftX
, y
+ shiftY
], {
53 y
+ 2 * shiftY
>= 0 &&
54 y
+ 2 * shiftY
< sizeY
&&
55 this.board
[x
+ 2 * shiftX
][y
+ 2 * shiftY
] == V
.EMPTY
59 this.getBasicMove([x
, y
], [x
+ 2 * shiftX
, y
+ 2 * shiftY
])
66 this.board
[x
+ shiftX
][y
] != V
.EMPTY
&&
67 this.canTake([x
, y
], [x
+ shiftX
, y
])
69 for (let piece
of finalPieces
)
71 this.getBasicMove([x
, y
], [x
+ shiftX
, y
], { c: color
, p: piece
})
76 const Lep
= this.epSquares
.length
;
77 const epSquare
= this.epSquares
[Lep
- 1]; //always at least one element
80 epSquare
[0].x
== x
+ shiftX
&&
82 Math
.abs(epSquare
[1] - y
) == 1
84 let enpassantMove
= this.getBasicMove([x
, y
], [x
+ shiftX
, y
]);
85 enpassantMove
.vanish
.push({
89 c: this.getColor(x
, epSquare
[1])
91 moves
.push(enpassantMove
);
97 isAttackedByPawn([x
, y
], colors
) {
98 for (let c
of colors
) {
99 let pawnShift
= c
== "w" ? 1 : -1;
100 if (x
+ pawnShift
>= 0 && x
+ pawnShift
< V
.size
.x
) {
102 this.getPiece(x
+ pawnShift
, y
) == V
.PAWN
&&
103 this.getColor(x
+ pawnShift
, y
) == c
113 const piece
= this.getPiece(move.start
.x
, move.start
.y
);
114 if (piece
== V
.PAWN
) {
116 const finalSquare
= V
.CoordsToSquare(move.end
);
118 if (move.vanish
.length
== 2)
120 notation
= "Px" + finalSquare
;
122 // No capture: indicate the initial square for potential ambiguity
123 const startSquare
= V
.CoordsToSquare(move.start
);
124 notation
= startSquare
+ finalSquare
;
126 if (move.appear
[0].p
!= V
.PAWN
)
128 notation
+= "=" + move.appear
[0].p
.toUpperCase();
131 return super.getNotation(move); //all other pieces are orthodox