e5a222bb8de5a84a26b898909dc45238cd014602
1 import { ChessRules
} from "@/base_rules";
3 export 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
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.
32 return undefined; //default
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;
47 const L
= this.epSquares
.length
;
48 if (!this.epSquares
[L
- 1]) return "-"; //no en-passant
50 V
.CoordsToSquare(this.epSquares
[L
- 1][0]) +
52 V
.CoordToColumn(this.epSquares
[L
- 1][1])
56 // Special pawns movements
57 getPotentialPawnMoves([x
, y
]) {
58 const color
= this.turn
;
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;
65 x
+ shiftX
== lastRank
66 ? [V
.ROOK
, V
.KNIGHT
, V
.BISHOP
, V
.QUEEN
]
69 // One square diagonally
70 for (let shiftY
of [-1, 1]) {
71 if (this.board
[x
+ shiftX
][y
+ shiftY
] == V
.EMPTY
) {
72 for (let piece
of finalPieces
) {
74 this.getBasicMove([x
, y
], [x
+ shiftX
, y
+ shiftY
], {
81 V
.PawnSpecs
.twoSquares
&&
83 y
+ 2 * shiftY
>= 0 &&
84 y
+ 2 * shiftY
< sizeY
&&
85 this.board
[x
+ 2 * shiftX
][y
+ 2 * shiftY
] == V
.EMPTY
89 this.getBasicMove([x
, y
], [x
+ 2 * shiftX
, y
+ 2 * shiftY
])
96 this.board
[x
+ shiftX
][y
] != V
.EMPTY
&&
97 this.canTake([x
, y
], [x
+ shiftX
, y
])
99 for (let piece
of finalPieces
)
101 this.getBasicMove([x
, y
], [x
+ shiftX
, y
], { c: color
, p: piece
})
105 // Next condition so that other variants could inherit from this class
106 if (V
.PawnSpecs
.enPassant
) {
108 const Lep
= this.epSquares
.length
;
109 const epSquare
= this.epSquares
[Lep
- 1]; //always at least one element
112 epSquare
[0].x
== x
+ shiftX
&&
115 let enpassantMove
= this.getBasicMove([x
, y
], [x
+ shiftX
, y
]);
116 enpassantMove
.vanish
.push({
120 c: this.getColor(x
, epSquare
[1])
122 moves
.push(enpassantMove
);
129 isAttackedByPawn([x
, y
], color
) {
130 let pawnShift
= (color
== "w" ? 1 : -1);
131 if (x
+ pawnShift
>= 0 && x
+ pawnShift
< V
.size
.x
) {
133 this.getPiece(x
+ pawnShift
, y
) == V
.PAWN
&&
134 this.getColor(x
+ pawnShift
, y
) == color
142 static get SEARCH_DEPTH() {
147 const piece
= this.getPiece(move.start
.x
, move.start
.y
);
148 if (piece
== V
.PAWN
) {
150 const finalSquare
= V
.CoordsToSquare(move.end
);
152 if (move.vanish
.length
== 2)
154 notation
= "Px" + finalSquare
;
156 // No capture: indicate the initial square for potential ambiguity
157 const startSquare
= V
.CoordsToSquare(move.start
);
158 notation
= startSquare
+ finalSquare
;
160 if (move.appear
[0].p
!= V
.PAWN
)
162 notation
+= "=" + move.appear
[0].p
.toUpperCase();
165 return super.getNotation(move); //all other pieces are orthodox