Some code cleaning + clarifying (TODO: work on variables names)
[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 initVariables(fen)
33 {
34 super.initVariables(fen);
35 const fenParts = fen.split(" ");
36 const position = fenParts[0].split("/");
37 if (this.kingPos["w"][0] < 0 || this.kingPos["b"][0] < 0)
38 {
39 // INIT_COL_XXX won't be used, so no need to set them for Alice kings
40 for (let i=0; i<position.length; i++)
41 {
42 let k = 0; //column index on board
43 for (let j=0; j<position[i].length; j++)
44 {
45 switch (position[i].charAt(j))
46 {
47 case 'l':
48 this.kingPos['b'] = [i,k];
49 break;
50 case 'L':
51 this.kingPos['w'] = [i,k];
52 break;
53 default:
54 let num = parseInt(position[i].charAt(j));
55 if (!isNaN(num))
56 k += (num-1);
57 }
58 k++;
59 }
60 }
61 }
62 }
63
64 // Return the (standard) color+piece notation at a square for a board
65 getSquareOccupation(i, j, mirrorSide)
66 {
67 const piece = this.getPiece(i,j);
68 const V = VariantRules;
69 if (mirrorSide==1 && Object.keys(V.ALICE_CODES).includes(piece))
70 return this.board[i][j];
71 else if (mirrorSide==2 && Object.keys(V.ALICE_PIECES).includes(piece))
72 return this.getColor(i,j) + V.ALICE_PIECES[piece];
73 return "";
74 }
75
76 // Build board of the given (mirror)side
77 getSideBoard(mirrorSide)
78 {
79 // Build corresponding board from complete board
80 const [sizeX,sizeY] = VariantRules.size;
81 let sideBoard = doubleArray(sizeX, sizeY, "");
82 for (let i=0; i<sizeX; i++)
83 {
84 for (let j=0; j<sizeY; j++)
85 sideBoard[i][j] = this.getSquareOccupation(i, j, mirrorSide);
86 }
87 return sideBoard;
88 }
89
90 // NOTE: castle & enPassant https://www.chessvariants.com/other.dir/alice.html
91 getPotentialMovesFrom([x,y], sideBoard)
92 {
93 const pieces = Object.keys(VariantRules.ALICE_CODES);
94 const codes = Object.keys(VariantRules.ALICE_PIECES);
95 const mirrorSide = (pieces.includes(this.getPiece(x,y)) ? 1 : 2);
96
97 // Search valid moves on sideBoard
98 let saveBoard = this.board;
99 this.board = sideBoard || this.getSideBoard(mirrorSide);
100 let moves = super.getPotentialMovesFrom([x,y]);
101 this.board = saveBoard;
102
103 // Finally filter impossible moves
104 let res = moves.filter(m => {
105 if (m.appear.length == 2) //castle
106 {
107 // appear[i] must be an empty square on the other board
108 for (let psq of m.appear)
109 {
110 if (this.getSquareOccupation(psq.x,psq.y,3-mirrorSide) != VariantRules.EMPTY)
111 return false;
112 }
113 }
114 else if (this.board[m.end.x][m.end.y] != VariantRules.EMPTY)
115 {
116 // Attempt to capture
117 const piece = this.getPiece(m.end.x,m.end.y);
118 if ((mirrorSide==1 && codes.includes(piece))
119 || (mirrorSide==2 && pieces.includes(piece)))
120 {
121 return false;
122 }
123 }
124 // If the move is computed on board1, m.appear change for Alice pieces.
125 if (mirrorSide==1)
126 {
127 m.appear.forEach(psq => { //forEach: castling taken into account
128 psq.p = VariantRules.ALICE_CODES[psq.p]; //goto board2
129 });
130 }
131 else //move on board2: mark vanishing pieces as Alice
132 {
133 m.vanish.forEach(psq => {
134 psq.p = VariantRules.ALICE_CODES[psq.p];
135 });
136 }
137 // Fix en-passant captures
138 if (m.vanish[0].p == VariantRules.PAWN
139 && m.vanish.length == 2 && this.board[m.end.x][m.end.y] == VariantRules.EMPTY)
140 {
141 m.vanish[1].c = this.getOppCol(this.getColor(x,y));
142 // In the special case of en-passant, if
143 // - board1 takes board2 : vanish[1] --> Alice
144 // - board2 takes board1 : vanish[1] --> normal
145 let van = m.vanish[1];
146 if (mirrorSide==1 && codes.includes(this.getPiece(van.x,van.y)))
147 van.p = VariantRules.ALICE_CODES[van.p];
148 else if (mirrorSide==2 && pieces.includes(this.getPiece(van.x,van.y)))
149 van.p = VariantRules.ALICE_PIECES[van.p];
150 }
151 return true;
152 });
153 return res;
154 }
155
156 filterValid(moves)
157 {
158 if (moves.length == 0)
159 return [];
160 let sideBoard = [this.getSideBoard(1), this.getSideBoard(2)];
161 return moves.filter(m => { return !this.underCheck(m, sideBoard); });
162 }
163
164 getAllValidMoves()
165 {
166 const color = this.turn;
167 const oppCol = this.getOppCol(color);
168 var potentialMoves = [];
169 let [sizeX,sizeY] = VariantRules.size;
170 let sideBoard = [this.getSideBoard(1), this.getSideBoard(2)];
171 for (var i=0; i<sizeX; i++)
172 {
173 for (var j=0; j<sizeY; j++)
174 {
175 if (this.board[i][j] != VariantRules.EMPTY && this.getColor(i,j) == color)
176 {
177 const mirrorSide =
178 Object.keys(VariantRules.ALICE_CODES).includes(this.getPiece(i,j))
179 ? 1
180 : 2;
181 Array.prototype.push.apply(potentialMoves,
182 this.getPotentialMovesFrom([i,j], sideBoard[mirrorSide-1]));
183 }
184 }
185 }
186 return this.filterValid(potentialMoves, sideBoard);
187 }
188
189 // Play on sideboards [TODO: only one sideBoard required]
190 playSide(move, sideBoard)
191 {
192 const pieces = Object.keys(VariantRules.ALICE_CODES);
193 move.vanish.forEach(psq => {
194 const mirrorSide = (pieces.includes(psq.p) ? 1 : 2);
195 sideBoard[mirrorSide-1][psq.x][psq.y] = VariantRules.EMPTY;
196 });
197 move.appear.forEach(psq => {
198 const mirrorSide = (pieces.includes(psq.p) ? 1 : 2);
199 const piece = (mirrorSide == 1 ? psq.p : VariantRules.ALICE_PIECES[psq.p]);
200 sideBoard[mirrorSide-1][psq.x][psq.y] = psq.c + piece;
201 if (piece == VariantRules.KING)
202 this.kingPos[psq.c] = [psq.x,psq.y];
203 });
204 }
205
206 // Undo on sideboards
207 undoSide(move, sideBoard)
208 {
209 const pieces = Object.keys(VariantRules.ALICE_CODES);
210 move.appear.forEach(psq => {
211 const mirrorSide = (pieces.includes(psq.p) ? 1 : 2);
212 sideBoard[mirrorSide-1][psq.x][psq.y] = VariantRules.EMPTY;
213 });
214 move.vanish.forEach(psq => {
215 const mirrorSide = (pieces.includes(psq.p) ? 1 : 2);
216 const piece = (mirrorSide == 1 ? psq.p : VariantRules.ALICE_PIECES[psq.p]);
217 sideBoard[mirrorSide-1][psq.x][psq.y] = psq.c + piece;
218 if (piece == VariantRules.KING)
219 this.kingPos[psq.c] = [psq.x,psq.y];
220 });
221 }
222
223 underCheck(move, sideBoard) //sideBoard arg always provided
224 {
225 const color = this.turn;
226 this.playSide(move, sideBoard); //no need to track flags
227 const kp = this.kingPos[color];
228 const mirrorSide = sideBoard[0][kp[0]][kp[1]] != VariantRules.EMPTY ? 1 : 2;
229 let saveBoard = this.board;
230 this.board = sideBoard[mirrorSide-1];
231 let res = this.isAttacked(kp, [this.getOppCol(color)]);
232 this.board = saveBoard;
233 this.undoSide(move, sideBoard);
234 return res;
235 }
236
237 getCheckSquares(move)
238 {
239 this.play(move);
240 const color = this.turn; //opponent
241 const pieces = Object.keys(VariantRules.ALICE_CODES);
242 const kp = this.kingPos[color];
243 const mirrorSide = (pieces.includes(this.getPiece(kp[0],kp[1])) ? 1 : 2);
244 let sideBoard = this.getSideBoard(mirrorSide);
245 let saveBoard = this.board;
246 this.board = sideBoard;
247 let res = this.isAttacked(this.kingPos[color], [this.getOppCol(color)])
248 ? [ JSON.parse(JSON.stringify(this.kingPos[color])) ]
249 : [ ];
250 this.board = saveBoard;
251 this.undo(move);
252 return res;
253 }
254
255 updateVariables(move)
256 {
257 super.updateVariables(move); //standard king
258 const piece = this.getPiece(move.start.x,move.start.y);
259 const c = this.getColor(move.start.x,move.start.y);
260 // "l" = Alice king
261 if (piece == "l")
262 {
263 this.kingPos[c][0] = move.appear[0].x;
264 this.kingPos[c][1] = move.appear[0].y;
265 this.castleFlags[c] = [false,false];
266 }
267 }
268
269 unupdateVariables(move)
270 {
271 super.unupdateVariables(move);
272 const c = this.getColor(move.start.x,move.start.y);
273 if (this.getPiece(move.start.x,move.start.y) == "l")
274 this.kingPos[c] = [move.start.x, move.start.y];
275 }
276
277 checkGameEnd()
278 {
279 const pieces = Object.keys(VariantRules.ALICE_CODES);
280 const color = this.turn;
281 const kp = this.kingPos[color];
282 const mirrorSide = (pieces.includes(this.getPiece(kp[0],kp[1])) ? 1 : 2);
283 let sideBoard = this.getSideBoard(mirrorSide);
284 let saveBoard = this.board;
285 this.board = sideBoard;
286 let res = "*";
287 if (!this.isAttacked(this.kingPos[color], [this.getOppCol(color)]))
288 res = "1/2";
289 else
290 res = (color == "w" ? "0-1" : "1-0");
291 this.board = saveBoard;
292 return res;
293 }
294
295 static get VALUES() {
296 return Object.assign(
297 ChessRules.VALUES,
298 {
299 's': 1,
300 'u': 5,
301 'o': 3,
302 'c': 3,
303 't': 9,
304 'l': 1000,
305 }
306 );
307 }
308
309 getNotation(move)
310 {
311 if (move.appear.length == 2 && move.appear[0].p == VariantRules.KING)
312 {
313 if (move.end.y < move.start.y)
314 return "0-0-0";
315 else
316 return "0-0";
317 }
318
319 const finalSquare =
320 String.fromCharCode(97 + move.end.y) + (VariantRules.size[0]-move.end.x);
321 const piece = this.getPiece(move.start.x, move.start.y);
322
323 const captureMark = (move.vanish.length > move.appear.length ? "x" : "");
324 let pawnMark = "";
325 if (["p","s"].includes(piece) && captureMark.length == 1)
326 pawnMark = String.fromCharCode(97 + move.start.y); //start column
327
328 // Piece or pawn movement
329 let notation = piece.toUpperCase() + pawnMark + captureMark + finalSquare;
330 if (['s','p'].includes(piece) && !['s','p'].includes(move.appear[0].p))
331 {
332 // Promotion
333 notation += "=" + move.appear[0].p.toUpperCase();
334 }
335 return notation;
336 }
337 }