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