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