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