Commit | Line | Data |
---|---|---|
83cecc0f | 1 | import { ChessRules, Move, PiPo } from "@/base_rules"; |
0c3fe8a6 BA |
2 | import { ArrayFun } from "@/utils/array"; |
3 | import { sample, randInt } from "@/utils/alea"; | |
4 | ||
32f6285e | 5 | export class WildebeestRules extends ChessRules { |
7e8a7ea1 | 6 | |
6808d7a1 BA |
7 | static get size() { |
8 | return { x: 10, y: 11 }; | |
9 | } | |
8a196305 | 10 | |
6808d7a1 BA |
11 | static get CAMEL() { |
12 | return "c"; | |
13 | } | |
14 | static get WILDEBEEST() { | |
15 | return "w"; | |
16 | } | |
a37076f1 | 17 | |
6808d7a1 BA |
18 | static get PIECES() { |
19 | return ChessRules.PIECES.concat([V.CAMEL, V.WILDEBEEST]); | |
dac39588 | 20 | } |
7931e479 | 21 | |
6808d7a1 | 22 | static get steps() { |
dac39588 | 23 | return Object.assign( |
6f2f9437 BA |
24 | {}, |
25 | ChessRules.steps, | |
26 | // Add camel moves: | |
6808d7a1 BA |
27 | { |
28 | c: [ | |
29 | [-3, -1], | |
30 | [-3, 1], | |
31 | [-1, -3], | |
32 | [-1, 3], | |
33 | [1, -3], | |
34 | [1, 3], | |
35 | [3, -1], | |
36 | [3, 1] | |
37 | ] | |
38 | } | |
dac39588 BA |
39 | ); |
40 | } | |
a37076f1 | 41 | |
6808d7a1 | 42 | static IsGoodEnpassant(enpassant) { |
472c0c4f | 43 | if (enpassant != "-") return !!enpassant.match(/^([a-j][0-9]{1,2},?)+$/); |
dac39588 BA |
44 | return true; |
45 | } | |
6e62b1c7 | 46 | |
241bf8f2 BA |
47 | getPpath(b) { |
48 | return ([V.CAMEL, V.WILDEBEEST].includes(b[1]) ? "Wildebeest/" : "") + b; | |
49 | } | |
50 | ||
dac39588 | 51 | // There may be 2 enPassant squares (if pawn jump 3 squares) |
6808d7a1 | 52 | getEnpassantFen() { |
dac39588 | 53 | const L = this.epSquares.length; |
6808d7a1 | 54 | if (!this.epSquares[L - 1]) return "-"; //no en-passant |
dac39588 | 55 | let res = ""; |
6808d7a1 | 56 | this.epSquares[L - 1].forEach(sq => { |
dac39588 BA |
57 | res += V.CoordsToSquare(sq) + ","; |
58 | }); | |
6808d7a1 | 59 | return res.slice(0, -1); //remove last comma |
dac39588 | 60 | } |
2d7194bd | 61 | |
dac39588 | 62 | // En-passant after 2-sq or 3-sq jumps |
6808d7a1 BA |
63 | getEpSquare(moveOrSquare) { |
64 | if (!moveOrSquare) return undefined; | |
65 | if (typeof moveOrSquare === "string") { | |
dac39588 | 66 | const square = moveOrSquare; |
6808d7a1 | 67 | if (square == "-") return undefined; |
dac39588 BA |
68 | let res = []; |
69 | square.split(",").forEach(sq => { | |
70 | res.push(V.SquareToCoords(sq)); | |
71 | }); | |
72 | return res; | |
73 | } | |
74 | // Argument is a move: | |
75 | const move = moveOrSquare; | |
6808d7a1 BA |
76 | const [sx, sy, ex] = [move.start.x, move.start.y, move.end.x]; |
77 | if (this.getPiece(sx, sy) == V.PAWN && Math.abs(sx - ex) >= 2) { | |
78 | const step = (ex - sx) / Math.abs(ex - sx); | |
79 | let res = [ | |
80 | { | |
81 | x: sx + step, | |
82 | y: sy | |
83 | } | |
84 | ]; | |
85 | if (sx + 2 * step != ex) { | |
a538d4b0 | 86 | // 3-squares move |
dac39588 | 87 | res.push({ |
6808d7a1 | 88 | x: sx + 2 * step, |
dac39588 BA |
89 | y: sy |
90 | }); | |
91 | } | |
92 | return res; | |
93 | } | |
94 | return undefined; //default | |
95 | } | |
a37076f1 | 96 | |
6808d7a1 BA |
97 | getPotentialMovesFrom([x, y]) { |
98 | switch (this.getPiece(x, y)) { | |
dac39588 | 99 | case V.CAMEL: |
6808d7a1 | 100 | return this.getPotentialCamelMoves([x, y]); |
dac39588 | 101 | case V.WILDEBEEST: |
6808d7a1 | 102 | return this.getPotentialWildebeestMoves([x, y]); |
dac39588 | 103 | default: |
6808d7a1 | 104 | return super.getPotentialMovesFrom([x, y]); |
dac39588 BA |
105 | } |
106 | } | |
a37076f1 | 107 | |
dac39588 | 108 | // Pawns jump 2 or 3 squares, and promote to queen or wildebeest |
6808d7a1 | 109 | getPotentialPawnMoves([x, y]) { |
dac39588 BA |
110 | const color = this.turn; |
111 | let moves = []; | |
6808d7a1 BA |
112 | const [sizeX, sizeY] = [V.size.x, V.size.y]; |
113 | const shiftX = color == "w" ? -1 : 1; | |
114 | const startRanks = color == "w" ? [sizeX - 2, sizeX - 3] : [1, 2]; | |
278a28a1 BA |
115 | const lastRanks = color == "w" ? [0, 1] : [sizeX - 1, sizeX -2]; |
116 | let finalPieces = [V.PAWN]; | |
117 | if (x + shiftX == lastRanks[1]) | |
118 | Array.prototype.push.apply(finalPieces, [V.WILDEBEEST, V.QUEEN]); | |
119 | else if (x + shiftX == lastRanks[0]) | |
120 | finalPieces = [V.WILDEBEEST, V.QUEEN]; | |
a37076f1 | 121 | |
6808d7a1 | 122 | if (this.board[x + shiftX][y] == V.EMPTY) { |
dac39588 BA |
123 | // One square forward |
124 | for (let piece of finalPieces) | |
6808d7a1 BA |
125 | moves.push( |
126 | this.getBasicMove([x, y], [x + shiftX, y], { c: color, p: piece }) | |
127 | ); | |
128 | if (startRanks.includes(x)) { | |
129 | if (this.board[x + 2 * shiftX][y] == V.EMPTY) { | |
dac39588 | 130 | // Two squares jump |
6808d7a1 BA |
131 | moves.push(this.getBasicMove([x, y], [x + 2 * shiftX, y])); |
132 | if (x == startRanks[0] && this.board[x + 3 * shiftX][y] == V.EMPTY) { | |
dac39588 | 133 | // Three squares jump |
6808d7a1 | 134 | moves.push(this.getBasicMove([x, y], [x + 3 * shiftX, y])); |
dac39588 BA |
135 | } |
136 | } | |
137 | } | |
138 | } | |
139 | // Captures | |
6808d7a1 BA |
140 | for (let shiftY of [-1, 1]) { |
141 | if ( | |
142 | y + shiftY >= 0 && | |
143 | y + shiftY < sizeY && | |
144 | this.board[x + shiftX][y + shiftY] != V.EMPTY && | |
145 | this.canTake([x, y], [x + shiftX, y + shiftY]) | |
146 | ) { | |
147 | for (let piece of finalPieces) { | |
148 | moves.push( | |
149 | this.getBasicMove([x, y], [x + shiftX, y + shiftY], { | |
150 | c: color, | |
151 | p: piece | |
152 | }) | |
153 | ); | |
dac39588 BA |
154 | } |
155 | } | |
156 | } | |
a37076f1 | 157 | |
dac39588 BA |
158 | // En passant |
159 | const Lep = this.epSquares.length; | |
6808d7a1 | 160 | const epSquare = this.epSquares[Lep - 1]; |
472c0c4f | 161 | if (!!epSquare) { |
6808d7a1 | 162 | for (let epsq of epSquare) { |
dac39588 | 163 | // TODO: some redundant checks |
6808d7a1 | 164 | if (epsq.x == x + shiftX && Math.abs(epsq.y - y) == 1) { |
f52671e5 | 165 | let enpassantMove = this.getBasicMove([x, y], [epsq.x, epsq.y]); |
dac39588 BA |
166 | // WARNING: the captured pawn may be diagonally behind us, |
167 | // if it's a 3-squares jump and we take on 1st passing square | |
6808d7a1 | 168 | const px = this.board[x][epsq.y] != V.EMPTY ? x : x - shiftX; |
dac39588 BA |
169 | enpassantMove.vanish.push({ |
170 | x: px, | |
171 | y: epsq.y, | |
6808d7a1 BA |
172 | p: "p", |
173 | c: this.getColor(px, epsq.y) | |
dac39588 BA |
174 | }); |
175 | moves.push(enpassantMove); | |
176 | } | |
177 | } | |
178 | } | |
a37076f1 | 179 | |
dac39588 BA |
180 | return moves; |
181 | } | |
a37076f1 | 182 | |
6808d7a1 | 183 | getPotentialCamelMoves(sq) { |
dac39588 BA |
184 | return this.getSlideNJumpMoves(sq, V.steps[V.CAMEL], "oneStep"); |
185 | } | |
a37076f1 | 186 | |
6808d7a1 | 187 | getPotentialWildebeestMoves(sq) { |
dac39588 | 188 | return this.getSlideNJumpMoves( |
6808d7a1 BA |
189 | sq, |
190 | V.steps[V.KNIGHT].concat(V.steps[V.CAMEL]), | |
191 | "oneStep" | |
192 | ); | |
dac39588 | 193 | } |
a37076f1 | 194 | |
83cecc0f BA |
195 | getPPpath(m) { |
196 | if ( | |
197 | m.appear.length == 2 && m.vanish.length == 2 && | |
198 | Math.abs(m.end.y - m.start.y) == 1 && | |
199 | this.board[m.end.x][m.end.y] == V.EMPTY | |
200 | ) { | |
201 | // Castle, king moved by one square only, not directly onto rook | |
202 | return "Wildebeest/castle"; | |
203 | } | |
204 | return super.getPPpath(m); | |
205 | } | |
206 | ||
207 | // Special Wildebeest castling rules: | |
208 | getCastleMoves([x, y]) { | |
209 | const c = this.getColor(x, y); | |
210 | const oppCol = V.GetOppCol(c); | |
211 | let moves = []; | |
212 | let i = 0; | |
213 | const castlingKing = this.board[x][y].charAt(1); | |
214 | castlingCheck: for ( | |
215 | let castleSide = 0; | |
216 | castleSide < 2; | |
217 | castleSide++ //"large", then "small" | |
218 | ) { | |
219 | if (this.castleFlags[c][castleSide] >= V.size.y) continue; | |
220 | // Rook and king are on initial position | |
221 | const rookPos = this.castleFlags[c][castleSide]; | |
222 | const range = (castleSide == 0 ? [rookPos, y] : [y, rookPos]); | |
223 | ||
224 | // King and rook must be connected: | |
225 | for (let i = range[0] + 1; i <= range[1] - 1; i++) { | |
226 | if (this.board[x][i] != V.EMPTY) continue castlingCheck; | |
227 | } | |
228 | const step = 2 * castleSide - 1; | |
229 | // No attacks on the path of the king ? | |
230 | for (let i = range[0]; i <= range[1]; i++) { | |
231 | if (i != rookPos && this.isAttacked([x, i], oppCol)) | |
232 | continue castlingCheck; | |
a538d4b0 BA |
233 | if ( |
234 | i != y && | |
235 | // Do not end in the corner, except if starting square is too near | |
236 | (i > 0 || y == 1) && | |
237 | (i < V.size.y - 1 || y == V.size.y - 2) | |
238 | ) { | |
83cecc0f BA |
239 | // Found a possible castle move: |
240 | moves.push( | |
241 | new Move({ | |
242 | appear: [ | |
243 | new PiPo({ | |
244 | x: x, | |
245 | y: i, | |
246 | p: V.KING, | |
247 | c: c | |
248 | }), | |
249 | new PiPo({ | |
250 | x: x, | |
251 | y: i - step, | |
252 | p: V.ROOK, | |
253 | c: c | |
254 | }) | |
255 | ], | |
256 | vanish: [ | |
257 | new PiPo({ x: x, y: y, p: V.KING, c: c }), | |
258 | new PiPo({ x: x, y: rookPos, p: V.ROOK, c: c }) | |
259 | ] | |
260 | }) | |
261 | ); | |
262 | } | |
263 | } | |
264 | } | |
265 | ||
266 | return moves; | |
267 | } | |
268 | ||
68e19a44 | 269 | isAttacked(sq, color) { |
6808d7a1 | 270 | return ( |
68e19a44 BA |
271 | super.isAttacked(sq, color) || |
272 | this.isAttackedByCamel(sq, color) || | |
273 | this.isAttackedByWildebeest(sq, color) | |
6808d7a1 | 274 | ); |
dac39588 | 275 | } |
a37076f1 | 276 | |
68e19a44 | 277 | isAttackedByCamel(sq, color) { |
6808d7a1 BA |
278 | return this.isAttackedBySlideNJump( |
279 | sq, | |
68e19a44 | 280 | color, |
6808d7a1 BA |
281 | V.CAMEL, |
282 | V.steps[V.CAMEL], | |
283 | "oneStep" | |
284 | ); | |
dac39588 | 285 | } |
a37076f1 | 286 | |
68e19a44 | 287 | isAttackedByWildebeest(sq, color) { |
6808d7a1 BA |
288 | return this.isAttackedBySlideNJump( |
289 | sq, | |
68e19a44 | 290 | color, |
6808d7a1 BA |
291 | V.WILDEBEEST, |
292 | V.steps[V.KNIGHT].concat(V.steps[V.CAMEL]), | |
293 | "oneStep" | |
294 | ); | |
dac39588 | 295 | } |
a37076f1 | 296 | |
6808d7a1 | 297 | getCurrentScore() { |
bb688df5 | 298 | if (this.atLeastOneMove()) return "*"; |
dac39588 | 299 | // No valid move: game is lost (stalemate is a win) |
6808d7a1 | 300 | return this.turn == "w" ? "0-1" : "1-0"; |
dac39588 | 301 | } |
efb20746 | 302 | |
dac39588 BA |
303 | static get VALUES() { |
304 | return Object.assign( | |
a97bdbda BA |
305 | { c: 3, w: 7 }, //experimental |
306 | ChessRules.VALUES | |
dac39588 BA |
307 | ); |
308 | } | |
a37076f1 | 309 | |
6808d7a1 BA |
310 | static get SEARCH_DEPTH() { |
311 | return 2; | |
312 | } | |
3c09dc49 | 313 | |
7ba4a5bc | 314 | static GenRandInitFen(randomness) { |
2c5d7b20 BA |
315 | if (randomness == 0) { |
316 | return ( | |
317 | "rnccwkqbbnr/ppppppppppp/92/92/92/92/92/92/PPPPPPPPPPP/RNBBQKWCCNR " + | |
318 | "w 0 akak -" | |
319 | ); | |
320 | } | |
7ba4a5bc | 321 | |
6f2f9437 | 322 | let pieces = { w: new Array(11), b: new Array(11) }; |
3a2a7b5f | 323 | let flags = ""; |
6808d7a1 | 324 | for (let c of ["w", "b"]) { |
7ba4a5bc BA |
325 | if (c == 'b' && randomness == 1) { |
326 | pieces['b'] = pieces['w']; | |
3a2a7b5f | 327 | flags += flags; |
7ba4a5bc BA |
328 | break; |
329 | } | |
330 | ||
dac39588 | 331 | let positions = ArrayFun.range(11); |
a37076f1 | 332 | |
dac39588 | 333 | // Get random squares for bishops + camels (different colors) |
6808d7a1 BA |
334 | let randIndexes = sample(ArrayFun.range(6), 2).map(i => { |
335 | return 2 * i; | |
336 | }); | |
dac39588 BA |
337 | let bishop1Pos = positions[randIndexes[0]]; |
338 | let camel1Pos = positions[randIndexes[1]]; | |
339 | // The second bishop (camel) must be on a square of different color | |
6808d7a1 BA |
340 | let randIndexes_tmp = sample(ArrayFun.range(5), 2).map(i => { |
341 | return 2 * i + 1; | |
342 | }); | |
dac39588 BA |
343 | let bishop2Pos = positions[randIndexes_tmp[0]]; |
344 | let camel2Pos = positions[randIndexes_tmp[1]]; | |
6808d7a1 BA |
345 | for (let idx of randIndexes.concat(randIndexes_tmp).sort((a, b) => { |
346 | return b - a; | |
347 | })) { | |
7ba4a5bc | 348 | // Largest indices first |
dac39588 BA |
349 | positions.splice(idx, 1); |
350 | } | |
a37076f1 | 351 | |
dac39588 BA |
352 | let randIndex = randInt(7); |
353 | let knight1Pos = positions[randIndex]; | |
354 | positions.splice(randIndex, 1); | |
355 | randIndex = randInt(6); | |
356 | let knight2Pos = positions[randIndex]; | |
357 | positions.splice(randIndex, 1); | |
a37076f1 | 358 | |
dac39588 BA |
359 | randIndex = randInt(5); |
360 | let queenPos = positions[randIndex]; | |
361 | positions.splice(randIndex, 1); | |
8a196305 | 362 | |
dac39588 BA |
363 | // Random square for wildebeest |
364 | randIndex = randInt(4); | |
365 | let wildebeestPos = positions[randIndex]; | |
366 | positions.splice(randIndex, 1); | |
8a196305 | 367 | |
dac39588 BA |
368 | let rook1Pos = positions[0]; |
369 | let kingPos = positions[1]; | |
370 | let rook2Pos = positions[2]; | |
a37076f1 | 371 | |
6808d7a1 BA |
372 | pieces[c][rook1Pos] = "r"; |
373 | pieces[c][knight1Pos] = "n"; | |
374 | pieces[c][bishop1Pos] = "b"; | |
375 | pieces[c][queenPos] = "q"; | |
376 | pieces[c][camel1Pos] = "c"; | |
377 | pieces[c][camel2Pos] = "c"; | |
378 | pieces[c][wildebeestPos] = "w"; | |
379 | pieces[c][kingPos] = "k"; | |
380 | pieces[c][bishop2Pos] = "b"; | |
381 | pieces[c][knight2Pos] = "n"; | |
382 | pieces[c][rook2Pos] = "r"; | |
3a2a7b5f | 383 | flags += V.CoordToColumn(rook1Pos) + V.CoordToColumn(rook2Pos); |
dac39588 | 384 | } |
6808d7a1 BA |
385 | return ( |
386 | pieces["b"].join("") + | |
6f2f9437 | 387 | "/ppppppppppp/92/92/92/92/92/92/PPPPPPPPPPP/" + |
dac39588 | 388 | pieces["w"].join("").toUpperCase() + |
3a2a7b5f | 389 | " w 0 " + flags + " -" |
6808d7a1 | 390 | ); |
dac39588 | 391 | } |
7e8a7ea1 | 392 | |
6808d7a1 | 393 | }; |