Advance in playing against computer
[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
03608482 5export const VariantRules = class AliceRules extends ChessRules
5bfb0956 6{
0cd8f2bd 7 static get ALICE_PIECES()
5bfb0956 8 {
270968d6
BA
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 };
5bfb0956 28 }
a3eb4cc5 29
0cd8f2bd
BA
30 static getPpath(b)
31 {
270968d6 32 return (Object.keys(this.ALICE_PIECES).includes(b[1]) ? "Alice/" : "") + b;
0cd8f2bd
BA
33 }
34
2d7194bd
BA
35 static get PIECES()
36 {
7931e479
BA
37 return ChessRules.PIECES.concat(Object.keys(V.ALICE_PIECES));
38 }
39
2d7194bd 40 setOtherVariables(fen)
0b5fa571 41 {
2d7194bd
BA
42 super.setOtherVariables(fen);
43 const rows = V.ParseFen(fen).position.split("/");
0b5fa571
BA
44 if (this.kingPos["w"][0] < 0 || this.kingPos["b"][0] < 0)
45 {
2d7194bd
BA
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++)
0b5fa571
BA
48 {
49 let k = 0; //column index on board
2d7194bd 50 for (let j=0; j<rows[i].length; j++)
0b5fa571 51 {
2d7194bd 52 switch (rows[i].charAt(j))
0b5fa571
BA
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:
2d7194bd 61 const num = parseInt(rows[i].charAt(j));
0b5fa571
BA
62 if (!isNaN(num))
63 k += (num-1);
64 }
65 k++;
66 }
67 }
68 }
69 }
70
364128d9
BA
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);
364128d9
BA
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
b8121223
BA
82 // Build board of the given (mirror)side
83 getSideBoard(mirrorSide)
0cd8f2bd 84 {
270968d6 85 // Build corresponding board from complete board
b7c32f1a 86 let sideBoard = ArrayFun.init(V.size.x, V.size.y, "");
0b7d99ec 87 for (let i=0; i<V.size.x; i++)
0cd8f2bd 88 {
0b7d99ec 89 for (let j=0; j<V.size.y; j++)
364128d9 90 sideBoard[i][j] = this.getSquareOccupation(i, j, mirrorSide);
0cd8f2bd 91 }
270968d6
BA
92 return sideBoard;
93 }
0cd8f2bd 94
55eb331d 95 // NOTE: castle & enPassant https://www.chessvariants.com/other.dir/alice.html
b8121223 96 getPotentialMovesFrom([x,y], sideBoard)
270968d6 97 {
0b7d99ec
BA
98 const pieces = Object.keys(V.ALICE_CODES);
99 const codes = Object.keys(V.ALICE_PIECES);
b8121223 100 const mirrorSide = (pieces.includes(this.getPiece(x,y)) ? 1 : 2);
b6487fb9 101 const color = this.getColor(x,y);
0cd8f2bd 102
270968d6
BA
103 // Search valid moves on sideBoard
104 let saveBoard = this.board;
b8121223 105 this.board = sideBoard || this.getSideBoard(mirrorSide);
b6487fb9
BA
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 });
0cd8f2bd
BA
120 this.board = saveBoard;
121
122 // Finally filter impossible moves
b8121223 123 let res = moves.filter(m => {
55eb331d 124 if (m.appear.length == 2) //castle
270968d6 125 {
364128d9
BA
126 // appear[i] must be an empty square on the other board
127 for (let psq of m.appear)
128 {
0b7d99ec 129 if (this.getSquareOccupation(psq.x,psq.y,3-mirrorSide) != V.EMPTY)
55eb331d 130 return false;
364128d9 131 }
55eb331d 132 }
0b7d99ec 133 else if (this.board[m.end.x][m.end.y] != V.EMPTY)
55eb331d
BA
134 {
135 // Attempt to capture
270968d6 136 const piece = this.getPiece(m.end.x,m.end.y);
06ddfe34
BA
137 if ((mirrorSide==1 && codes.includes(piece))
138 || (mirrorSide==2 && pieces.includes(piece)))
270968d6
BA
139 {
140 return false;
141 }
142 }
f6cc7faf
BA
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
0b7d99ec 147 psq.p = V.ALICE_CODES[psq.p]; //goto board2
f6cc7faf
BA
148 });
149 }
0b5fa571
BA
150 else //move on board2: mark vanishing pieces as Alice
151 {
152 m.vanish.forEach(psq => {
0b7d99ec 153 psq.p = V.ALICE_CODES[psq.p];
0b5fa571
BA
154 });
155 }
9de73b71 156 // Fix en-passant captures
0b7d99ec
BA
157 if (m.vanish[0].p == V.PAWN && m.vanish.length == 2
158 && this.board[m.end.x][m.end.y] == V.EMPTY)
06ddfe34 159 {
26b8e4f7 160 m.vanish[1].c = V.GetOppCol(this.getColor(x,y));
06ddfe34
BA
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)))
0b7d99ec 166 van.p = V.ALICE_CODES[van.p];
06ddfe34 167 else if (mirrorSide==2 && pieces.includes(this.getPiece(van.x,van.y)))
0b7d99ec 168 van.p = V.ALICE_PIECES[van.p];
06ddfe34 169 }
270968d6
BA
170 return true;
171 });
b8121223
BA
172 return res;
173 }
174
4b353936 175 filterValid(moves)
b8121223
BA
176 {
177 if (moves.length == 0)
178 return [];
4b353936 179 let sideBoard = [this.getSideBoard(1), this.getSideBoard(2)];
f6dbe8e3
BA
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 });
0cd8f2bd
BA
187 }
188
b8121223
BA
189 getAllValidMoves()
190 {
191 const color = this.turn;
26b8e4f7 192 const oppCol = V.GetOppCol(color);
b8121223 193 var potentialMoves = [];
b8121223 194 let sideBoard = [this.getSideBoard(1), this.getSideBoard(2)];
0b7d99ec 195 for (var i=0; i<V.size.x; i++)
b8121223 196 {
0b7d99ec 197 for (var j=0; j<V.size.y; j++)
b8121223 198 {
0b7d99ec 199 if (this.board[i][j] != V.EMPTY && this.getColor(i,j) == color)
b8121223
BA
200 {
201 const mirrorSide =
0b7d99ec 202 Object.keys(V.ALICE_CODES).includes(this.getPiece(i,j))
92342261
BA
203 ? 1
204 : 2;
b8121223
BA
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
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
f6dbe8e3 247 underCheck(color, sideBoard) //sideBoard arg always provided
4b353936 248 {
b8121223 249 const kp = this.kingPos[color];
0b7d99ec 250 const mirrorSide = (sideBoard[0][kp[0]][kp[1]] != V.EMPTY ? 1 : 2);
270968d6 251 let saveBoard = this.board;
4b353936 252 this.board = sideBoard[mirrorSide-1];
26b8e4f7 253 let res = this.isAttacked(kp, [V.GetOppCol(color)]);
270968d6 254 this.board = saveBoard;
0cd8f2bd
BA
255 return res;
256 }
257
f6dbe8e3 258 getCheckSquares(color)
0cd8f2bd 259 {
0b7d99ec 260 const pieces = Object.keys(V.ALICE_CODES);
b8121223
BA
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);
270968d6
BA
264 let saveBoard = this.board;
265 this.board = sideBoard;
26b8e4f7 266 let res = this.isAttacked(this.kingPos[color], [V.GetOppCol(color)])
270968d6
BA
267 ? [ JSON.parse(JSON.stringify(this.kingPos[color])) ]
268 : [ ];
269 this.board = saveBoard;
270968d6 270 return res;
0cd8f2bd 271 }
270968d6 272
0b5fa571
BA
273 updateVariables(move)
274 {
275 super.updateVariables(move); //standard king
388e4c40
BA
276 const piece = move.vanish[0].p;
277 const c = move.vanish[0].c;
0b5fa571
BA
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);
388e4c40
BA
290 const c = move.vanish[0].c;
291 if (move.vanish[0].p == "l")
0b5fa571
BA
292 this.kingPos[c] = [move.start.x, move.start.y];
293 }
294
0cd8f2bd
BA
295 checkGameEnd()
296 {
0b7d99ec 297 const pieces = Object.keys(V.ALICE_CODES);
0cd8f2bd 298 const color = this.turn;
b8121223
BA
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);
270968d6
BA
302 let saveBoard = this.board;
303 this.board = sideBoard;
304 let res = "*";
26b8e4f7 305 if (!this.isAttacked(this.kingPos[color], [V.GetOppCol(color)]))
270968d6
BA
306 res = "1/2";
307 else
308 res = (color == "w" ? "0-1" : "1-0");
309 this.board = saveBoard;
310 return res;
0cd8f2bd 311 }
9de73b71 312
2d7194bd
BA
313 static get VALUES()
314 {
92342261
BA
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 );
9de73b71 326 }
0f51ef98
BA
327
328 getNotation(move)
329 {
0b7d99ec 330 if (move.appear.length == 2 && move.appear[0].p == V.KING)
0f51ef98
BA
331 {
332 if (move.end.y < move.start.y)
333 return "0-0-0";
334 else
335 return "0-0";
336 }
337
2d7194bd 338 const finalSquare = V.CoordsToSquare(move.end);
0f51ef98
BA
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)
26c1e3bd 344 pawnMark = V.CoordToColumn(move.start.y); //start column
0f51ef98
BA
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 }
5bfb0956 355}