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]]); | |
290 | // Only option is knight / bishop swap: | |
291 | if (x == firstRank && !!initDestFile.get(y)) { | |
292 | const destFile = initDestFile.get(y); | |
293 | return [ | |
294 | new Move({ | |
295 | appear: [ | |
296 | new PiPo({ | |
297 | x: x, | |
298 | y: destFile, | |
299 | c: c, | |
300 | p: V.KNIGHT | |
301 | }), | |
302 | new PiPo({ | |
303 | x: x, | |
304 | y: y, | |
305 | c: c, | |
306 | p: V.BISHOP | |
307 | }) | |
308 | ], | |
309 | vanish: [ | |
310 | new PiPo({ | |
311 | x: x, | |
312 | y: y, | |
313 | c: c, | |
314 | p: V.KNIGHT | |
315 | }), | |
316 | new PiPo({ | |
317 | x: x, | |
318 | y: destFile, | |
319 | c: c, | |
320 | p: V.BISHOP | |
321 | }) | |
322 | ], | |
323 | start: { x: x, y: y }, | |
324 | end: { x: x, y: destFile } | |
325 | }) | |
326 | ]; | |
327 | } | |
328 | return []; | |
329 | } | |
330 | // Normal move (after initial setup) | |
331 | if (x >= V.size.x) return this.getReserveMoves(x, y); | |
332 | const p = this.getPiece(x, y); | |
333 | const sq = [x, y]; | |
334 | let moves = []; | |
335 | if (ChessRules.PIECES.includes(p)) | |
336 | moves = super.getPotentialMovesFrom(sq); | |
337 | if ([V.GILDING, V.APRICOT, V.WHOLE].includes(p)) | |
338 | moves = super.getPotentialQueenMoves(sq); | |
339 | switch (p) { | |
340 | case V.SCEPTER: | |
341 | moves = this.getPotentialScepterMoves(sq); | |
342 | break; | |
343 | case V.HORSE: | |
344 | moves = this.getPotentialHorseMoves(sq); | |
345 | break; | |
346 | case V.DRAGON: | |
347 | moves = this.getPotentialDragonMoves(sq); | |
348 | break; | |
349 | case V.CARDINAL: | |
350 | moves = this.getPotentialCardinalMoves(sq); | |
351 | break; | |
352 | case V.MARSHAL: | |
353 | moves = this.getPotentialMarshalMoves(sq); | |
354 | break; | |
355 | } | |
356 | // Maybe apply promotions: | |
357 | if (Object.keys(V.PromoteMap).includes(p)) { | |
358 | const promoted = V.PromoteMap[p]; | |
359 | const lastRank = (c == 'w' ? 0 : 9); | |
360 | let promotions = []; | |
361 | moves.forEach(m => { | |
362 | if (m.start.x == lastRank || m.end.x == lastRank) { | |
363 | let pMove = JSON.parse(JSON.stringify(m)); | |
364 | pMove.appear[0].p = promoted; | |
365 | promotions.push(pMove); | |
366 | } | |
367 | }); | |
368 | Array.prototype.push.apply(moves, promotions); | |
369 | } | |
370 | return moves; | |
371 | } | |
372 | ||
373 | getPotentialPawnMoves([x, y]) { | |
374 | const color = this.turn; | |
375 | const shiftX = V.PawnSpecs.directions[color]; | |
376 | let moves = []; | |
377 | if (this.board[x + shiftX][y] == V.EMPTY) { | |
378 | this.addPawnMoves([x, y], [x + shiftX, y], moves); | |
379 | if ((color == 'w' && x >= V.size.x - 3) || (color == 'b' && x <= 3)) { | |
380 | if (this.board[x + 2 * shiftX][y] == V.EMPTY) { | |
381 | moves.push(this.getBasicMove([x, y], [x + 2 * shiftX, y])); | |
382 | if ( | |
383 | ( | |
384 | (color == 'w' && x >= V.size.x - 2) || | |
385 | (color == 'b' && x <= 2) | |
386 | ) | |
387 | && | |
388 | this.board[x + 3 * shiftX][y] == V.EMPTY | |
389 | ) { | |
390 | moves.push(this.getBasicMove([x, y], [x + 3 * shiftX, y])); | |
391 | } | |
392 | } | |
393 | } | |
394 | } | |
395 | for (let shiftY of [-1, 1]) { | |
396 | if (y + shiftY >= 0 && y + shiftY < V.size.y) { | |
397 | if ( | |
398 | this.board[x + shiftX][y + shiftY] != V.EMPTY && | |
399 | this.canTake([x, y], [x + shiftX, y + shiftY]) | |
400 | ) { | |
401 | this.addPawnMoves([x, y], [x + shiftX, y + shiftY], moves); | |
402 | } | |
403 | } | |
404 | } | |
405 | Array.prototype.push.apply( | |
406 | moves, | |
407 | this.getEnpassantCaptures([x, y], shiftX) | |
408 | ); | |
409 | return moves; | |
410 | } | |
411 | ||
412 | getPotentialMarshalMoves(sq) { | |
413 | return this.getSlideNJumpMoves(sq, V.steps[V.ROOK]).concat( | |
414 | this.getSlideNJumpMoves(sq, V.steps[V.KNIGHT], "oneStep") | |
415 | ); | |
416 | } | |
417 | ||
418 | getPotentialCardinalMoves(sq) { | |
419 | return this.getSlideNJumpMoves(sq, V.steps[V.BISHOP]).concat( | |
420 | this.getSlideNJumpMoves(sq, V.steps[V.KNIGHT], "oneStep") | |
421 | ); | |
422 | } | |
423 | ||
424 | getPotentialScepterMoves(sq) { | |
425 | const steps = | |
426 | V.steps[V.KNIGHT].concat(V.steps[V.BISHOP]).concat(V.steps[V.ROOK]); | |
427 | return this.getSlideNJumpMoves(sq, steps, "oneStep"); | |
428 | } | |
429 | ||
430 | getPotentialHorseMoves(sq) { | |
431 | return this.getSlideNJumpMoves(sq, V.steps[V.BISHOP]).concat( | |
432 | this.getSlideNJumpMoves(sq, V.steps[V.ROOK], "oneStep")); | |
433 | } | |
434 | ||
435 | getPotentialDragonMoves(sq) { | |
436 | return this.getSlideNJumpMoves(sq, V.steps[V.ROOK]).concat( | |
437 | this.getSlideNJumpMoves(sq, V.steps[V.BISHOP], "oneStep")); | |
438 | } | |
439 | ||
440 | getEnpassantCaptures([x, y], shiftX) { | |
441 | const Lep = this.epSquares.length; | |
442 | const epSquare = this.epSquares[Lep - 1]; | |
443 | let moves = []; | |
444 | if (!!epSquare) { | |
445 | for (let epsq of epSquare) { | |
446 | // TODO: some redundant checks | |
447 | if (epsq.x == x + shiftX && Math.abs(epsq.y - y) == 1) { | |
448 | let enpassantMove = this.getBasicMove([x, y], [epsq.x, epsq.y]); | |
449 | // WARNING: the captured pawn may be diagonally behind us, | |
450 | // if it's a 3-squares jump and we take on 1st passing square | |
451 | const px = this.board[x][epsq.y] != V.EMPTY ? x : x - shiftX; | |
452 | enpassantMove.vanish.push({ | |
453 | x: px, | |
454 | y: epsq.y, | |
455 | p: "p", | |
456 | c: this.getColor(px, epsq.y) | |
457 | }); | |
458 | moves.push(enpassantMove); | |
459 | } | |
460 | } | |
461 | } | |
462 | return moves; | |
463 | } | |
464 | ||
465 | getPotentialKingMoves(sq) { | |
466 | // Initialize with normal moves | |
467 | let moves = this.getSlideNJumpMoves( | |
468 | sq, | |
469 | V.steps[V.ROOK].concat(V.steps[V.BISHOP]), | |
470 | "oneStep" | |
471 | ); | |
472 | const c = this.turn; | |
473 | if ( | |
474 | this.castleFlags[c][0] < V.size.y || | |
475 | this.castleFlags[c][1] < V.size.y | |
476 | ) { | |
477 | moves = moves.concat(this.getCastleMoves(sq)); | |
478 | } | |
479 | return moves; | |
480 | } | |
481 | ||
482 | getCastleMoves([x, y]) { | |
483 | const c = this.getColor(x, y); | |
484 | if ( | |
485 | ((c == 'w' && x == 9) || (c == 'b' && x == 0)) && | |
486 | y == this.castleFlags[c][2] | |
487 | ) { | |
488 | const finalSquares = [ | |
489 | [1, 2], | |
490 | [7, 6] | |
491 | ]; | |
492 | return super.getCastleMoves([x, y], finalSquares, false, [V.ROOK]); | |
493 | } | |
494 | return []; | |
495 | } | |
496 | ||
497 | isAttacked(sq, color) { | |
498 | return ( | |
499 | this.isAttackedByPawn(sq, color) || | |
500 | this.isAttackedByRook(sq, color) || | |
501 | this.isAttackedByKnight(sq, color) || | |
502 | this.isAttackedByBishop(sq, color) || | |
503 | this.isAttackedByKing(sq, color) || | |
504 | this.isAttackedByQueens(sq, color) || | |
505 | this.isAttackedByScepter(sq, color) || | |
506 | this.isAttackedByDragon(sq, color) || | |
507 | this.isAttackedByHorse(sq, color) || | |
508 | this.isAttackedByMarshal(sq, color) || | |
509 | this.isAttackedByCardinal(sq, color) | |
510 | ); | |
511 | } | |
512 | ||
513 | isAttackedByQueens([x, y], color) { | |
514 | // pieces: because queen = gilding = whole = apricot | |
515 | const pieces = [V.QUEEN, V.GILDING, V.WHOLE, V.APRICOT]; | |
516 | const steps = V.steps[V.ROOK].concat(V.steps[V.BISHOP]); | |
517 | for (let step of steps) { | |
518 | let rx = x + step[0], | |
519 | ry = y + step[1]; | |
520 | while (V.OnBoard(rx, ry) && this.board[rx][ry] == V.EMPTY) { | |
521 | rx += step[0]; | |
522 | ry += step[1]; | |
523 | } | |
524 | if ( | |
525 | V.OnBoard(rx, ry) && | |
526 | this.board[rx][ry] != V.EMPTY && | |
527 | pieces.includes(this.getPiece(rx, ry)) && | |
528 | this.getColor(rx, ry) == color | |
529 | ) { | |
530 | return true; | |
531 | } | |
532 | } | |
533 | return false; | |
534 | } | |
535 | ||
536 | isAttackedByScepter(sq, color) { | |
537 | const steps = | |
538 | V.steps[V.KNIGHT].concat(V.steps[V.ROOK]).concat(V.steps[V.BISHOP]); | |
539 | return ( | |
540 | super.isAttackedBySlideNJump(sq, color, steps, V.SCEPTER, "oneStep") | |
541 | ); | |
542 | } | |
543 | ||
544 | isAttackedByHorse(sq, color) { | |
545 | return ( | |
546 | super.isAttackedBySlideNJump(sq, color, V.steps[V.BISHOP], V.HORSE) || | |
547 | super.isAttackedBySlideNJump( | |
548 | sq, color, V.steps[V.ROOK], V.HORSE, "oneStep") | |
549 | ); | |
550 | } | |
551 | ||
552 | isAttackedByDragon(sq, color) { | |
553 | return ( | |
554 | super.isAttackedBySlideNJump(sq, color, V.steps[V.ROOK], V.DRAGON) || | |
555 | super.isAttackedBySlideNJump( | |
556 | sq, color, V.steps[V.BISHOP], V.DRAGON, "oneStep") | |
557 | ); | |
558 | } | |
559 | ||
560 | isAttackedByMarshal(sq, color) { | |
561 | return ( | |
562 | super.isAttackedBySlideNJump(sq, color, V.MARSHAL, V.steps[V.ROOK]) || | |
563 | super.isAttackedBySlideNJump( | |
564 | sq, | |
565 | color, | |
566 | V.MARSHAL, | |
567 | V.steps[V.KNIGHT], | |
568 | "oneStep" | |
569 | ) | |
570 | ); | |
571 | } | |
572 | ||
573 | isAttackedByCardinal(sq, color) { | |
574 | return ( | |
575 | super.isAttackedBySlideNJump(sq, color, V.CARDINAL, V.steps[V.BISHOP]) || | |
576 | super.isAttackedBySlideNJump( | |
577 | sq, | |
578 | color, | |
579 | V.CARDINAL, | |
580 | V.steps[V.KNIGHT], | |
581 | "oneStep" | |
582 | ) | |
583 | ); | |
584 | } | |
585 | ||
586 | getAllValidMoves() { | |
587 | let moves = super.getAllPotentialMoves(); | |
588 | const color = this.turn; | |
589 | for (let i = 0; i < V.RESERVE_PIECES.length; i++) { | |
590 | moves = moves.concat( | |
591 | this.getReserveMoves([V.size.x + (color == "w" ? 0 : 1), i]) | |
592 | ); | |
593 | } | |
594 | return this.filterValid(moves); | |
595 | } | |
596 | ||
597 | atLeastOneMove(noReserve) { | |
598 | if (!super.atLeastOneMove()) { | |
599 | if (!noReserve) { | |
600 | // Search one reserve move | |
601 | for (let i = 0; i < V.RESERVE_PIECES.length; i++) { | |
602 | let moves = this.filterValid( | |
603 | this.getReserveMoves([V.size.x + (this.turn == "w" ? 0 : 1), i]) | |
604 | ); | |
605 | if (moves.length > 0) return true; | |
606 | } | |
607 | } | |
608 | return false; | |
609 | } | |
610 | return true; | |
611 | } | |
612 | ||
613 | // Reverse 'PromoteMap' | |
614 | static get P_CORRESPONDANCES() { | |
615 | return { | |
616 | d: 'r', | |
617 | s: 'n', | |
618 | h: 'b', | |
619 | w: 'c', | |
620 | a: 'm' | |
621 | }; | |
622 | } | |
623 | ||
624 | static MayDecode(piece) { | |
625 | if (Object.keys(V.P_CORRESPONDANCES).includes(piece)) | |
626 | return V.P_CORRESPONDANCES[piece]; | |
627 | return piece; | |
628 | } | |
629 | ||
630 | play(move) { | |
631 | move.subTurn = this.subTurn; //much easier | |
632 | if (this.movesCount >= 2 || this.subTurn == 2 || move.vanish.length == 0) { | |
633 | this.turn = V.GetOppCol(this.turn); | |
634 | this.subTurn = 1; | |
635 | this.movesCount++; | |
636 | } | |
637 | else this.subTurn = 2; | |
638 | move.flags = JSON.stringify(this.aggregateFlags()); | |
639 | this.epSquares.push(this.getEpSquare(move)); | |
640 | V.PlayOnBoard(this.board, move); | |
641 | this.postPlay(move); | |
642 | } | |
643 | ||
644 | updateCastleFlags(move, piece) { | |
645 | if (move.appear.length == 2) { | |
646 | // Castling (only move which disable flags) | |
647 | this.castleFlags[move.appear[0].c][0] = 10; | |
648 | this.castleFlags[move.appear[0].c][1] = 10; | |
649 | } | |
650 | } | |
651 | ||
652 | postPlay(move) { | |
653 | if (move.vanish.length == 0 && move.appear.length == 0) return; | |
654 | super.postPlay(move); | |
655 | const color = move.appear[0].c; | |
656 | if (move.vanish.length == 0) | |
657 | // Drop unpromoted piece: | |
658 | this.reserve[color][move.appear[0].p]--; | |
659 | else if (move.vanish.length == 2) | |
660 | // May capture a promoted piece: | |
661 | this.reserve[color][V.MayDecode(move.vanish[1].p)]++; | |
662 | } | |
663 | ||
664 | undo(move) { | |
665 | this.epSquares.pop(); | |
666 | this.disaggregateFlags(JSON.parse(move.flags)); | |
667 | V.UndoOnBoard(this.board, move); | |
668 | if (this.movesCount >= 2 || this.subTurn == 1 || move.vanish.length == 0) { | |
669 | this.turn = V.GetOppCol(this.turn); | |
670 | this.movesCount--; | |
671 | } | |
672 | this.subTurn = move.subTurn; | |
673 | this.postUndo(move); | |
674 | } | |
675 | ||
676 | postUndo(move) { | |
677 | if (move.vanish.length == 0 && move.appear.length == 0) return; | |
678 | super.postUndo(move); | |
679 | const color = move.appear[0].c; | |
680 | if (move.vanish.length == 0) | |
681 | this.reserve[color][move.appear[0].p]++; | |
682 | else if (move.vanish.length == 2) | |
683 | this.reserve[color][V.MayDecode(move.vanish[1].p)]--; | |
684 | } | |
685 | ||
686 | getCurrentScore() { | |
687 | const c = this.turn, | |
688 | oppCol = V.GetOppCol(this.turn); | |
689 | let facingKings = false; | |
690 | if ( | |
691 | this.kingPos[c][0] == this.kingPos[oppCol][0] || | |
692 | this.kingPos[c][1] == this.kingPos[oppCol][1] | |
693 | ) { | |
694 | facingKings = true; | |
695 | let step = [ | |
696 | this.kingPos[oppCol][0] - this.kingPos[c][0], | |
697 | this.kingPos[oppCol][1] - this.kingPos[c][1] | |
698 | ]; | |
699 | if (step[0] != 0) step[0] /= Math.abs(step[0]); | |
700 | else step[1] /= Math.abs(step[1]); | |
701 | let [x, y] = | |
702 | [ this.kingPos[c][0] + step[0], this.kingPos[c][1] + step[1] ]; | |
703 | while (x != this.kingPos[oppCol][0] || y != this.kingPos[oppCol][1]) { | |
704 | if (this.board[x][y] != V.EMPTY) { | |
705 | facingKings = false; | |
706 | break; | |
707 | } | |
708 | x += step[0]; | |
709 | y += step[1]; | |
710 | } | |
711 | } | |
712 | if (facingKings) return (c == "w" ? "1-0" : "0-1"); | |
713 | if (!this.atLeastOneMove()) return (c == "w" ? "0-1" : "1-0"); | |
714 | return "*"; | |
715 | } | |
716 | ||
717 | static get VALUES() { | |
718 | return Object.assign( | |
719 | { | |
720 | g: 9, | |
721 | s: 5, | |
722 | h: 6, | |
723 | d: 7, | |
724 | c: 7, | |
725 | w: 9, | |
726 | m: 8, | |
727 | a: 9 | |
728 | }, | |
729 | ChessRules.VALUES | |
730 | ); | |
731 | } | |
732 | ||
733 | static get SEARCH_DEPTH() { | |
734 | return 2; | |
735 | } | |
736 | ||
737 | getComputerMove() { | |
738 | if (this.movesCount <= 1) { | |
739 | // Special case: swap and pass at random | |
740 | const moves1 = this.getAllValidMoves(); | |
741 | const m1 = moves1[randInt(moves1.length)]; | |
742 | this.play(m1); | |
743 | if (m1.vanish.length == 0) { | |
744 | this.undo(m1); | |
745 | return m1; | |
746 | } | |
747 | const moves2 = this.getAllValidMoves(); | |
748 | const m2 = moves2[randInt(moves2.length)]; | |
749 | this.undo(m1); | |
750 | return [m1, m2]; | |
751 | } | |
752 | return super.getComputerMove(); | |
753 | } | |
754 | ||
755 | evalPosition() { | |
756 | let evaluation = super.evalPosition(); | |
757 | // Add reserves: | |
758 | for (let i = 0; i < V.RESERVE_PIECES.length; i++) { | |
759 | const p = V.RESERVE_PIECES[i]; | |
760 | evaluation += this.reserve["w"][p] * V.VALUES[p]; | |
761 | evaluation -= this.reserve["b"][p] * V.VALUES[p]; | |
762 | } | |
763 | return evaluation; | |
764 | } | |
765 | ||
766 | getNotation(move) { | |
767 | if (move.vanish.length == 0) { | |
768 | if (move.appear.length == 0) return "pass"; | |
769 | const pieceName = | |
770 | (move.appear[0].p == V.PAWN ? "" : move.appear[0].p.toUpperCase()); | |
771 | return pieceName + "@" + V.CoordsToSquare(move.end); | |
772 | } | |
773 | if (move.appear.length == 2) { | |
774 | if (move.appear[0].p != V.KING) | |
775 | return V.CoordsToSquare(move.start) + "S" + V.CoordsToSquare(move.end); | |
776 | return (move.end.y < move.start.y ? "0-0" : "0-0-0"); | |
777 | } | |
778 | let notation = super.getNotation(move); | |
779 | if (move.vanish[0].p != V.PAWN && move.appear[0].p != move.vanish[0].p) | |
780 | // Add promotion indication: | |
781 | notation += "=" + move.appear[0].p.toUpperCase(); | |
782 | return notation; | |
783 | } | |
784 | ||
785 | }; |