Commit | Line | Data |
---|---|---|
03608482 | 1 | import { ChessRules } from "@/base_rules"; |
6808d7a1 | 2 | import { ArrayFun } from "@/utils/array"; |
b7c32f1a | 3 | |
4b353936 | 4 | // NOTE: alternative implementation, probably cleaner = use only 1 board |
2c5d7b20 BA |
5 | // TODO? atLeastOneMove() would be more efficient if rewritten here |
6 | // (less sideBoard computations) | |
32f6285e | 7 | export class AliceRules extends ChessRules { |
6808d7a1 | 8 | static get ALICE_PIECES() { |
dac39588 | 9 | return { |
6808d7a1 BA |
10 | s: "p", |
11 | t: "q", | |
12 | u: "r", | |
13 | c: "b", | |
14 | o: "n", | |
15 | l: "k" | |
dac39588 BA |
16 | }; |
17 | } | |
6808d7a1 | 18 | static get ALICE_CODES() { |
dac39588 | 19 | return { |
6808d7a1 BA |
20 | p: "s", |
21 | q: "t", | |
22 | r: "u", | |
23 | b: "c", | |
24 | n: "o", | |
25 | k: "l" | |
dac39588 BA |
26 | }; |
27 | } | |
a3eb4cc5 | 28 | |
6808d7a1 | 29 | static get PIECES() { |
dac39588 BA |
30 | return ChessRules.PIECES.concat(Object.keys(V.ALICE_PIECES)); |
31 | } | |
7931e479 | 32 | |
241bf8f2 | 33 | getPpath(b) { |
41217d97 | 34 | return (Object.keys(V.ALICE_PIECES).includes(b[1]) ? "Alice/" : "") + b; |
241bf8f2 BA |
35 | } |
36 | ||
1c15969e BA |
37 | getEpSquare(moveOrSquare) { |
38 | if (!moveOrSquare) return undefined; | |
39 | if (typeof moveOrSquare === "string") { | |
40 | const square = moveOrSquare; | |
41 | if (square == "-") return undefined; | |
42 | return V.SquareToCoords(square); | |
43 | } | |
44 | // Argument is a move: | |
45 | const move = moveOrSquare; | |
46 | const s = move.start, | |
47 | e = move.end; | |
48 | if ( | |
49 | s.y == e.y && | |
50 | Math.abs(s.x - e.x) == 2 && | |
51 | // Special conditions: a pawn can be on the other side | |
52 | ['p','s'].includes(move.appear[0].p) && | |
53 | ['p','s'].includes(move.vanish[0].p) | |
54 | ) { | |
55 | return { | |
56 | x: (s.x + e.x) / 2, | |
57 | y: s.y | |
58 | }; | |
59 | } | |
60 | return undefined; //default | |
61 | } | |
62 | ||
6808d7a1 | 63 | setOtherVariables(fen) { |
dac39588 BA |
64 | super.setOtherVariables(fen); |
65 | const rows = V.ParseFen(fen).position.split("/"); | |
6808d7a1 | 66 | if (this.kingPos["w"][0] < 0 || this.kingPos["b"][0] < 0) { |
2c5d7b20 BA |
67 | // INIT_COL_XXX won't be required if Alice kings are found |
68 | // (it means 'king moved') | |
6808d7a1 | 69 | for (let i = 0; i < rows.length; i++) { |
dac39588 | 70 | let k = 0; //column index on board |
6808d7a1 BA |
71 | for (let j = 0; j < rows[i].length; j++) { |
72 | switch (rows[i].charAt(j)) { | |
73 | case "l": | |
74 | this.kingPos["b"] = [i, k]; | |
dac39588 | 75 | break; |
6808d7a1 BA |
76 | case "L": |
77 | this.kingPos["w"] = [i, k]; | |
dac39588 | 78 | break; |
6808d7a1 | 79 | default: { |
dac39588 | 80 | const num = parseInt(rows[i].charAt(j)); |
6808d7a1 BA |
81 | if (!isNaN(num)) k += num - 1; |
82 | } | |
dac39588 BA |
83 | } |
84 | k++; | |
85 | } | |
86 | } | |
87 | } | |
88 | } | |
0b5fa571 | 89 | |
dac39588 | 90 | // Return the (standard) color+piece notation at a square for a board |
6808d7a1 BA |
91 | getSquareOccupation(i, j, mirrorSide) { |
92 | const piece = this.getPiece(i, j); | |
93 | if (mirrorSide == 1 && Object.keys(V.ALICE_CODES).includes(piece)) | |
dac39588 | 94 | return this.board[i][j]; |
6808d7a1 BA |
95 | if (mirrorSide == 2 && Object.keys(V.ALICE_PIECES).includes(piece)) |
96 | return this.getColor(i, j) + V.ALICE_PIECES[piece]; | |
dac39588 BA |
97 | return ""; |
98 | } | |
364128d9 | 99 | |
dac39588 | 100 | // Build board of the given (mirror)side |
6808d7a1 | 101 | getSideBoard(mirrorSide) { |
dac39588 BA |
102 | // Build corresponding board from complete board |
103 | let sideBoard = ArrayFun.init(V.size.x, V.size.y, ""); | |
6808d7a1 BA |
104 | for (let i = 0; i < V.size.x; i++) { |
105 | for (let j = 0; j < V.size.y; j++) | |
dac39588 BA |
106 | sideBoard[i][j] = this.getSquareOccupation(i, j, mirrorSide); |
107 | } | |
108 | return sideBoard; | |
109 | } | |
0cd8f2bd | 110 | |
2c5d7b20 BA |
111 | // NOTE: castle & enPassant |
112 | // https://www.chessvariants.com/other.dir/alice.html | |
6808d7a1 | 113 | getPotentialMovesFrom([x, y], sideBoard) { |
dac39588 BA |
114 | const pieces = Object.keys(V.ALICE_CODES); |
115 | const codes = Object.keys(V.ALICE_PIECES); | |
6808d7a1 BA |
116 | const mirrorSide = pieces.includes(this.getPiece(x, y)) ? 1 : 2; |
117 | if (!sideBoard) sideBoard = [this.getSideBoard(1), this.getSideBoard(2)]; | |
118 | const color = this.getColor(x, y); | |
0cd8f2bd | 119 | |
dac39588 BA |
120 | // Search valid moves on sideBoard |
121 | const saveBoard = this.board; | |
6808d7a1 BA |
122 | this.board = sideBoard[mirrorSide - 1]; |
123 | const moves = super.getPotentialMovesFrom([x, y]).filter(m => { | |
124 | // Filter out king moves which result in under-check position on | |
125 | // current board (before mirror traversing) | |
126 | let aprioriValid = true; | |
127 | if (m.appear[0].p == V.KING) { | |
128 | this.play(m); | |
129 | if (this.underCheck(color, sideBoard)) aprioriValid = false; | |
130 | this.undo(m); | |
131 | } | |
132 | return aprioriValid; | |
133 | }); | |
dac39588 | 134 | this.board = saveBoard; |
0cd8f2bd | 135 | |
dac39588 BA |
136 | // Finally filter impossible moves |
137 | const res = moves.filter(m => { | |
6808d7a1 | 138 | if (m.appear.length == 2) { |
6b7b2cf7 | 139 | // Castle: appear[i] must be an empty square on the other board |
6808d7a1 | 140 | for (let psq of m.appear) { |
2c5d7b20 BA |
141 | if ( |
142 | this.getSquareOccupation(psq.x, psq.y, 3 - mirrorSide) != V.EMPTY | |
143 | ) { | |
dac39588 | 144 | return false; |
2c5d7b20 | 145 | } |
dac39588 | 146 | } |
6808d7a1 | 147 | } else if (this.board[m.end.x][m.end.y] != V.EMPTY) { |
dac39588 | 148 | // Attempt to capture |
6808d7a1 BA |
149 | const piece = this.getPiece(m.end.x, m.end.y); |
150 | if ( | |
151 | (mirrorSide == 1 && codes.includes(piece)) || | |
152 | (mirrorSide == 2 && pieces.includes(piece)) | |
153 | ) { | |
dac39588 BA |
154 | return false; |
155 | } | |
156 | } | |
157 | // If the move is computed on board1, m.appear change for Alice pieces. | |
6808d7a1 BA |
158 | if (mirrorSide == 1) { |
159 | m.appear.forEach(psq => { | |
f9c36b2d | 160 | // forEach: castling taken into account |
dac39588 BA |
161 | psq.p = V.ALICE_CODES[psq.p]; //goto board2 |
162 | }); | |
f9c36b2d | 163 | } |
6808d7a1 | 164 | else { |
f9c36b2d | 165 | // Move on board2: mark vanishing pieces as Alice |
dac39588 BA |
166 | m.vanish.forEach(psq => { |
167 | psq.p = V.ALICE_CODES[psq.p]; | |
168 | }); | |
169 | } | |
170 | // Fix en-passant captures | |
6808d7a1 BA |
171 | if ( |
172 | m.vanish[0].p == V.PAWN && | |
173 | m.vanish.length == 2 && | |
174 | this.board[m.end.x][m.end.y] == V.EMPTY | |
175 | ) { | |
176 | m.vanish[1].c = V.GetOppCol(this.getColor(x, y)); | |
dac39588 BA |
177 | // In the special case of en-passant, if |
178 | // - board1 takes board2 : vanish[1] --> Alice | |
179 | // - board2 takes board1 : vanish[1] --> normal | |
180 | let van = m.vanish[1]; | |
6808d7a1 | 181 | if (mirrorSide == 1 && codes.includes(this.getPiece(van.x, van.y))) |
dac39588 | 182 | van.p = V.ALICE_CODES[van.p]; |
6808d7a1 BA |
183 | else if ( |
184 | mirrorSide == 2 && | |
185 | pieces.includes(this.getPiece(van.x, van.y)) | |
186 | ) | |
dac39588 BA |
187 | van.p = V.ALICE_PIECES[van.p]; |
188 | } | |
189 | return true; | |
190 | }); | |
191 | return res; | |
192 | } | |
b8121223 | 193 | |
6808d7a1 BA |
194 | filterValid(moves, sideBoard) { |
195 | if (moves.length == 0) return []; | |
196 | if (!sideBoard) sideBoard = [this.getSideBoard(1), this.getSideBoard(2)]; | |
dac39588 BA |
197 | const color = this.turn; |
198 | return moves.filter(m => { | |
199 | this.playSide(m, sideBoard); //no need to track flags | |
200 | const res = !this.underCheck(color, sideBoard); | |
201 | this.undoSide(m, sideBoard); | |
202 | return res; | |
203 | }); | |
204 | } | |
0cd8f2bd | 205 | |
6808d7a1 | 206 | getAllValidMoves() { |
dac39588 | 207 | const color = this.turn; |
dac39588 BA |
208 | let potentialMoves = []; |
209 | const sideBoard = [this.getSideBoard(1), this.getSideBoard(2)]; | |
6808d7a1 BA |
210 | for (var i = 0; i < V.size.x; i++) { |
211 | for (var j = 0; j < V.size.y; j++) { | |
212 | if (this.board[i][j] != V.EMPTY && this.getColor(i, j) == color) { | |
213 | Array.prototype.push.apply( | |
214 | potentialMoves, | |
215 | this.getPotentialMovesFrom([i, j], sideBoard) | |
216 | ); | |
dac39588 BA |
217 | } |
218 | } | |
219 | } | |
220 | return this.filterValid(potentialMoves, sideBoard); | |
221 | } | |
b8121223 | 222 | |
dac39588 | 223 | // Play on sideboards [TODO: only one sideBoard required] |
6808d7a1 | 224 | playSide(move, sideBoard) { |
dac39588 BA |
225 | const pieces = Object.keys(V.ALICE_CODES); |
226 | move.vanish.forEach(psq => { | |
6808d7a1 BA |
227 | const mirrorSide = pieces.includes(psq.p) ? 1 : 2; |
228 | sideBoard[mirrorSide - 1][psq.x][psq.y] = V.EMPTY; | |
dac39588 BA |
229 | }); |
230 | move.appear.forEach(psq => { | |
6808d7a1 BA |
231 | const mirrorSide = pieces.includes(psq.p) ? 1 : 2; |
232 | const piece = mirrorSide == 1 ? psq.p : V.ALICE_PIECES[psq.p]; | |
233 | sideBoard[mirrorSide - 1][psq.x][psq.y] = psq.c + piece; | |
234 | if (piece == V.KING) this.kingPos[psq.c] = [psq.x, psq.y]; | |
dac39588 BA |
235 | }); |
236 | } | |
4b353936 | 237 | |
dac39588 | 238 | // Undo on sideboards |
6808d7a1 | 239 | undoSide(move, sideBoard) { |
dac39588 BA |
240 | const pieces = Object.keys(V.ALICE_CODES); |
241 | move.appear.forEach(psq => { | |
6808d7a1 BA |
242 | const mirrorSide = pieces.includes(psq.p) ? 1 : 2; |
243 | sideBoard[mirrorSide - 1][psq.x][psq.y] = V.EMPTY; | |
dac39588 BA |
244 | }); |
245 | move.vanish.forEach(psq => { | |
6808d7a1 BA |
246 | const mirrorSide = pieces.includes(psq.p) ? 1 : 2; |
247 | const piece = mirrorSide == 1 ? psq.p : V.ALICE_PIECES[psq.p]; | |
248 | sideBoard[mirrorSide - 1][psq.x][psq.y] = psq.c + piece; | |
249 | if (piece == V.KING) this.kingPos[psq.c] = [psq.x, psq.y]; | |
dac39588 BA |
250 | }); |
251 | } | |
4b353936 | 252 | |
1c9f093d | 253 | // sideBoard: arg containing both boards (see getAllValidMoves()) |
6808d7a1 | 254 | underCheck(color, sideBoard) { |
dac39588 | 255 | const kp = this.kingPos[color]; |
6808d7a1 | 256 | const mirrorSide = sideBoard[0][kp[0]][kp[1]] != V.EMPTY ? 1 : 2; |
dac39588 | 257 | let saveBoard = this.board; |
6808d7a1 | 258 | this.board = sideBoard[mirrorSide - 1]; |
dac39588 BA |
259 | let res = this.isAttacked(kp, [V.GetOppCol(color)]); |
260 | this.board = saveBoard; | |
261 | return res; | |
262 | } | |
0cd8f2bd | 263 | |
af34341d BA |
264 | getCheckSquares() { |
265 | const color = this.turn; | |
dac39588 BA |
266 | const pieces = Object.keys(V.ALICE_CODES); |
267 | const kp = this.kingPos[color]; | |
6808d7a1 | 268 | const mirrorSide = pieces.includes(this.getPiece(kp[0], kp[1])) ? 1 : 2; |
dac39588 BA |
269 | let sideBoard = this.getSideBoard(mirrorSide); |
270 | let saveBoard = this.board; | |
271 | this.board = sideBoard; | |
272 | let res = this.isAttacked(this.kingPos[color], [V.GetOppCol(color)]) | |
6808d7a1 BA |
273 | ? [JSON.parse(JSON.stringify(this.kingPos[color]))] |
274 | : []; | |
dac39588 BA |
275 | this.board = saveBoard; |
276 | return res; | |
277 | } | |
270968d6 | 278 | |
3a2a7b5f BA |
279 | postPlay(move) { |
280 | super.postPlay(move); //standard king | |
dac39588 BA |
281 | const piece = move.vanish[0].p; |
282 | const c = move.vanish[0].c; | |
283 | // "l" = Alice king | |
6808d7a1 | 284 | if (piece == "l") { |
dac39588 BA |
285 | this.kingPos[c][0] = move.appear[0].x; |
286 | this.kingPos[c][1] = move.appear[0].y; | |
3a2a7b5f | 287 | this.castleFlags[c] = [8, 8]; |
dac39588 BA |
288 | } |
289 | } | |
0b5fa571 | 290 | |
3a2a7b5f BA |
291 | postUndo(move) { |
292 | super.postUndo(move); | |
dac39588 | 293 | const c = move.vanish[0].c; |
3a2a7b5f BA |
294 | if (move.vanish[0].p == "l") |
295 | this.kingPos[c] = [move.start.x, move.start.y]; | |
dac39588 | 296 | } |
0b5fa571 | 297 | |
6808d7a1 | 298 | getCurrentScore() { |
bb688df5 | 299 | if (this.atLeastOneMove()) return "*"; |
0c3fe8a6 | 300 | const pieces = Object.keys(V.ALICE_CODES); |
dac39588 BA |
301 | const color = this.turn; |
302 | const kp = this.kingPos[color]; | |
6808d7a1 | 303 | const mirrorSide = pieces.includes(this.getPiece(kp[0], kp[1])) ? 1 : 2; |
dac39588 BA |
304 | let sideBoard = this.getSideBoard(mirrorSide); |
305 | let saveBoard = this.board; | |
306 | this.board = sideBoard; | |
307 | let res = "*"; | |
308 | if (!this.isAttacked(this.kingPos[color], [V.GetOppCol(color)])) | |
309 | res = "1/2"; | |
6808d7a1 | 310 | else res = color == "w" ? "0-1" : "1-0"; |
dac39588 BA |
311 | this.board = saveBoard; |
312 | return res; | |
313 | } | |
9de73b71 | 314 | |
6808d7a1 | 315 | static get VALUES() { |
a97bdbda BA |
316 | return Object.assign( |
317 | { | |
318 | s: 1, | |
319 | u: 5, | |
320 | o: 3, | |
321 | c: 3, | |
322 | t: 9, | |
323 | l: 1000 | |
324 | }, | |
325 | ChessRules.VALUES | |
326 | ); | |
dac39588 | 327 | } |
0f51ef98 | 328 | |
b83a675a BA |
329 | static get SEARCH_DEPTH() { |
330 | return 2; | |
331 | } | |
332 | ||
6808d7a1 BA |
333 | getNotation(move) { |
334 | if (move.appear.length == 2 && move.appear[0].p == V.KING) { | |
335 | if (move.end.y < move.start.y) return "0-0-0"; | |
336 | return "0-0"; | |
dac39588 | 337 | } |
0f51ef98 | 338 | |
dac39588 BA |
339 | const finalSquare = V.CoordsToSquare(move.end); |
340 | const piece = this.getPiece(move.start.x, move.start.y); | |
0f51ef98 | 341 | |
6808d7a1 | 342 | const captureMark = move.vanish.length > move.appear.length ? "x" : ""; |
dac39588 | 343 | let pawnMark = ""; |
6808d7a1 | 344 | if (["p", "s"].includes(piece) && captureMark.length == 1) |
dac39588 | 345 | pawnMark = V.CoordToColumn(move.start.y); //start column |
0f51ef98 | 346 | |
dac39588 BA |
347 | // Piece or pawn movement |
348 | let notation = piece.toUpperCase() + pawnMark + captureMark + finalSquare; | |
6808d7a1 | 349 | if (["s", "p"].includes(piece) && !["s", "p"].includes(move.appear[0].p)) { |
dac39588 BA |
350 | // Promotion |
351 | notation += "=" + move.appear[0].p.toUpperCase(); | |
352 | } | |
353 | return notation; | |
354 | } | |
6808d7a1 | 355 | }; |