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