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