Fix bug in Allmate2
[vchess.git] / client / src / variants / Allmate2.js
CommitLineData
e9b736ee
BA
1import { ChessRules, PiPo, Move } from "@/base_rules";
2
32f6285e 3export class Allmate2Rules extends ChessRules {
e9b736ee
BA
4 static get HasEnpassant() {
5 return false;
6 }
7
e9b736ee
BA
8 getCheckSquares() {
9 // No notion of check
10 return [];
11 }
12
7ba4a5bc 13 static GenRandInitFen(randomness) {
3a2a7b5f 14 return ChessRules.GenRandInitFen(randomness).slice(0, -2);
e9b736ee
BA
15 }
16
17 getPotentialMovesFrom([x, y]) {
18 let moves = super.getPotentialMovesFrom([x, y]);
4404e58c
BA
19 // Remove standard captures (without removing castling):
20 moves = moves.filter(m => {
21 return m.vanish.length == 1 || m.appear.length == 2;
22 });
e9b736ee
BA
23
24 // Augment moves with "mate-captures":
25 // TODO: this is coded in a highly inefficient way...
26 const color = this.turn;
27 const oppCol = V.GetOppCol(this.turn);
28 moves.forEach(m => {
29 this.play(m);
30
31 // 1) What is attacked?
32 let attacked = {};
33 for (let i=0; i<V.size.x; i++) {
34 for (let j=0; j<V.size.y; j++) {
32f6285e 35 if (this.getColor(i,j) == oppCol && this.isAttacked([i,j], color))
e9b736ee
BA
36 attacked[i+"_"+j] = [i,j];
37 }
38 }
39
40 // 2) Among attacked pieces, which cannot escape capture?
3a2a7b5f 41 // --> without (normal-)capturing: difference with Allmate1 variant
e9b736ee
BA
42 // Avoid "oppMoves = this.getAllValidMoves();" => infinite recursion
43 outerLoop: for (let i=0; i<V.size.x; i++) {
44 for (let j=0; j<V.size.y; j++) {
45 if (this.getColor(i,j) == oppCol) {
46 let oppMoves = [];
47 switch (this.getPiece(i, j)) {
48 case V.PAWN:
49 oppMoves = this.getPotentialPawnMoves([i, j]);
50 break;
51 case V.ROOK:
52 oppMoves = this.getPotentialRookMoves([i, j]);
53 break;
54 case V.KNIGHT:
55 oppMoves = this.getPotentialKnightMoves([i, j]);
56 break;
57 case V.BISHOP:
58 oppMoves = this.getPotentialBishopMoves([i, j]);
59 break;
60 case V.QUEEN:
61 oppMoves = this.getPotentialQueenMoves([i, j]);
62 break;
63 case V.KING:
fd7295e8
BA
64 // Do not allow castling to escape from check
65 oppMoves = super.getSlideNJumpMoves(
6f10b383 66 [i, j],
fd7295e8
BA
67 V.steps[V.ROOK].concat(V.steps[V.BISHOP]),
68 "oneStep"
69 );
e9b736ee
BA
70 break;
71 }
72 for (let om of oppMoves) {
0eebb0df 73 if (om.vanish.length == 2)
9618937e
BA
74 // Skip captures: forbidden in this mode
75 continue;
e9b736ee
BA
76 V.PlayOnBoard(this.board, om);
77 Object.values(attacked).forEach(sq => {
78 const origSq = [sq[0], sq[1]];
79 if (om.start.x == sq[0] && om.start.y == sq[1])
80 // Piece moved:
81 sq = [om.appear[0].x, om.appear[0].y];
32f6285e 82 if (!this.isAttacked(sq, color))
e9b736ee
BA
83 delete attacked[origSq[0]+"_"+origSq[1]];
84 });
85 V.UndoOnBoard(this.board, om);
86 if (Object.keys(attacked).length == 0)
87 // No need to explore more moves
88 break outerLoop;
89 }
90 }
91 }
92 }
0eebb0df 93 this.undo(m);
e9b736ee
BA
94
95 // 3) Add mate-captures:
96 Object.values(attacked).forEach(sq => {
97 m.vanish.push(new PiPo({
98 x: sq[0],
99 y: sq[1],
100 c: oppCol,
101 p: this.getPiece(sq[0], sq[1])
102 }));
103 });
e9b736ee
BA
104 });
105
106 return moves;
107 }
108
109 // No "under check" conditions in castling
a6836242
BA
110 getCastleMoves(sq) {
111 return super.getCastleMoves(sq, "castleInCheck");
e9b736ee
BA
112 }
113
54b7b81b 114 // TODO: allow pieces to "commit suicide"? (Currently yes except king)
e9b736ee 115 filterValid(moves) {
54b7b81b
BA
116 // Remove moves which let the king mate-captured:
117 if (moves.length == 0) return [];
118 const color = this.turn;
119 const oppCol = V.GetOppCol(color);
120 return moves.filter(m => {
121 let res = true;
122 this.play(m);
123 if (this.underCheck(color)) {
124 res = false;
125 const attacked = this.kingPos[color];
126 // Try to find a move to escape check
127 // TODO: very inefficient method.
128 outerLoop: for (let i=0; i<V.size.x; i++) {
129 for (let j=0; j<V.size.y; j++) {
130 if (this.getColor(i,j) == color) {
131 let emoves = [];
132 // Artficial turn change to "play twice":
133 this.turn = color;
134 switch (this.getPiece(i, j)) {
135 case V.PAWN:
136 emoves = this.getPotentialPawnMoves([i, j]);
137 break;
138 case V.ROOK:
139 emoves = this.getPotentialRookMoves([i, j]);
140 break;
141 case V.KNIGHT:
142 emoves = this.getPotentialKnightMoves([i, j]);
143 break;
144 case V.BISHOP:
145 emoves = this.getPotentialBishopMoves([i, j]);
146 break;
147 case V.QUEEN:
148 emoves = this.getPotentialQueenMoves([i, j]);
149 break;
150 case V.KING:
151 emoves = this.getPotentialKingMoves([i, j]);
152 break;
153 }
154 this.turn = oppCol;
155 for (let em of emoves) {
156 V.PlayOnBoard(this.board, em);
157 let sq = attacked;
158 if (em.start.x == attacked[0] && em.start.y == attacked[1])
159 // King moved:
160 sq = [em.appear[0].x, em.appear[0].y];
32f6285e 161 if (!this.isAttacked(sq, oppCol))
54b7b81b
BA
162 res = true;
163 V.UndoOnBoard(this.board, em);
164 if (res)
165 // No need to explore more moves
166 break outerLoop;
167 }
168 }
169 }
170 }
171 }
172 this.undo(m);
173 return res;
174 });
e9b736ee
BA
175 }
176
3a2a7b5f
BA
177 postPlay(move) {
178 super.postPlay(move);
094e11f2 179 if (move.vanish.length >= 2 && move.appear.length == 1) {
3a2a7b5f
BA
180 for (let i = 1; i<move.vanish.length; i++) {
181 const v = move.vanish[i];
094e11f2
BA
182 // Did opponent king disappeared?
183 if (v.p == V.KING)
184 this.kingPos[this.turn] = [-1, -1];
185 // Or maybe a rook?
186 else if (v.p == V.ROOK) {
187 if (v.y < this.INIT_COL_KING[v.c])
3a2a7b5f 188 this.castleFlags[v.c][0] = 8;
094e11f2
BA
189 else
190 // v.y > this.INIT_COL_KING[v.c]
3a2a7b5f 191 this.castleFlags[v.c][1] = 8;
094e11f2 192 }
3a2a7b5f 193 }
e9b736ee
BA
194 }
195 }
196
3a2a7b5f
BA
197 preUndo(move) {
198 super.preUndo(move);
199 const oppCol = this.turn;
094e11f2 200 if (move.vanish.length >= 2 && move.appear.length == 1) {
e9b736ee 201 // Did opponent king disappeared?
3a2a7b5f 202 const psq = move.vanish.find(v => v.p == V.KING && v.c == oppCol)
094e11f2
BA
203 if (psq)
204 this.kingPos[psq.c] = [psq.x, psq.y];
e9b736ee
BA
205 }
206 }
207
208 getCurrentScore() {
209 const color = this.turn;
210 const kp = this.kingPos[color];
211 if (kp[0] < 0)
212 // King disappeared
213 return color == "w" ? "0-1" : "1-0";
bb688df5 214 if (this.atLeastOneMove()) return "*";
e9b736ee
BA
215 // Kings still there, no moves:
216 return "1/2";
217 }
218
219 static get SEARCH_DEPTH() {
b83a675a 220 return 1;
e9b736ee
BA
221 }
222
223 getNotation(move) {
224 let notation = super.getNotation(move);
225 // Add a capture mark (not describing what is captured...):
23ecf008 226 if (move.vanish.length > 1 && move.appear.length == 1) {
188b4a8f 227 if (!!(notation.match(/^[a-h]x/)))
23ecf008
BA
228 // Pawn capture: remove initial "b" in bxc4 for example
229 notation = notation.substr(1);
230 notation = notation.replace("x","") + "X";
54b7b81b 231 }
e9b736ee
BA
232 return notation;
233 }
234};