A few fixes (for updateCastleFlags()) + add Madhouse and Pocketknight variants
[vchess.git] / client / src / variants / Absorption.js
1 import { ChessRules } from "@/base_rules";
2
3 export class AbsorptionRules extends ChessRules {
4 getPpath(b) {
5 if ([V.BN, V.RN, V.QN].includes(b[1])) return "Absorption/" + b;
6 return b;
7 }
8
9 // Three new pieces: rook+knight, bishop+knight and queen+knight
10 static get RN() {
11 // Empress
12 return 'e';
13 }
14 static get BN() {
15 // Princess
16 return 's';
17 }
18 static get QN() {
19 // Amazon
20 return 'a';
21 }
22
23 static get PIECES() {
24 return ChessRules.PIECES.concat([V.RN, V.BN, V.QN]);
25 }
26
27 static get MergeComposed() {
28 return {
29 "be": "a",
30 "bs": "s",
31 "er": "e",
32 "rs": "a",
33 "eq": "a",
34 "qs": "a",
35 "ee": "e",
36 "es": "a",
37 "ss": "s"
38 };
39 }
40
41 static Fusion(p1, p2) {
42 if (p1 == V.KING) return p1;
43 if (p1 == V.PAWN) return p2;
44 if (p2 == V.PAWN) return p1;
45 if ([p1, p2].includes(V.KNIGHT)) {
46 if ([p1, p2].includes(V.QUEEN)) return V.QN;
47 if ([p1, p2].includes(V.ROOK)) return V.RN;
48 if ([p1, p2].includes(V.BISHOP)) return V.BN;
49 // p1 or p2 already have knight + other piece
50 return (p1 == V.KNIGHT ? p2 : p1);
51 }
52 for (let p of [p1, p2]) {
53 if (p == V.QN) return V.QN;
54 if ([V.BN, V.RN].includes(p))
55 return V.MergeComposed[[p1, p2].sort().join("")];
56 }
57 // bishop + rook, or queen + [bishop or rook]
58 return V.QUEEN;
59 }
60
61 getPotentialMovesFrom(sq) {
62 let moves = [];
63 const piece = this.getPiece(sq[0], sq[1]);
64 switch (piece) {
65 case V.RN:
66 moves =
67 super.getPotentialRookMoves(sq).concat(
68 super.getPotentialKnightMoves(sq));
69 break;
70 case V.BN:
71 moves =
72 super.getPotentialBishopMoves(sq).concat(
73 super.getPotentialKnightMoves(sq));
74 break;
75 case V.QN:
76 moves =
77 super.getPotentialQueenMoves(sq).concat(
78 super.getPotentialKnightMoves(sq));
79 break;
80 default:
81 moves = super.getPotentialMovesFrom(sq);
82 }
83 moves.forEach(m => {
84 if (m.vanish.length == 2) {
85 // Augment pieces abilities in case of captures
86 const piece2 = m.vanish[1].p;
87 if (piece != piece2) m.appear[0].p = V.Fusion(piece, piece2);
88 }
89 });
90 return moves;
91 }
92
93 isAttacked(sq, color) {
94 return (
95 super.isAttacked(sq, color) ||
96 this.isAttackedByBN(sq, color) ||
97 this.isAttackedByRN(sq, color) ||
98 this.isAttackedByQN(sq, color)
99 );
100 }
101
102 isAttackedByBN(sq, color) {
103 return (
104 this.isAttackedBySlideNJump(sq, color, V.BN, V.steps[V.BISHOP]) ||
105 this.isAttackedBySlideNJump(
106 sq, color, V.BN, V.steps[V.KNIGHT], "oneStep")
107 );
108 }
109
110 isAttackedByRN(sq, color) {
111 return (
112 this.isAttackedBySlideNJump(sq, color, V.RN, V.steps[V.ROOK]) ||
113 this.isAttackedBySlideNJump(
114 sq, color, V.RN, V.steps[V.KNIGHT], "oneStep")
115 );
116 }
117
118 isAttackedByQN(sq, color) {
119 return (
120 this.isAttackedBySlideNJump(
121 sq, color, V.QN, V.steps[V.BISHOP].concat(V.steps[V.ROOK])) ||
122 this.isAttackedBySlideNJump(
123 sq, color, V.QN, V.steps[V.KNIGHT], "oneStep")
124 );
125 }
126
127 static get VALUES() {
128 return Object.assign(
129 { a: 12, e: 7, s: 5 },
130 ChessRules.VALUES
131 );
132 }
133
134 getNotation(move) {
135 let notation = super.getNotation(move);
136 if (move.vanish[0].p != V.PAWN && move.appear[0].p != move.vanish[0].p)
137 // Fusion (not from a pawn: handled in ChessRules)
138 notation += "=" + move.appear[0].p.toUpperCase();
139 return notation;
140 }
141 };