e80e13b6a1ee0bc37b790b4f88f8ff9318337811
[vchess.git] / client / src / variants / Alice.js
1 import { ChessRules } from "@/base_rules";
2 import { ArrayFun} from "@/utils/array";
3
4 // NOTE: alternative implementation, probably cleaner = use only 1 board
5 // TODO? atLeastOneMove() would be more efficient if rewritten here (less sideBoard computations)
6 export const VariantRules = class AliceRules extends ChessRules
7 {
8 static get ALICE_PIECES()
9 {
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 };
29 }
30
31 static getPpath(b)
32 {
33 return (Object.keys(this.ALICE_PIECES).includes(b[1]) ? "Alice/" : "") + b;
34 }
35
36 static get PIECES()
37 {
38 return ChessRules.PIECES.concat(Object.keys(V.ALICE_PIECES));
39 }
40
41 setOtherVariables(fen)
42 {
43 super.setOtherVariables(fen);
44 const rows = V.ParseFen(fen).position.split("/");
45 if (this.kingPos["w"][0] < 0 || this.kingPos["b"][0] < 0)
46 {
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++)
49 {
50 let k = 0; //column index on board
51 for (let j=0; j<rows[i].length; j++)
52 {
53 switch (rows[i].charAt(j))
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:
62 const num = parseInt(rows[i].charAt(j));
63 if (!isNaN(num))
64 k += (num-1);
65 }
66 k++;
67 }
68 }
69 }
70 }
71
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);
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
83 // Build board of the given (mirror)side
84 getSideBoard(mirrorSide)
85 {
86 // Build corresponding board from complete board
87 let sideBoard = ArrayFun.init(V.size.x, V.size.y, "");
88 for (let i=0; i<V.size.x; i++)
89 {
90 for (let j=0; j<V.size.y; j++)
91 sideBoard[i][j] = this.getSquareOccupation(i, j, mirrorSide);
92 }
93 return sideBoard;
94 }
95
96 // NOTE: castle & enPassant https://www.chessvariants.com/other.dir/alice.html
97 getPotentialMovesFrom([x,y], sideBoard)
98 {
99 const pieces = Object.keys(V.ALICE_CODES);
100 const codes = Object.keys(V.ALICE_PIECES);
101 const mirrorSide = (pieces.includes(this.getPiece(x,y)) ? 1 : 2);
102 if (!sideBoard)
103 sideBoard = [this.getSideBoard(1), this.getSideBoard(2)];
104 const color = this.getColor(x,y);
105
106 // Search valid moves on sideBoard
107 const saveBoard = this.board;
108 this.board = sideBoard[mirrorSide-1];
109 const moves = super.getPotentialMovesFrom([x,y])
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);
117 if (this.underCheck(color, sideBoard))
118 aprioriValid = false;
119 this.undo(m);
120 }
121 return aprioriValid;
122 });
123 this.board = saveBoard;
124
125 // Finally filter impossible moves
126 const res = moves.filter(m => {
127 if (m.appear.length == 2) //castle
128 {
129 // appear[i] must be an empty square on the other board
130 for (let psq of m.appear)
131 {
132 if (this.getSquareOccupation(psq.x,psq.y,3-mirrorSide) != V.EMPTY)
133 return false;
134 }
135 }
136 else if (this.board[m.end.x][m.end.y] != V.EMPTY)
137 {
138 // Attempt to capture
139 const piece = this.getPiece(m.end.x,m.end.y);
140 if ((mirrorSide==1 && codes.includes(piece))
141 || (mirrorSide==2 && pieces.includes(piece)))
142 {
143 return false;
144 }
145 }
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
150 psq.p = V.ALICE_CODES[psq.p]; //goto board2
151 });
152 }
153 else //move on board2: mark vanishing pieces as Alice
154 {
155 m.vanish.forEach(psq => {
156 psq.p = V.ALICE_CODES[psq.p];
157 });
158 }
159 // Fix en-passant captures
160 if (m.vanish[0].p == V.PAWN && m.vanish.length == 2
161 && this.board[m.end.x][m.end.y] == V.EMPTY)
162 {
163 m.vanish[1].c = V.GetOppCol(this.getColor(x,y));
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)))
169 van.p = V.ALICE_CODES[van.p];
170 else if (mirrorSide==2 && pieces.includes(this.getPiece(van.x,van.y)))
171 van.p = V.ALICE_PIECES[van.p];
172 }
173 return true;
174 });
175 return res;
176 }
177
178 filterValid(moves, sideBoard)
179 {
180 if (moves.length == 0)
181 return [];
182 if (!sideBoard)
183 sideBoard = [this.getSideBoard(1), this.getSideBoard(2)];
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 });
191 }
192
193 getAllValidMoves()
194 {
195 const color = this.turn;
196 const oppCol = V.GetOppCol(color);
197 let potentialMoves = [];
198 const sideBoard = [this.getSideBoard(1), this.getSideBoard(2)];
199 for (var i=0; i<V.size.x; i++)
200 {
201 for (var j=0; j<V.size.y; j++)
202 {
203 if (this.board[i][j] != V.EMPTY && this.getColor(i,j) == color)
204 {
205 Array.prototype.push.apply(potentialMoves,
206 this.getPotentialMovesFrom([i,j], sideBoard));
207 }
208 }
209 }
210 return this.filterValid(potentialMoves, sideBoard);
211 }
212
213 // Play on sideboards [TODO: only one sideBoard required]
214 playSide(move, sideBoard)
215 {
216 const pieces = Object.keys(V.ALICE_CODES);
217 move.vanish.forEach(psq => {
218 const mirrorSide = (pieces.includes(psq.p) ? 1 : 2);
219 sideBoard[mirrorSide-1][psq.x][psq.y] = V.EMPTY;
220 });
221 move.appear.forEach(psq => {
222 const mirrorSide = (pieces.includes(psq.p) ? 1 : 2);
223 const piece = (mirrorSide == 1 ? psq.p : V.ALICE_PIECES[psq.p]);
224 sideBoard[mirrorSide-1][psq.x][psq.y] = psq.c + piece;
225 if (piece == V.KING)
226 this.kingPos[psq.c] = [psq.x,psq.y];
227 });
228 }
229
230 // Undo on sideboards
231 undoSide(move, sideBoard)
232 {
233 const pieces = Object.keys(V.ALICE_CODES);
234 move.appear.forEach(psq => {
235 const mirrorSide = (pieces.includes(psq.p) ? 1 : 2);
236 sideBoard[mirrorSide-1][psq.x][psq.y] = V.EMPTY;
237 });
238 move.vanish.forEach(psq => {
239 const mirrorSide = (pieces.includes(psq.p) ? 1 : 2);
240 const piece = (mirrorSide == 1 ? psq.p : V.ALICE_PIECES[psq.p]);
241 sideBoard[mirrorSide-1][psq.x][psq.y] = psq.c + piece;
242 if (piece == V.KING)
243 this.kingPos[psq.c] = [psq.x,psq.y];
244 });
245 }
246
247 // sideBoard: arg containing both boards (see getAllValidMoves())
248 underCheck(color, sideBoard)
249 {
250 const kp = this.kingPos[color];
251 const mirrorSide = (sideBoard[0][kp[0]][kp[1]] != V.EMPTY ? 1 : 2);
252 let saveBoard = this.board;
253 this.board = sideBoard[mirrorSide-1];
254 let res = this.isAttacked(kp, [V.GetOppCol(color)]);
255 this.board = saveBoard;
256 return res;
257 }
258
259 getCheckSquares(color)
260 {
261 const pieces = Object.keys(V.ALICE_CODES);
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);
265 let saveBoard = this.board;
266 this.board = sideBoard;
267 let res = this.isAttacked(this.kingPos[color], [V.GetOppCol(color)])
268 ? [ JSON.parse(JSON.stringify(this.kingPos[color])) ]
269 : [ ];
270 this.board = saveBoard;
271 return res;
272 }
273
274 updateVariables(move)
275 {
276 super.updateVariables(move); //standard king
277 const piece = move.vanish[0].p;
278 const c = move.vanish[0].c;
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);
291 const c = move.vanish[0].c;
292 if (move.vanish[0].p == "l")
293 this.kingPos[c] = [move.start.x, move.start.y];
294 }
295
296 checkGameEnd()
297 {
298 const pieces = Object.keys(V.ALICE_CODES);
299 const color = this.turn;
300 const kp = this.kingPos[color];
301 const mirrorSide = (pieces.includes(this.getPiece(kp[0],kp[1])) ? 1 : 2);
302 let sideBoard = this.getSideBoard(mirrorSide);
303 let saveBoard = this.board;
304 this.board = sideBoard;
305 let res = "*";
306 if (!this.isAttacked(this.kingPos[color], [V.GetOppCol(color)]))
307 res = "1/2";
308 else
309 res = (color == "w" ? "0-1" : "1-0");
310 this.board = saveBoard;
311 return res;
312 }
313
314 static get VALUES()
315 {
316 return Object.assign(
317 ChessRules.VALUES,
318 {
319 's': 1,
320 'u': 5,
321 'o': 3,
322 'c': 3,
323 't': 9,
324 'l': 1000,
325 }
326 );
327 }
328
329 getNotation(move)
330 {
331 if (move.appear.length == 2 && move.appear[0].p == V.KING)
332 {
333 if (move.end.y < move.start.y)
334 return "0-0-0";
335 else
336 return "0-0";
337 }
338
339 const finalSquare = V.CoordsToSquare(move.end);
340 const piece = this.getPiece(move.start.x, move.start.y);
341
342 const captureMark = (move.vanish.length > move.appear.length ? "x" : "");
343 let pawnMark = "";
344 if (["p","s"].includes(piece) && captureMark.length == 1)
345 pawnMark = V.CoordToColumn(move.start.y); //start column
346
347 // Piece or pawn movement
348 let notation = piece.toUpperCase() + pawnMark + captureMark + finalSquare;
349 if (['s','p'].includes(piece) && !['s','p'].includes(move.appear[0].p))
350 {
351 // Promotion
352 notation += "=" + move.appear[0].p.toUpperCase();
353 }
354 return notation;
355 }
356 }