Add Swap, Switching, pawns variants
[vchess.git] / client / src / base_rules.js
1 // (Orthodox) Chess rules are defined in ChessRules class.
2 // Variants generally inherit from it, and modify some parts.
3
4 import { ArrayFun } from "@/utils/array";
5 import { randInt, shuffle } from "@/utils/alea";
6
7 // class "PiPo": Piece + Position
8 export const PiPo = class PiPo {
9 // o: {piece[p], color[c], posX[x], posY[y]}
10 constructor(o) {
11 this.p = o.p;
12 this.c = o.c;
13 this.x = o.x;
14 this.y = o.y;
15 }
16 };
17
18 export const Move = class Move {
19 // o: {appear, vanish, [start,] [end,]}
20 // appear,vanish = arrays of PiPo
21 // start,end = coordinates to apply to trigger move visually (think castle)
22 constructor(o) {
23 this.appear = o.appear;
24 this.vanish = o.vanish;
25 this.start = o.start ? o.start : { x: o.vanish[0].x, y: o.vanish[0].y };
26 this.end = o.end ? o.end : { x: o.appear[0].x, y: o.appear[0].y };
27 }
28 };
29
30 // NOTE: x coords = top to bottom; y = left to right
31 // (from white player perspective)
32 export const ChessRules = class ChessRules {
33 //////////////
34 // MISC UTILS
35
36 // Some variants don't have flags:
37 static get HasFlags() {
38 return true;
39 }
40
41 // Or castle
42 static get HasCastle() {
43 return V.HasFlags;
44 }
45
46 // Pawns specifications
47 static get PawnSpecs() {
48 return {
49 directions: { 'w': -1, 'b': 1 },
50 initShift: { w: 1, b: 1 },
51 twoSquares: true,
52 threeSquares: false,
53 promotions: [V.ROOK, V.KNIGHT, V.BISHOP, V.QUEEN],
54 canCapture: true,
55 captureBackward: false,
56 bidirectional: false
57 };
58 }
59
60 // En-passant captures need a stack of squares:
61 static get HasEnpassant() {
62 return true;
63 }
64
65 // Some variants cannot have analyse mode
66 static get CanAnalyze() {
67 return true;
68 }
69 // Patch: issues with javascript OOP, objects can't access static fields.
70 get canAnalyze() {
71 return V.CanAnalyze;
72 }
73
74 // Some variants show incomplete information,
75 // and thus show only a partial moves list or no list at all.
76 static get ShowMoves() {
77 return "all";
78 }
79 get showMoves() {
80 return V.ShowMoves;
81 }
82
83 // Sometimes moves must remain hidden until game ends
84 static get SomeHiddenMoves() {
85 return false;
86 }
87 get someHiddenMoves() {
88 return V.SomeHiddenMoves;
89 }
90
91 // Generally true, unless the variant includes random effects
92 static get CorrConfirm() {
93 return true;
94 }
95
96 // Used for Monochrome variant (TODO: harmonize: !canFlip ==> showFirstTurn)
97 get showFirstTurn() {
98 return false;
99 }
100
101 // Some variants always show the same orientation
102 static get CanFlip() {
103 return true;
104 }
105 get canFlip() {
106 return V.CanFlip;
107 }
108
109 // For (generally old) variants without checkered board
110 static get Monochrome() {
111 return false;
112 }
113
114 // Some variants require lines drawing
115 static get Lines() {
116 if (V.Monochrome) {
117 let lines = [];
118 // Draw all inter-squares lines
119 for (let i = 0; i <= V.size.x; i++)
120 lines.push([[i, 0], [i, V.size.y]]);
121 for (let j = 0; j <= V.size.y; j++)
122 lines.push([[0, j], [V.size.x, j]]);
123 return lines;
124 }
125 return null;
126 }
127
128 // Some variants use click infos:
129 doClick() {
130 return null;
131 }
132
133 // Some variants may need to highlight squares on hover (Hamilton, Weiqi...)
134 hoverHighlight() {
135 return false;
136 }
137
138 static get IMAGE_EXTENSION() {
139 // All pieces should be in the SVG format
140 return ".svg";
141 }
142
143 // Turn "wb" into "B" (for FEN)
144 static board2fen(b) {
145 return b[0] == "w" ? b[1].toUpperCase() : b[1];
146 }
147
148 // Turn "p" into "bp" (for board)
149 static fen2board(f) {
150 return f.charCodeAt() <= 90 ? "w" + f.toLowerCase() : "b" + f;
151 }
152
153 // Check if FEN describes a board situation correctly
154 static IsGoodFen(fen) {
155 const fenParsed = V.ParseFen(fen);
156 // 1) Check position
157 if (!V.IsGoodPosition(fenParsed.position)) return false;
158 // 2) Check turn
159 if (!fenParsed.turn || !V.IsGoodTurn(fenParsed.turn)) return false;
160 // 3) Check moves count
161 if (!fenParsed.movesCount || !(parseInt(fenParsed.movesCount) >= 0))
162 return false;
163 // 4) Check flags
164 if (V.HasFlags && (!fenParsed.flags || !V.IsGoodFlags(fenParsed.flags)))
165 return false;
166 // 5) Check enpassant
167 if (
168 V.HasEnpassant &&
169 (!fenParsed.enpassant || !V.IsGoodEnpassant(fenParsed.enpassant))
170 ) {
171 return false;
172 }
173 return true;
174 }
175
176 // Is position part of the FEN a priori correct?
177 static IsGoodPosition(position) {
178 if (position.length == 0) return false;
179 const rows = position.split("/");
180 if (rows.length != V.size.x) return false;
181 let kings = { "k": 0, "K": 0 };
182 for (let row of rows) {
183 let sumElts = 0;
184 for (let i = 0; i < row.length; i++) {
185 if (['K','k'].includes(row[i])) kings[row[i]]++;
186 if (V.PIECES.includes(row[i].toLowerCase())) sumElts++;
187 else {
188 const num = parseInt(row[i]);
189 if (isNaN(num)) return false;
190 sumElts += num;
191 }
192 }
193 if (sumElts != V.size.y) return false;
194 }
195 // Both kings should be on board. Exactly one per color.
196 if (Object.values(kings).some(v => v != 1)) return false;
197 return true;
198 }
199
200 // For FEN checking
201 static IsGoodTurn(turn) {
202 return ["w", "b"].includes(turn);
203 }
204
205 // For FEN checking
206 static IsGoodFlags(flags) {
207 // NOTE: a little too permissive to work with more variants
208 return !!flags.match(/^[a-z]{4,4}$/);
209 }
210
211 // NOTE: not with regexp to adapt to different board sizes. (TODO?)
212 static IsGoodEnpassant(enpassant) {
213 if (enpassant != "-") {
214 const ep = V.SquareToCoords(enpassant);
215 if (isNaN(ep.x) || !V.OnBoard(ep)) return false;
216 }
217 return true;
218 }
219
220 // 3 --> d (column number to letter)
221 static CoordToColumn(colnum) {
222 return String.fromCharCode(97 + colnum);
223 }
224
225 // d --> 3 (column letter to number)
226 static ColumnToCoord(column) {
227 return column.charCodeAt(0) - 97;
228 }
229
230 // a4 --> {x:3,y:0}
231 static SquareToCoords(sq) {
232 return {
233 // NOTE: column is always one char => max 26 columns
234 // row is counted from black side => subtraction
235 x: V.size.x - parseInt(sq.substr(1)),
236 y: sq[0].charCodeAt() - 97
237 };
238 }
239
240 // {x:0,y:4} --> e8
241 static CoordsToSquare(coords) {
242 return V.CoordToColumn(coords.y) + (V.size.x - coords.x);
243 }
244
245 // Path to pieces (standard ones in pieces/ folder)
246 getPpath(b) {
247 return b;
248 }
249
250 // Path to promotion pieces (usually the same)
251 getPPpath(m) {
252 return this.getPpath(m.appear[0].c + m.appear[0].p);
253 }
254
255 // Aggregates flags into one object
256 aggregateFlags() {
257 return this.castleFlags;
258 }
259
260 // Reverse operation
261 disaggregateFlags(flags) {
262 this.castleFlags = flags;
263 }
264
265 // En-passant square, if any
266 getEpSquare(moveOrSquare) {
267 if (!moveOrSquare) return undefined;
268 if (typeof moveOrSquare === "string") {
269 const square = moveOrSquare;
270 if (square == "-") return undefined;
271 return V.SquareToCoords(square);
272 }
273 // Argument is a move:
274 const move = moveOrSquare;
275 const s = move.start,
276 e = move.end;
277 if (
278 s.y == e.y &&
279 Math.abs(s.x - e.x) == 2 &&
280 // Next conditions for variants like Atomic or Rifle, Recycle...
281 (move.appear.length > 0 && move.appear[0].p == V.PAWN) &&
282 (move.vanish.length > 0 && move.vanish[0].p == V.PAWN)
283 ) {
284 return {
285 x: (s.x + e.x) / 2,
286 y: s.y
287 };
288 }
289 return undefined; //default
290 }
291
292 // Can thing on square1 take thing on square2
293 canTake([x1, y1], [x2, y2]) {
294 return this.getColor(x1, y1) !== this.getColor(x2, y2);
295 }
296
297 // Is (x,y) on the chessboard?
298 static OnBoard(x, y) {
299 return x >= 0 && x < V.size.x && y >= 0 && y < V.size.y;
300 }
301
302 // Used in interface: 'side' arg == player color
303 canIplay(side, [x, y]) {
304 return this.turn == side && this.getColor(x, y) == side;
305 }
306
307 // On which squares is color under check ? (for interface)
308 getCheckSquares() {
309 const color = this.turn;
310 return (
311 this.underCheck(color)
312 // kingPos must be duplicated, because it may change:
313 ? [JSON.parse(JSON.stringify(this.kingPos[color]))]
314 : []
315 );
316 }
317
318 /////////////
319 // FEN UTILS
320
321 // Setup the initial random (asymmetric) position
322 static GenRandInitFen(randomness) {
323 if (randomness == 0)
324 // Deterministic:
325 return "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w 0 ahah -";
326
327 let pieces = { w: new Array(8), b: new Array(8) };
328 let flags = "";
329 // Shuffle pieces on first (and last rank if randomness == 2)
330 for (let c of ["w", "b"]) {
331 if (c == 'b' && randomness == 1) {
332 pieces['b'] = pieces['w'];
333 flags += flags;
334 break;
335 }
336
337 let positions = ArrayFun.range(8);
338
339 // Get random squares for bishops
340 let randIndex = 2 * randInt(4);
341 const bishop1Pos = positions[randIndex];
342 // The second bishop must be on a square of different color
343 let randIndex_tmp = 2 * randInt(4) + 1;
344 const bishop2Pos = positions[randIndex_tmp];
345 // Remove chosen squares
346 positions.splice(Math.max(randIndex, randIndex_tmp), 1);
347 positions.splice(Math.min(randIndex, randIndex_tmp), 1);
348
349 // Get random squares for knights
350 randIndex = randInt(6);
351 const knight1Pos = positions[randIndex];
352 positions.splice(randIndex, 1);
353 randIndex = randInt(5);
354 const knight2Pos = positions[randIndex];
355 positions.splice(randIndex, 1);
356
357 // Get random square for queen
358 randIndex = randInt(4);
359 const queenPos = positions[randIndex];
360 positions.splice(randIndex, 1);
361
362 // Rooks and king positions are now fixed,
363 // because of the ordering rook-king-rook
364 const rook1Pos = positions[0];
365 const kingPos = positions[1];
366 const rook2Pos = positions[2];
367
368 // Finally put the shuffled pieces in the board array
369 pieces[c][rook1Pos] = "r";
370 pieces[c][knight1Pos] = "n";
371 pieces[c][bishop1Pos] = "b";
372 pieces[c][queenPos] = "q";
373 pieces[c][kingPos] = "k";
374 pieces[c][bishop2Pos] = "b";
375 pieces[c][knight2Pos] = "n";
376 pieces[c][rook2Pos] = "r";
377 flags += V.CoordToColumn(rook1Pos) + V.CoordToColumn(rook2Pos);
378 }
379 // Add turn + flags + enpassant
380 return (
381 pieces["b"].join("") +
382 "/pppppppp/8/8/8/8/PPPPPPPP/" +
383 pieces["w"].join("").toUpperCase() +
384 " w 0 " + flags + " -"
385 );
386 }
387
388 // "Parse" FEN: just return untransformed string data
389 static ParseFen(fen) {
390 const fenParts = fen.split(" ");
391 let res = {
392 position: fenParts[0],
393 turn: fenParts[1],
394 movesCount: fenParts[2]
395 };
396 let nextIdx = 3;
397 if (V.HasFlags) Object.assign(res, { flags: fenParts[nextIdx++] });
398 if (V.HasEnpassant) Object.assign(res, { enpassant: fenParts[nextIdx] });
399 return res;
400 }
401
402 // Return current fen (game state)
403 getFen() {
404 return (
405 this.getBaseFen() + " " +
406 this.getTurnFen() + " " +
407 this.movesCount +
408 (V.HasFlags ? " " + this.getFlagsFen() : "") +
409 (V.HasEnpassant ? " " + this.getEnpassantFen() : "")
410 );
411 }
412
413 getFenForRepeat() {
414 // Omit movesCount, only variable allowed to differ
415 return (
416 this.getBaseFen() + "_" +
417 this.getTurnFen() +
418 (V.HasFlags ? "_" + this.getFlagsFen() : "") +
419 (V.HasEnpassant ? "_" + this.getEnpassantFen() : "")
420 );
421 }
422
423 // Position part of the FEN string
424 getBaseFen() {
425 const format = (count) => {
426 // if more than 9 consecutive free spaces, break the integer,
427 // otherwise FEN parsing will fail.
428 if (count <= 9) return count;
429 // Currently only boards of size up to 11 or 12:
430 return "9" + (count - 9);
431 };
432 let position = "";
433 for (let i = 0; i < V.size.x; i++) {
434 let emptyCount = 0;
435 for (let j = 0; j < V.size.y; j++) {
436 if (this.board[i][j] == V.EMPTY) emptyCount++;
437 else {
438 if (emptyCount > 0) {
439 // Add empty squares in-between
440 position += format(emptyCount);
441 emptyCount = 0;
442 }
443 position += V.board2fen(this.board[i][j]);
444 }
445 }
446 if (emptyCount > 0) {
447 // "Flush remainder"
448 position += format(emptyCount);
449 }
450 if (i < V.size.x - 1) position += "/"; //separate rows
451 }
452 return position;
453 }
454
455 getTurnFen() {
456 return this.turn;
457 }
458
459 // Flags part of the FEN string
460 getFlagsFen() {
461 let flags = "";
462 // Castling flags
463 for (let c of ["w", "b"])
464 flags += this.castleFlags[c].map(V.CoordToColumn).join("");
465 return flags;
466 }
467
468 // Enpassant part of the FEN string
469 getEnpassantFen() {
470 const L = this.epSquares.length;
471 if (!this.epSquares[L - 1]) return "-"; //no en-passant
472 return V.CoordsToSquare(this.epSquares[L - 1]);
473 }
474
475 // Turn position fen into double array ["wb","wp","bk",...]
476 static GetBoard(position) {
477 const rows = position.split("/");
478 let board = ArrayFun.init(V.size.x, V.size.y, "");
479 for (let i = 0; i < rows.length; i++) {
480 let j = 0;
481 for (let indexInRow = 0; indexInRow < rows[i].length; indexInRow++) {
482 const character = rows[i][indexInRow];
483 const num = parseInt(character);
484 // If num is a number, just shift j:
485 if (!isNaN(num)) j += num;
486 // Else: something at position i,j
487 else board[i][j++] = V.fen2board(character);
488 }
489 }
490 return board;
491 }
492
493 // Extract (relevant) flags from fen
494 setFlags(fenflags) {
495 // white a-castle, h-castle, black a-castle, h-castle
496 this.castleFlags = { w: [-1, -1], b: [-1, -1] };
497 for (let i = 0; i < 4; i++) {
498 this.castleFlags[i < 2 ? "w" : "b"][i % 2] =
499 V.ColumnToCoord(fenflags.charAt(i));
500 }
501 }
502
503 //////////////////
504 // INITIALIZATION
505
506 // Fen string fully describes the game state
507 constructor(fen) {
508 if (!fen)
509 // In printDiagram() fen isn't supply because only getPpath() is used
510 // TODO: find a better solution!
511 return;
512 const fenParsed = V.ParseFen(fen);
513 this.board = V.GetBoard(fenParsed.position);
514 this.turn = fenParsed.turn;
515 this.movesCount = parseInt(fenParsed.movesCount);
516 this.setOtherVariables(fen);
517 }
518
519 // Scan board for kings positions
520 scanKings(fen) {
521 this.INIT_COL_KING = { w: -1, b: -1 };
522 // Squares of white and black king:
523 this.kingPos = { w: [-1, -1], b: [-1, -1] };
524 const fenRows = V.ParseFen(fen).position.split("/");
525 const startRow = { 'w': V.size.x - 1, 'b': 0 };
526 for (let i = 0; i < fenRows.length; i++) {
527 let k = 0; //column index on board
528 for (let j = 0; j < fenRows[i].length; j++) {
529 switch (fenRows[i].charAt(j)) {
530 case "k":
531 this.kingPos["b"] = [i, k];
532 this.INIT_COL_KING["b"] = k;
533 break;
534 case "K":
535 this.kingPos["w"] = [i, k];
536 this.INIT_COL_KING["w"] = k;
537 break;
538 default: {
539 const num = parseInt(fenRows[i].charAt(j));
540 if (!isNaN(num)) k += num - 1;
541 }
542 }
543 k++;
544 }
545 }
546 }
547
548 // Some additional variables from FEN (variant dependant)
549 setOtherVariables(fen) {
550 // Set flags and enpassant:
551 const parsedFen = V.ParseFen(fen);
552 if (V.HasFlags) this.setFlags(parsedFen.flags);
553 if (V.HasEnpassant) {
554 const epSq =
555 parsedFen.enpassant != "-"
556 ? this.getEpSquare(parsedFen.enpassant)
557 : undefined;
558 this.epSquares = [epSq];
559 }
560 // Search for kings positions:
561 this.scanKings(fen);
562 }
563
564 /////////////////////
565 // GETTERS & SETTERS
566
567 static get size() {
568 return { x: 8, y: 8 };
569 }
570
571 // Color of thing on square (i,j). 'undefined' if square is empty
572 getColor(i, j) {
573 return this.board[i][j].charAt(0);
574 }
575
576 // Piece type on square (i,j). 'undefined' if square is empty
577 getPiece(i, j) {
578 return this.board[i][j].charAt(1);
579 }
580
581 // Get opponent color
582 static GetOppCol(color) {
583 return color == "w" ? "b" : "w";
584 }
585
586 // Pieces codes (for a clearer code)
587 static get PAWN() {
588 return "p";
589 }
590 static get ROOK() {
591 return "r";
592 }
593 static get KNIGHT() {
594 return "n";
595 }
596 static get BISHOP() {
597 return "b";
598 }
599 static get QUEEN() {
600 return "q";
601 }
602 static get KING() {
603 return "k";
604 }
605
606 // For FEN checking:
607 static get PIECES() {
608 return [V.PAWN, V.ROOK, V.KNIGHT, V.BISHOP, V.QUEEN, V.KING];
609 }
610
611 // Empty square
612 static get EMPTY() {
613 return "";
614 }
615
616 // Some pieces movements
617 static get steps() {
618 return {
619 r: [
620 [-1, 0],
621 [1, 0],
622 [0, -1],
623 [0, 1]
624 ],
625 n: [
626 [-1, -2],
627 [-1, 2],
628 [1, -2],
629 [1, 2],
630 [-2, -1],
631 [-2, 1],
632 [2, -1],
633 [2, 1]
634 ],
635 b: [
636 [-1, -1],
637 [-1, 1],
638 [1, -1],
639 [1, 1]
640 ]
641 };
642 }
643
644 ////////////////////
645 // MOVES GENERATION
646
647 // All possible moves from selected square
648 getPotentialMovesFrom([x, y]) {
649 switch (this.getPiece(x, y)) {
650 case V.PAWN:
651 return this.getPotentialPawnMoves([x, y]);
652 case V.ROOK:
653 return this.getPotentialRookMoves([x, y]);
654 case V.KNIGHT:
655 return this.getPotentialKnightMoves([x, y]);
656 case V.BISHOP:
657 return this.getPotentialBishopMoves([x, y]);
658 case V.QUEEN:
659 return this.getPotentialQueenMoves([x, y]);
660 case V.KING:
661 return this.getPotentialKingMoves([x, y]);
662 }
663 return []; //never reached
664 }
665
666 // Build a regular move from its initial and destination squares.
667 // tr: transformation
668 getBasicMove([sx, sy], [ex, ey], tr) {
669 const initColor = this.getColor(sx, sy);
670 const initPiece = this.getPiece(sx, sy);
671 let mv = new Move({
672 appear: [
673 new PiPo({
674 x: ex,
675 y: ey,
676 c: tr ? tr.c : initColor,
677 p: tr ? tr.p : initPiece
678 })
679 ],
680 vanish: [
681 new PiPo({
682 x: sx,
683 y: sy,
684 c: initColor,
685 p: initPiece
686 })
687 ]
688 });
689
690 // The opponent piece disappears if we take it
691 if (this.board[ex][ey] != V.EMPTY) {
692 mv.vanish.push(
693 new PiPo({
694 x: ex,
695 y: ey,
696 c: this.getColor(ex, ey),
697 p: this.getPiece(ex, ey)
698 })
699 );
700 }
701
702 return mv;
703 }
704
705 // Generic method to find possible moves of non-pawn pieces:
706 // "sliding or jumping"
707 getSlideNJumpMoves([x, y], steps, oneStep) {
708 let moves = [];
709 outerLoop: for (let step of steps) {
710 let i = x + step[0];
711 let j = y + step[1];
712 while (V.OnBoard(i, j) && this.board[i][j] == V.EMPTY) {
713 moves.push(this.getBasicMove([x, y], [i, j]));
714 if (oneStep) continue outerLoop;
715 i += step[0];
716 j += step[1];
717 }
718 if (V.OnBoard(i, j) && this.canTake([x, y], [i, j]))
719 moves.push(this.getBasicMove([x, y], [i, j]));
720 }
721 return moves;
722 }
723
724 // Special case of en-passant captures: treated separately
725 getEnpassantCaptures([x, y], shiftX) {
726 const Lep = this.epSquares.length;
727 const epSquare = this.epSquares[Lep - 1]; //always at least one element
728 let enpassantMove = null;
729 if (
730 !!epSquare &&
731 epSquare.x == x + shiftX &&
732 Math.abs(epSquare.y - y) == 1
733 ) {
734 enpassantMove = this.getBasicMove([x, y], [epSquare.x, epSquare.y]);
735 enpassantMove.vanish.push({
736 x: x,
737 y: epSquare.y,
738 // Captured piece is usually a pawn, but next line seems harmless
739 p: this.getPiece(x, epSquare.y),
740 c: this.getColor(x, epSquare.y)
741 });
742 }
743 return !!enpassantMove ? [enpassantMove] : [];
744 }
745
746 // Consider all potential promotions:
747 addPawnMoves([x1, y1], [x2, y2], moves, promotions) {
748 let finalPieces = [V.PAWN];
749 const color = this.turn; //this.getColor(x1, y1);
750 const lastRank = (color == "w" ? 0 : V.size.x - 1);
751 if (x2 == lastRank) {
752 // promotions arg: special override for Hiddenqueen variant
753 if (!!promotions) finalPieces = promotions;
754 else if (!!V.PawnSpecs.promotions) finalPieces = V.PawnSpecs.promotions;
755 }
756 let tr = null;
757 for (let piece of finalPieces) {
758 tr = (piece != V.PAWN ? { c: color, p: piece } : null);
759 moves.push(this.getBasicMove([x1, y1], [x2, y2], tr));
760 }
761 }
762
763 // What are the pawn moves from square x,y ?
764 getPotentialPawnMoves([x, y], promotions) {
765 const color = this.turn; //this.getColor(x, y);
766 const [sizeX, sizeY] = [V.size.x, V.size.y];
767 const pawnShiftX = V.PawnSpecs.directions[color];
768 const firstRank = (color == "w" ? sizeX - 1 : 0);
769
770 // Pawn movements in shiftX direction:
771 const getPawnMoves = (shiftX) => {
772 let moves = [];
773 // NOTE: next condition is generally true (no pawn on last rank)
774 if (x + shiftX >= 0 && x + shiftX < sizeX) {
775 if (this.board[x + shiftX][y] == V.EMPTY) {
776 // One square forward
777 this.addPawnMoves([x, y], [x + shiftX, y], moves, promotions);
778 // Next condition because pawns on 1st rank can generally jump
779 if (
780 V.PawnSpecs.twoSquares &&
781 (
782 (color == 'w' && x >= V.size.x - 1 - V.PawnSpecs.initShift['w'])
783 ||
784 (color == 'b' && x <= V.PawnSpecs.initShift['b'])
785 )
786 ) {
787 if (this.board[x + 2 * shiftX][y] == V.EMPTY) {
788 // Two squares jump
789 moves.push(this.getBasicMove([x, y], [x + 2 * shiftX, y]));
790 if (
791 V.PawnSpecs.threeSquares &&
792 this.board[x + 3 * shiftX][y] == V.EMPTY
793 ) {
794 // Three squares jump
795 moves.push(this.getBasicMove([x, y], [x + 3 * shiftX, y]));
796 }
797 }
798 }
799 }
800 // Captures
801 if (V.PawnSpecs.canCapture) {
802 for (let shiftY of [-1, 1]) {
803 if (y + shiftY >= 0 && y + shiftY < sizeY) {
804 if (
805 this.board[x + shiftX][y + shiftY] != V.EMPTY &&
806 this.canTake([x, y], [x + shiftX, y + shiftY])
807 ) {
808 this.addPawnMoves(
809 [x, y], [x + shiftX, y + shiftY],
810 moves, promotions
811 );
812 }
813 if (
814 V.PawnSpecs.captureBackward &&
815 x - shiftX >= 0 && x - shiftX < V.size.x &&
816 this.board[x - shiftX][y + shiftY] != V.EMPTY &&
817 this.canTake([x, y], [x - shiftX, y + shiftY])
818 ) {
819 this.addPawnMoves(
820 [x, y], [x + shiftX, y + shiftY],
821 moves, promotions
822 );
823 }
824 }
825 }
826 }
827 }
828 return moves;
829 }
830
831 let pMoves = getPawnMoves(pawnShiftX);
832 if (V.PawnSpecs.bidirectional)
833 pMoves = pMoves.concat(getPawnMoves(-pawnShiftX));
834
835 if (V.HasEnpassant) {
836 // NOTE: backward en-passant captures are not considered
837 // because no rules define them (for now).
838 Array.prototype.push.apply(
839 pMoves,
840 this.getEnpassantCaptures([x, y], pawnShiftX)
841 );
842 }
843
844 return pMoves;
845 }
846
847 // What are the rook moves from square x,y ?
848 getPotentialRookMoves(sq) {
849 return this.getSlideNJumpMoves(sq, V.steps[V.ROOK]);
850 }
851
852 // What are the knight moves from square x,y ?
853 getPotentialKnightMoves(sq) {
854 return this.getSlideNJumpMoves(sq, V.steps[V.KNIGHT], "oneStep");
855 }
856
857 // What are the bishop moves from square x,y ?
858 getPotentialBishopMoves(sq) {
859 return this.getSlideNJumpMoves(sq, V.steps[V.BISHOP]);
860 }
861
862 // What are the queen moves from square x,y ?
863 getPotentialQueenMoves(sq) {
864 return this.getSlideNJumpMoves(
865 sq,
866 V.steps[V.ROOK].concat(V.steps[V.BISHOP])
867 );
868 }
869
870 // What are the king moves from square x,y ?
871 getPotentialKingMoves(sq) {
872 // Initialize with normal moves
873 let moves = this.getSlideNJumpMoves(
874 sq,
875 V.steps[V.ROOK].concat(V.steps[V.BISHOP]),
876 "oneStep"
877 );
878 if (V.HasCastle) moves = moves.concat(this.getCastleMoves(sq));
879 return moves;
880 }
881
882 // "castleInCheck" arg to let some variants castle under check
883 getCastleMoves([x, y], castleInCheck) {
884 const c = this.getColor(x, y);
885 if (x != (c == "w" ? V.size.x - 1 : 0) || y != this.INIT_COL_KING[c])
886 return []; //x isn't first rank, or king has moved (shortcut)
887
888 // Castling ?
889 const oppCol = V.GetOppCol(c);
890 let moves = [];
891 let i = 0;
892 // King, then rook:
893 const finalSquares = [
894 [2, 3],
895 [V.size.y - 2, V.size.y - 3]
896 ];
897 castlingCheck: for (
898 let castleSide = 0;
899 castleSide < 2;
900 castleSide++ //large, then small
901 ) {
902 if (this.castleFlags[c][castleSide] >= V.size.y) continue;
903 // If this code is reached, rook and king are on initial position
904
905 // NOTE: in some variants this is not a rook
906 const rookPos = this.castleFlags[c][castleSide];
907 if (this.board[x][rookPos] == V.EMPTY || this.getColor(x, rookPos) != c)
908 // Rook is not here, or changed color (see Benedict)
909 continue;
910
911 // Nothing on the path of the king ? (and no checks)
912 const castlingPiece = this.getPiece(x, rookPos);
913 const finDist = finalSquares[castleSide][0] - y;
914 let step = finDist / Math.max(1, Math.abs(finDist));
915 i = y;
916 do {
917 if (
918 // NOTE: "castling" arg is used by some variants (Monster),
919 // where "isAttacked" is overloaded in an infinite-recursive way.
920 // TODO: not used anymore (Monster + Doublemove2 are simplified).
921 (!castleInCheck && this.isAttacked([x, i], oppCol, "castling")) ||
922 (this.board[x][i] != V.EMPTY &&
923 // NOTE: next check is enough, because of chessboard constraints
924 (this.getColor(x, i) != c ||
925 ![V.KING, castlingPiece].includes(this.getPiece(x, i))))
926 ) {
927 continue castlingCheck;
928 }
929 i += step;
930 } while (i != finalSquares[castleSide][0]);
931
932 // Nothing on the path to the rook?
933 step = castleSide == 0 ? -1 : 1;
934 for (i = y + step; i != rookPos; i += step) {
935 if (this.board[x][i] != V.EMPTY) continue castlingCheck;
936 }
937
938 // Nothing on final squares, except maybe king and castling rook?
939 for (i = 0; i < 2; i++) {
940 if (
941 finalSquares[castleSide][i] != rookPos &&
942 this.board[x][finalSquares[castleSide][i]] != V.EMPTY &&
943 (
944 this.getPiece(x, finalSquares[castleSide][i]) != V.KING ||
945 this.getColor(x, finalSquares[castleSide][i]) != c
946 )
947 ) {
948 continue castlingCheck;
949 }
950 }
951
952 // If this code is reached, castle is valid
953 moves.push(
954 new Move({
955 appear: [
956 new PiPo({
957 x: x,
958 y: finalSquares[castleSide][0],
959 p: V.KING,
960 c: c
961 }),
962 new PiPo({
963 x: x,
964 y: finalSquares[castleSide][1],
965 p: castlingPiece,
966 c: c
967 })
968 ],
969 vanish: [
970 new PiPo({ x: x, y: y, p: V.KING, c: c }),
971 new PiPo({ x: x, y: rookPos, p: castlingPiece, c: c })
972 ],
973 end:
974 Math.abs(y - rookPos) <= 2
975 ? { x: x, y: rookPos }
976 : { x: x, y: y + 2 * (castleSide == 0 ? -1 : 1) }
977 })
978 );
979 }
980
981 return moves;
982 }
983
984 ////////////////////
985 // MOVES VALIDATION
986
987 // For the interface: possible moves for the current turn from square sq
988 getPossibleMovesFrom(sq) {
989 return this.filterValid(this.getPotentialMovesFrom(sq));
990 }
991
992 // TODO: promotions (into R,B,N,Q) should be filtered only once
993 filterValid(moves) {
994 if (moves.length == 0) return [];
995 const color = this.turn;
996 return moves.filter(m => {
997 this.play(m);
998 const res = !this.underCheck(color);
999 this.undo(m);
1000 return res;
1001 });
1002 }
1003
1004 getAllPotentialMoves() {
1005 const color = this.turn;
1006 let potentialMoves = [];
1007 for (let i = 0; i < V.size.x; i++) {
1008 for (let j = 0; j < V.size.y; j++) {
1009 if (this.board[i][j] != V.EMPTY && this.getColor(i, j) == color) {
1010 Array.prototype.push.apply(
1011 potentialMoves,
1012 this.getPotentialMovesFrom([i, j])
1013 );
1014 }
1015 }
1016 }
1017 return potentialMoves;
1018 }
1019
1020 // Search for all valid moves considering current turn
1021 // (for engine and game end)
1022 getAllValidMoves() {
1023 return this.filterValid(this.getAllPotentialMoves());
1024 }
1025
1026 // Stop at the first move found
1027 // TODO: not really, it explores all moves from a square (one is enough).
1028 atLeastOneMove() {
1029 const color = this.turn;
1030 for (let i = 0; i < V.size.x; i++) {
1031 for (let j = 0; j < V.size.y; j++) {
1032 if (this.board[i][j] != V.EMPTY && this.getColor(i, j) == color) {
1033 const moves = this.getPotentialMovesFrom([i, j]);
1034 if (moves.length > 0) {
1035 for (let k = 0; k < moves.length; k++)
1036 if (this.filterValid([moves[k]]).length > 0) return true;
1037 }
1038 }
1039 }
1040 }
1041 return false;
1042 }
1043
1044 // Check if pieces of given color are attacking (king) on square x,y
1045 isAttacked(sq, color) {
1046 return (
1047 this.isAttackedByPawn(sq, color) ||
1048 this.isAttackedByRook(sq, color) ||
1049 this.isAttackedByKnight(sq, color) ||
1050 this.isAttackedByBishop(sq, color) ||
1051 this.isAttackedByQueen(sq, color) ||
1052 this.isAttackedByKing(sq, color)
1053 );
1054 }
1055
1056 // Generic method for non-pawn pieces ("sliding or jumping"):
1057 // is x,y attacked by a piece of given color ?
1058 isAttackedBySlideNJump([x, y], color, piece, steps, oneStep) {
1059 for (let step of steps) {
1060 let rx = x + step[0],
1061 ry = y + step[1];
1062 while (V.OnBoard(rx, ry) && this.board[rx][ry] == V.EMPTY && !oneStep) {
1063 rx += step[0];
1064 ry += step[1];
1065 }
1066 if (
1067 V.OnBoard(rx, ry) &&
1068 this.getPiece(rx, ry) == piece &&
1069 this.getColor(rx, ry) == color
1070 ) {
1071 return true;
1072 }
1073 }
1074 return false;
1075 }
1076
1077 // Is square x,y attacked by 'color' pawns ?
1078 isAttackedByPawn(sq, color) {
1079 const pawnShift = (color == "w" ? 1 : -1);
1080 return this.isAttackedBySlideNJump(
1081 sq,
1082 color,
1083 V.PAWN,
1084 [[pawnShift, 1], [pawnShift, -1]],
1085 "oneStep"
1086 );
1087 }
1088
1089 // Is square x,y attacked by 'color' rooks ?
1090 isAttackedByRook(sq, color) {
1091 return this.isAttackedBySlideNJump(sq, color, V.ROOK, V.steps[V.ROOK]);
1092 }
1093
1094 // Is square x,y attacked by 'color' knights ?
1095 isAttackedByKnight(sq, color) {
1096 return this.isAttackedBySlideNJump(
1097 sq,
1098 color,
1099 V.KNIGHT,
1100 V.steps[V.KNIGHT],
1101 "oneStep"
1102 );
1103 }
1104
1105 // Is square x,y attacked by 'color' bishops ?
1106 isAttackedByBishop(sq, color) {
1107 return this.isAttackedBySlideNJump(sq, color, V.BISHOP, V.steps[V.BISHOP]);
1108 }
1109
1110 // Is square x,y attacked by 'color' queens ?
1111 isAttackedByQueen(sq, color) {
1112 return this.isAttackedBySlideNJump(
1113 sq,
1114 color,
1115 V.QUEEN,
1116 V.steps[V.ROOK].concat(V.steps[V.BISHOP])
1117 );
1118 }
1119
1120 // Is square x,y attacked by 'color' king(s) ?
1121 isAttackedByKing(sq, color) {
1122 return this.isAttackedBySlideNJump(
1123 sq,
1124 color,
1125 V.KING,
1126 V.steps[V.ROOK].concat(V.steps[V.BISHOP]),
1127 "oneStep"
1128 );
1129 }
1130
1131 // Is color under check after his move ?
1132 underCheck(color) {
1133 return this.isAttacked(this.kingPos[color], V.GetOppCol(color));
1134 }
1135
1136 /////////////////
1137 // MOVES PLAYING
1138
1139 // Apply a move on board
1140 static PlayOnBoard(board, move) {
1141 for (let psq of move.vanish) board[psq.x][psq.y] = V.EMPTY;
1142 for (let psq of move.appear) board[psq.x][psq.y] = psq.c + psq.p;
1143 }
1144 // Un-apply the played move
1145 static UndoOnBoard(board, move) {
1146 for (let psq of move.appear) board[psq.x][psq.y] = V.EMPTY;
1147 for (let psq of move.vanish) board[psq.x][psq.y] = psq.c + psq.p;
1148 }
1149
1150 prePlay() {}
1151
1152 play(move) {
1153 // DEBUG:
1154 // if (!this.states) this.states = [];
1155 // const stateFen = this.getFen() + JSON.stringify(this.kingPos);
1156 // this.states.push(stateFen);
1157
1158 this.prePlay(move);
1159 // Save flags (for undo)
1160 if (V.HasFlags) move.flags = JSON.stringify(this.aggregateFlags());
1161 if (V.HasEnpassant) this.epSquares.push(this.getEpSquare(move));
1162 V.PlayOnBoard(this.board, move);
1163 this.turn = V.GetOppCol(this.turn);
1164 this.movesCount++;
1165 this.postPlay(move);
1166 }
1167
1168 updateCastleFlags(move, piece) {
1169 const c = V.GetOppCol(this.turn);
1170 const firstRank = (c == "w" ? V.size.x - 1 : 0);
1171 // Update castling flags if rooks are moved
1172 const oppCol = this.turn;
1173 const oppFirstRank = V.size.x - 1 - firstRank;
1174 if (piece == V.KING && move.appear.length > 0)
1175 this.castleFlags[c] = [V.size.y, V.size.y];
1176 else if (
1177 move.start.x == firstRank && //our rook moves?
1178 this.castleFlags[c].includes(move.start.y)
1179 ) {
1180 const flagIdx = (move.start.y == this.castleFlags[c][0] ? 0 : 1);
1181 this.castleFlags[c][flagIdx] = V.size.y;
1182 }
1183 // NOTE: not "else if" because a rook could take an opposing rook
1184 if (
1185 move.end.x == oppFirstRank && //we took opponent rook?
1186 this.castleFlags[oppCol].includes(move.end.y)
1187 ) {
1188 const flagIdx = (move.end.y == this.castleFlags[oppCol][0] ? 0 : 1);
1189 this.castleFlags[oppCol][flagIdx] = V.size.y;
1190 }
1191 }
1192
1193 // After move is played, update variables + flags
1194 postPlay(move) {
1195 const c = V.GetOppCol(this.turn);
1196 let piece = undefined;
1197 if (move.vanish.length >= 1)
1198 // Usual case, something is moved
1199 piece = move.vanish[0].p;
1200 else
1201 // Crazyhouse-like variants
1202 piece = move.appear[0].p;
1203
1204 // Update king position + flags
1205 if (piece == V.KING && move.appear.length > 0)
1206 this.kingPos[c] = [move.appear[0].x, move.appear[0].y];
1207 if (V.HasCastle) this.updateCastleFlags(move, piece);
1208 }
1209
1210 preUndo() {}
1211
1212 undo(move) {
1213 this.preUndo(move);
1214 if (V.HasEnpassant) this.epSquares.pop();
1215 if (V.HasFlags) this.disaggregateFlags(JSON.parse(move.flags));
1216 V.UndoOnBoard(this.board, move);
1217 this.turn = V.GetOppCol(this.turn);
1218 this.movesCount--;
1219 this.postUndo(move);
1220
1221 // DEBUG:
1222 // const stateFen = this.getFen() + JSON.stringify(this.kingPos);
1223 // if (stateFen != this.states[this.states.length-1]) debugger;
1224 // this.states.pop();
1225 }
1226
1227 // After move is undo-ed *and flags resetted*, un-update other variables
1228 // TODO: more symmetry, by storing flags increment in move (?!)
1229 postUndo(move) {
1230 // (Potentially) Reset king position
1231 const c = this.getColor(move.start.x, move.start.y);
1232 if (this.getPiece(move.start.x, move.start.y) == V.KING)
1233 this.kingPos[c] = [move.start.x, move.start.y];
1234 }
1235
1236 ///////////////
1237 // END OF GAME
1238
1239 // What is the score ? (Interesting if game is over)
1240 getCurrentScore() {
1241 if (this.atLeastOneMove()) return "*";
1242 // Game over
1243 const color = this.turn;
1244 // No valid move: stalemate or checkmate?
1245 if (!this.underCheck(color)) return "1/2";
1246 // OK, checkmate
1247 return (color == "w" ? "0-1" : "1-0");
1248 }
1249
1250 ///////////////
1251 // ENGINE PLAY
1252
1253 // Pieces values
1254 static get VALUES() {
1255 return {
1256 p: 1,
1257 r: 5,
1258 n: 3,
1259 b: 3,
1260 q: 9,
1261 k: 1000
1262 };
1263 }
1264
1265 // "Checkmate" (unreachable eval)
1266 static get INFINITY() {
1267 return 9999;
1268 }
1269
1270 // At this value or above, the game is over
1271 static get THRESHOLD_MATE() {
1272 return V.INFINITY;
1273 }
1274
1275 // Search depth: 1,2 for e.g. higher branching factor, 4 for smaller
1276 static get SEARCH_DEPTH() {
1277 return 3;
1278 }
1279
1280 // 'movesList' arg for some variants to provide a custom list
1281 getComputerMove(movesList) {
1282 const maxeval = V.INFINITY;
1283 const color = this.turn;
1284 let moves1 = movesList || this.getAllValidMoves();
1285
1286 if (moves1.length == 0)
1287 // TODO: this situation should not happen
1288 return null;
1289
1290 // Rank moves using a min-max at depth 2 (if search_depth >= 2!)
1291 for (let i = 0; i < moves1.length; i++) {
1292 this.play(moves1[i]);
1293 const score1 = this.getCurrentScore();
1294 if (score1 != "*") {
1295 moves1[i].eval =
1296 score1 == "1/2"
1297 ? 0
1298 : (score1 == "1-0" ? 1 : -1) * maxeval;
1299 }
1300 if (V.SEARCH_DEPTH == 1 || score1 != "*") {
1301 if (!moves1[i].eval) moves1[i].eval = this.evalPosition();
1302 this.undo(moves1[i]);
1303 continue;
1304 }
1305 // Initial self evaluation is very low: "I'm checkmated"
1306 moves1[i].eval = (color == "w" ? -1 : 1) * maxeval;
1307 // Initial enemy evaluation is very low too, for him
1308 let eval2 = (color == "w" ? 1 : -1) * maxeval;
1309 // Second half-move:
1310 let moves2 = this.getAllValidMoves();
1311 for (let j = 0; j < moves2.length; j++) {
1312 this.play(moves2[j]);
1313 const score2 = this.getCurrentScore();
1314 let evalPos = 0; //1/2 value
1315 switch (score2) {
1316 case "*":
1317 evalPos = this.evalPosition();
1318 break;
1319 case "1-0":
1320 evalPos = maxeval;
1321 break;
1322 case "0-1":
1323 evalPos = -maxeval;
1324 break;
1325 }
1326 if (
1327 (color == "w" && evalPos < eval2) ||
1328 (color == "b" && evalPos > eval2)
1329 ) {
1330 eval2 = evalPos;
1331 }
1332 this.undo(moves2[j]);
1333 }
1334 if (
1335 (color == "w" && eval2 > moves1[i].eval) ||
1336 (color == "b" && eval2 < moves1[i].eval)
1337 ) {
1338 moves1[i].eval = eval2;
1339 }
1340 this.undo(moves1[i]);
1341 }
1342 moves1.sort((a, b) => {
1343 return (color == "w" ? 1 : -1) * (b.eval - a.eval);
1344 });
1345 // console.log(moves1.map(m => { return [this.getNotation(m), m.eval]; }));
1346
1347 // Skip depth 3+ if we found a checkmate (or if we are checkmated in 1...)
1348 if (V.SEARCH_DEPTH >= 3 && Math.abs(moves1[0].eval) < V.THRESHOLD_MATE) {
1349 for (let i = 0; i < moves1.length; i++) {
1350 this.play(moves1[i]);
1351 // 0.1 * oldEval : heuristic to avoid some bad moves (not all...)
1352 moves1[i].eval =
1353 0.1 * moves1[i].eval +
1354 this.alphabeta(V.SEARCH_DEPTH - 1, -maxeval, maxeval);
1355 this.undo(moves1[i]);
1356 }
1357 moves1.sort((a, b) => {
1358 return (color == "w" ? 1 : -1) * (b.eval - a.eval);
1359 });
1360 }
1361
1362 let candidates = [0];
1363 for (let i = 1; i < moves1.length && moves1[i].eval == moves1[0].eval; i++)
1364 candidates.push(i);
1365 return moves1[candidates[randInt(candidates.length)]];
1366 }
1367
1368 alphabeta(depth, alpha, beta) {
1369 const maxeval = V.INFINITY;
1370 const color = this.turn;
1371 const score = this.getCurrentScore();
1372 if (score != "*")
1373 return score == "1/2" ? 0 : (score == "1-0" ? 1 : -1) * maxeval;
1374 if (depth == 0) return this.evalPosition();
1375 const moves = this.getAllValidMoves();
1376 let v = color == "w" ? -maxeval : maxeval;
1377 if (color == "w") {
1378 for (let i = 0; i < moves.length; i++) {
1379 this.play(moves[i]);
1380 v = Math.max(v, this.alphabeta(depth - 1, alpha, beta));
1381 this.undo(moves[i]);
1382 alpha = Math.max(alpha, v);
1383 if (alpha >= beta) break; //beta cutoff
1384 }
1385 }
1386 else {
1387 // color=="b"
1388 for (let i = 0; i < moves.length; i++) {
1389 this.play(moves[i]);
1390 v = Math.min(v, this.alphabeta(depth - 1, alpha, beta));
1391 this.undo(moves[i]);
1392 beta = Math.min(beta, v);
1393 if (alpha >= beta) break; //alpha cutoff
1394 }
1395 }
1396 return v;
1397 }
1398
1399 evalPosition() {
1400 let evaluation = 0;
1401 // Just count material for now
1402 for (let i = 0; i < V.size.x; i++) {
1403 for (let j = 0; j < V.size.y; j++) {
1404 if (this.board[i][j] != V.EMPTY) {
1405 const sign = this.getColor(i, j) == "w" ? 1 : -1;
1406 evaluation += sign * V.VALUES[this.getPiece(i, j)];
1407 }
1408 }
1409 }
1410 return evaluation;
1411 }
1412
1413 /////////////////////////
1414 // MOVES + GAME NOTATION
1415 /////////////////////////
1416
1417 // Context: just before move is played, turn hasn't changed
1418 // TODO: un-ambiguous notation (switch on piece type, check directions...)
1419 getNotation(move) {
1420 if (move.appear.length == 2 && move.appear[0].p == V.KING)
1421 // Castle
1422 return move.end.y < move.start.y ? "0-0-0" : "0-0";
1423
1424 // Translate final square
1425 const finalSquare = V.CoordsToSquare(move.end);
1426
1427 const piece = this.getPiece(move.start.x, move.start.y);
1428 if (piece == V.PAWN) {
1429 // Pawn move
1430 let notation = "";
1431 if (move.vanish.length > move.appear.length) {
1432 // Capture
1433 const startColumn = V.CoordToColumn(move.start.y);
1434 notation = startColumn + "x" + finalSquare;
1435 }
1436 else notation = finalSquare;
1437 if (move.appear.length > 0 && move.appear[0].p != V.PAWN)
1438 // Promotion
1439 notation += "=" + move.appear[0].p.toUpperCase();
1440 return notation;
1441 }
1442 // Piece movement
1443 return (
1444 piece.toUpperCase() +
1445 (move.vanish.length > move.appear.length ? "x" : "") +
1446 finalSquare
1447 );
1448 }
1449
1450 static GetUnambiguousNotation(move) {
1451 // Machine-readable format with all the informations about the move
1452 return (
1453 (!!move.start && V.OnBoard(move.start.x, move.start.y)
1454 ? V.CoordsToSquare(move.start)
1455 : "-"
1456 ) + "." +
1457 (!!move.end && V.OnBoard(move.end.x, move.end.y)
1458 ? V.CoordsToSquare(move.end)
1459 : "-"
1460 ) + " " +
1461 (!!move.appear && move.appear.length > 0
1462 ? move.appear.map(a =>
1463 a.c + a.p + V.CoordsToSquare({ x: a.x, y: a.y })).join(".")
1464 : "-"
1465 ) + "/" +
1466 (!!move.vanish && move.vanish.length > 0
1467 ? move.vanish.map(a =>
1468 a.c + a.p + V.CoordsToSquare({ x: a.x, y: a.y })).join(".")
1469 : "-"
1470 )
1471 );
1472 }
1473 };