Fix non-augmenting self absorptions
[vchess.git] / client / src / variants / Selfabsorption.js
CommitLineData
e023d747
BA
1import { ChessRules } from "@/base_rules";
2import { AbsorptionRules } from "@/variants/Absorption";
3
4export class SelfabsorptionRules extends AbsorptionRules {
5
6 canTake([x1, y1], [x2, y2]) {
7 if (this.getColor(x1, y1) !== this.getColor(x2, y2)) return true;
8 const p1 = this.getPiece(x1, y1);
9 const p2 = this.getPiece(x2, y2);
10 return (
11 p1 != p2 &&
12 [V.QUEEN, V.ROOK, V.KNIGHT, V.BISHOP].includes(p1) &&
34b79fe4
BA
13 [V.QUEEN, V.ROOK, V.KNIGHT, V.BISHOP].includes(p2) &&
14 (p1 != V.QUEEN || p2 == V.KNIGHT) &&
15 (p2 != V.QUEEN || p1 == V.KNIGHT)
e023d747
BA
16 );
17 }
18
19 getPotentialMovesFrom(sq) {
20 let moves = [];
21 const piece = this.getPiece(sq[0], sq[1]);
22 switch (piece) {
23 case V.RN:
24 moves =
25 super.getPotentialRookMoves(sq).concat(
26 super.getPotentialKnightMoves(sq));
27 break;
28 case V.BN:
29 moves =
30 super.getPotentialBishopMoves(sq).concat(
31 super.getPotentialKnightMoves(sq));
32 break;
33 case V.QN:
34 moves =
35 super.getPotentialQueenMoves(sq).concat(
36 super.getPotentialKnightMoves(sq));
37 break;
38 case V.PAWN:
39 moves = super.getPotentialPawnMoves(sq);
40 break;
41 case V.ROOK:
42 moves = super.getPotentialRookMoves(sq);
43 break;
44 case V.KNIGHT:
45 moves = super.getPotentialKnightMoves(sq);
46 break;
47 case V.BISHOP:
48 moves = super.getPotentialBishopMoves(sq);
49 break;
50 case V.QUEEN:
51 moves = super.getPotentialQueenMoves(sq);
52 break;
53 case V.KING:
54 moves = super.getPotentialKingMoves(sq);
55 break;
56 }
57 moves.forEach(m => {
58 if (
59 m.vanish.length == 2 && m.appear.length == 1 &&
60 piece != m.vanish[1].p && m.vanish[0].c == m.vanish[1].c
61 ) {
62 // Augment pieces abilities in case of self-captures
63 m.appear[0].p = V.Fusion(piece, m.vanish[1].p);
64 }
65 });
66 return moves;
67 }
68
69};