Avoid score redundancy in PGN
[vchess.git] / public / 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
0b5fa571
BA
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
364128d9
BA
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
b8121223
BA
76 // Build board of the given (mirror)side
77 getSideBoard(mirrorSide)
0cd8f2bd 78 {
270968d6 79 // Build corresponding board from complete board
364128d9 80 const [sizeX,sizeY] = VariantRules.size;
270968d6 81 let sideBoard = doubleArray(sizeX, sizeY, "");
0cd8f2bd
BA
82 for (let i=0; i<sizeX; i++)
83 {
84 for (let j=0; j<sizeY; j++)
364128d9 85 sideBoard[i][j] = this.getSquareOccupation(i, j, mirrorSide);
0cd8f2bd 86 }
270968d6
BA
87 return sideBoard;
88 }
0cd8f2bd 89
55eb331d 90 // NOTE: castle & enPassant https://www.chessvariants.com/other.dir/alice.html
b8121223 91 getPotentialMovesFrom([x,y], sideBoard)
270968d6 92 {
b8121223
BA
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);
0cd8f2bd 96
270968d6
BA
97 // Search valid moves on sideBoard
98 let saveBoard = this.board;
b8121223 99 this.board = sideBoard || this.getSideBoard(mirrorSide);
270968d6 100 let moves = super.getPotentialMovesFrom([x,y]);
0cd8f2bd
BA
101 this.board = saveBoard;
102
103 // Finally filter impossible moves
b8121223 104 let res = moves.filter(m => {
55eb331d 105 if (m.appear.length == 2) //castle
270968d6 106 {
364128d9
BA
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)
55eb331d 111 return false;
364128d9 112 }
55eb331d
BA
113 }
114 else if (this.board[m.end.x][m.end.y] != VariantRules.EMPTY)
115 {
116 // Attempt to capture
270968d6 117 const piece = this.getPiece(m.end.x,m.end.y);
06ddfe34
BA
118 if ((mirrorSide==1 && codes.includes(piece))
119 || (mirrorSide==2 && pieces.includes(piece)))
270968d6
BA
120 {
121 return false;
122 }
123 }
f6cc7faf
BA
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
270968d6 128 psq.p = VariantRules.ALICE_CODES[psq.p]; //goto board2
f6cc7faf
BA
129 });
130 }
0b5fa571
BA
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 }
9de73b71 137 // Fix en-passant captures
a0f5dbaa
BA
138 if (m.vanish[0].p == VariantRules.PAWN
139 && m.vanish.length == 2 && this.board[m.end.x][m.end.y] == VariantRules.EMPTY)
06ddfe34 140 {
9de73b71 141 m.vanish[1].c = this.getOppCol(this.getColor(x,y));
06ddfe34
BA
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 }
270968d6
BA
151 return true;
152 });
b8121223
BA
153 return res;
154 }
155
4b353936 156 filterValid(moves)
b8121223
BA
157 {
158 if (moves.length == 0)
159 return [];
4b353936
BA
160 let sideBoard = [this.getSideBoard(1), this.getSideBoard(2)];
161 return moves.filter(m => { return !this.underCheck(m, sideBoard); });
0cd8f2bd
BA
162 }
163
b8121223
BA
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
4b353936
BA
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)
0cd8f2bd 206 {
b8121223 207 const pieces = Object.keys(VariantRules.ALICE_CODES);
4b353936
BA
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
b8121223 225 const kp = this.kingPos[color];
4b353936 226 const mirrorSide = sideBoard[0][kp[0]][kp[1]] != VariantRules.EMPTY ? 1 : 2;
270968d6 227 let saveBoard = this.board;
4b353936 228 this.board = sideBoard[mirrorSide-1];
cf130369 229 let res = this.isAttacked(kp, [this.getOppCol(color)]);
270968d6 230 this.board = saveBoard;
4b353936 231 this.undoSide(move, sideBoard);
0cd8f2bd
BA
232 return res;
233 }
234
270968d6 235 getCheckSquares(move)
0cd8f2bd 236 {
270968d6
BA
237 this.play(move);
238 const color = this.turn; //opponent
b8121223
BA
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);
270968d6
BA
243 let saveBoard = this.board;
244 this.board = sideBoard;
cf130369 245 let res = this.isAttacked(this.kingPos[color], [this.getOppCol(color)])
270968d6
BA
246 ? [ JSON.parse(JSON.stringify(this.kingPos[color])) ]
247 : [ ];
248 this.board = saveBoard;
249 this.undo(move);
250 return res;
0cd8f2bd 251 }
270968d6 252
0b5fa571
BA
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
0cd8f2bd
BA
275 checkGameEnd()
276 {
b8121223 277 const pieces = Object.keys(VariantRules.ALICE_CODES);
0cd8f2bd 278 const color = this.turn;
b8121223
BA
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);
270968d6
BA
282 let saveBoard = this.board;
283 this.board = sideBoard;
284 let res = "*";
cf130369 285 if (!this.isAttacked(this.kingPos[color], [this.getOppCol(color)]))
270968d6
BA
286 res = "1/2";
287 else
288 res = (color == "w" ? "0-1" : "1-0");
289 this.board = saveBoard;
290 return res;
0cd8f2bd 291 }
9de73b71
BA
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 }
0f51ef98
BA
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 }
5bfb0956 338}