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