Commit | Line | Data |
---|---|---|
4b353936 | 1 | // NOTE: alternative implementation, probably cleaner = use only 1 board |
270968d6 | 2 | class 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); | |
364128d9 BA |
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 | ||
b8121223 BA |
75 | // Build board of the given (mirror)side |
76 | getSideBoard(mirrorSide) | |
0cd8f2bd | 77 | { |
270968d6 | 78 | // Build corresponding board from complete board |
0b7d99ec BA |
79 | let sideBoard = doubleArray(V.size.x, V.size.y, ""); |
80 | for (let i=0; i<V.size.x; i++) | |
0cd8f2bd | 81 | { |
0b7d99ec | 82 | for (let j=0; j<V.size.y; j++) |
364128d9 | 83 | sideBoard[i][j] = this.getSquareOccupation(i, j, mirrorSide); |
0cd8f2bd | 84 | } |
270968d6 BA |
85 | return sideBoard; |
86 | } | |
0cd8f2bd | 87 | |
55eb331d | 88 | // NOTE: castle & enPassant https://www.chessvariants.com/other.dir/alice.html |
b8121223 | 89 | getPotentialMovesFrom([x,y], sideBoard) |
270968d6 | 90 | { |
0b7d99ec BA |
91 | const pieces = Object.keys(V.ALICE_CODES); |
92 | const codes = Object.keys(V.ALICE_PIECES); | |
b8121223 | 93 | const mirrorSide = (pieces.includes(this.getPiece(x,y)) ? 1 : 2); |
0cd8f2bd | 94 | |
270968d6 BA |
95 | // Search valid moves on sideBoard |
96 | let saveBoard = this.board; | |
b8121223 | 97 | this.board = sideBoard || this.getSideBoard(mirrorSide); |
270968d6 | 98 | let moves = super.getPotentialMovesFrom([x,y]); |
0cd8f2bd BA |
99 | this.board = saveBoard; |
100 | ||
101 | // Finally filter impossible moves | |
b8121223 | 102 | let res = moves.filter(m => { |
55eb331d | 103 | if (m.appear.length == 2) //castle |
270968d6 | 104 | { |
364128d9 BA |
105 | // appear[i] must be an empty square on the other board |
106 | for (let psq of m.appear) | |
107 | { | |
0b7d99ec | 108 | if (this.getSquareOccupation(psq.x,psq.y,3-mirrorSide) != V.EMPTY) |
55eb331d | 109 | return false; |
364128d9 | 110 | } |
55eb331d | 111 | } |
0b7d99ec | 112 | else if (this.board[m.end.x][m.end.y] != V.EMPTY) |
55eb331d BA |
113 | { |
114 | // Attempt to capture | |
270968d6 | 115 | const piece = this.getPiece(m.end.x,m.end.y); |
06ddfe34 BA |
116 | if ((mirrorSide==1 && codes.includes(piece)) |
117 | || (mirrorSide==2 && pieces.includes(piece))) | |
270968d6 BA |
118 | { |
119 | return false; | |
120 | } | |
121 | } | |
f6cc7faf BA |
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 | |
0b7d99ec | 126 | psq.p = V.ALICE_CODES[psq.p]; //goto board2 |
f6cc7faf BA |
127 | }); |
128 | } | |
0b5fa571 BA |
129 | else //move on board2: mark vanishing pieces as Alice |
130 | { | |
131 | m.vanish.forEach(psq => { | |
0b7d99ec | 132 | psq.p = V.ALICE_CODES[psq.p]; |
0b5fa571 BA |
133 | }); |
134 | } | |
9de73b71 | 135 | // Fix en-passant captures |
0b7d99ec BA |
136 | if (m.vanish[0].p == V.PAWN && m.vanish.length == 2 |
137 | && this.board[m.end.x][m.end.y] == V.EMPTY) | |
06ddfe34 | 138 | { |
9de73b71 | 139 | m.vanish[1].c = this.getOppCol(this.getColor(x,y)); |
06ddfe34 BA |
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))) | |
0b7d99ec | 145 | van.p = V.ALICE_CODES[van.p]; |
06ddfe34 | 146 | else if (mirrorSide==2 && pieces.includes(this.getPiece(van.x,van.y))) |
0b7d99ec | 147 | van.p = V.ALICE_PIECES[van.p]; |
06ddfe34 | 148 | } |
270968d6 BA |
149 | return true; |
150 | }); | |
b8121223 BA |
151 | return res; |
152 | } | |
153 | ||
4b353936 | 154 | filterValid(moves) |
b8121223 BA |
155 | { |
156 | if (moves.length == 0) | |
157 | return []; | |
4b353936 BA |
158 | let sideBoard = [this.getSideBoard(1), this.getSideBoard(2)]; |
159 | return moves.filter(m => { return !this.underCheck(m, sideBoard); }); | |
0cd8f2bd BA |
160 | } |
161 | ||
b8121223 BA |
162 | getAllValidMoves() |
163 | { | |
164 | const color = this.turn; | |
165 | const oppCol = this.getOppCol(color); | |
166 | var potentialMoves = []; | |
b8121223 | 167 | let sideBoard = [this.getSideBoard(1), this.getSideBoard(2)]; |
0b7d99ec | 168 | for (var i=0; i<V.size.x; i++) |
b8121223 | 169 | { |
0b7d99ec | 170 | for (var j=0; j<V.size.y; j++) |
b8121223 | 171 | { |
0b7d99ec | 172 | if (this.board[i][j] != V.EMPTY && this.getColor(i,j) == color) |
b8121223 BA |
173 | { |
174 | const mirrorSide = | |
0b7d99ec | 175 | Object.keys(V.ALICE_CODES).includes(this.getPiece(i,j)) |
92342261 BA |
176 | ? 1 |
177 | : 2; | |
b8121223 BA |
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 | ||
4b353936 BA |
186 | // Play on sideboards [TODO: only one sideBoard required] |
187 | playSide(move, sideBoard) | |
188 | { | |
0b7d99ec | 189 | const pieces = Object.keys(V.ALICE_CODES); |
4b353936 BA |
190 | move.vanish.forEach(psq => { |
191 | const mirrorSide = (pieces.includes(psq.p) ? 1 : 2); | |
0b7d99ec | 192 | sideBoard[mirrorSide-1][psq.x][psq.y] = V.EMPTY; |
4b353936 BA |
193 | }); |
194 | move.appear.forEach(psq => { | |
195 | const mirrorSide = (pieces.includes(psq.p) ? 1 : 2); | |
0b7d99ec | 196 | const piece = (mirrorSide == 1 ? psq.p : V.ALICE_PIECES[psq.p]); |
4b353936 | 197 | sideBoard[mirrorSide-1][psq.x][psq.y] = psq.c + piece; |
0b7d99ec | 198 | if (piece == V.KING) |
4b353936 BA |
199 | this.kingPos[psq.c] = [psq.x,psq.y]; |
200 | }); | |
201 | } | |
202 | ||
203 | // Undo on sideboards | |
204 | undoSide(move, sideBoard) | |
0cd8f2bd | 205 | { |
0b7d99ec | 206 | const pieces = Object.keys(V.ALICE_CODES); |
4b353936 BA |
207 | move.appear.forEach(psq => { |
208 | const mirrorSide = (pieces.includes(psq.p) ? 1 : 2); | |
0b7d99ec | 209 | sideBoard[mirrorSide-1][psq.x][psq.y] = V.EMPTY; |
4b353936 BA |
210 | }); |
211 | move.vanish.forEach(psq => { | |
212 | const mirrorSide = (pieces.includes(psq.p) ? 1 : 2); | |
0b7d99ec | 213 | const piece = (mirrorSide == 1 ? psq.p : V.ALICE_PIECES[psq.p]); |
4b353936 | 214 | sideBoard[mirrorSide-1][psq.x][psq.y] = psq.c + piece; |
0b7d99ec | 215 | if (piece == V.KING) |
4b353936 BA |
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 | |
b8121223 | 224 | const kp = this.kingPos[color]; |
0b7d99ec | 225 | const mirrorSide = (sideBoard[0][kp[0]][kp[1]] != V.EMPTY ? 1 : 2); |
270968d6 | 226 | let saveBoard = this.board; |
4b353936 | 227 | this.board = sideBoard[mirrorSide-1]; |
cf130369 | 228 | let res = this.isAttacked(kp, [this.getOppCol(color)]); |
270968d6 | 229 | this.board = saveBoard; |
4b353936 | 230 | this.undoSide(move, sideBoard); |
0cd8f2bd BA |
231 | return res; |
232 | } | |
233 | ||
270968d6 | 234 | getCheckSquares(move) |
0cd8f2bd | 235 | { |
270968d6 BA |
236 | this.play(move); |
237 | const color = this.turn; //opponent | |
0b7d99ec | 238 | const pieces = Object.keys(V.ALICE_CODES); |
b8121223 BA |
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); | |
270968d6 BA |
242 | let saveBoard = this.board; |
243 | this.board = sideBoard; | |
cf130369 | 244 | let res = this.isAttacked(this.kingPos[color], [this.getOppCol(color)]) |
270968d6 BA |
245 | ? [ JSON.parse(JSON.stringify(this.kingPos[color])) ] |
246 | : [ ]; | |
247 | this.board = saveBoard; | |
248 | this.undo(move); | |
249 | return res; | |
0cd8f2bd | 250 | } |
270968d6 | 251 | |
0b5fa571 BA |
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 | ||
0cd8f2bd BA |
274 | checkGameEnd() |
275 | { | |
0b7d99ec | 276 | const pieces = Object.keys(V.ALICE_CODES); |
0cd8f2bd | 277 | const color = this.turn; |
b8121223 BA |
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); | |
270968d6 BA |
281 | let saveBoard = this.board; |
282 | this.board = sideBoard; | |
283 | let res = "*"; | |
cf130369 | 284 | if (!this.isAttacked(this.kingPos[color], [this.getOppCol(color)])) |
270968d6 BA |
285 | res = "1/2"; |
286 | else | |
287 | res = (color == "w" ? "0-1" : "1-0"); | |
288 | this.board = saveBoard; | |
289 | return res; | |
0cd8f2bd | 290 | } |
9de73b71 BA |
291 | |
292 | static get VALUES() { | |
92342261 BA |
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 | ); | |
9de73b71 | 304 | } |
0f51ef98 BA |
305 | |
306 | getNotation(move) | |
307 | { | |
0b7d99ec | 308 | if (move.appear.length == 2 && move.appear[0].p == V.KING) |
0f51ef98 BA |
309 | { |
310 | if (move.end.y < move.start.y) | |
311 | return "0-0-0"; | |
312 | else | |
313 | return "0-0"; | |
314 | } | |
315 | ||
0b7d99ec | 316 | const finalSquare = String.fromCharCode(97 + move.end.y) + (V.size.x-move.end.x); |
0f51ef98 BA |
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 | } | |
5bfb0956 | 333 | } |