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