374a620c932069fe6661e18f9d0fbe73f5b459a8
1 import { ChessRules
, PiPo
, Move
} from "@/base_rules";
3 export const VariantRules
= class EnpassantRules
extends ChessRules
{
5 static IsGoodEnpassant(enpassant
) {
6 if (enpassant
!= "-") {
7 const squares
= enpassant
.split(",");
8 for (let sq
of squares
) {
9 const ep
= V
.SquareToCoords(sq
);
10 if (isNaN(ep
.x
) || !V
.OnBoard(ep
)) return false;
16 getEpSquare(moveOrSquare
) {
17 if (!moveOrSquare
) return undefined;
18 if (typeof moveOrSquare
=== "string") {
19 const square
= moveOrSquare
;
20 if (square
== "-") return undefined;
22 square
.split(",").forEach(sq
=> {
23 res
.push(V
.SquareToCoords(sq
));
27 // Argument is a move: all intermediate squares are en-passant candidates,
28 // except if the moving piece is a king.
29 const move = moveOrSquare
;
30 const piece
= move.appear
[0].p
;
31 if (piece
== V
.KING
||
33 Math
.abs(move.end
.x
-move.start
.x
) <= 1 &&
34 Math
.abs(move.end
.y
-move.start
.y
) <= 1
39 const delta
= [move.end
.x
-move.start
.x
, move.end
.y
-move.start
.y
];
41 if (piece
== V
.KNIGHT
) {
42 const divisor
= Math
.min(Math
.abs(delta
[0]), Math
.abs(delta
[1]));
43 step
= [delta
[0]/divisor
|| 0, delta
[1]/divisor
|| 0];
45 step
= [delta
[0]/Math
.abs(delta
[0]) || 0, delta
[1]/Math
.abs(delta
[1]) || 0];
49 let [x
,y
] = [move.start
.x
+step
[0],move.start
.y
+step
[1]];
50 x
!= move.end
.x
|| y
!= move.end
.y
;
51 x
+= step
[0], y
+= step
[1]
55 // Add final square to know which piece is taken en passant:
61 const L
= this.epSquares
.length
;
62 if (!this.epSquares
[L
- 1]) return "-"; //no en-passant
64 this.epSquares
[L
- 1].forEach(sq
=> {
65 res
+= V
.CoordsToSquare(sq
) + ",";
67 return res
.slice(0, -1); //remove last comma
70 // TODO: this getPotentialPawnMovesFrom() is mostly duplicated:
71 // it could be split in "capture", "promotion", "enpassant"...
72 getPotentialPawnMoves([x
, y
]) {
73 const color
= this.turn
;
75 const [sizeX
, sizeY
] = [V
.size
.x
, V
.size
.y
];
76 const shiftX
= color
== "w" ? -1 : 1;
77 const startRank
= color
== "w" ? sizeX
- 2 : 1;
78 const lastRank
= color
== "w" ? 0 : sizeX
- 1;
81 x
+ shiftX
== lastRank
82 ? [V
.ROOK
, V
.KNIGHT
, V
.BISHOP
, V
.QUEEN
]
85 if (this.board
[x
+ shiftX
][y
] == V
.EMPTY
) {
86 for (let piece
of finalPieces
) {
88 this.getBasicMove([x
, y
], [x
+ shiftX
, y
], {
96 this.board
[x
+ 2 * shiftX
][y
] == V
.EMPTY
99 moves
.push(this.getBasicMove([x
, y
], [x
+ 2 * shiftX
, y
]));
103 for (let shiftY
of [-1, 1]) {
106 y
+ shiftY
< sizeY
&&
107 this.board
[x
+ shiftX
][y
+ shiftY
] != V
.EMPTY
&&
108 this.canTake([x
, y
], [x
+ shiftX
, y
+ shiftY
])
110 for (let piece
of finalPieces
) {
112 this.getBasicMove([x
, y
], [x
+ shiftX
, y
+ shiftY
], {
122 const Lep
= this.epSquares
.length
;
123 const squares
= this.epSquares
[Lep
- 1];
125 const S
= squares
.length
;
126 const taken
= squares
[S
-1];
127 const pipoV
= new PiPo({
130 p: this.getPiece(taken
.x
, taken
.y
),
131 c: this.getColor(taken
.x
, taken
.y
)
133 [...Array(S
-1).keys()].forEach(i
=> {
134 const sq
= squares
[i
];
135 if (sq
.x
== x
+ shiftX
&& Math
.abs(sq
.y
- y
) == 1) {
136 let enpassantMove
= this.getBasicMove([x
, y
], [sq
.x
, sq
.y
]);
137 enpassantMove
.vanish
.push(pipoV
);
138 moves
.push(enpassantMove
);
146 // Remove the "onestep" condition: knight promote to knightrider:
148 getPotentialKnightMoves(sq
) {
149 return this.getSlideNJumpMoves(sq
, V
.steps
[V
.KNIGHT
]);
152 isAttackedByKnight(sq
, colors
) {
153 return this.isAttackedBySlideNJump(
161 getPotentialMovesFrom([x
, y
]) {
162 let moves
= super.getPotentialMovesFrom([x
,y
]);
163 // Add en-passant captures from this square:
164 const L
= this.epSquares
.length
;
165 if (!this.epSquares
[L
- 1]) return moves
;
166 const squares
= this.epSquares
[L
- 1];
167 const S
= squares
.length
;
168 // Object describing the removed opponent's piece:
169 const pipoV
= new PiPo({
172 c: V
.GetOppCol(this.turn
),
173 p: this.getPiece(squares
[S
-1].x
, squares
[S
-1].y
)
175 // Check if existing non-capturing moves could also capture en passant
178 m
.appear
[0].p
!= V
.PAWN
&& //special pawn case is handled elsewhere
179 m
.vanish
.length
<= 1 &&
180 [...Array(S
-1).keys()].some(i
=> {
181 return m
.end
.x
== squares
[i
].x
&& m
.end
.y
== squares
[i
].y
;
184 m
.vanish
.push(pipoV
);
187 // Special case of the king knight's movement:
188 if (this.getPiece(x
, y
) == V
.KING
) {
189 V
.steps
[V
.KNIGHT
].forEach(step
=> {
190 const endX
= x
+ step
[0];
191 const endY
= y
+ step
[1];
193 V
.OnBoard(endX
, endY
) &&
194 [...Array(S
-1).keys()].some(i
=> {
195 return endX
== squares
[i
].x
&& endY
== squares
[i
].y
;
198 let enpassantMove
= this.getBasicMove([x
, y
], [endX
, endY
]);
199 enpassantMove
.vanish
.push(pipoV
);
200 moves
.push(enpassantMove
);
207 static get VALUES() {