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