- // Check if pieces of color in 'colors' are attacking (king) on square x,y
- isAttacked(sq, colors)
- {
- return (this.isAttackedByPawn(sq, colors)
- || this.isAttackedByRook(sq, colors)
- || this.isAttackedByKnight(sq, colors)
- || this.isAttackedByBishop(sq, colors)
- || this.isAttackedByQueen(sq, colors)
- || this.isAttackedByKing(sq, colors));
- }
-
- // Is square x,y attacked by 'colors' pawns ?
- isAttackedByPawn([x,y], colors)
- {
- for (let c of colors)
- {
- let pawnShift = (c=="w" ? 1 : -1);
- if (x+pawnShift>=0 && x+pawnShift<V.size.x)
- {
- for (let i of [-1,1])
- {
- if (y+i>=0 && y+i<V.size.y && this.getPiece(x+pawnShift,y+i)==V.PAWN
- && this.getColor(x+pawnShift,y+i)==c)
- {
- return true;
- }
- }
+ // Check if pieces of given color are attacking (king) on square x,y
+ isAttacked(sq, color) {
+ return (
+ this.isAttackedByPawn(sq, color) ||
+ this.isAttackedByRook(sq, color) ||
+ this.isAttackedByKnight(sq, color) ||
+ this.isAttackedByBishop(sq, color) ||
+ this.isAttackedByQueen(sq, color) ||
+ this.isAttackedByKing(sq, color)
+ );
+ }
+
+ // Generic method for non-pawn pieces ("sliding or jumping"):
+ // is x,y attacked by a piece of given color ?
+ isAttackedBySlideNJump([x, y], color, piece, steps, oneStep) {
+ for (let step of steps) {
+ let rx = x + step[0],
+ ry = y + step[1];
+ while (V.OnBoard(rx, ry) && this.board[rx][ry] == V.EMPTY && !oneStep) {
+ rx += step[0];
+ ry += step[1];
+ }
+ if (
+ V.OnBoard(rx, ry) &&
+ this.getPiece(rx, ry) == piece &&
+ this.getColor(rx, ry) == color
+ ) {
+ return true;