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 { |
6808d7a1 BA |
6 | static get size() { |
7 | return { x: 10, y: 11 }; | |
8 | } | |
8a196305 | 9 | |
6808d7a1 BA |
10 | static get CAMEL() { |
11 | return "c"; | |
12 | } | |
13 | static get WILDEBEEST() { | |
14 | return "w"; | |
15 | } | |
a37076f1 | 16 | |
6808d7a1 BA |
17 | static get PIECES() { |
18 | return ChessRules.PIECES.concat([V.CAMEL, V.WILDEBEEST]); | |
dac39588 | 19 | } |
7931e479 | 20 | |
6808d7a1 | 21 | static get steps() { |
dac39588 | 22 | return Object.assign( |
6f2f9437 BA |
23 | {}, |
24 | ChessRules.steps, | |
25 | // Add camel moves: | |
6808d7a1 BA |
26 | { |
27 | c: [ | |
28 | [-3, -1], | |
29 | [-3, 1], | |
30 | [-1, -3], | |
31 | [-1, 3], | |
32 | [1, -3], | |
33 | [1, 3], | |
34 | [3, -1], | |
35 | [3, 1] | |
36 | ] | |
37 | } | |
dac39588 BA |
38 | ); |
39 | } | |
a37076f1 | 40 | |
6808d7a1 | 41 | static IsGoodEnpassant(enpassant) { |
472c0c4f | 42 | if (enpassant != "-") return !!enpassant.match(/^([a-j][0-9]{1,2},?)+$/); |
dac39588 BA |
43 | return true; |
44 | } | |
6e62b1c7 | 45 | |
241bf8f2 BA |
46 | getPpath(b) { |
47 | return ([V.CAMEL, V.WILDEBEEST].includes(b[1]) ? "Wildebeest/" : "") + b; | |
48 | } | |
49 | ||
dac39588 | 50 | // There may be 2 enPassant squares (if pawn jump 3 squares) |
6808d7a1 | 51 | getEnpassantFen() { |
dac39588 | 52 | const L = this.epSquares.length; |
6808d7a1 | 53 | if (!this.epSquares[L - 1]) return "-"; //no en-passant |
dac39588 | 54 | let res = ""; |
6808d7a1 | 55 | this.epSquares[L - 1].forEach(sq => { |
dac39588 BA |
56 | res += V.CoordsToSquare(sq) + ","; |
57 | }); | |
6808d7a1 | 58 | return res.slice(0, -1); //remove last comma |
dac39588 | 59 | } |
2d7194bd | 60 | |
dac39588 | 61 | // En-passant after 2-sq or 3-sq jumps |
6808d7a1 BA |
62 | getEpSquare(moveOrSquare) { |
63 | if (!moveOrSquare) return undefined; | |
64 | if (typeof moveOrSquare === "string") { | |
dac39588 | 65 | const square = moveOrSquare; |
6808d7a1 | 66 | if (square == "-") return undefined; |
dac39588 BA |
67 | let res = []; |
68 | square.split(",").forEach(sq => { | |
69 | res.push(V.SquareToCoords(sq)); | |
70 | }); | |
71 | return res; | |
72 | } | |
73 | // Argument is a move: | |
74 | const move = moveOrSquare; | |
6808d7a1 BA |
75 | const [sx, sy, ex] = [move.start.x, move.start.y, move.end.x]; |
76 | if (this.getPiece(sx, sy) == V.PAWN && Math.abs(sx - ex) >= 2) { | |
77 | const step = (ex - sx) / Math.abs(ex - sx); | |
78 | let res = [ | |
79 | { | |
80 | x: sx + step, | |
81 | y: sy | |
82 | } | |
83 | ]; | |
84 | if (sx + 2 * step != ex) { | |
85 | //3-squares move | |
dac39588 | 86 | res.push({ |
6808d7a1 | 87 | x: sx + 2 * step, |
dac39588 BA |
88 | y: sy |
89 | }); | |
90 | } | |
91 | return res; | |
92 | } | |
93 | return undefined; //default | |
94 | } | |
a37076f1 | 95 | |
6808d7a1 BA |
96 | getPotentialMovesFrom([x, y]) { |
97 | switch (this.getPiece(x, y)) { | |
dac39588 | 98 | case V.CAMEL: |
6808d7a1 | 99 | return this.getPotentialCamelMoves([x, y]); |
dac39588 | 100 | case V.WILDEBEEST: |
6808d7a1 | 101 | return this.getPotentialWildebeestMoves([x, y]); |
dac39588 | 102 | default: |
6808d7a1 | 103 | return super.getPotentialMovesFrom([x, y]); |
dac39588 BA |
104 | } |
105 | } | |
a37076f1 | 106 | |
dac39588 | 107 | // Pawns jump 2 or 3 squares, and promote to queen or wildebeest |
6808d7a1 | 108 | getPotentialPawnMoves([x, y]) { |
dac39588 BA |
109 | const color = this.turn; |
110 | let moves = []; | |
6808d7a1 BA |
111 | const [sizeX, sizeY] = [V.size.x, V.size.y]; |
112 | const shiftX = color == "w" ? -1 : 1; | |
113 | const startRanks = color == "w" ? [sizeX - 2, sizeX - 3] : [1, 2]; | |
114 | const lastRank = color == "w" ? 0 : sizeX - 1; | |
e3ef199c BA |
115 | const finalPieces = x + shiftX == lastRank |
116 | ? [V.WILDEBEEST, V.QUEEN] | |
117 | : [V.PAWN]; | |
a37076f1 | 118 | |
6808d7a1 | 119 | if (this.board[x + shiftX][y] == V.EMPTY) { |
dac39588 BA |
120 | // One square forward |
121 | for (let piece of finalPieces) | |
6808d7a1 BA |
122 | moves.push( |
123 | this.getBasicMove([x, y], [x + shiftX, y], { c: color, p: piece }) | |
124 | ); | |
125 | if (startRanks.includes(x)) { | |
126 | if (this.board[x + 2 * shiftX][y] == V.EMPTY) { | |
dac39588 | 127 | // Two squares jump |
6808d7a1 BA |
128 | moves.push(this.getBasicMove([x, y], [x + 2 * shiftX, y])); |
129 | if (x == startRanks[0] && this.board[x + 3 * shiftX][y] == V.EMPTY) { | |
dac39588 | 130 | // Three squares jump |
6808d7a1 | 131 | moves.push(this.getBasicMove([x, y], [x + 3 * shiftX, y])); |
dac39588 BA |
132 | } |
133 | } | |
134 | } | |
135 | } | |
136 | // Captures | |
6808d7a1 BA |
137 | for (let shiftY of [-1, 1]) { |
138 | if ( | |
139 | y + shiftY >= 0 && | |
140 | y + shiftY < sizeY && | |
141 | this.board[x + shiftX][y + shiftY] != V.EMPTY && | |
142 | this.canTake([x, y], [x + shiftX, y + shiftY]) | |
143 | ) { | |
144 | for (let piece of finalPieces) { | |
145 | moves.push( | |
146 | this.getBasicMove([x, y], [x + shiftX, y + shiftY], { | |
147 | c: color, | |
148 | p: piece | |
149 | }) | |
150 | ); | |
dac39588 BA |
151 | } |
152 | } | |
153 | } | |
a37076f1 | 154 | |
dac39588 BA |
155 | // En passant |
156 | const Lep = this.epSquares.length; | |
6808d7a1 | 157 | const epSquare = this.epSquares[Lep - 1]; |
472c0c4f | 158 | if (!!epSquare) { |
6808d7a1 | 159 | for (let epsq of epSquare) { |
dac39588 | 160 | // TODO: some redundant checks |
6808d7a1 BA |
161 | if (epsq.x == x + shiftX && Math.abs(epsq.y - y) == 1) { |
162 | var enpassantMove = this.getBasicMove([x, y], [epsq.x, epsq.y]); | |
dac39588 BA |
163 | // WARNING: the captured pawn may be diagonally behind us, |
164 | // if it's a 3-squares jump and we take on 1st passing square | |
6808d7a1 | 165 | const px = this.board[x][epsq.y] != V.EMPTY ? x : x - shiftX; |
dac39588 BA |
166 | enpassantMove.vanish.push({ |
167 | x: px, | |
168 | y: epsq.y, | |
6808d7a1 BA |
169 | p: "p", |
170 | c: this.getColor(px, epsq.y) | |
dac39588 BA |
171 | }); |
172 | moves.push(enpassantMove); | |
173 | } | |
174 | } | |
175 | } | |
a37076f1 | 176 | |
dac39588 BA |
177 | return moves; |
178 | } | |
a37076f1 | 179 | |
dac39588 | 180 | // TODO: wildebeest castle |
8a196305 | 181 | |
6808d7a1 | 182 | getPotentialCamelMoves(sq) { |
dac39588 BA |
183 | return this.getSlideNJumpMoves(sq, V.steps[V.CAMEL], "oneStep"); |
184 | } | |
a37076f1 | 185 | |
6808d7a1 | 186 | getPotentialWildebeestMoves(sq) { |
dac39588 | 187 | return this.getSlideNJumpMoves( |
6808d7a1 BA |
188 | sq, |
189 | V.steps[V.KNIGHT].concat(V.steps[V.CAMEL]), | |
190 | "oneStep" | |
191 | ); | |
dac39588 | 192 | } |
a37076f1 | 193 | |
68e19a44 | 194 | isAttacked(sq, color) { |
6808d7a1 | 195 | return ( |
68e19a44 BA |
196 | super.isAttacked(sq, color) || |
197 | this.isAttackedByCamel(sq, color) || | |
198 | this.isAttackedByWildebeest(sq, color) | |
6808d7a1 | 199 | ); |
dac39588 | 200 | } |
a37076f1 | 201 | |
68e19a44 | 202 | isAttackedByCamel(sq, color) { |
6808d7a1 BA |
203 | return this.isAttackedBySlideNJump( |
204 | sq, | |
68e19a44 | 205 | color, |
6808d7a1 BA |
206 | V.CAMEL, |
207 | V.steps[V.CAMEL], | |
208 | "oneStep" | |
209 | ); | |
dac39588 | 210 | } |
a37076f1 | 211 | |
68e19a44 | 212 | isAttackedByWildebeest(sq, color) { |
6808d7a1 BA |
213 | return this.isAttackedBySlideNJump( |
214 | sq, | |
68e19a44 | 215 | color, |
6808d7a1 BA |
216 | V.WILDEBEEST, |
217 | V.steps[V.KNIGHT].concat(V.steps[V.CAMEL]), | |
218 | "oneStep" | |
219 | ); | |
dac39588 | 220 | } |
a37076f1 | 221 | |
6808d7a1 | 222 | getCurrentScore() { |
bb688df5 | 223 | if (this.atLeastOneMove()) return "*"; |
dac39588 | 224 | // No valid move: game is lost (stalemate is a win) |
6808d7a1 | 225 | return this.turn == "w" ? "0-1" : "1-0"; |
dac39588 | 226 | } |
efb20746 | 227 | |
dac39588 BA |
228 | static get VALUES() { |
229 | return Object.assign( | |
a97bdbda BA |
230 | { c: 3, w: 7 }, //experimental |
231 | ChessRules.VALUES | |
dac39588 BA |
232 | ); |
233 | } | |
a37076f1 | 234 | |
6808d7a1 BA |
235 | static get SEARCH_DEPTH() { |
236 | return 2; | |
237 | } | |
3c09dc49 | 238 | |
7ba4a5bc BA |
239 | static GenRandInitFen(randomness) { |
240 | if (!randomness) randomness = 2; | |
2c5d7b20 BA |
241 | if (randomness == 0) { |
242 | return ( | |
243 | "rnccwkqbbnr/ppppppppppp/92/92/92/92/92/92/PPPPPPPPPPP/RNBBQKWCCNR " + | |
244 | "w 0 akak -" | |
245 | ); | |
246 | } | |
7ba4a5bc | 247 | |
6f2f9437 | 248 | let pieces = { w: new Array(11), b: new Array(11) }; |
3a2a7b5f | 249 | let flags = ""; |
6808d7a1 | 250 | for (let c of ["w", "b"]) { |
7ba4a5bc BA |
251 | if (c == 'b' && randomness == 1) { |
252 | pieces['b'] = pieces['w']; | |
3a2a7b5f | 253 | flags += flags; |
7ba4a5bc BA |
254 | break; |
255 | } | |
256 | ||
dac39588 | 257 | let positions = ArrayFun.range(11); |
a37076f1 | 258 | |
dac39588 | 259 | // Get random squares for bishops + camels (different colors) |
6808d7a1 BA |
260 | let randIndexes = sample(ArrayFun.range(6), 2).map(i => { |
261 | return 2 * i; | |
262 | }); | |
dac39588 BA |
263 | let bishop1Pos = positions[randIndexes[0]]; |
264 | let camel1Pos = positions[randIndexes[1]]; | |
265 | // The second bishop (camel) must be on a square of different color | |
6808d7a1 BA |
266 | let randIndexes_tmp = sample(ArrayFun.range(5), 2).map(i => { |
267 | return 2 * i + 1; | |
268 | }); | |
dac39588 BA |
269 | let bishop2Pos = positions[randIndexes_tmp[0]]; |
270 | let camel2Pos = positions[randIndexes_tmp[1]]; | |
6808d7a1 BA |
271 | for (let idx of randIndexes.concat(randIndexes_tmp).sort((a, b) => { |
272 | return b - a; | |
273 | })) { | |
7ba4a5bc | 274 | // Largest indices first |
dac39588 BA |
275 | positions.splice(idx, 1); |
276 | } | |
a37076f1 | 277 | |
dac39588 BA |
278 | let randIndex = randInt(7); |
279 | let knight1Pos = positions[randIndex]; | |
280 | positions.splice(randIndex, 1); | |
281 | randIndex = randInt(6); | |
282 | let knight2Pos = positions[randIndex]; | |
283 | positions.splice(randIndex, 1); | |
a37076f1 | 284 | |
dac39588 BA |
285 | randIndex = randInt(5); |
286 | let queenPos = positions[randIndex]; | |
287 | positions.splice(randIndex, 1); | |
8a196305 | 288 | |
dac39588 BA |
289 | // Random square for wildebeest |
290 | randIndex = randInt(4); | |
291 | let wildebeestPos = positions[randIndex]; | |
292 | positions.splice(randIndex, 1); | |
8a196305 | 293 | |
dac39588 BA |
294 | let rook1Pos = positions[0]; |
295 | let kingPos = positions[1]; | |
296 | let rook2Pos = positions[2]; | |
a37076f1 | 297 | |
6808d7a1 BA |
298 | pieces[c][rook1Pos] = "r"; |
299 | pieces[c][knight1Pos] = "n"; | |
300 | pieces[c][bishop1Pos] = "b"; | |
301 | pieces[c][queenPos] = "q"; | |
302 | pieces[c][camel1Pos] = "c"; | |
303 | pieces[c][camel2Pos] = "c"; | |
304 | pieces[c][wildebeestPos] = "w"; | |
305 | pieces[c][kingPos] = "k"; | |
306 | pieces[c][bishop2Pos] = "b"; | |
307 | pieces[c][knight2Pos] = "n"; | |
308 | pieces[c][rook2Pos] = "r"; | |
3a2a7b5f | 309 | flags += V.CoordToColumn(rook1Pos) + V.CoordToColumn(rook2Pos); |
dac39588 | 310 | } |
6808d7a1 BA |
311 | return ( |
312 | pieces["b"].join("") + | |
6f2f9437 | 313 | "/ppppppppppp/92/92/92/92/92/92/PPPPPPPPPPP/" + |
dac39588 | 314 | pieces["w"].join("").toUpperCase() + |
3a2a7b5f | 315 | " w 0 " + flags + " -" |
6808d7a1 | 316 | ); |
dac39588 | 317 | } |
6808d7a1 | 318 | }; |