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