Saving current state
[vchess.git] / client / src / variants / Alice.js
1 import { ChessRules } from "@/base_rules";
2 // NOTE: alternative implementation, probably cleaner = use only 1 board
3 export const VariantRules = class AliceRules extends ChessRules
4 {
5 static get ALICE_PIECES()
6 {
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 };
26 }
27
28 static getPpath(b)
29 {
30 return (Object.keys(this.ALICE_PIECES).includes(b[1]) ? "Alice/" : "") + b;
31 }
32
33 static get PIECES()
34 {
35 return ChessRules.PIECES.concat(Object.keys(V.ALICE_PIECES));
36 }
37
38 setOtherVariables(fen)
39 {
40 super.setOtherVariables(fen);
41 const rows = V.ParseFen(fen).position.split("/");
42 if (this.kingPos["w"][0] < 0 || this.kingPos["b"][0] < 0)
43 {
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++)
46 {
47 let k = 0; //column index on board
48 for (let j=0; j<rows[i].length; j++)
49 {
50 switch (rows[i].charAt(j))
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:
59 const num = parseInt(rows[i].charAt(j));
60 if (!isNaN(num))
61 k += (num-1);
62 }
63 k++;
64 }
65 }
66 }
67 }
68
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);
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
80 // Build board of the given (mirror)side
81 getSideBoard(mirrorSide)
82 {
83 // Build corresponding board from complete board
84 let sideBoard = doubleArray(V.size.x, V.size.y, "");
85 for (let i=0; i<V.size.x; i++)
86 {
87 for (let j=0; j<V.size.y; j++)
88 sideBoard[i][j] = this.getSquareOccupation(i, j, mirrorSide);
89 }
90 return sideBoard;
91 }
92
93 // NOTE: castle & enPassant https://www.chessvariants.com/other.dir/alice.html
94 getPotentialMovesFrom([x,y], sideBoard)
95 {
96 const pieces = Object.keys(V.ALICE_CODES);
97 const codes = Object.keys(V.ALICE_PIECES);
98 const mirrorSide = (pieces.includes(this.getPiece(x,y)) ? 1 : 2);
99 const color = this.getColor(x,y);
100
101 // Search valid moves on sideBoard
102 let saveBoard = this.board;
103 this.board = sideBoard || this.getSideBoard(mirrorSide);
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 });
118 this.board = saveBoard;
119
120 // Finally filter impossible moves
121 let res = moves.filter(m => {
122 if (m.appear.length == 2) //castle
123 {
124 // appear[i] must be an empty square on the other board
125 for (let psq of m.appear)
126 {
127 if (this.getSquareOccupation(psq.x,psq.y,3-mirrorSide) != V.EMPTY)
128 return false;
129 }
130 }
131 else if (this.board[m.end.x][m.end.y] != V.EMPTY)
132 {
133 // Attempt to capture
134 const piece = this.getPiece(m.end.x,m.end.y);
135 if ((mirrorSide==1 && codes.includes(piece))
136 || (mirrorSide==2 && pieces.includes(piece)))
137 {
138 return false;
139 }
140 }
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
145 psq.p = V.ALICE_CODES[psq.p]; //goto board2
146 });
147 }
148 else //move on board2: mark vanishing pieces as Alice
149 {
150 m.vanish.forEach(psq => {
151 psq.p = V.ALICE_CODES[psq.p];
152 });
153 }
154 // Fix en-passant captures
155 if (m.vanish[0].p == V.PAWN && m.vanish.length == 2
156 && this.board[m.end.x][m.end.y] == V.EMPTY)
157 {
158 m.vanish[1].c = V.GetOppCol(this.getColor(x,y));
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)))
164 van.p = V.ALICE_CODES[van.p];
165 else if (mirrorSide==2 && pieces.includes(this.getPiece(van.x,van.y)))
166 van.p = V.ALICE_PIECES[van.p];
167 }
168 return true;
169 });
170 return res;
171 }
172
173 filterValid(moves)
174 {
175 if (moves.length == 0)
176 return [];
177 let sideBoard = [this.getSideBoard(1), this.getSideBoard(2)];
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 });
185 }
186
187 getAllValidMoves()
188 {
189 const color = this.turn;
190 const oppCol = V.GetOppCol(color);
191 var potentialMoves = [];
192 let sideBoard = [this.getSideBoard(1), this.getSideBoard(2)];
193 for (var i=0; i<V.size.x; i++)
194 {
195 for (var j=0; j<V.size.y; j++)
196 {
197 if (this.board[i][j] != V.EMPTY && this.getColor(i,j) == color)
198 {
199 const mirrorSide =
200 Object.keys(V.ALICE_CODES).includes(this.getPiece(i,j))
201 ? 1
202 : 2;
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
211 // Play on sideboards [TODO: only one sideBoard required]
212 playSide(move, sideBoard)
213 {
214 const pieces = Object.keys(V.ALICE_CODES);
215 move.vanish.forEach(psq => {
216 const mirrorSide = (pieces.includes(psq.p) ? 1 : 2);
217 sideBoard[mirrorSide-1][psq.x][psq.y] = V.EMPTY;
218 });
219 move.appear.forEach(psq => {
220 const mirrorSide = (pieces.includes(psq.p) ? 1 : 2);
221 const piece = (mirrorSide == 1 ? psq.p : V.ALICE_PIECES[psq.p]);
222 sideBoard[mirrorSide-1][psq.x][psq.y] = psq.c + piece;
223 if (piece == V.KING)
224 this.kingPos[psq.c] = [psq.x,psq.y];
225 });
226 }
227
228 // Undo on sideboards
229 undoSide(move, sideBoard)
230 {
231 const pieces = Object.keys(V.ALICE_CODES);
232 move.appear.forEach(psq => {
233 const mirrorSide = (pieces.includes(psq.p) ? 1 : 2);
234 sideBoard[mirrorSide-1][psq.x][psq.y] = V.EMPTY;
235 });
236 move.vanish.forEach(psq => {
237 const mirrorSide = (pieces.includes(psq.p) ? 1 : 2);
238 const piece = (mirrorSide == 1 ? psq.p : V.ALICE_PIECES[psq.p]);
239 sideBoard[mirrorSide-1][psq.x][psq.y] = psq.c + piece;
240 if (piece == V.KING)
241 this.kingPos[psq.c] = [psq.x,psq.y];
242 });
243 }
244
245 underCheck(color, sideBoard) //sideBoard arg always provided
246 {
247 const kp = this.kingPos[color];
248 const mirrorSide = (sideBoard[0][kp[0]][kp[1]] != V.EMPTY ? 1 : 2);
249 let saveBoard = this.board;
250 this.board = sideBoard[mirrorSide-1];
251 let res = this.isAttacked(kp, [V.GetOppCol(color)]);
252 this.board = saveBoard;
253 return res;
254 }
255
256 getCheckSquares(color)
257 {
258 const pieces = Object.keys(V.ALICE_CODES);
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);
262 let saveBoard = this.board;
263 this.board = sideBoard;
264 let res = this.isAttacked(this.kingPos[color], [V.GetOppCol(color)])
265 ? [ JSON.parse(JSON.stringify(this.kingPos[color])) ]
266 : [ ];
267 this.board = saveBoard;
268 return res;
269 }
270
271 updateVariables(move)
272 {
273 super.updateVariables(move); //standard king
274 const piece = move.vanish[0].p;
275 const c = move.vanish[0].c;
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);
288 const c = move.vanish[0].c;
289 if (move.vanish[0].p == "l")
290 this.kingPos[c] = [move.start.x, move.start.y];
291 }
292
293 checkGameEnd()
294 {
295 const pieces = Object.keys(V.ALICE_CODES);
296 const color = this.turn;
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);
300 let saveBoard = this.board;
301 this.board = sideBoard;
302 let res = "*";
303 if (!this.isAttacked(this.kingPos[color], [V.GetOppCol(color)]))
304 res = "1/2";
305 else
306 res = (color == "w" ? "0-1" : "1-0");
307 this.board = saveBoard;
308 return res;
309 }
310
311 static get VALUES()
312 {
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 );
324 }
325
326 getNotation(move)
327 {
328 if (move.appear.length == 2 && move.appear[0].p == V.KING)
329 {
330 if (move.end.y < move.start.y)
331 return "0-0-0";
332 else
333 return "0-0";
334 }
335
336 const finalSquare = V.CoordsToSquare(move.end);
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)
342 pawnMark = V.CoordToColumn(move.start.y); //start column
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 }
353 }