Commit | Line | Data |
---|---|---|
0c3fe8a6 BA |
1 | import { ChessRules, PiPo, Move } from "@/base_rules"; |
2 | import { ArrayFun } from "@/utils/array"; | |
32f6285e | 3 | import { shuffle } from "@/utils/alea"; |
0c3fe8a6 | 4 | |
32f6285e | 5 | export class BaroqueRules extends ChessRules { |
7e8a7ea1 | 6 | |
6808d7a1 BA |
7 | static get HasFlags() { |
8 | return false; | |
9 | } | |
dac39588 | 10 | |
6808d7a1 BA |
11 | static get HasEnpassant() { |
12 | return false; | |
13 | } | |
dac39588 | 14 | |
241bf8f2 BA |
15 | static get PIECES() { |
16 | return ChessRules.PIECES.concat([V.IMMOBILIZER]); | |
17 | } | |
18 | ||
19 | getPpath(b) { | |
6808d7a1 BA |
20 | if (b[1] == "m") |
21 | //'m' for Immobilizer (I is too similar to 1) | |
dac39588 BA |
22 | return "Baroque/" + b; |
23 | return b; //usual piece | |
24 | } | |
25 | ||
dac39588 | 26 | // No castling, but checks, so keep track of kings |
6808d7a1 BA |
27 | setOtherVariables(fen) { |
28 | this.kingPos = { w: [-1, -1], b: [-1, -1] }; | |
dac39588 BA |
29 | const fenParts = fen.split(" "); |
30 | const position = fenParts[0].split("/"); | |
6808d7a1 | 31 | for (let i = 0; i < position.length; i++) { |
dac39588 | 32 | let k = 0; |
6808d7a1 BA |
33 | for (let j = 0; j < position[i].length; j++) { |
34 | switch (position[i].charAt(j)) { | |
35 | case "k": | |
36 | this.kingPos["b"] = [i, k]; | |
dac39588 | 37 | break; |
6808d7a1 BA |
38 | case "K": |
39 | this.kingPos["w"] = [i, k]; | |
dac39588 | 40 | break; |
6808d7a1 | 41 | default: { |
e50a8025 | 42 | const num = parseInt(position[i].charAt(j), 10); |
6808d7a1 BA |
43 | if (!isNaN(num)) k += num - 1; |
44 | } | |
dac39588 BA |
45 | } |
46 | k++; | |
47 | } | |
48 | } | |
49 | } | |
50 | ||
6808d7a1 BA |
51 | static get IMMOBILIZER() { |
52 | return "m"; | |
53 | } | |
dac39588 BA |
54 | // Although other pieces keep their names here for coding simplicity, |
55 | // keep in mind that: | |
56 | // - a "rook" is a coordinator, capturing by coordinating with the king | |
57 | // - a "knight" is a long-leaper, capturing as in draughts | |
58 | // - a "bishop" is a chameleon, capturing as its prey | |
59 | // - a "queen" is a withdrawer, capturing by moving away from pieces | |
60 | ||
61 | // Is piece on square (x,y) immobilized? | |
6808d7a1 BA |
62 | isImmobilized([x, y]) { |
63 | const piece = this.getPiece(x, y); | |
64 | const color = this.getColor(x, y); | |
dac39588 BA |
65 | const oppCol = V.GetOppCol(color); |
66 | const adjacentSteps = V.steps[V.ROOK].concat(V.steps[V.BISHOP]); | |
6808d7a1 BA |
67 | for (let step of adjacentSteps) { |
68 | const [i, j] = [x + step[0], y + step[1]]; | |
69 | if ( | |
70 | V.OnBoard(i, j) && | |
71 | this.board[i][j] != V.EMPTY && | |
72 | this.getColor(i, j) == oppCol | |
73 | ) { | |
74 | const oppPiece = this.getPiece(i, j); | |
75 | if (oppPiece == V.IMMOBILIZER) { | |
4404e58c | 76 | // Moving is possible only if this immobilizer is neutralized |
6808d7a1 BA |
77 | for (let step2 of adjacentSteps) { |
78 | const [i2, j2] = [i + step2[0], j + step2[1]]; | |
79 | if (i2 == x && j2 == y) continue; //skip initial piece! | |
80 | if ( | |
81 | V.OnBoard(i2, j2) && | |
82 | this.board[i2][j2] != V.EMPTY && | |
83 | this.getColor(i2, j2) == color | |
84 | ) { | |
85 | if ([V.BISHOP, V.IMMOBILIZER].includes(this.getPiece(i2, j2))) | |
dac39588 BA |
86 | return false; |
87 | } | |
88 | } | |
89 | return true; //immobilizer isn't neutralized | |
90 | } | |
2c5d7b20 BA |
91 | // Chameleons can't be immobilized twice, |
92 | // because there is only one immobilizer | |
6808d7a1 | 93 | if (oppPiece == V.BISHOP && piece == V.IMMOBILIZER) return true; |
dac39588 BA |
94 | } |
95 | } | |
96 | return false; | |
97 | } | |
98 | ||
6808d7a1 | 99 | getPotentialMovesFrom([x, y]) { |
dac39588 | 100 | // Pre-check: is thing on this square immobilized? |
6808d7a1 BA |
101 | if (this.isImmobilized([x, y])) return []; |
102 | switch (this.getPiece(x, y)) { | |
dac39588 | 103 | case V.IMMOBILIZER: |
6808d7a1 | 104 | return this.getPotentialImmobilizerMoves([x, y]); |
dac39588 | 105 | default: |
6808d7a1 | 106 | return super.getPotentialMovesFrom([x, y]); |
dac39588 BA |
107 | } |
108 | } | |
109 | ||
6808d7a1 BA |
110 | getSlideNJumpMoves([x, y], steps, oneStep) { |
111 | const piece = this.getPiece(x, y); | |
dac39588 | 112 | let moves = []; |
6808d7a1 | 113 | outerLoop: for (let step of steps) { |
dac39588 BA |
114 | let i = x + step[0]; |
115 | let j = y + step[1]; | |
6808d7a1 BA |
116 | while (V.OnBoard(i, j) && this.board[i][j] == V.EMPTY) { |
117 | moves.push(this.getBasicMove([x, y], [i, j])); | |
118 | if (oneStep !== undefined) continue outerLoop; | |
dac39588 BA |
119 | i += step[0]; |
120 | j += step[1]; | |
121 | } | |
122 | // Only king can take on occupied square: | |
6808d7a1 BA |
123 | if (piece == V.KING && V.OnBoard(i, j) && this.canTake([x, y], [i, j])) |
124 | moves.push(this.getBasicMove([x, y], [i, j])); | |
dac39588 BA |
125 | } |
126 | return moves; | |
127 | } | |
128 | ||
129 | // Modify capturing moves among listed pawn moves | |
6808d7a1 | 130 | addPawnCaptures(moves, byChameleon) { |
dac39588 BA |
131 | const steps = V.steps[V.ROOK]; |
132 | const color = this.turn; | |
133 | const oppCol = V.GetOppCol(color); | |
134 | moves.forEach(m => { | |
2c5d7b20 BA |
135 | if (!!byChameleon && m.start.x != m.end.x && m.start.y != m.end.y) |
136 | // Chameleon not moving as pawn | |
137 | return; | |
dac39588 | 138 | // Try capturing in every direction |
6808d7a1 BA |
139 | for (let step of steps) { |
140 | const sq2 = [m.end.x + 2 * step[0], m.end.y + 2 * step[1]]; | |
141 | if ( | |
142 | V.OnBoard(sq2[0], sq2[1]) && | |
143 | this.board[sq2[0]][sq2[1]] != V.EMPTY && | |
144 | this.getColor(sq2[0], sq2[1]) == color | |
145 | ) { | |
dac39588 | 146 | // Potential capture |
6808d7a1 BA |
147 | const sq1 = [m.end.x + step[0], m.end.y + step[1]]; |
148 | if ( | |
149 | this.board[sq1[0]][sq1[1]] != V.EMPTY && | |
150 | this.getColor(sq1[0], sq1[1]) == oppCol | |
151 | ) { | |
152 | const piece1 = this.getPiece(sq1[0], sq1[1]); | |
153 | if (!byChameleon || piece1 == V.PAWN) { | |
154 | m.vanish.push( | |
155 | new PiPo({ | |
156 | x: sq1[0], | |
157 | y: sq1[1], | |
158 | c: oppCol, | |
159 | p: piece1 | |
160 | }) | |
161 | ); | |
dac39588 BA |
162 | } |
163 | } | |
164 | } | |
165 | } | |
166 | }); | |
167 | } | |
168 | ||
169 | // "Pincer" | |
6808d7a1 BA |
170 | getPotentialPawnMoves([x, y]) { |
171 | let moves = super.getPotentialRookMoves([x, y]); | |
dac39588 BA |
172 | this.addPawnCaptures(moves); |
173 | return moves; | |
174 | } | |
175 | ||
6808d7a1 | 176 | addRookCaptures(moves, byChameleon) { |
dac39588 BA |
177 | const color = this.turn; |
178 | const oppCol = V.GetOppCol(color); | |
179 | const kp = this.kingPos[color]; | |
180 | moves.forEach(m => { | |
181 | // Check piece-king rectangle (if any) corners for enemy pieces | |
6808d7a1 | 182 | if (m.end.x == kp[0] || m.end.y == kp[1]) return; //"flat rectangle" |
dac39588 BA |
183 | const corner1 = [m.end.x, kp[1]]; |
184 | const corner2 = [kp[0], m.end.y]; | |
6808d7a1 BA |
185 | for (let [i, j] of [corner1, corner2]) { |
186 | if (this.board[i][j] != V.EMPTY && this.getColor(i, j) == oppCol) { | |
187 | const piece = this.getPiece(i, j); | |
188 | if (!byChameleon || piece == V.ROOK) { | |
189 | m.vanish.push( | |
190 | new PiPo({ | |
191 | x: i, | |
192 | y: j, | |
193 | p: piece, | |
194 | c: oppCol | |
195 | }) | |
196 | ); | |
dac39588 BA |
197 | } |
198 | } | |
199 | } | |
200 | }); | |
201 | } | |
202 | ||
203 | // Coordinator | |
6808d7a1 | 204 | getPotentialRookMoves(sq) { |
dac39588 BA |
205 | let moves = super.getPotentialQueenMoves(sq); |
206 | this.addRookCaptures(moves); | |
207 | return moves; | |
208 | } | |
209 | ||
6808d7a1 | 210 | getKnightCaptures(startSquare, byChameleon) { |
dac39588 BA |
211 | // Look in every direction for captures |
212 | const steps = V.steps[V.ROOK].concat(V.steps[V.BISHOP]); | |
213 | const color = this.turn; | |
214 | const oppCol = V.GetOppCol(color); | |
215 | let moves = []; | |
6808d7a1 BA |
216 | const [x, y] = [startSquare[0], startSquare[1]]; |
217 | const piece = this.getPiece(x, y); //might be a chameleon! | |
218 | outerLoop: for (let step of steps) { | |
219 | let [i, j] = [x + step[0], y + step[1]]; | |
220 | while (V.OnBoard(i, j) && this.board[i][j] == V.EMPTY) { | |
dac39588 BA |
221 | i += step[0]; |
222 | j += step[1]; | |
223 | } | |
6808d7a1 BA |
224 | if ( |
225 | !V.OnBoard(i, j) || | |
226 | this.getColor(i, j) == color || | |
227 | (!!byChameleon && this.getPiece(i, j) != V.KNIGHT) | |
228 | ) { | |
dac39588 BA |
229 | continue; |
230 | } | |
2c5d7b20 BA |
231 | // last(thing), cur(thing) : stop if "cur" is our color, |
232 | // or beyond board limits, or if "last" isn't empty and cur neither. | |
233 | // Otherwise, if cur is empty then add move until cur square; | |
234 | // if cur is occupied then stop if !!byChameleon and the square not | |
235 | // occupied by a leaper. | |
6808d7a1 BA |
236 | let last = [i, j]; |
237 | let cur = [i + step[0], j + step[1]]; | |
238 | let vanished = [new PiPo({ x: x, y: y, c: color, p: piece })]; | |
239 | while (V.OnBoard(cur[0], cur[1])) { | |
240 | if (this.board[last[0]][last[1]] != V.EMPTY) { | |
241 | const oppPiece = this.getPiece(last[0], last[1]); | |
242 | if (!!byChameleon && oppPiece != V.KNIGHT) continue outerLoop; | |
dac39588 | 243 | // Something to eat: |
6808d7a1 BA |
244 | vanished.push( |
245 | new PiPo({ x: last[0], y: last[1], c: oppCol, p: oppPiece }) | |
246 | ); | |
dac39588 | 247 | } |
6808d7a1 BA |
248 | if (this.board[cur[0]][cur[1]] != V.EMPTY) { |
249 | if ( | |
250 | this.getColor(cur[0], cur[1]) == color || | |
251 | this.board[last[0]][last[1]] != V.EMPTY | |
252 | ) { | |
253 | //TODO: redundant test | |
dac39588 BA |
254 | continue outerLoop; |
255 | } | |
6808d7a1 BA |
256 | } else { |
257 | moves.push( | |
258 | new Move({ | |
259 | appear: [new PiPo({ x: cur[0], y: cur[1], c: color, p: piece })], | |
260 | vanish: JSON.parse(JSON.stringify(vanished)), //TODO: required? | |
261 | start: { x: x, y: y }, | |
262 | end: { x: cur[0], y: cur[1] } | |
263 | }) | |
264 | ); | |
dac39588 | 265 | } |
6808d7a1 BA |
266 | last = [last[0] + step[0], last[1] + step[1]]; |
267 | cur = [cur[0] + step[0], cur[1] + step[1]]; | |
dac39588 BA |
268 | } |
269 | } | |
270 | return moves; | |
271 | } | |
272 | ||
273 | // Long-leaper | |
6808d7a1 | 274 | getPotentialKnightMoves(sq) { |
dac39588 BA |
275 | return super.getPotentialQueenMoves(sq).concat(this.getKnightCaptures(sq)); |
276 | } | |
277 | ||
78d64531 | 278 | // Chameleon |
6808d7a1 BA |
279 | getPotentialBishopMoves([x, y]) { |
280 | let moves = super | |
281 | .getPotentialQueenMoves([x, y]) | |
282 | .concat(this.getKnightCaptures([x, y], "asChameleon")); | |
dac39588 BA |
283 | // No "king capture" because king cannot remain under check |
284 | this.addPawnCaptures(moves, "asChameleon"); | |
285 | this.addRookCaptures(moves, "asChameleon"); | |
286 | this.addQueenCaptures(moves, "asChameleon"); | |
287 | // Post-processing: merge similar moves, concatenating vanish arrays | |
288 | let mergedMoves = {}; | |
289 | moves.forEach(m => { | |
290 | const key = m.end.x + V.size.x * m.end.y; | |
6808d7a1 BA |
291 | if (!mergedMoves[key]) mergedMoves[key] = m; |
292 | else { | |
293 | for (let i = 1; i < m.vanish.length; i++) | |
dac39588 BA |
294 | mergedMoves[key].vanish.push(m.vanish[i]); |
295 | } | |
296 | }); | |
2f8dce6a | 297 | return Object.values(mergedMoves); |
dac39588 BA |
298 | } |
299 | ||
6808d7a1 BA |
300 | addQueenCaptures(moves, byChameleon) { |
301 | if (moves.length == 0) return; | |
302 | const [x, y] = [moves[0].start.x, moves[0].start.y]; | |
dac39588 BA |
303 | const adjacentSteps = V.steps[V.ROOK].concat(V.steps[V.BISHOP]); |
304 | let capturingDirections = []; | |
305 | const color = this.turn; | |
306 | const oppCol = V.GetOppCol(color); | |
307 | adjacentSteps.forEach(step => { | |
6808d7a1 BA |
308 | const [i, j] = [x + step[0], y + step[1]]; |
309 | if ( | |
310 | V.OnBoard(i, j) && | |
311 | this.board[i][j] != V.EMPTY && | |
312 | this.getColor(i, j) == oppCol && | |
313 | (!byChameleon || this.getPiece(i, j) == V.QUEEN) | |
314 | ) { | |
dac39588 BA |
315 | capturingDirections.push(step); |
316 | } | |
317 | }); | |
318 | moves.forEach(m => { | |
319 | const step = [ | |
6808d7a1 BA |
320 | m.end.x != x ? (m.end.x - x) / Math.abs(m.end.x - x) : 0, |
321 | m.end.y != y ? (m.end.y - y) / Math.abs(m.end.y - y) : 0 | |
dac39588 BA |
322 | ]; |
323 | // NOTE: includes() and even _.isEqual() functions fail... | |
324 | // TODO: this test should be done only once per direction | |
6808d7a1 BA |
325 | if ( |
326 | capturingDirections.some(dir => { | |
327 | return dir[0] == -step[0] && dir[1] == -step[1]; | |
328 | }) | |
329 | ) { | |
330 | const [i, j] = [x - step[0], y - step[1]]; | |
331 | m.vanish.push( | |
332 | new PiPo({ | |
333 | x: i, | |
334 | y: j, | |
335 | p: this.getPiece(i, j), | |
336 | c: oppCol | |
337 | }) | |
338 | ); | |
dac39588 BA |
339 | } |
340 | }); | |
341 | } | |
342 | ||
78d64531 | 343 | // Withdrawer |
6808d7a1 | 344 | getPotentialQueenMoves(sq) { |
dac39588 BA |
345 | let moves = super.getPotentialQueenMoves(sq); |
346 | this.addQueenCaptures(moves); | |
347 | return moves; | |
348 | } | |
349 | ||
6808d7a1 | 350 | getPotentialImmobilizerMoves(sq) { |
dac39588 BA |
351 | // Immobilizer doesn't capture |
352 | return super.getPotentialQueenMoves(sq); | |
353 | } | |
354 | ||
dac39588 BA |
355 | // isAttacked() is OK because the immobilizer doesn't take |
356 | ||
68e19a44 | 357 | isAttackedByPawn([x, y], color) { |
dac39588 BA |
358 | // Square (x,y) must be surroundable by two enemy pieces, |
359 | // and one of them at least should be a pawn (moving). | |
6808d7a1 BA |
360 | const dirs = [ |
361 | [1, 0], | |
362 | [0, 1] | |
363 | ]; | |
dac39588 | 364 | const steps = V.steps[V.ROOK]; |
6808d7a1 BA |
365 | for (let dir of dirs) { |
366 | const [i1, j1] = [x - dir[0], y - dir[1]]; //"before" | |
367 | const [i2, j2] = [x + dir[0], y + dir[1]]; //"after" | |
368 | if (V.OnBoard(i1, j1) && V.OnBoard(i2, j2)) { | |
369 | if ( | |
68e19a44 BA |
370 | ( |
371 | this.board[i1][j1] != V.EMPTY && | |
372 | this.getColor(i1, j1) == color && | |
373 | this.board[i2][j2] == V.EMPTY | |
374 | ) | |
375 | || | |
376 | ( | |
377 | this.board[i2][j2] != V.EMPTY && | |
378 | this.getColor(i2, j2) == color && | |
379 | this.board[i1][j1] == V.EMPTY | |
380 | ) | |
6808d7a1 | 381 | ) { |
dac39588 | 382 | // Search a movable enemy pawn landing on the empty square |
6808d7a1 BA |
383 | for (let step of steps) { |
384 | let [ii, jj] = this.board[i1][j1] == V.EMPTY ? [i1, j1] : [i2, j2]; | |
385 | let [i3, j3] = [ii + step[0], jj + step[1]]; | |
386 | while (V.OnBoard(i3, j3) && this.board[i3][j3] == V.EMPTY) { | |
dac39588 BA |
387 | i3 += step[0]; |
388 | j3 += step[1]; | |
389 | } | |
6808d7a1 BA |
390 | if ( |
391 | V.OnBoard(i3, j3) && | |
68e19a44 | 392 | this.getColor(i3, j3) == color && |
6808d7a1 BA |
393 | this.getPiece(i3, j3) == V.PAWN && |
394 | !this.isImmobilized([i3, j3]) | |
395 | ) { | |
dac39588 BA |
396 | return true; |
397 | } | |
398 | } | |
399 | } | |
400 | } | |
401 | } | |
402 | return false; | |
403 | } | |
404 | ||
68e19a44 | 405 | isAttackedByRook([x, y], color) { |
dac39588 BA |
406 | // King must be on same column or row, |
407 | // and a rook should be able to reach a capturing square | |
68e19a44 BA |
408 | const sameRow = x == this.kingPos[color][0]; |
409 | const sameColumn = y == this.kingPos[color][1]; | |
6808d7a1 | 410 | if (sameRow || sameColumn) { |
dac39588 | 411 | // Look for the enemy rook (maximum 1) |
6808d7a1 BA |
412 | for (let i = 0; i < V.size.x; i++) { |
413 | for (let j = 0; j < V.size.y; j++) { | |
414 | if ( | |
415 | this.board[i][j] != V.EMPTY && | |
68e19a44 | 416 | this.getColor(i, j) == color && |
6808d7a1 BA |
417 | this.getPiece(i, j) == V.ROOK |
418 | ) { | |
2c5d7b20 BA |
419 | if (this.isImmobilized([i, j])) |
420 | // Because only one rook: | |
421 | return false; | |
422 | // Can it reach a capturing square? Easy but quite suboptimal way | |
423 | // (TODO: generate all moves (turn is OK)) | |
6808d7a1 BA |
424 | const moves = this.getPotentialMovesFrom([i, j]); |
425 | for (let move of moves) { | |
426 | if ( | |
427 | (sameRow && move.end.y == y) || | |
428 | (sameColumn && move.end.x == x) | |
e90bafa8 | 429 | ) { |
dac39588 | 430 | return true; |
e90bafa8 | 431 | } |
dac39588 BA |
432 | } |
433 | } | |
434 | } | |
435 | } | |
436 | } | |
437 | return false; | |
438 | } | |
439 | ||
68e19a44 | 440 | isAttackedByKnight([x, y], color) { |
dac39588 BA |
441 | // Square (x,y) must be on same line as a knight, |
442 | // and there must be empty square(s) behind. | |
443 | const steps = V.steps[V.ROOK].concat(V.steps[V.BISHOP]); | |
6808d7a1 BA |
444 | outerLoop: for (let step of steps) { |
445 | const [i0, j0] = [x + step[0], y + step[1]]; | |
446 | if (V.OnBoard(i0, j0) && this.board[i0][j0] == V.EMPTY) { | |
dac39588 | 447 | // Try in opposite direction: |
6808d7a1 BA |
448 | let [i, j] = [x - step[0], y - step[1]]; |
449 | while (V.OnBoard(i, j)) { | |
450 | while (V.OnBoard(i, j) && this.board[i][j] == V.EMPTY) { | |
dac39588 BA |
451 | i -= step[0]; |
452 | j -= step[1]; | |
453 | } | |
6808d7a1 | 454 | if (V.OnBoard(i, j)) { |
68e19a44 | 455 | if (this.getColor(i, j) == color) { |
6808d7a1 BA |
456 | if ( |
457 | this.getPiece(i, j) == V.KNIGHT && | |
458 | !this.isImmobilized([i, j]) | |
e90bafa8 | 459 | ) { |
dac39588 | 460 | return true; |
e90bafa8 | 461 | } |
dac39588 BA |
462 | continue outerLoop; |
463 | } | |
2c5d7b20 BA |
464 | // [else] Our color, |
465 | // could be captured *if there was an empty space* | |
6808d7a1 | 466 | if (this.board[i + step[0]][j + step[1]] != V.EMPTY) |
dac39588 BA |
467 | continue outerLoop; |
468 | i -= step[0]; | |
469 | j -= step[1]; | |
470 | } | |
471 | } | |
472 | } | |
473 | } | |
474 | return false; | |
475 | } | |
476 | ||
68e19a44 | 477 | isAttackedByBishop([x, y], color) { |
4404e58c BA |
478 | // We cheat a little here: since this function is used exclusively for |
479 | // the king, it's enough to check the immediate surrounding of the square. | |
dac39588 | 480 | const adjacentSteps = V.steps[V.ROOK].concat(V.steps[V.BISHOP]); |
6808d7a1 BA |
481 | for (let step of adjacentSteps) { |
482 | const [i, j] = [x + step[0], y + step[1]]; | |
483 | if ( | |
484 | V.OnBoard(i, j) && | |
485 | this.board[i][j] != V.EMPTY && | |
68e19a44 | 486 | this.getColor(i, j) == color && |
2f8dce6a BA |
487 | this.getPiece(i, j) == V.BISHOP && |
488 | !this.isImmobilized([i, j]) | |
6808d7a1 | 489 | ) { |
2f8dce6a | 490 | return true; |
dac39588 BA |
491 | } |
492 | } | |
493 | return false; | |
494 | } | |
495 | ||
68e19a44 | 496 | isAttackedByQueen([x, y], color) { |
dac39588 BA |
497 | // Square (x,y) must be adjacent to a queen, and the queen must have |
498 | // some free space in the opposite direction from (x,y) | |
499 | const adjacentSteps = V.steps[V.ROOK].concat(V.steps[V.BISHOP]); | |
6808d7a1 BA |
500 | for (let step of adjacentSteps) { |
501 | const sq2 = [x + 2 * step[0], y + 2 * step[1]]; | |
502 | if (V.OnBoard(sq2[0], sq2[1]) && this.board[sq2[0]][sq2[1]] == V.EMPTY) { | |
503 | const sq1 = [x + step[0], y + step[1]]; | |
504 | if ( | |
505 | this.board[sq1[0]][sq1[1]] != V.EMPTY && | |
68e19a44 | 506 | this.getColor(sq1[0], sq1[1]) == color && |
6808d7a1 BA |
507 | this.getPiece(sq1[0], sq1[1]) == V.QUEEN && |
508 | !this.isImmobilized(sq1) | |
509 | ) { | |
dac39588 BA |
510 | return true; |
511 | } | |
512 | } | |
513 | } | |
514 | return false; | |
515 | } | |
516 | ||
68e19a44 | 517 | isAttackedByKing([x, y], color) { |
4404e58c BA |
518 | const steps = V.steps[V.ROOK].concat(V.steps[V.BISHOP]); |
519 | for (let step of steps) { | |
520 | let rx = x + step[0], | |
521 | ry = y + step[1]; | |
522 | if ( | |
523 | V.OnBoard(rx, ry) && | |
524 | this.getPiece(rx, ry) === V.KING && | |
68e19a44 | 525 | this.getColor(rx, ry) == color && |
4404e58c BA |
526 | !this.isImmobilized([rx, ry]) |
527 | ) { | |
528 | return true; | |
529 | } | |
530 | } | |
531 | return false; | |
532 | } | |
533 | ||
7ba4a5bc | 534 | static GenRandInitFen(randomness) { |
7ba4a5bc BA |
535 | if (randomness == 0) |
536 | // Deterministic: | |
2f8dce6a | 537 | return "rnbkqbnm/pppppppp/8/8/8/8/PPPPPPPP/MNBQKBNR w 0"; |
7ba4a5bc | 538 | |
6808d7a1 | 539 | let pieces = { w: new Array(8), b: new Array(8) }; |
dac39588 | 540 | // Shuffle pieces on first and last rank |
6808d7a1 | 541 | for (let c of ["w", "b"]) { |
7ba4a5bc BA |
542 | if (c == 'b' && randomness == 1) { |
543 | pieces['b'] = pieces['w']; | |
544 | break; | |
545 | } | |
546 | ||
dac39588 | 547 | // Get random squares for every piece, totally freely |
32f6285e BA |
548 | let positions = shuffle(ArrayFun.range(8)); |
549 | const composition = ['r', 'n', 'b', 'q', 'k', 'b', 'n', 'm']; | |
550 | for (let i = 0; i < 8; i++) pieces[c][positions[i]] = composition[i]; | |
dac39588 | 551 | } |
6808d7a1 BA |
552 | return ( |
553 | pieces["b"].join("") + | |
dac39588 BA |
554 | "/pppppppp/8/8/8/8/PPPPPPPP/" + |
555 | pieces["w"].join("").toUpperCase() + | |
6808d7a1 BA |
556 | " w 0" |
557 | ); | |
dac39588 BA |
558 | } |
559 | ||
2f8dce6a BA |
560 | static get VALUES() { |
561 | return { | |
562 | p: 1, | |
563 | r: 2, | |
564 | n: 5, | |
565 | b: 3, | |
566 | q: 3, | |
567 | m: 5, | |
568 | k: 1000 | |
569 | }; | |
570 | } | |
571 | ||
572 | static get SEARCH_DEPTH() { | |
573 | return 2; | |
574 | } | |
575 | ||
6808d7a1 | 576 | getNotation(move) { |
dac39588 BA |
577 | const initialSquare = V.CoordsToSquare(move.start); |
578 | const finalSquare = V.CoordsToSquare(move.end); | |
579 | let notation = undefined; | |
6808d7a1 | 580 | if (move.appear[0].p == V.PAWN) { |
dac39588 BA |
581 | // Pawn: generally ambiguous short notation, so we use full description |
582 | notation = "P" + initialSquare + finalSquare; | |
6808d7a1 BA |
583 | } else if (move.appear[0].p == V.KING) |
584 | notation = "K" + (move.vanish.length > 1 ? "x" : "") + finalSquare; | |
585 | else notation = move.appear[0].p.toUpperCase() + finalSquare; | |
e9b736ee BA |
586 | // Add a capture mark (not describing what is captured...): |
587 | if (move.vanish.length > 1 && move.appear[0].p != V.KING) notation += "X"; | |
dac39588 BA |
588 | return notation; |
589 | } | |
7e8a7ea1 | 590 | |
6808d7a1 | 591 | }; |