Commit | Line | Data |
---|---|---|
0c3fe8a6 BA |
1 | import { ChessRules } from "@/base_rules"; |
2 | import { ArrayFun } from "@/utils/array"; | |
3 | import { randInt } from "@/utils/alea"; | |
4 | ||
92342261 BA |
5 | // NOTE: initial setup differs from the original; see |
6 | // https://www.chessvariants.com/large.dir/freeling.html | |
32f6285e | 7 | export class GrandRules extends ChessRules { |
6808d7a1 BA |
8 | static IsGoodFen(fen) { |
9 | if (!ChessRules.IsGoodFen(fen)) return false; | |
dac39588 BA |
10 | const fenParsed = V.ParseFen(fen); |
11 | // 5) Check captures | |
472c0c4f | 12 | if (!fenParsed.captured || !fenParsed.captured.match(/^[0-9]{12,12}$/)) |
dac39588 BA |
13 | return false; |
14 | return true; | |
15 | } | |
16 | ||
6808d7a1 | 17 | static IsGoodEnpassant(enpassant) { |
472c0c4f | 18 | if (enpassant != "-") return !!enpassant.match(/^([a-j][0-9]{1,2},?)+$/); |
dac39588 BA |
19 | return true; |
20 | } | |
21 | ||
6808d7a1 | 22 | static ParseFen(fen) { |
dac39588 | 23 | const fenParts = fen.split(" "); |
6f2f9437 BA |
24 | return Object.assign( |
25 | ChessRules.ParseFen(fen), | |
26 | { captured: fenParts[5] } | |
27 | ); | |
dac39588 BA |
28 | } |
29 | ||
241bf8f2 BA |
30 | getPpath(b) { |
31 | return ([V.MARSHALL, V.CARDINAL].includes(b[1]) ? "Grand/" : "") + b; | |
32 | } | |
33 | ||
6808d7a1 | 34 | getFen() { |
dac39588 BA |
35 | return super.getFen() + " " + this.getCapturedFen(); |
36 | } | |
37 | ||
f9c36b2d | 38 | getFenForRepeat() { |
90e814b6 | 39 | return super.getFenForRepeat() + "_" + this.getCapturedFen(); |
f9c36b2d BA |
40 | } |
41 | ||
6808d7a1 | 42 | getCapturedFen() { |
dac39588 BA |
43 | let counts = [...Array(14).fill(0)]; |
44 | let i = 0; | |
6808d7a1 BA |
45 | for (let j = 0; j < V.PIECES.length; j++) { |
46 | if (V.PIECES[j] == V.KING) | |
47 | //no king captured | |
dac39588 BA |
48 | continue; |
49 | counts[i] = this.captured["w"][V.PIECES[i]]; | |
6808d7a1 | 50 | counts[7 + i] = this.captured["b"][V.PIECES[i]]; |
dac39588 BA |
51 | i++; |
52 | } | |
53 | return counts.join(""); | |
54 | } | |
55 | ||
6808d7a1 | 56 | setOtherVariables(fen) { |
dac39588 BA |
57 | super.setOtherVariables(fen); |
58 | const fenParsed = V.ParseFen(fen); | |
59 | // Initialize captured pieces' counts from FEN | |
6808d7a1 BA |
60 | this.captured = { |
61 | w: { | |
dac39588 BA |
62 | [V.PAWN]: parseInt(fenParsed.captured[0]), |
63 | [V.ROOK]: parseInt(fenParsed.captured[1]), | |
64 | [V.KNIGHT]: parseInt(fenParsed.captured[2]), | |
65 | [V.BISHOP]: parseInt(fenParsed.captured[3]), | |
66 | [V.QUEEN]: parseInt(fenParsed.captured[4]), | |
67 | [V.MARSHALL]: parseInt(fenParsed.captured[5]), | |
6808d7a1 | 68 | [V.CARDINAL]: parseInt(fenParsed.captured[6]) |
dac39588 | 69 | }, |
6808d7a1 | 70 | b: { |
dac39588 BA |
71 | [V.PAWN]: parseInt(fenParsed.captured[7]), |
72 | [V.ROOK]: parseInt(fenParsed.captured[8]), | |
73 | [V.KNIGHT]: parseInt(fenParsed.captured[9]), | |
74 | [V.BISHOP]: parseInt(fenParsed.captured[10]), | |
75 | [V.QUEEN]: parseInt(fenParsed.captured[11]), | |
76 | [V.MARSHALL]: parseInt(fenParsed.captured[12]), | |
6808d7a1 | 77 | [V.CARDINAL]: parseInt(fenParsed.captured[13]) |
dac39588 BA |
78 | } |
79 | }; | |
80 | } | |
81 | ||
6808d7a1 BA |
82 | static get size() { |
83 | return { x: 10, y: 10 }; | |
84 | } | |
dac39588 | 85 | |
a6836242 | 86 | // Rook + knight: |
6808d7a1 BA |
87 | static get MARSHALL() { |
88 | return "m"; | |
a6836242 BA |
89 | } |
90 | ||
91 | // Bishop + knight | |
6808d7a1 BA |
92 | static get CARDINAL() { |
93 | return "c"; | |
a6836242 | 94 | } |
dac39588 | 95 | |
6808d7a1 BA |
96 | static get PIECES() { |
97 | return ChessRules.PIECES.concat([V.MARSHALL, V.CARDINAL]); | |
dac39588 BA |
98 | } |
99 | ||
100 | // There may be 2 enPassant squares (if pawn jump 3 squares) | |
6808d7a1 | 101 | getEnpassantFen() { |
dac39588 | 102 | const L = this.epSquares.length; |
6808d7a1 | 103 | if (!this.epSquares[L - 1]) return "-"; //no en-passant |
dac39588 | 104 | let res = ""; |
6808d7a1 | 105 | this.epSquares[L - 1].forEach(sq => { |
dac39588 BA |
106 | res += V.CoordsToSquare(sq) + ","; |
107 | }); | |
6808d7a1 | 108 | return res.slice(0, -1); //remove last comma |
dac39588 BA |
109 | } |
110 | ||
111 | // En-passant after 2-sq or 3-sq jumps | |
6808d7a1 BA |
112 | getEpSquare(moveOrSquare) { |
113 | if (!moveOrSquare) return undefined; | |
114 | if (typeof moveOrSquare === "string") { | |
dac39588 | 115 | const square = moveOrSquare; |
6808d7a1 | 116 | if (square == "-") return undefined; |
dac39588 BA |
117 | let res = []; |
118 | square.split(",").forEach(sq => { | |
119 | res.push(V.SquareToCoords(sq)); | |
120 | }); | |
121 | return res; | |
122 | } | |
123 | // Argument is a move: | |
124 | const move = moveOrSquare; | |
6808d7a1 BA |
125 | const [sx, sy, ex] = [move.start.x, move.start.y, move.end.x]; |
126 | if (this.getPiece(sx, sy) == V.PAWN && Math.abs(sx - ex) >= 2) { | |
127 | const step = (ex - sx) / Math.abs(ex - sx); | |
128 | let res = [ | |
129 | { | |
130 | x: sx + step, | |
131 | y: sy | |
132 | } | |
133 | ]; | |
134 | if (sx + 2 * step != ex) { | |
472c0c4f | 135 | // 3-squares jump |
dac39588 | 136 | res.push({ |
6808d7a1 | 137 | x: sx + 2 * step, |
dac39588 BA |
138 | y: sy |
139 | }); | |
140 | } | |
141 | return res; | |
142 | } | |
143 | return undefined; //default | |
144 | } | |
145 | ||
6808d7a1 BA |
146 | getPotentialMovesFrom([x, y]) { |
147 | switch (this.getPiece(x, y)) { | |
dac39588 | 148 | case V.MARSHALL: |
6808d7a1 | 149 | return this.getPotentialMarshallMoves([x, y]); |
dac39588 | 150 | case V.CARDINAL: |
6808d7a1 | 151 | return this.getPotentialCardinalMoves([x, y]); |
dac39588 | 152 | default: |
6808d7a1 | 153 | return super.getPotentialMovesFrom([x, y]); |
dac39588 BA |
154 | } |
155 | } | |
156 | ||
157 | // Special pawn rules: promotions to captured friendly pieces, | |
158 | // optional on ranks 8-9 and mandatory on rank 10. | |
6808d7a1 | 159 | getPotentialPawnMoves([x, y]) { |
dac39588 BA |
160 | const color = this.turn; |
161 | let moves = []; | |
6808d7a1 BA |
162 | const [sizeX, sizeY] = [V.size.x, V.size.y]; |
163 | const shiftX = color == "w" ? -1 : 1; | |
164 | const startRanks = color == "w" ? [sizeX - 2, sizeX - 3] : [1, 2]; | |
165 | const lastRanks = | |
166 | color == "w" ? [0, 1, 2] : [sizeX - 1, sizeX - 2, sizeX - 3]; | |
167 | const promotionPieces = [ | |
168 | V.ROOK, | |
169 | V.KNIGHT, | |
170 | V.BISHOP, | |
171 | V.QUEEN, | |
172 | V.MARSHALL, | |
173 | V.CARDINAL | |
174 | ]; | |
dac39588 BA |
175 | |
176 | // Always x+shiftX >= 0 && x+shiftX < sizeX, because no pawns on last rank | |
177 | let finalPieces = undefined; | |
6808d7a1 | 178 | if (lastRanks.includes(x + shiftX)) { |
dac39588 | 179 | finalPieces = promotionPieces.filter(p => this.captured[color][p] > 0); |
6808d7a1 BA |
180 | if (x + shiftX != lastRanks[0]) finalPieces.push(V.PAWN); |
181 | } else finalPieces = [V.PAWN]; | |
182 | if (this.board[x + shiftX][y] == V.EMPTY) { | |
dac39588 BA |
183 | // One square forward |
184 | for (let piece of finalPieces) | |
6808d7a1 BA |
185 | moves.push( |
186 | this.getBasicMove([x, y], [x + shiftX, y], { c: color, p: piece }) | |
187 | ); | |
188 | if (startRanks.includes(x)) { | |
189 | if (this.board[x + 2 * shiftX][y] == V.EMPTY) { | |
dac39588 | 190 | // Two squares jump |
6808d7a1 BA |
191 | moves.push(this.getBasicMove([x, y], [x + 2 * shiftX, y])); |
192 | if (x == startRanks[0] && this.board[x + 3 * shiftX][y] == V.EMPTY) { | |
dac39588 | 193 | // Three squares jump |
6808d7a1 | 194 | moves.push(this.getBasicMove([x, y], [x + 3 * shiftX, y])); |
dac39588 BA |
195 | } |
196 | } | |
197 | } | |
198 | } | |
199 | // Captures | |
6808d7a1 BA |
200 | for (let shiftY of [-1, 1]) { |
201 | if ( | |
202 | y + shiftY >= 0 && | |
203 | y + shiftY < sizeY && | |
204 | this.board[x + shiftX][y + shiftY] != V.EMPTY && | |
205 | this.canTake([x, y], [x + shiftX, y + shiftY]) | |
206 | ) { | |
207 | for (let piece of finalPieces) { | |
208 | moves.push( | |
209 | this.getBasicMove([x, y], [x + shiftX, y + shiftY], { | |
210 | c: color, | |
211 | p: piece | |
212 | }) | |
213 | ); | |
dac39588 BA |
214 | } |
215 | } | |
216 | } | |
217 | ||
218 | // En passant | |
219 | const Lep = this.epSquares.length; | |
6808d7a1 | 220 | const epSquare = this.epSquares[Lep - 1]; |
472c0c4f | 221 | if (!!epSquare) { |
6808d7a1 | 222 | for (let epsq of epSquare) { |
dac39588 | 223 | // TODO: some redundant checks |
6808d7a1 | 224 | if (epsq.x == x + shiftX && Math.abs(epsq.y - y) == 1) { |
32f6285e | 225 | let enpassantMove = this.getBasicMove([x, y], [epsq.x, epsq.y]); |
dac39588 BA |
226 | // WARNING: the captured pawn may be diagonally behind us, |
227 | // if it's a 3-squares jump and we take on 1st passing square | |
6808d7a1 | 228 | const px = this.board[x][epsq.y] != V.EMPTY ? x : x - shiftX; |
dac39588 BA |
229 | enpassantMove.vanish.push({ |
230 | x: px, | |
231 | y: epsq.y, | |
6808d7a1 BA |
232 | p: "p", |
233 | c: this.getColor(px, epsq.y) | |
dac39588 BA |
234 | }); |
235 | moves.push(enpassantMove); | |
236 | } | |
237 | } | |
238 | } | |
239 | ||
240 | return moves; | |
241 | } | |
242 | ||
243 | // TODO: different castle? | |
244 | ||
6808d7a1 | 245 | getPotentialMarshallMoves(sq) { |
dac39588 | 246 | return this.getSlideNJumpMoves(sq, V.steps[V.ROOK]).concat( |
6808d7a1 BA |
247 | this.getSlideNJumpMoves(sq, V.steps[V.KNIGHT], "oneStep") |
248 | ); | |
dac39588 BA |
249 | } |
250 | ||
6808d7a1 | 251 | getPotentialCardinalMoves(sq) { |
dac39588 | 252 | return this.getSlideNJumpMoves(sq, V.steps[V.BISHOP]).concat( |
6808d7a1 BA |
253 | this.getSlideNJumpMoves(sq, V.steps[V.KNIGHT], "oneStep") |
254 | ); | |
dac39588 BA |
255 | } |
256 | ||
68e19a44 | 257 | isAttacked(sq, color) { |
6808d7a1 | 258 | return ( |
68e19a44 BA |
259 | super.isAttacked(sq, color) || |
260 | this.isAttackedByMarshall(sq, color) || | |
261 | this.isAttackedByCardinal(sq, color) | |
6808d7a1 | 262 | ); |
dac39588 BA |
263 | } |
264 | ||
68e19a44 | 265 | isAttackedByMarshall(sq, color) { |
6808d7a1 | 266 | return ( |
68e19a44 | 267 | this.isAttackedBySlideNJump(sq, color, V.MARSHALL, V.steps[V.ROOK]) || |
6808d7a1 BA |
268 | this.isAttackedBySlideNJump( |
269 | sq, | |
68e19a44 | 270 | color, |
6808d7a1 BA |
271 | V.MARSHALL, |
272 | V.steps[V.KNIGHT], | |
273 | "oneStep" | |
274 | ) | |
275 | ); | |
dac39588 BA |
276 | } |
277 | ||
68e19a44 | 278 | isAttackedByCardinal(sq, color) { |
6808d7a1 | 279 | return ( |
68e19a44 | 280 | this.isAttackedBySlideNJump(sq, color, V.CARDINAL, V.steps[V.BISHOP]) || |
6808d7a1 BA |
281 | this.isAttackedBySlideNJump( |
282 | sq, | |
68e19a44 | 283 | color, |
6808d7a1 BA |
284 | V.CARDINAL, |
285 | V.steps[V.KNIGHT], | |
286 | "oneStep" | |
287 | ) | |
288 | ); | |
dac39588 BA |
289 | } |
290 | ||
3a2a7b5f BA |
291 | postPlay(move) { |
292 | super.postPlay(move); | |
6808d7a1 | 293 | if (move.vanish.length == 2 && move.appear.length == 1) { |
dac39588 BA |
294 | // Capture: update this.captured |
295 | this.captured[move.vanish[1].c][move.vanish[1].p]++; | |
296 | } | |
6808d7a1 | 297 | if (move.vanish[0].p != move.appear[0].p) { |
dac39588 BA |
298 | // Promotion: update this.captured |
299 | this.captured[move.vanish[0].c][move.appear[0].p]--; | |
300 | } | |
301 | } | |
302 | ||
3a2a7b5f BA |
303 | postUndo(move) { |
304 | super.postUndo(move); | |
dac39588 BA |
305 | if (move.vanish.length == 2 && move.appear.length == 1) |
306 | this.captured[move.vanish[1].c][move.vanish[1].p]--; | |
307 | if (move.vanish[0].p != move.appear[0].p) | |
308 | this.captured[move.vanish[0].c][move.appear[0].p]++; | |
309 | } | |
310 | ||
6808d7a1 | 311 | static get VALUES() { |
dac39588 | 312 | return Object.assign( |
a97bdbda BA |
313 | { c: 5, m: 7 }, //experimental |
314 | ChessRules.VALUES | |
dac39588 BA |
315 | ); |
316 | } | |
317 | ||
6808d7a1 BA |
318 | static get SEARCH_DEPTH() { |
319 | return 2; | |
320 | } | |
dac39588 | 321 | |
7ba4a5bc | 322 | static GenRandInitFen(randomness) { |
7ba4a5bc | 323 | if (randomness == 0) { |
2c5d7b20 BA |
324 | return ( |
325 | "r8r/1nbqkmcbn1/pppppppppp/91/91/91/91/PPPPPPPPPP/1NBQKMCBN1/R8R " + | |
326 | // No castling in the official initial setup | |
327 | "w 0 zzzz - 00000000000000" | |
328 | ); | |
7ba4a5bc BA |
329 | } |
330 | ||
6808d7a1 | 331 | let pieces = { w: new Array(10), b: new Array(10) }; |
6f2f9437 | 332 | let flags = ""; |
dac39588 | 333 | // Shuffle pieces on first and last rank |
6808d7a1 | 334 | for (let c of ["w", "b"]) { |
7ba4a5bc BA |
335 | if (c == 'b' && randomness == 1) { |
336 | pieces['b'] = pieces['w']; | |
6f2f9437 | 337 | flags += flags; |
7ba4a5bc BA |
338 | break; |
339 | } | |
340 | ||
dac39588 BA |
341 | let positions = ArrayFun.range(10); |
342 | ||
343 | // Get random squares for bishops | |
344 | let randIndex = 2 * randInt(5); | |
345 | let bishop1Pos = positions[randIndex]; | |
346 | // The second bishop must be on a square of different color | |
347 | let randIndex_tmp = 2 * randInt(5) + 1; | |
348 | let bishop2Pos = positions[randIndex_tmp]; | |
349 | // Remove chosen squares | |
6808d7a1 BA |
350 | positions.splice(Math.max(randIndex, randIndex_tmp), 1); |
351 | positions.splice(Math.min(randIndex, randIndex_tmp), 1); | |
dac39588 BA |
352 | |
353 | // Get random squares for knights | |
354 | randIndex = randInt(8); | |
355 | let knight1Pos = positions[randIndex]; | |
356 | positions.splice(randIndex, 1); | |
357 | randIndex = randInt(7); | |
358 | let knight2Pos = positions[randIndex]; | |
359 | positions.splice(randIndex, 1); | |
360 | ||
361 | // Get random square for queen | |
362 | randIndex = randInt(6); | |
363 | let queenPos = positions[randIndex]; | |
364 | positions.splice(randIndex, 1); | |
365 | ||
366 | // ...random square for marshall | |
367 | randIndex = randInt(5); | |
368 | let marshallPos = positions[randIndex]; | |
369 | positions.splice(randIndex, 1); | |
370 | ||
371 | // ...random square for cardinal | |
372 | randIndex = randInt(4); | |
373 | let cardinalPos = positions[randIndex]; | |
374 | positions.splice(randIndex, 1); | |
375 | ||
2c5d7b20 BA |
376 | // Rooks and king positions are now fixed, |
377 | // because of the ordering rook-king-rook | |
dac39588 BA |
378 | let rook1Pos = positions[0]; |
379 | let kingPos = positions[1]; | |
380 | let rook2Pos = positions[2]; | |
381 | ||
382 | // Finally put the shuffled pieces in the board array | |
6808d7a1 BA |
383 | pieces[c][rook1Pos] = "r"; |
384 | pieces[c][knight1Pos] = "n"; | |
385 | pieces[c][bishop1Pos] = "b"; | |
386 | pieces[c][queenPos] = "q"; | |
387 | pieces[c][marshallPos] = "m"; | |
388 | pieces[c][cardinalPos] = "c"; | |
389 | pieces[c][kingPos] = "k"; | |
390 | pieces[c][bishop2Pos] = "b"; | |
391 | pieces[c][knight2Pos] = "n"; | |
392 | pieces[c][rook2Pos] = "r"; | |
6f2f9437 | 393 | flags += V.CoordToColumn(rook1Pos) + V.CoordToColumn(rook2Pos); |
dac39588 | 394 | } |
6808d7a1 BA |
395 | return ( |
396 | pieces["b"].join("") + | |
6f2f9437 | 397 | "/pppppppppp/91/91/91/91/91/91/PPPPPPPPPP/" + |
dac39588 | 398 | pieces["w"].join("").toUpperCase() + |
6f2f9437 | 399 | " w 0 " + flags + " - 00000000000000" |
6808d7a1 | 400 | ); |
dac39588 | 401 | } |
6808d7a1 | 402 | }; |