Commit | Line | Data |
---|---|---|
723262f9 BA |
1 | import { ChessRules, Move, PiPo } from "@/base_rules"; |
2 | import { randInt } from "@/utils/alea"; | |
3 | ||
4 | export class PandemoniumRules extends ChessRules { | |
5 | ||
6 | static get PawnSpecs() { | |
7 | return Object.assign( | |
8 | {}, | |
9 | ChessRules.PawnSpecs, | |
10 | { | |
11 | threeSquares: true, | |
12 | promotions: [V.GILDING] | |
13 | } | |
14 | ); | |
15 | } | |
16 | ||
17 | static get GILDING() { | |
18 | return "g"; | |
19 | } | |
20 | ||
21 | static get SCEPTER() { | |
22 | return "s"; | |
23 | } | |
24 | ||
25 | static get HORSE() { | |
26 | return "h"; | |
27 | } | |
28 | ||
29 | static get DRAGON() { | |
30 | return "d"; | |
31 | } | |
32 | ||
33 | static get CARDINAL() { | |
34 | return "c"; | |
35 | } | |
36 | ||
37 | static get WHOLE() { | |
38 | return "w"; | |
39 | } | |
40 | ||
41 | static get MARSHAL() { | |
42 | return "m"; | |
43 | } | |
44 | ||
45 | static get APRICOT() { | |
46 | return "a"; | |
47 | } | |
48 | ||
49 | static get PIECES() { | |
50 | return ( | |
51 | ChessRules.PIECES.concat([ | |
52 | V.GILDING, V.SCEPTER, V.HORSE, V.DRAGON, | |
53 | V.CARDINAL, V.WHOLE, V.MARSHAL, V.APRICOT]) | |
54 | ); | |
55 | } | |
56 | ||
57 | getPpath(b) { | |
58 | const prefix = (ChessRules.PIECES.includes(b[1]) ? "" : "Pandemonium/"); | |
59 | return prefix + b; | |
60 | } | |
61 | ||
62 | static get size() { | |
63 | return { x: 10, y: 10}; | |
64 | } | |
65 | ||
66 | getColor(i, j) { | |
67 | if (i >= V.size.x) return i == V.size.x ? "w" : "b"; | |
68 | return this.board[i][j].charAt(0); | |
69 | } | |
70 | ||
71 | getPiece(i, j) { | |
72 | if (i >= V.size.x) return V.RESERVE_PIECES[j]; | |
73 | return this.board[i][j].charAt(1); | |
74 | } | |
75 | ||
76 | setOtherVariables(fen) { | |
77 | super.setOtherVariables(fen); | |
78 | // Sub-turn is useful only at first move... | |
79 | this.subTurn = 1; | |
80 | // Also init reserves (used by the interface to show landable pieces) | |
81 | const reserve = | |
82 | V.ParseFen(fen).reserve.split("").map(x => parseInt(x, 10)); | |
83 | this.reserve = { | |
84 | w: { | |
85 | [V.PAWN]: reserve[0], | |
86 | [V.ROOK]: reserve[1], | |
87 | [V.KNIGHT]: reserve[2], | |
88 | [V.BISHOP]: reserve[3], | |
89 | [V.QUEEN]: reserve[4], | |
90 | [V.CARDINAL]: reserve[5], | |
91 | [V.MARSHAL]: reserve[6], | |
92 | }, | |
93 | b: { | |
94 | [V.PAWN]: reserve[7], | |
95 | [V.ROOK]: reserve[8], | |
96 | [V.KNIGHT]: reserve[9], | |
97 | [V.BISHOP]: reserve[10], | |
98 | [V.QUEEN]: reserve[11], | |
99 | [V.CARDINAL]: reserve[12], | |
100 | [V.MARSHAL]: reserve[13] | |
101 | } | |
102 | }; | |
103 | } | |
104 | ||
105 | static IsGoodEnpassant(enpassant) { | |
106 | if (enpassant != "-") { | |
107 | const squares = enpassant.split(","); | |
108 | if (squares.length > 2) return false; | |
109 | for (let sq of squares) { | |
110 | if (!sq.match(/[a-j0-9]/)) return false; | |
111 | } | |
112 | } | |
113 | return true; | |
114 | } | |
115 | ||
116 | static IsGoodFen(fen) { | |
117 | if (!ChessRules.IsGoodFen(fen)) return false; | |
118 | const fenParsed = V.ParseFen(fen); | |
119 | // Check reserves | |
120 | if (!fenParsed.reserve || !fenParsed.reserve.match(/^[0-9]{14,14}$/)) | |
121 | return false; | |
122 | return true; | |
123 | } | |
124 | ||
125 | static ParseFen(fen) { | |
126 | const fenParts = fen.split(" "); | |
127 | return Object.assign( | |
128 | ChessRules.ParseFen(fen), | |
129 | { reserve: fenParts[5] } | |
130 | ); | |
131 | } | |
132 | ||
133 | getFen() { | |
134 | return super.getFen() + " " + this.getReserveFen(); | |
135 | } | |
136 | ||
137 | getFenForRepeat() { | |
138 | return super.getFenForRepeat() + "_" + this.getReserveFen(); | |
139 | } | |
140 | ||
141 | getReserveFen() { | |
142 | let counts = new Array(14); | |
143 | for (let i = 0; i < V.RESERVE_PIECES.length; i++) { | |
144 | counts[i] = this.reserve["w"][V.RESERVE_PIECES[i]]; | |
145 | counts[7 + i] = this.reserve["b"][V.RESERVE_PIECES[i]]; | |
146 | } | |
147 | return counts.join(""); | |
148 | } | |
149 | ||
150 | setFlags(fenflags) { | |
151 | // white a-castle, h-castle, king pos, then same for black. | |
152 | this.castleFlags = { w: [-1, -1, -1], b: [-1, -1, -1] }; | |
153 | for (let i = 0; i < 6; i++) { | |
154 | this.castleFlags[i < 3 ? "w" : "b"][i % 3] = | |
155 | V.ColumnToCoord(fenflags.charAt(i)); | |
156 | } | |
157 | } | |
158 | ||
159 | static GenRandInitFen(randomness) { | |
160 | // No randomization here for now (but initial setup choice) | |
161 | return ( | |
162 | "rnbqkmcbnr/pppppppppp/91/91/91/91/91/91/PPPPPPPPPP/RNBQKMCBNR " + | |
163 | "w 0 ajeaje - 00000000000000" | |
164 | ); | |
165 | // TODO later: randomization too --> 2 bishops, not next to each other. | |
166 | // then knights next to bishops. Then other pieces (...). | |
167 | } | |
168 | ||
169 | getEnpassantFen() { | |
170 | const L = this.epSquares.length; | |
171 | if (!this.epSquares[L - 1]) return "-"; //no en-passant | |
172 | let res = ""; | |
173 | this.epSquares[L - 1].forEach(sq => { | |
174 | res += V.CoordsToSquare(sq) + ","; | |
175 | }); | |
176 | return res.slice(0, -1); //remove last comma | |
177 | } | |
178 | ||
179 | getEpSquare(moveOrSquare) { | |
180 | if (!moveOrSquare) return undefined; | |
181 | if (typeof moveOrSquare === "string") { | |
182 | const square = moveOrSquare; | |
183 | if (square == "-") return undefined; | |
184 | let res = []; | |
185 | square.split(",").forEach(sq => { | |
186 | res.push(V.SquareToCoords(sq)); | |
187 | }); | |
188 | return res; | |
189 | } | |
190 | // Argument is a move: | |
191 | const move = moveOrSquare; | |
192 | const [sx, sy, ex] = [move.start.x, move.start.y, move.end.x]; | |
193 | if (this.getPiece(sx, sy) == V.PAWN && Math.abs(sx - ex) >= 2) { | |
194 | const step = (ex - sx) / Math.abs(ex - sx); | |
195 | let res = [{ | |
196 | x: sx + step, | |
197 | y: sy | |
198 | }]; | |
199 | if (sx + 2 * step != ex) { | |
200 | // 3-squares jump | |
201 | res.push({ | |
202 | x: sx + 2 * step, | |
203 | y: sy | |
204 | }); | |
205 | } | |
206 | return res; | |
207 | } | |
208 | return undefined; //default | |
209 | } | |
210 | ||
211 | getReservePpath(index, color) { | |
212 | const p = V.RESERVE_PIECES[index]; | |
213 | const prefix = (ChessRules.PIECES.includes(p) ? "" : "Pandemonium/"); | |
214 | return prefix + color + p;; | |
215 | } | |
216 | ||
217 | // Ordering on reserve pieces | |
218 | static get RESERVE_PIECES() { | |
219 | return ( | |
220 | [V.PAWN, V.ROOK, V.KNIGHT, V.BISHOP, V.QUEEN, V.CARDINAL, V.MARSHAL] | |
221 | ); | |
222 | } | |
223 | ||
224 | getReserveMoves([x, y]) { | |
225 | const color = this.turn; | |
226 | const p = V.RESERVE_PIECES[y]; | |
227 | if (this.reserve[color][p] == 0) return []; | |
228 | const bounds = (p == V.PAWN ? [1, V.size.x - 1] : [0, V.size.x]); | |
229 | let moves = []; | |
230 | for (let i = bounds[0]; i < bounds[1]; i++) { | |
231 | for (let j = 0; j < V.size.y; j++) { | |
232 | if (this.board[i][j] == V.EMPTY) { | |
233 | let mv = new Move({ | |
234 | appear: [ | |
235 | new PiPo({ | |
236 | x: i, | |
237 | y: j, | |
238 | c: color, | |
239 | p: p | |
240 | }) | |
241 | ], | |
242 | vanish: [], | |
243 | start: { x: x, y: y }, //a bit artificial... | |
244 | end: { x: i, y: j } | |
245 | }); | |
246 | if (p == V.PAWN) { | |
247 | // Do not drop on checkmate: | |
248 | this.play(mv); | |
249 | const res = ( | |
250 | this.underCheck(oppCol) && !this.atLeastOneMove("noReserve") | |
251 | ); | |
252 | this.undo(mv); | |
253 | if (res) continue; | |
254 | } | |
255 | moves.push(mv); | |
256 | } | |
257 | } | |
258 | } | |
259 | return moves; | |
260 | } | |
261 | ||
262 | static get PromoteMap() { | |
263 | return { | |
264 | r: 'd', | |
265 | n: 's', | |
266 | b: 'h', | |
267 | c: 'w', | |
268 | m: 'a' | |
269 | }; | |
270 | } | |
271 | ||
272 | getPotentialMovesFrom([x, y]) { | |
273 | const c = this.getColor(x, y); | |
274 | const oppCol = V.GetOppCol(c); | |
275 | if (this.movesCount <= 1) { | |
276 | if (this.kingPos[c][0] == x && this.kingPos[c][1] == y) { | |
277 | // Pass (if setup is ok) | |
278 | return [ | |
279 | new Move({ | |
280 | appear: [], | |
281 | vanish: [], | |
282 | start: { x: this.kingPos[c][0], y: this.kingPos[c][1] }, | |
283 | end: { x: this.kingPos[oppCol][0], y: this.kingPos[oppCol][1] } | |
284 | }) | |
285 | ]; | |
286 | } | |
287 | const firstRank = (this.movesCount == 0 ? 9 : 0); | |
288 | // TODO: initDestFile currently hardcoded for deterministic setup | |
289 | const initDestFile = new Map([[1, 2], [8, 7]]); | |
7721a36a BA |
290 | // Only option is knight --> bishop swap: |
291 | if ( | |
292 | x == firstRank && | |
293 | !!initDestFile.get(y) && | |
294 | this.getPiece(x, y) == V.KNIGHT | |
295 | ) { | |
723262f9 BA |
296 | const destFile = initDestFile.get(y); |
297 | return [ | |
298 | new Move({ | |
299 | appear: [ | |
300 | new PiPo({ | |
301 | x: x, | |
302 | y: destFile, | |
303 | c: c, | |
304 | p: V.KNIGHT | |
305 | }), | |
306 | new PiPo({ | |
307 | x: x, | |
308 | y: y, | |
309 | c: c, | |
310 | p: V.BISHOP | |
311 | }) | |
312 | ], | |
313 | vanish: [ | |
314 | new PiPo({ | |
315 | x: x, | |
316 | y: y, | |
317 | c: c, | |
318 | p: V.KNIGHT | |
319 | }), | |
320 | new PiPo({ | |
321 | x: x, | |
322 | y: destFile, | |
323 | c: c, | |
324 | p: V.BISHOP | |
325 | }) | |
326 | ], | |
327 | start: { x: x, y: y }, | |
328 | end: { x: x, y: destFile } | |
329 | }) | |
330 | ]; | |
331 | } | |
332 | return []; | |
333 | } | |
334 | // Normal move (after initial setup) | |
335 | if (x >= V.size.x) return this.getReserveMoves(x, y); | |
336 | const p = this.getPiece(x, y); | |
337 | const sq = [x, y]; | |
338 | let moves = []; | |
339 | if (ChessRules.PIECES.includes(p)) | |
340 | moves = super.getPotentialMovesFrom(sq); | |
341 | if ([V.GILDING, V.APRICOT, V.WHOLE].includes(p)) | |
342 | moves = super.getPotentialQueenMoves(sq); | |
343 | switch (p) { | |
344 | case V.SCEPTER: | |
345 | moves = this.getPotentialScepterMoves(sq); | |
346 | break; | |
347 | case V.HORSE: | |
348 | moves = this.getPotentialHorseMoves(sq); | |
349 | break; | |
350 | case V.DRAGON: | |
351 | moves = this.getPotentialDragonMoves(sq); | |
352 | break; | |
353 | case V.CARDINAL: | |
354 | moves = this.getPotentialCardinalMoves(sq); | |
355 | break; | |
356 | case V.MARSHAL: | |
357 | moves = this.getPotentialMarshalMoves(sq); | |
358 | break; | |
359 | } | |
360 | // Maybe apply promotions: | |
361 | if (Object.keys(V.PromoteMap).includes(p)) { | |
362 | const promoted = V.PromoteMap[p]; | |
363 | const lastRank = (c == 'w' ? 0 : 9); | |
364 | let promotions = []; | |
365 | moves.forEach(m => { | |
366 | if (m.start.x == lastRank || m.end.x == lastRank) { | |
367 | let pMove = JSON.parse(JSON.stringify(m)); | |
368 | pMove.appear[0].p = promoted; | |
369 | promotions.push(pMove); | |
370 | } | |
371 | }); | |
372 | Array.prototype.push.apply(moves, promotions); | |
373 | } | |
374 | return moves; | |
375 | } | |
376 | ||
377 | getPotentialPawnMoves([x, y]) { | |
378 | const color = this.turn; | |
379 | const shiftX = V.PawnSpecs.directions[color]; | |
380 | let moves = []; | |
381 | if (this.board[x + shiftX][y] == V.EMPTY) { | |
382 | this.addPawnMoves([x, y], [x + shiftX, y], moves); | |
383 | if ((color == 'w' && x >= V.size.x - 3) || (color == 'b' && x <= 3)) { | |
384 | if (this.board[x + 2 * shiftX][y] == V.EMPTY) { | |
385 | moves.push(this.getBasicMove([x, y], [x + 2 * shiftX, y])); | |
386 | if ( | |
387 | ( | |
388 | (color == 'w' && x >= V.size.x - 2) || | |
389 | (color == 'b' && x <= 2) | |
390 | ) | |
391 | && | |
392 | this.board[x + 3 * shiftX][y] == V.EMPTY | |
393 | ) { | |
394 | moves.push(this.getBasicMove([x, y], [x + 3 * shiftX, y])); | |
395 | } | |
396 | } | |
397 | } | |
398 | } | |
399 | for (let shiftY of [-1, 1]) { | |
400 | if (y + shiftY >= 0 && y + shiftY < V.size.y) { | |
401 | if ( | |
402 | this.board[x + shiftX][y + shiftY] != V.EMPTY && | |
403 | this.canTake([x, y], [x + shiftX, y + shiftY]) | |
404 | ) { | |
405 | this.addPawnMoves([x, y], [x + shiftX, y + shiftY], moves); | |
406 | } | |
407 | } | |
408 | } | |
409 | Array.prototype.push.apply( | |
410 | moves, | |
411 | this.getEnpassantCaptures([x, y], shiftX) | |
412 | ); | |
413 | return moves; | |
414 | } | |
415 | ||
416 | getPotentialMarshalMoves(sq) { | |
417 | return this.getSlideNJumpMoves(sq, V.steps[V.ROOK]).concat( | |
418 | this.getSlideNJumpMoves(sq, V.steps[V.KNIGHT], "oneStep") | |
419 | ); | |
420 | } | |
421 | ||
422 | getPotentialCardinalMoves(sq) { | |
423 | return this.getSlideNJumpMoves(sq, V.steps[V.BISHOP]).concat( | |
424 | this.getSlideNJumpMoves(sq, V.steps[V.KNIGHT], "oneStep") | |
425 | ); | |
426 | } | |
427 | ||
428 | getPotentialScepterMoves(sq) { | |
429 | const steps = | |
430 | V.steps[V.KNIGHT].concat(V.steps[V.BISHOP]).concat(V.steps[V.ROOK]); | |
431 | return this.getSlideNJumpMoves(sq, steps, "oneStep"); | |
432 | } | |
433 | ||
434 | getPotentialHorseMoves(sq) { | |
435 | return this.getSlideNJumpMoves(sq, V.steps[V.BISHOP]).concat( | |
436 | this.getSlideNJumpMoves(sq, V.steps[V.ROOK], "oneStep")); | |
437 | } | |
438 | ||
439 | getPotentialDragonMoves(sq) { | |
440 | return this.getSlideNJumpMoves(sq, V.steps[V.ROOK]).concat( | |
441 | this.getSlideNJumpMoves(sq, V.steps[V.BISHOP], "oneStep")); | |
442 | } | |
443 | ||
444 | getEnpassantCaptures([x, y], shiftX) { | |
445 | const Lep = this.epSquares.length; | |
446 | const epSquare = this.epSquares[Lep - 1]; | |
447 | let moves = []; | |
448 | if (!!epSquare) { | |
449 | for (let epsq of epSquare) { | |
450 | // TODO: some redundant checks | |
451 | if (epsq.x == x + shiftX && Math.abs(epsq.y - y) == 1) { | |
452 | let enpassantMove = this.getBasicMove([x, y], [epsq.x, epsq.y]); | |
453 | // WARNING: the captured pawn may be diagonally behind us, | |
454 | // if it's a 3-squares jump and we take on 1st passing square | |
455 | const px = this.board[x][epsq.y] != V.EMPTY ? x : x - shiftX; | |
456 | enpassantMove.vanish.push({ | |
457 | x: px, | |
458 | y: epsq.y, | |
459 | p: "p", | |
460 | c: this.getColor(px, epsq.y) | |
461 | }); | |
462 | moves.push(enpassantMove); | |
463 | } | |
464 | } | |
465 | } | |
466 | return moves; | |
467 | } | |
468 | ||
469 | getPotentialKingMoves(sq) { | |
470 | // Initialize with normal moves | |
471 | let moves = this.getSlideNJumpMoves( | |
472 | sq, | |
473 | V.steps[V.ROOK].concat(V.steps[V.BISHOP]), | |
474 | "oneStep" | |
475 | ); | |
476 | const c = this.turn; | |
477 | if ( | |
478 | this.castleFlags[c][0] < V.size.y || | |
479 | this.castleFlags[c][1] < V.size.y | |
480 | ) { | |
481 | moves = moves.concat(this.getCastleMoves(sq)); | |
482 | } | |
483 | return moves; | |
484 | } | |
485 | ||
486 | getCastleMoves([x, y]) { | |
487 | const c = this.getColor(x, y); | |
488 | if ( | |
489 | ((c == 'w' && x == 9) || (c == 'b' && x == 0)) && | |
490 | y == this.castleFlags[c][2] | |
491 | ) { | |
492 | const finalSquares = [ | |
493 | [1, 2], | |
494 | [7, 6] | |
495 | ]; | |
496 | return super.getCastleMoves([x, y], finalSquares, false, [V.ROOK]); | |
497 | } | |
498 | return []; | |
499 | } | |
500 | ||
501 | isAttacked(sq, color) { | |
502 | return ( | |
503 | this.isAttackedByPawn(sq, color) || | |
504 | this.isAttackedByRook(sq, color) || | |
505 | this.isAttackedByKnight(sq, color) || | |
506 | this.isAttackedByBishop(sq, color) || | |
507 | this.isAttackedByKing(sq, color) || | |
508 | this.isAttackedByQueens(sq, color) || | |
509 | this.isAttackedByScepter(sq, color) || | |
510 | this.isAttackedByDragon(sq, color) || | |
511 | this.isAttackedByHorse(sq, color) || | |
512 | this.isAttackedByMarshal(sq, color) || | |
513 | this.isAttackedByCardinal(sq, color) | |
514 | ); | |
515 | } | |
516 | ||
517 | isAttackedByQueens([x, y], color) { | |
518 | // pieces: because queen = gilding = whole = apricot | |
519 | const pieces = [V.QUEEN, V.GILDING, V.WHOLE, V.APRICOT]; | |
520 | const steps = V.steps[V.ROOK].concat(V.steps[V.BISHOP]); | |
521 | for (let step of steps) { | |
522 | let rx = x + step[0], | |
523 | ry = y + step[1]; | |
524 | while (V.OnBoard(rx, ry) && this.board[rx][ry] == V.EMPTY) { | |
525 | rx += step[0]; | |
526 | ry += step[1]; | |
527 | } | |
528 | if ( | |
529 | V.OnBoard(rx, ry) && | |
530 | this.board[rx][ry] != V.EMPTY && | |
531 | pieces.includes(this.getPiece(rx, ry)) && | |
532 | this.getColor(rx, ry) == color | |
533 | ) { | |
534 | return true; | |
535 | } | |
536 | } | |
537 | return false; | |
538 | } | |
539 | ||
540 | isAttackedByScepter(sq, color) { | |
541 | const steps = | |
542 | V.steps[V.KNIGHT].concat(V.steps[V.ROOK]).concat(V.steps[V.BISHOP]); | |
543 | return ( | |
544 | super.isAttackedBySlideNJump(sq, color, steps, V.SCEPTER, "oneStep") | |
545 | ); | |
546 | } | |
547 | ||
548 | isAttackedByHorse(sq, color) { | |
549 | return ( | |
550 | super.isAttackedBySlideNJump(sq, color, V.steps[V.BISHOP], V.HORSE) || | |
551 | super.isAttackedBySlideNJump( | |
552 | sq, color, V.steps[V.ROOK], V.HORSE, "oneStep") | |
553 | ); | |
554 | } | |
555 | ||
556 | isAttackedByDragon(sq, color) { | |
557 | return ( | |
558 | super.isAttackedBySlideNJump(sq, color, V.steps[V.ROOK], V.DRAGON) || | |
559 | super.isAttackedBySlideNJump( | |
560 | sq, color, V.steps[V.BISHOP], V.DRAGON, "oneStep") | |
561 | ); | |
562 | } | |
563 | ||
564 | isAttackedByMarshal(sq, color) { | |
565 | return ( | |
566 | super.isAttackedBySlideNJump(sq, color, V.MARSHAL, V.steps[V.ROOK]) || | |
567 | super.isAttackedBySlideNJump( | |
568 | sq, | |
569 | color, | |
570 | V.MARSHAL, | |
571 | V.steps[V.KNIGHT], | |
572 | "oneStep" | |
573 | ) | |
574 | ); | |
575 | } | |
576 | ||
577 | isAttackedByCardinal(sq, color) { | |
578 | return ( | |
579 | super.isAttackedBySlideNJump(sq, color, V.CARDINAL, V.steps[V.BISHOP]) || | |
580 | super.isAttackedBySlideNJump( | |
581 | sq, | |
582 | color, | |
583 | V.CARDINAL, | |
584 | V.steps[V.KNIGHT], | |
585 | "oneStep" | |
586 | ) | |
587 | ); | |
588 | } | |
589 | ||
590 | getAllValidMoves() { | |
591 | let moves = super.getAllPotentialMoves(); | |
1220a5b9 BA |
592 | if (this.movesCount >= 2) { |
593 | const color = this.turn; | |
594 | for (let i = 0; i < V.RESERVE_PIECES.length; i++) { | |
595 | moves = moves.concat( | |
596 | this.getReserveMoves([V.size.x + (color == "w" ? 0 : 1), i]) | |
597 | ); | |
598 | } | |
723262f9 BA |
599 | } |
600 | return this.filterValid(moves); | |
601 | } | |
602 | ||
603 | atLeastOneMove(noReserve) { | |
604 | if (!super.atLeastOneMove()) { | |
605 | if (!noReserve) { | |
606 | // Search one reserve move | |
607 | for (let i = 0; i < V.RESERVE_PIECES.length; i++) { | |
608 | let moves = this.filterValid( | |
609 | this.getReserveMoves([V.size.x + (this.turn == "w" ? 0 : 1), i]) | |
610 | ); | |
611 | if (moves.length > 0) return true; | |
612 | } | |
613 | } | |
614 | return false; | |
615 | } | |
616 | return true; | |
617 | } | |
618 | ||
619 | // Reverse 'PromoteMap' | |
620 | static get P_CORRESPONDANCES() { | |
621 | return { | |
622 | d: 'r', | |
623 | s: 'n', | |
624 | h: 'b', | |
625 | w: 'c', | |
626 | a: 'm' | |
627 | }; | |
628 | } | |
629 | ||
630 | static MayDecode(piece) { | |
631 | if (Object.keys(V.P_CORRESPONDANCES).includes(piece)) | |
632 | return V.P_CORRESPONDANCES[piece]; | |
633 | return piece; | |
634 | } | |
635 | ||
636 | play(move) { | |
637 | move.subTurn = this.subTurn; //much easier | |
638 | if (this.movesCount >= 2 || this.subTurn == 2 || move.vanish.length == 0) { | |
639 | this.turn = V.GetOppCol(this.turn); | |
640 | this.subTurn = 1; | |
641 | this.movesCount++; | |
642 | } | |
643 | else this.subTurn = 2; | |
644 | move.flags = JSON.stringify(this.aggregateFlags()); | |
645 | this.epSquares.push(this.getEpSquare(move)); | |
646 | V.PlayOnBoard(this.board, move); | |
647 | this.postPlay(move); | |
648 | } | |
649 | ||
650 | updateCastleFlags(move, piece) { | |
acf20712 | 651 | if (piece == V.KING && move.appear.length == 2) { |
723262f9 BA |
652 | // Castling (only move which disable flags) |
653 | this.castleFlags[move.appear[0].c][0] = 10; | |
654 | this.castleFlags[move.appear[0].c][1] = 10; | |
655 | } | |
656 | } | |
657 | ||
658 | postPlay(move) { | |
659 | if (move.vanish.length == 0 && move.appear.length == 0) return; | |
660 | super.postPlay(move); | |
661 | const color = move.appear[0].c; | |
662 | if (move.vanish.length == 0) | |
663 | // Drop unpromoted piece: | |
664 | this.reserve[color][move.appear[0].p]--; | |
1220a5b9 | 665 | else if (move.vanish.length == 2 && move.appear.length == 1) |
723262f9 BA |
666 | // May capture a promoted piece: |
667 | this.reserve[color][V.MayDecode(move.vanish[1].p)]++; | |
668 | } | |
669 | ||
670 | undo(move) { | |
671 | this.epSquares.pop(); | |
672 | this.disaggregateFlags(JSON.parse(move.flags)); | |
673 | V.UndoOnBoard(this.board, move); | |
674 | if (this.movesCount >= 2 || this.subTurn == 1 || move.vanish.length == 0) { | |
675 | this.turn = V.GetOppCol(this.turn); | |
676 | this.movesCount--; | |
677 | } | |
678 | this.subTurn = move.subTurn; | |
679 | this.postUndo(move); | |
680 | } | |
681 | ||
682 | postUndo(move) { | |
683 | if (move.vanish.length == 0 && move.appear.length == 0) return; | |
684 | super.postUndo(move); | |
685 | const color = move.appear[0].c; | |
686 | if (move.vanish.length == 0) | |
687 | this.reserve[color][move.appear[0].p]++; | |
1220a5b9 | 688 | else if (move.vanish.length == 2 && move.appear.length == 1) |
723262f9 BA |
689 | this.reserve[color][V.MayDecode(move.vanish[1].p)]--; |
690 | } | |
691 | ||
692 | getCurrentScore() { | |
693 | const c = this.turn, | |
694 | oppCol = V.GetOppCol(this.turn); | |
695 | let facingKings = false; | |
696 | if ( | |
697 | this.kingPos[c][0] == this.kingPos[oppCol][0] || | |
698 | this.kingPos[c][1] == this.kingPos[oppCol][1] | |
699 | ) { | |
700 | facingKings = true; | |
701 | let step = [ | |
702 | this.kingPos[oppCol][0] - this.kingPos[c][0], | |
703 | this.kingPos[oppCol][1] - this.kingPos[c][1] | |
704 | ]; | |
705 | if (step[0] != 0) step[0] /= Math.abs(step[0]); | |
706 | else step[1] /= Math.abs(step[1]); | |
707 | let [x, y] = | |
708 | [ this.kingPos[c][0] + step[0], this.kingPos[c][1] + step[1] ]; | |
709 | while (x != this.kingPos[oppCol][0] || y != this.kingPos[oppCol][1]) { | |
710 | if (this.board[x][y] != V.EMPTY) { | |
711 | facingKings = false; | |
712 | break; | |
713 | } | |
714 | x += step[0]; | |
715 | y += step[1]; | |
716 | } | |
717 | } | |
718 | if (facingKings) return (c == "w" ? "1-0" : "0-1"); | |
719 | if (!this.atLeastOneMove()) return (c == "w" ? "0-1" : "1-0"); | |
720 | return "*"; | |
721 | } | |
722 | ||
723 | static get VALUES() { | |
724 | return Object.assign( | |
99ba622a BA |
725 | {}, |
726 | ChessRules.VALUES, | |
723262f9 | 727 | { |
99ba622a | 728 | n: 2.5, //knight is weaker |
723262f9 BA |
729 | g: 9, |
730 | s: 5, | |
731 | h: 6, | |
732 | d: 7, | |
733 | c: 7, | |
734 | w: 9, | |
735 | m: 8, | |
736 | a: 9 | |
99ba622a | 737 | } |
723262f9 BA |
738 | ); |
739 | } | |
740 | ||
741 | static get SEARCH_DEPTH() { | |
742 | return 2; | |
743 | } | |
744 | ||
745 | getComputerMove() { | |
746 | if (this.movesCount <= 1) { | |
747 | // Special case: swap and pass at random | |
748 | const moves1 = this.getAllValidMoves(); | |
749 | const m1 = moves1[randInt(moves1.length)]; | |
750 | this.play(m1); | |
751 | if (m1.vanish.length == 0) { | |
752 | this.undo(m1); | |
753 | return m1; | |
754 | } | |
755 | const moves2 = this.getAllValidMoves(); | |
756 | const m2 = moves2[randInt(moves2.length)]; | |
757 | this.undo(m1); | |
758 | return [m1, m2]; | |
759 | } | |
760 | return super.getComputerMove(); | |
761 | } | |
762 | ||
763 | evalPosition() { | |
764 | let evaluation = super.evalPosition(); | |
765 | // Add reserves: | |
766 | for (let i = 0; i < V.RESERVE_PIECES.length; i++) { | |
767 | const p = V.RESERVE_PIECES[i]; | |
768 | evaluation += this.reserve["w"][p] * V.VALUES[p]; | |
769 | evaluation -= this.reserve["b"][p] * V.VALUES[p]; | |
770 | } | |
771 | return evaluation; | |
772 | } | |
773 | ||
774 | getNotation(move) { | |
775 | if (move.vanish.length == 0) { | |
776 | if (move.appear.length == 0) return "pass"; | |
777 | const pieceName = | |
778 | (move.appear[0].p == V.PAWN ? "" : move.appear[0].p.toUpperCase()); | |
779 | return pieceName + "@" + V.CoordsToSquare(move.end); | |
780 | } | |
781 | if (move.appear.length == 2) { | |
782 | if (move.appear[0].p != V.KING) | |
783 | return V.CoordsToSquare(move.start) + "S" + V.CoordsToSquare(move.end); | |
784 | return (move.end.y < move.start.y ? "0-0" : "0-0-0"); | |
785 | } | |
786 | let notation = super.getNotation(move); | |
787 | if (move.vanish[0].p != V.PAWN && move.appear[0].p != move.vanish[0].p) | |
788 | // Add promotion indication: | |
789 | notation += "=" + move.appear[0].p.toUpperCase(); | |
790 | return notation; | |
791 | } | |
792 | ||
793 | }; |