Add Queenpawns + small fix to vs pawns variants + remove HQ inventor (webmaster but...
[vchess.git] / client / src / variants / Alice.js
CommitLineData
03608482 1import { ChessRules } from "@/base_rules";
6808d7a1 2import { ArrayFun } from "@/utils/array";
b7c32f1a 3
4b353936 4// NOTE: alternative implementation, probably cleaner = use only 1 board
2c5d7b20
BA
5// TODO? atLeastOneMove() would be more efficient if rewritten here
6// (less sideBoard computations)
32f6285e 7export class AliceRules extends ChessRules {
6808d7a1 8 static get ALICE_PIECES() {
dac39588 9 return {
6808d7a1
BA
10 s: "p",
11 t: "q",
12 u: "r",
13 c: "b",
14 o: "n",
15 l: "k"
dac39588
BA
16 };
17 }
6808d7a1 18 static get ALICE_CODES() {
dac39588 19 return {
6808d7a1
BA
20 p: "s",
21 q: "t",
22 r: "u",
23 b: "c",
24 n: "o",
25 k: "l"
dac39588
BA
26 };
27 }
a3eb4cc5 28
6808d7a1 29 static get PIECES() {
dac39588
BA
30 return ChessRules.PIECES.concat(Object.keys(V.ALICE_PIECES));
31 }
7931e479 32
241bf8f2 33 getPpath(b) {
41217d97 34 return (Object.keys(V.ALICE_PIECES).includes(b[1]) ? "Alice/" : "") + b;
241bf8f2
BA
35 }
36
1c15969e
BA
37 getEpSquare(moveOrSquare) {
38 if (!moveOrSquare) return undefined;
39 if (typeof moveOrSquare === "string") {
40 const square = moveOrSquare;
41 if (square == "-") return undefined;
42 return V.SquareToCoords(square);
43 }
44 // Argument is a move:
45 const move = moveOrSquare;
46 const s = move.start,
47 e = move.end;
48 if (
49 s.y == e.y &&
50 Math.abs(s.x - e.x) == 2 &&
51 // Special conditions: a pawn can be on the other side
52 ['p','s'].includes(move.appear[0].p) &&
53 ['p','s'].includes(move.vanish[0].p)
54 ) {
55 return {
56 x: (s.x + e.x) / 2,
57 y: s.y
58 };
59 }
60 return undefined; //default
61 }
62
b9ce3d0f
BA
63 // king can be l or L (on the other mirror side)
64 static IsGoodPosition(position) {
65 if (position.length == 0) return false;
66 const rows = position.split("/");
67 if (rows.length != V.size.x) return false;
68 let kings = { "k": 0, "K": 0, 'l': 0, 'L': 0 };
69 for (let row of rows) {
70 let sumElts = 0;
71 for (let i = 0; i < row.length; i++) {
72 if (['K','k','L','l'].includes(row[i])) kings[row[i]]++;
73 if (V.PIECES.includes(row[i].toLowerCase())) sumElts++;
74 else {
e50a8025 75 const num = parseInt(row[i], 10);
b9ce3d0f
BA
76 if (isNaN(num)) return false;
77 sumElts += num;
78 }
79 }
80 if (sumElts != V.size.y) return false;
81 }
82 if (kings['k'] + kings['l'] != 1 || kings['K'] + kings['L'] != 1)
83 return false;
84 return true;
85 }
86
6808d7a1 87 setOtherVariables(fen) {
dac39588
BA
88 super.setOtherVariables(fen);
89 const rows = V.ParseFen(fen).position.split("/");
6808d7a1 90 if (this.kingPos["w"][0] < 0 || this.kingPos["b"][0] < 0) {
2c5d7b20
BA
91 // INIT_COL_XXX won't be required if Alice kings are found
92 // (it means 'king moved')
6808d7a1 93 for (let i = 0; i < rows.length; i++) {
dac39588 94 let k = 0; //column index on board
6808d7a1
BA
95 for (let j = 0; j < rows[i].length; j++) {
96 switch (rows[i].charAt(j)) {
97 case "l":
98 this.kingPos["b"] = [i, k];
dac39588 99 break;
6808d7a1
BA
100 case "L":
101 this.kingPos["w"] = [i, k];
dac39588 102 break;
6808d7a1 103 default: {
e50a8025 104 const num = parseInt(rows[i].charAt(j), 10);
6808d7a1
BA
105 if (!isNaN(num)) k += num - 1;
106 }
dac39588
BA
107 }
108 k++;
109 }
110 }
111 }
112 }
0b5fa571 113
dac39588 114 // Return the (standard) color+piece notation at a square for a board
6808d7a1
BA
115 getSquareOccupation(i, j, mirrorSide) {
116 const piece = this.getPiece(i, j);
117 if (mirrorSide == 1 && Object.keys(V.ALICE_CODES).includes(piece))
dac39588 118 return this.board[i][j];
6808d7a1
BA
119 if (mirrorSide == 2 && Object.keys(V.ALICE_PIECES).includes(piece))
120 return this.getColor(i, j) + V.ALICE_PIECES[piece];
dac39588
BA
121 return "";
122 }
364128d9 123
dac39588 124 // Build board of the given (mirror)side
6808d7a1 125 getSideBoard(mirrorSide) {
dac39588
BA
126 // Build corresponding board from complete board
127 let sideBoard = ArrayFun.init(V.size.x, V.size.y, "");
6808d7a1
BA
128 for (let i = 0; i < V.size.x; i++) {
129 for (let j = 0; j < V.size.y; j++)
dac39588
BA
130 sideBoard[i][j] = this.getSquareOccupation(i, j, mirrorSide);
131 }
132 return sideBoard;
133 }
0cd8f2bd 134
2c5d7b20
BA
135 // NOTE: castle & enPassant
136 // https://www.chessvariants.com/other.dir/alice.html
6808d7a1 137 getPotentialMovesFrom([x, y], sideBoard) {
dac39588
BA
138 const pieces = Object.keys(V.ALICE_CODES);
139 const codes = Object.keys(V.ALICE_PIECES);
6808d7a1
BA
140 const mirrorSide = pieces.includes(this.getPiece(x, y)) ? 1 : 2;
141 if (!sideBoard) sideBoard = [this.getSideBoard(1), this.getSideBoard(2)];
142 const color = this.getColor(x, y);
0cd8f2bd 143
dac39588
BA
144 // Search valid moves on sideBoard
145 const saveBoard = this.board;
6808d7a1
BA
146 this.board = sideBoard[mirrorSide - 1];
147 const moves = super.getPotentialMovesFrom([x, y]).filter(m => {
148 // Filter out king moves which result in under-check position on
149 // current board (before mirror traversing)
150 let aprioriValid = true;
151 if (m.appear[0].p == V.KING) {
152 this.play(m);
153 if (this.underCheck(color, sideBoard)) aprioriValid = false;
154 this.undo(m);
155 }
156 return aprioriValid;
157 });
dac39588 158 this.board = saveBoard;
0cd8f2bd 159
dac39588
BA
160 // Finally filter impossible moves
161 const res = moves.filter(m => {
6808d7a1 162 if (m.appear.length == 2) {
6b7b2cf7 163 // Castle: appear[i] must be an empty square on the other board
6808d7a1 164 for (let psq of m.appear) {
2c5d7b20
BA
165 if (
166 this.getSquareOccupation(psq.x, psq.y, 3 - mirrorSide) != V.EMPTY
167 ) {
dac39588 168 return false;
2c5d7b20 169 }
dac39588 170 }
6808d7a1 171 } else if (this.board[m.end.x][m.end.y] != V.EMPTY) {
dac39588 172 // Attempt to capture
6808d7a1
BA
173 const piece = this.getPiece(m.end.x, m.end.y);
174 if (
175 (mirrorSide == 1 && codes.includes(piece)) ||
176 (mirrorSide == 2 && pieces.includes(piece))
177 ) {
dac39588
BA
178 return false;
179 }
180 }
181 // If the move is computed on board1, m.appear change for Alice pieces.
6808d7a1
BA
182 if (mirrorSide == 1) {
183 m.appear.forEach(psq => {
f9c36b2d 184 // forEach: castling taken into account
dac39588
BA
185 psq.p = V.ALICE_CODES[psq.p]; //goto board2
186 });
f9c36b2d 187 }
6808d7a1 188 else {
f9c36b2d 189 // Move on board2: mark vanishing pieces as Alice
dac39588
BA
190 m.vanish.forEach(psq => {
191 psq.p = V.ALICE_CODES[psq.p];
192 });
193 }
194 // Fix en-passant captures
6808d7a1
BA
195 if (
196 m.vanish[0].p == V.PAWN &&
197 m.vanish.length == 2 &&
198 this.board[m.end.x][m.end.y] == V.EMPTY
199 ) {
294fe29f
BA
200 m.vanish[1].c = V.GetOppCol(this.turn);
201 const [epX, epY] = [m.vanish[1].x, m.vanish[1].y];
202 m.vanish[1].p = this.getPiece(epX, epY);
dac39588
BA
203 }
204 return true;
205 });
206 return res;
207 }
b8121223 208
6808d7a1
BA
209 filterValid(moves, sideBoard) {
210 if (moves.length == 0) return [];
211 if (!sideBoard) sideBoard = [this.getSideBoard(1), this.getSideBoard(2)];
dac39588
BA
212 const color = this.turn;
213 return moves.filter(m => {
214 this.playSide(m, sideBoard); //no need to track flags
215 const res = !this.underCheck(color, sideBoard);
216 this.undoSide(m, sideBoard);
217 return res;
218 });
219 }
0cd8f2bd 220
6808d7a1 221 getAllValidMoves() {
dac39588 222 const color = this.turn;
dac39588
BA
223 let potentialMoves = [];
224 const sideBoard = [this.getSideBoard(1), this.getSideBoard(2)];
6808d7a1
BA
225 for (var i = 0; i < V.size.x; i++) {
226 for (var j = 0; j < V.size.y; j++) {
227 if (this.board[i][j] != V.EMPTY && this.getColor(i, j) == color) {
228 Array.prototype.push.apply(
229 potentialMoves,
230 this.getPotentialMovesFrom([i, j], sideBoard)
231 );
dac39588
BA
232 }
233 }
234 }
235 return this.filterValid(potentialMoves, sideBoard);
236 }
b8121223 237
dac39588 238 // Play on sideboards [TODO: only one sideBoard required]
6808d7a1 239 playSide(move, sideBoard) {
dac39588
BA
240 const pieces = Object.keys(V.ALICE_CODES);
241 move.vanish.forEach(psq => {
6808d7a1
BA
242 const mirrorSide = pieces.includes(psq.p) ? 1 : 2;
243 sideBoard[mirrorSide - 1][psq.x][psq.y] = V.EMPTY;
dac39588
BA
244 });
245 move.appear.forEach(psq => {
6808d7a1
BA
246 const mirrorSide = pieces.includes(psq.p) ? 1 : 2;
247 const piece = mirrorSide == 1 ? psq.p : V.ALICE_PIECES[psq.p];
248 sideBoard[mirrorSide - 1][psq.x][psq.y] = psq.c + piece;
249 if (piece == V.KING) this.kingPos[psq.c] = [psq.x, psq.y];
dac39588
BA
250 });
251 }
4b353936 252
dac39588 253 // Undo on sideboards
6808d7a1 254 undoSide(move, sideBoard) {
dac39588
BA
255 const pieces = Object.keys(V.ALICE_CODES);
256 move.appear.forEach(psq => {
6808d7a1
BA
257 const mirrorSide = pieces.includes(psq.p) ? 1 : 2;
258 sideBoard[mirrorSide - 1][psq.x][psq.y] = V.EMPTY;
dac39588
BA
259 });
260 move.vanish.forEach(psq => {
6808d7a1
BA
261 const mirrorSide = pieces.includes(psq.p) ? 1 : 2;
262 const piece = mirrorSide == 1 ? psq.p : V.ALICE_PIECES[psq.p];
263 sideBoard[mirrorSide - 1][psq.x][psq.y] = psq.c + piece;
264 if (piece == V.KING) this.kingPos[psq.c] = [psq.x, psq.y];
dac39588
BA
265 });
266 }
4b353936 267
1c9f093d 268 // sideBoard: arg containing both boards (see getAllValidMoves())
6808d7a1 269 underCheck(color, sideBoard) {
dac39588 270 const kp = this.kingPos[color];
6808d7a1 271 const mirrorSide = sideBoard[0][kp[0]][kp[1]] != V.EMPTY ? 1 : 2;
dac39588 272 let saveBoard = this.board;
6808d7a1 273 this.board = sideBoard[mirrorSide - 1];
dac39588
BA
274 let res = this.isAttacked(kp, [V.GetOppCol(color)]);
275 this.board = saveBoard;
276 return res;
277 }
0cd8f2bd 278
af34341d
BA
279 getCheckSquares() {
280 const color = this.turn;
dac39588
BA
281 const pieces = Object.keys(V.ALICE_CODES);
282 const kp = this.kingPos[color];
6808d7a1 283 const mirrorSide = pieces.includes(this.getPiece(kp[0], kp[1])) ? 1 : 2;
dac39588
BA
284 let sideBoard = this.getSideBoard(mirrorSide);
285 let saveBoard = this.board;
286 this.board = sideBoard;
287 let res = this.isAttacked(this.kingPos[color], [V.GetOppCol(color)])
6808d7a1
BA
288 ? [JSON.parse(JSON.stringify(this.kingPos[color]))]
289 : [];
dac39588
BA
290 this.board = saveBoard;
291 return res;
292 }
270968d6 293
3a2a7b5f
BA
294 postPlay(move) {
295 super.postPlay(move); //standard king
dac39588
BA
296 const piece = move.vanish[0].p;
297 const c = move.vanish[0].c;
298 // "l" = Alice king
6808d7a1 299 if (piece == "l") {
dac39588
BA
300 this.kingPos[c][0] = move.appear[0].x;
301 this.kingPos[c][1] = move.appear[0].y;
3a2a7b5f 302 this.castleFlags[c] = [8, 8];
dac39588
BA
303 }
304 }
0b5fa571 305
3a2a7b5f
BA
306 postUndo(move) {
307 super.postUndo(move);
dac39588 308 const c = move.vanish[0].c;
3a2a7b5f
BA
309 if (move.vanish[0].p == "l")
310 this.kingPos[c] = [move.start.x, move.start.y];
dac39588 311 }
0b5fa571 312
6808d7a1 313 getCurrentScore() {
bb688df5 314 if (this.atLeastOneMove()) return "*";
0c3fe8a6 315 const pieces = Object.keys(V.ALICE_CODES);
dac39588
BA
316 const color = this.turn;
317 const kp = this.kingPos[color];
6808d7a1 318 const mirrorSide = pieces.includes(this.getPiece(kp[0], kp[1])) ? 1 : 2;
dac39588
BA
319 let sideBoard = this.getSideBoard(mirrorSide);
320 let saveBoard = this.board;
321 this.board = sideBoard;
322 let res = "*";
323 if (!this.isAttacked(this.kingPos[color], [V.GetOppCol(color)]))
324 res = "1/2";
6808d7a1 325 else res = color == "w" ? "0-1" : "1-0";
dac39588
BA
326 this.board = saveBoard;
327 return res;
328 }
9de73b71 329
6808d7a1 330 static get VALUES() {
a97bdbda
BA
331 return Object.assign(
332 {
333 s: 1,
334 u: 5,
335 o: 3,
336 c: 3,
337 t: 9,
338 l: 1000
339 },
340 ChessRules.VALUES
341 );
dac39588 342 }
0f51ef98 343
b83a675a
BA
344 static get SEARCH_DEPTH() {
345 return 2;
346 }
347
6808d7a1
BA
348 getNotation(move) {
349 if (move.appear.length == 2 && move.appear[0].p == V.KING) {
350 if (move.end.y < move.start.y) return "0-0-0";
351 return "0-0";
dac39588 352 }
0f51ef98 353
dac39588
BA
354 const finalSquare = V.CoordsToSquare(move.end);
355 const piece = this.getPiece(move.start.x, move.start.y);
0f51ef98 356
6808d7a1 357 const captureMark = move.vanish.length > move.appear.length ? "x" : "";
dac39588 358 let pawnMark = "";
6808d7a1 359 if (["p", "s"].includes(piece) && captureMark.length == 1)
dac39588 360 pawnMark = V.CoordToColumn(move.start.y); //start column
0f51ef98 361
dac39588
BA
362 // Piece or pawn movement
363 let notation = piece.toUpperCase() + pawnMark + captureMark + finalSquare;
6e0f2842 364 if (["s", "p"].includes(piece) && !["s", "p"].includes(move.appear[0].p))
dac39588
BA
365 // Promotion
366 notation += "=" + move.appear[0].p.toUpperCase();
dac39588
BA
367 return notation;
368 }
6808d7a1 369};