Commit | Line | Data |
---|---|---|
0c3fe8a6 BA |
1 | import { ChessRules } from "@/base_rules"; |
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) { | |
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]; | |
115 | const lastRank = color == "w" ? 0 : sizeX - 1; | |
e3ef199c BA |
116 | const finalPieces = x + shiftX == lastRank |
117 | ? [V.WILDEBEEST, V.QUEEN] | |
118 | : [V.PAWN]; | |
a37076f1 | 119 | |
6808d7a1 | 120 | if (this.board[x + shiftX][y] == V.EMPTY) { |
dac39588 BA |
121 | // One square forward |
122 | for (let piece of finalPieces) | |
6808d7a1 BA |
123 | moves.push( |
124 | this.getBasicMove([x, y], [x + shiftX, y], { c: color, p: piece }) | |
125 | ); | |
126 | if (startRanks.includes(x)) { | |
127 | if (this.board[x + 2 * shiftX][y] == V.EMPTY) { | |
dac39588 | 128 | // Two squares jump |
6808d7a1 BA |
129 | moves.push(this.getBasicMove([x, y], [x + 2 * shiftX, y])); |
130 | if (x == startRanks[0] && this.board[x + 3 * shiftX][y] == V.EMPTY) { | |
dac39588 | 131 | // Three squares jump |
6808d7a1 | 132 | moves.push(this.getBasicMove([x, y], [x + 3 * shiftX, y])); |
dac39588 BA |
133 | } |
134 | } | |
135 | } | |
136 | } | |
137 | // Captures | |
6808d7a1 BA |
138 | for (let shiftY of [-1, 1]) { |
139 | if ( | |
140 | y + shiftY >= 0 && | |
141 | y + shiftY < sizeY && | |
142 | this.board[x + shiftX][y + shiftY] != V.EMPTY && | |
143 | this.canTake([x, y], [x + shiftX, y + shiftY]) | |
144 | ) { | |
145 | for (let piece of finalPieces) { | |
146 | moves.push( | |
147 | this.getBasicMove([x, y], [x + shiftX, y + shiftY], { | |
148 | c: color, | |
149 | p: piece | |
150 | }) | |
151 | ); | |
dac39588 BA |
152 | } |
153 | } | |
154 | } | |
a37076f1 | 155 | |
dac39588 BA |
156 | // En passant |
157 | const Lep = this.epSquares.length; | |
6808d7a1 | 158 | const epSquare = this.epSquares[Lep - 1]; |
472c0c4f | 159 | if (!!epSquare) { |
6808d7a1 | 160 | for (let epsq of epSquare) { |
dac39588 | 161 | // TODO: some redundant checks |
6808d7a1 BA |
162 | if (epsq.x == x + shiftX && Math.abs(epsq.y - y) == 1) { |
163 | var enpassantMove = this.getBasicMove([x, y], [epsq.x, epsq.y]); | |
dac39588 BA |
164 | // WARNING: the captured pawn may be diagonally behind us, |
165 | // if it's a 3-squares jump and we take on 1st passing square | |
6808d7a1 | 166 | const px = this.board[x][epsq.y] != V.EMPTY ? x : x - shiftX; |
dac39588 BA |
167 | enpassantMove.vanish.push({ |
168 | x: px, | |
169 | y: epsq.y, | |
6808d7a1 BA |
170 | p: "p", |
171 | c: this.getColor(px, epsq.y) | |
dac39588 BA |
172 | }); |
173 | moves.push(enpassantMove); | |
174 | } | |
175 | } | |
176 | } | |
a37076f1 | 177 | |
dac39588 BA |
178 | return moves; |
179 | } | |
a37076f1 | 180 | |
dac39588 | 181 | // TODO: wildebeest castle |
8a196305 | 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 | |
68e19a44 | 195 | isAttacked(sq, color) { |
6808d7a1 | 196 | return ( |
68e19a44 BA |
197 | super.isAttacked(sq, color) || |
198 | this.isAttackedByCamel(sq, color) || | |
199 | this.isAttackedByWildebeest(sq, color) | |
6808d7a1 | 200 | ); |
dac39588 | 201 | } |
a37076f1 | 202 | |
68e19a44 | 203 | isAttackedByCamel(sq, color) { |
6808d7a1 BA |
204 | return this.isAttackedBySlideNJump( |
205 | sq, | |
68e19a44 | 206 | color, |
6808d7a1 BA |
207 | V.CAMEL, |
208 | V.steps[V.CAMEL], | |
209 | "oneStep" | |
210 | ); | |
dac39588 | 211 | } |
a37076f1 | 212 | |
68e19a44 | 213 | isAttackedByWildebeest(sq, color) { |
6808d7a1 BA |
214 | return this.isAttackedBySlideNJump( |
215 | sq, | |
68e19a44 | 216 | color, |
6808d7a1 BA |
217 | V.WILDEBEEST, |
218 | V.steps[V.KNIGHT].concat(V.steps[V.CAMEL]), | |
219 | "oneStep" | |
220 | ); | |
dac39588 | 221 | } |
a37076f1 | 222 | |
6808d7a1 | 223 | getCurrentScore() { |
bb688df5 | 224 | if (this.atLeastOneMove()) return "*"; |
dac39588 | 225 | // No valid move: game is lost (stalemate is a win) |
6808d7a1 | 226 | return this.turn == "w" ? "0-1" : "1-0"; |
dac39588 | 227 | } |
efb20746 | 228 | |
dac39588 BA |
229 | static get VALUES() { |
230 | return Object.assign( | |
a97bdbda BA |
231 | { c: 3, w: 7 }, //experimental |
232 | ChessRules.VALUES | |
dac39588 BA |
233 | ); |
234 | } | |
a37076f1 | 235 | |
6808d7a1 BA |
236 | static get SEARCH_DEPTH() { |
237 | return 2; | |
238 | } | |
3c09dc49 | 239 | |
7ba4a5bc BA |
240 | static GenRandInitFen(randomness) { |
241 | if (!randomness) randomness = 2; | |
2c5d7b20 BA |
242 | if (randomness == 0) { |
243 | return ( | |
244 | "rnccwkqbbnr/ppppppppppp/92/92/92/92/92/92/PPPPPPPPPPP/RNBBQKWCCNR " + | |
245 | "w 0 akak -" | |
246 | ); | |
247 | } | |
7ba4a5bc | 248 | |
6f2f9437 | 249 | let pieces = { w: new Array(11), b: new Array(11) }; |
3a2a7b5f | 250 | let flags = ""; |
6808d7a1 | 251 | for (let c of ["w", "b"]) { |
7ba4a5bc BA |
252 | if (c == 'b' && randomness == 1) { |
253 | pieces['b'] = pieces['w']; | |
3a2a7b5f | 254 | flags += flags; |
7ba4a5bc BA |
255 | break; |
256 | } | |
257 | ||
dac39588 | 258 | let positions = ArrayFun.range(11); |
a37076f1 | 259 | |
dac39588 | 260 | // Get random squares for bishops + camels (different colors) |
6808d7a1 BA |
261 | let randIndexes = sample(ArrayFun.range(6), 2).map(i => { |
262 | return 2 * i; | |
263 | }); | |
dac39588 BA |
264 | let bishop1Pos = positions[randIndexes[0]]; |
265 | let camel1Pos = positions[randIndexes[1]]; | |
266 | // The second bishop (camel) must be on a square of different color | |
6808d7a1 BA |
267 | let randIndexes_tmp = sample(ArrayFun.range(5), 2).map(i => { |
268 | return 2 * i + 1; | |
269 | }); | |
dac39588 BA |
270 | let bishop2Pos = positions[randIndexes_tmp[0]]; |
271 | let camel2Pos = positions[randIndexes_tmp[1]]; | |
6808d7a1 BA |
272 | for (let idx of randIndexes.concat(randIndexes_tmp).sort((a, b) => { |
273 | return b - a; | |
274 | })) { | |
7ba4a5bc | 275 | // Largest indices first |
dac39588 BA |
276 | positions.splice(idx, 1); |
277 | } | |
a37076f1 | 278 | |
dac39588 BA |
279 | let randIndex = randInt(7); |
280 | let knight1Pos = positions[randIndex]; | |
281 | positions.splice(randIndex, 1); | |
282 | randIndex = randInt(6); | |
283 | let knight2Pos = positions[randIndex]; | |
284 | positions.splice(randIndex, 1); | |
a37076f1 | 285 | |
dac39588 BA |
286 | randIndex = randInt(5); |
287 | let queenPos = positions[randIndex]; | |
288 | positions.splice(randIndex, 1); | |
8a196305 | 289 | |
dac39588 BA |
290 | // Random square for wildebeest |
291 | randIndex = randInt(4); | |
292 | let wildebeestPos = positions[randIndex]; | |
293 | positions.splice(randIndex, 1); | |
8a196305 | 294 | |
dac39588 BA |
295 | let rook1Pos = positions[0]; |
296 | let kingPos = positions[1]; | |
297 | let rook2Pos = positions[2]; | |
a37076f1 | 298 | |
6808d7a1 BA |
299 | pieces[c][rook1Pos] = "r"; |
300 | pieces[c][knight1Pos] = "n"; | |
301 | pieces[c][bishop1Pos] = "b"; | |
302 | pieces[c][queenPos] = "q"; | |
303 | pieces[c][camel1Pos] = "c"; | |
304 | pieces[c][camel2Pos] = "c"; | |
305 | pieces[c][wildebeestPos] = "w"; | |
306 | pieces[c][kingPos] = "k"; | |
307 | pieces[c][bishop2Pos] = "b"; | |
308 | pieces[c][knight2Pos] = "n"; | |
309 | pieces[c][rook2Pos] = "r"; | |
3a2a7b5f | 310 | flags += V.CoordToColumn(rook1Pos) + V.CoordToColumn(rook2Pos); |
dac39588 | 311 | } |
6808d7a1 BA |
312 | return ( |
313 | pieces["b"].join("") + | |
6f2f9437 | 314 | "/ppppppppppp/92/92/92/92/92/92/PPPPPPPPPPP/" + |
dac39588 | 315 | pieces["w"].join("").toUpperCase() + |
3a2a7b5f | 316 | " w 0 " + flags + " -" |
6808d7a1 | 317 | ); |
dac39588 | 318 | } |
7e8a7ea1 | 319 | |
6808d7a1 | 320 | }; |