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