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