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