b286bf6679cda027acd8fc12caad5a23c83e5829
[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)) ? 1 : 2);
179 Array.prototype.push.apply(potentialMoves,
180 this.getPotentialMovesFrom([i,j], sideBoard[mirrorSide-1]));
181 }
182 }
183 }
184 return this.filterValid(potentialMoves, sideBoard);
185 }
186
187 // Play on sideboards [TODO: only one sideBoard required]
188 playSide(move, sideBoard)
189 {
190 const pieces = Object.keys(VariantRules.ALICE_CODES);
191 move.vanish.forEach(psq => {
192 const mirrorSide = (pieces.includes(psq.p) ? 1 : 2);
193 sideBoard[mirrorSide-1][psq.x][psq.y] = VariantRules.EMPTY;
194 });
195 move.appear.forEach(psq => {
196 const mirrorSide = (pieces.includes(psq.p) ? 1 : 2);
197 const piece = (mirrorSide == 1 ? psq.p : VariantRules.ALICE_PIECES[psq.p]);
198 sideBoard[mirrorSide-1][psq.x][psq.y] = psq.c + piece;
199 if (piece == VariantRules.KING)
200 this.kingPos[psq.c] = [psq.x,psq.y];
201 });
202 }
203
204 // Undo on sideboards
205 undoSide(move, sideBoard)
206 {
207 const pieces = Object.keys(VariantRules.ALICE_CODES);
208 move.appear.forEach(psq => {
209 const mirrorSide = (pieces.includes(psq.p) ? 1 : 2);
210 sideBoard[mirrorSide-1][psq.x][psq.y] = VariantRules.EMPTY;
211 });
212 move.vanish.forEach(psq => {
213 const mirrorSide = (pieces.includes(psq.p) ? 1 : 2);
214 const piece = (mirrorSide == 1 ? psq.p : VariantRules.ALICE_PIECES[psq.p]);
215 sideBoard[mirrorSide-1][psq.x][psq.y] = psq.c + piece;
216 if (piece == VariantRules.KING)
217 this.kingPos[psq.c] = [psq.x,psq.y];
218 });
219 }
220
221 underCheck(move, sideBoard) //sideBoard arg always provided
222 {
223 const color = this.turn;
224 this.playSide(move, sideBoard); //no need to track flags
225 const kp = this.kingPos[color];
226 const mirrorSide = sideBoard[0][kp[0]][kp[1]] != VariantRules.EMPTY ? 1 : 2;
227 let saveBoard = this.board;
228 this.board = sideBoard[mirrorSide-1];
229 let res = this.isAttacked(kp, [this.getOppCol(color)]);
230 this.board = saveBoard;
231 this.undoSide(move, sideBoard);
232 return res;
233 }
234
235 getCheckSquares(move)
236 {
237 this.play(move);
238 const color = this.turn; //opponent
239 const pieces = Object.keys(VariantRules.ALICE_CODES);
240 const kp = this.kingPos[color];
241 const mirrorSide = (pieces.includes(this.getPiece(kp[0],kp[1])) ? 1 : 2);
242 let sideBoard = this.getSideBoard(mirrorSide);
243 let saveBoard = this.board;
244 this.board = sideBoard;
245 let res = this.isAttacked(this.kingPos[color], [this.getOppCol(color)])
246 ? [ JSON.parse(JSON.stringify(this.kingPos[color])) ]
247 : [ ];
248 this.board = saveBoard;
249 this.undo(move);
250 return res;
251 }
252
253 updateVariables(move)
254 {
255 super.updateVariables(move); //standard king
256 const piece = this.getPiece(move.start.x,move.start.y);
257 const c = this.getColor(move.start.x,move.start.y);
258 // "l" = Alice king
259 if (piece == "l")
260 {
261 this.kingPos[c][0] = move.appear[0].x;
262 this.kingPos[c][1] = move.appear[0].y;
263 this.castleFlags[c] = [false,false];
264 }
265 }
266
267 unupdateVariables(move)
268 {
269 super.unupdateVariables(move);
270 const c = this.getColor(move.start.x,move.start.y);
271 if (this.getPiece(move.start.x,move.start.y) == "l")
272 this.kingPos[c] = [move.start.x, move.start.y];
273 }
274
275 checkGameEnd()
276 {
277 const pieces = Object.keys(VariantRules.ALICE_CODES);
278 const color = this.turn;
279 const kp = this.kingPos[color];
280 const mirrorSide = (pieces.includes(this.getPiece(kp[0],kp[1])) ? 1 : 2);
281 let sideBoard = this.getSideBoard(mirrorSide);
282 let saveBoard = this.board;
283 this.board = sideBoard;
284 let res = "*";
285 if (!this.isAttacked(this.kingPos[color], [this.getOppCol(color)]))
286 res = "1/2";
287 else
288 res = (color == "w" ? "0-1" : "1-0");
289 this.board = saveBoard;
290 return res;
291 }
292
293 static get VALUES() {
294 return {
295 'p': 1,
296 's': 1,
297 'r': 5,
298 'u': 5,
299 'n': 3,
300 'o': 3,
301 'b': 3,
302 'c': 3,
303 'q': 9,
304 't': 9,
305 'k': 1000,
306 'l': 1000
307 };
308 }
309
310 getNotation(move)
311 {
312 if (move.appear.length == 2 && move.appear[0].p == VariantRules.KING)
313 {
314 if (move.end.y < move.start.y)
315 return "0-0-0";
316 else
317 return "0-0";
318 }
319
320 const finalSquare =
321 String.fromCharCode(97 + move.end.y) + (VariantRules.size[0]-move.end.x);
322 const piece = this.getPiece(move.start.x, move.start.y);
323
324 const captureMark = (move.vanish.length > move.appear.length ? "x" : "");
325 let pawnMark = "";
326 if (["p","s"].includes(piece) && captureMark.length == 1)
327 pawnMark = String.fromCharCode(97 + move.start.y); //start column
328
329 // Piece or pawn movement
330 let notation = piece.toUpperCase() + pawnMark + captureMark + finalSquare;
331 if (['s','p'].includes(piece) && !['s','p'].includes(move.appear[0].p))
332 {
333 // Promotion
334 notation += "=" + move.appear[0].p.toUpperCase();
335 }
336 return notation;
337 }
338 }