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