1 import { ChessRules
} from "@/base_rules";
3 export class ZenRules
extends ChessRules
{
5 getEpSquare(moveOrSquare
) {
6 if (!moveOrSquare
) return undefined;
7 if (typeof moveOrSquare
=== "string") {
8 const square
= moveOrSquare
;
9 if (square
== "-") return undefined;
10 return V
.SquareToCoords(square
);
12 const move = moveOrSquare
;
16 // Exclude captures (of rooks for example)
17 move.vanish
.length
== 1 &&
19 Math
.abs(s
.x
- e
.x
) == 2 &&
20 move.appear
[0].p
== V
.PAWN
30 // follow steps from x,y until something is met.
31 // if met piece is opponent and same movement (asA): eat it!
32 findCaptures_aux([x
, y
], asA
) {
33 const color
= this.getColor(x
, y
);
38 ? V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
])
49 const oneStep
= [V
.KNIGHT
,V
.PAWN
].includes(asA
); //we don't capture king
50 const lastRank
= color
== "w" ? 0 : V
.size
.x
- 1;
51 const promotionPieces
= [V
.ROOK
, V
.KNIGHT
, V
.BISHOP
, V
.QUEEN
];
52 const oppCol
= V
.GetOppCol(color
);
53 outerLoop: for (let loop
= 0; loop
< steps
.length
; loop
++) {
54 const step
= steps
[loop
];
57 while (V
.OnBoard(i
, j
) && this.board
[i
][j
] == V
.EMPTY
) {
58 if (oneStep
) continue outerLoop
;
64 this.board
[i
][j
] != V
.EMPTY
&&
65 this.getColor(i
, j
) == oppCol
&&
66 this.getPiece(i
, j
) == asA
69 if (this.getPiece(x
, y
) == V
.PAWN
&& i
== lastRank
) {
70 // Special case of promotion:
71 promotionPieces
.forEach(p
=> {
72 moves
.push(this.getBasicMove([x
, y
], [i
, j
], { c: color
, p: p
}));
77 moves
.push(this.getBasicMove([x
, y
], [i
, j
]));
84 // Find possible captures from a square: look in every direction!
87 Array
.prototype.push
.apply(moves
, this.findCaptures_aux(sq
, V
.PAWN
));
88 Array
.prototype.push
.apply(moves
, this.findCaptures_aux(sq
, V
.ROOK
));
89 Array
.prototype.push
.apply(moves
, this.findCaptures_aux(sq
, V
.KNIGHT
));
90 Array
.prototype.push
.apply(moves
, this.findCaptures_aux(sq
, V
.BISHOP
));
91 Array
.prototype.push
.apply(moves
, this.findCaptures_aux(sq
, V
.QUEEN
));
96 return false; //captures handled separately
99 getPotentialMovesFrom(sq
) {
100 return super.getPotentialMovesFrom(sq
).concat(this.findCaptures(sq
));
104 // Recognize special moves first
105 if (move.appear
.length
== 2) {
107 if (move.end
.y
< move.start
.y
) return "0-0-0";
111 // Translate initial square (because pieces may fly unusually!)
112 const initialSquare
= V
.CoordsToSquare(move.start
);
114 // Translate final square
115 const finalSquare
= V
.CoordsToSquare(move.end
);
118 const piece
= this.getPiece(move.start
.x
, move.start
.y
);
119 if (piece
== V
.PAWN
) {
120 // pawn move (TODO: enPassant indication)
121 if (move.vanish
.length
== 2) {
123 notation
= initialSquare
+ "x" + finalSquare
;
125 else notation
= finalSquare
;
126 if (piece
!= move.appear
[0].p
)
128 notation
+= "=" + move.appear
[0].p
.toUpperCase();
132 notation
= piece
.toUpperCase();
133 if (move.vanish
.length
> 1) notation
+= initialSquare
+ "x";
134 notation
+= finalSquare
;
139 static get VALUES() {