107b4126a13efba955cd803b61a21ae43b7fe94a
1 import { Random
} from "/utils/alea.js";
2 import { ArrayFun
} from "/utils/array.js";
3 import PiPo
from "/utils/PiPo.js";
4 import Move
from "/utils/Move.js";
6 // Helper class for move animation
9 constructor(callOnComplete
) {
12 this.callOnComplete
= callOnComplete
;
15 if (++this.value
== this.target
)
16 this.callOnComplete();
21 // NOTE: x coords: top to bottom (white perspective); y: left to right
22 // NOTE: ChessRules is aliased as window.C, and variants as window.V
23 export default class ChessRules
{
25 static get Aliases() {
26 return {'C': ChessRules
};
29 /////////////////////////
30 // VARIANT SPECIFICATIONS
32 // Some variants have specific options, like the number of pawns in Monster,
33 // or the board size for Pandemonium.
34 // Users can generally select a randomness level from 0 to 2.
35 static get Options() {
39 variable: "randomness",
42 {label: "Deterministic", value: 0},
43 {label: "Symmetric random", value: 1},
44 {label: "Asymmetric random", value: 2}
49 label: "Capture king",
55 label: "Falling pawn",
61 // Game modifiers (using "elementary variants"). Default: false
64 "balance", //takes precedence over doublemove & progressive
68 "cylinder", //ok with all
72 "progressive", //(natural) priority over doublemove
81 get pawnPromotions() {
82 return ['q', 'r', 'n', 'b'];
85 // Some variants don't have flags:
94 // En-passant captures allowed?
101 !!this.options
["crazyhouse"] ||
102 (!!this.options
["recycle"] && !this.options
["teleport"])
105 // Some variants do not store reserve state (Align4, Chakart...)
106 get hasReserveFen() {
107 return this.hasReserve
;
111 return !!this.options
["dark"];
114 // Some variants use click infos:
116 if (typeof coords
.x
!= "number")
117 return null; //click on reserves
119 this.options
["teleport"] && this.subTurnTeleport
== 2 &&
120 this.board
[coords
.x
][coords
.y
] == ""
123 start: {x: this.captured
.x
, y: this.captured
.y
},
128 c: this.captured
.c
, //this.turn,
134 res
.drag
= {c: this.captured
.c
, p: this.captured
.p
};
143 // 3a --> {x:3, y:10}
144 static SquareToCoords(sq
) {
145 return ArrayFun
.toObject(["x", "y"],
146 [0, 1].map(i
=> parseInt(sq
[i
], 36)));
149 // {x:11, y:12} --> bc
150 static CoordsToSquare(cd
) {
151 return Object
.values(cd
).map(c
=> c
.toString(36)).join("");
155 if (typeof cd
.x
== "number") {
157 `${this.containerId}|sq-${cd.x.toString(36)}-${cd.y.toString(36)}`
161 return `${this.containerId}|rsq-${cd.x}-${cd.y}`;
164 idToCoords(targetId
) {
166 return null; //outside page, maybe...
167 const idParts
= targetId
.split('|'); //prefix|sq-2-3 (start at 0 => 3,4)
169 idParts
.length
< 2 ||
170 idParts
[0] != this.containerId
||
171 !idParts
[1].match(/sq-[0-9a-zA-Z]-[0-9a-zA-Z]/)
175 const squares
= idParts
[1].split('-');
176 if (squares
[0] == "sq")
177 return {x: parseInt(squares
[1], 36), y: parseInt(squares
[2], 36)};
178 // squares[0] == "rsq" : reserve, 'c' + 'p' (letters color & piece)
179 return {x: squares
[1], y: squares
[2]};
185 // Turn "wb" into "B" (for FEN)
187 return (b
[0] == "w" ? b
[1].toUpperCase() : b
[1]);
190 // Turn "p" into "bp" (for board)
192 return (f
.charCodeAt(0) <= 90 ? "w" + f
.toLowerCase() : "b" + f
);
195 // Setup the initial random-or-not (asymmetric-or-not) position
196 genRandInitFen(seed
) {
197 let fen
, flags
= "0707";
198 if (!this.options
.randomness
)
200 fen
= "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w 0";
204 Random
.setSeed(seed
);
205 let pieces
= {w: new Array(8), b: new Array(8)};
207 // Shuffle pieces on first (and last rank if randomness == 2)
208 for (let c
of ["w", "b"]) {
209 if (c
== 'b' && this.options
.randomness
== 1) {
210 pieces
['b'] = pieces
['w'];
215 let positions
= ArrayFun
.range(8);
217 // Get random squares for bishops
218 let randIndex
= 2 * Random
.randInt(4);
219 const bishop1Pos
= positions
[randIndex
];
220 // The second bishop must be on a square of different color
221 let randIndex_tmp
= 2 * Random
.randInt(4) + 1;
222 const bishop2Pos
= positions
[randIndex_tmp
];
223 // Remove chosen squares
224 positions
.splice(Math
.max(randIndex
, randIndex_tmp
), 1);
225 positions
.splice(Math
.min(randIndex
, randIndex_tmp
), 1);
227 // Get random squares for knights
228 randIndex
= Random
.randInt(6);
229 const knight1Pos
= positions
[randIndex
];
230 positions
.splice(randIndex
, 1);
231 randIndex
= Random
.randInt(5);
232 const knight2Pos
= positions
[randIndex
];
233 positions
.splice(randIndex
, 1);
235 // Get random square for queen
236 randIndex
= Random
.randInt(4);
237 const queenPos
= positions
[randIndex
];
238 positions
.splice(randIndex
, 1);
240 // Rooks and king positions are now fixed,
241 // because of the ordering rook-king-rook
242 const rook1Pos
= positions
[0];
243 const kingPos
= positions
[1];
244 const rook2Pos
= positions
[2];
246 // Finally put the shuffled pieces in the board array
247 pieces
[c
][rook1Pos
] = "r";
248 pieces
[c
][knight1Pos
] = "n";
249 pieces
[c
][bishop1Pos
] = "b";
250 pieces
[c
][queenPos
] = "q";
251 pieces
[c
][kingPos
] = "k";
252 pieces
[c
][bishop2Pos
] = "b";
253 pieces
[c
][knight2Pos
] = "n";
254 pieces
[c
][rook2Pos
] = "r";
255 flags
+= rook1Pos
.toString() + rook2Pos
.toString();
258 pieces
["b"].join("") +
259 "/pppppppp/8/8/8/8/PPPPPPPP/" +
260 pieces
["w"].join("").toUpperCase() +
264 // Add turn + flags + enpassant (+ reserve)
267 parts
.push(`"flags":"${flags}"`);
268 if (this.hasEnpassant
)
269 parts
.push('"enpassant":"-"');
270 if (this.hasReserveFen
)
271 parts
.push('"reserve":"000000000000"');
272 if (this.options
["crazyhouse"])
273 parts
.push('"ispawn":"-"');
274 if (parts
.length
>= 1)
275 fen
+= " {" + parts
.join(",") + "}";
279 // "Parse" FEN: just return untransformed string data
281 const fenParts
= fen
.split(" ");
283 position: fenParts
[0],
285 movesCount: fenParts
[2]
287 if (fenParts
.length
> 3)
288 res
= Object
.assign(res
, JSON
.parse(fenParts
[3]));
292 // Return current fen (game state)
295 this.getPosition() + " " +
296 this.getTurnFen() + " " +
301 parts
.push(`"flags":"${this.getFlagsFen()}"`);
302 if (this.hasEnpassant
)
303 parts
.push(`"enpassant":"${this.getEnpassantFen()}"`);
304 if (this.hasReserveFen
)
305 parts
.push(`"reserve":"${this.getReserveFen()}"`);
306 if (this.options
["crazyhouse"])
307 parts
.push(`"ispawn":"${this.getIspawnFen()}"`);
308 if (parts
.length
>= 1)
309 fen
+= " {" + parts
.join(",") + "}";
313 static FenEmptySquares(count
) {
314 // if more than 9 consecutive free spaces, break the integer,
315 // otherwise FEN parsing will fail.
318 // Most boards of size < 18:
320 return "9" + (count
- 9);
322 return "99" + (count
- 18);
325 // Position part of the FEN string
328 for (let i
= 0; i
< this.size
.y
; i
++) {
330 for (let j
= 0; j
< this.size
.x
; j
++) {
331 if (this.board
[i
][j
] == "")
334 if (emptyCount
> 0) {
335 // Add empty squares in-between
336 position
+= C
.FenEmptySquares(emptyCount
);
339 position
+= this.board2fen(this.board
[i
][j
]);
344 position
+= C
.FenEmptySquares(emptyCount
);
345 if (i
< this.size
.y
- 1)
346 position
+= "/"; //separate rows
355 // Flags part of the FEN string
357 return ["w", "b"].map(c
=> {
358 return this.castleFlags
[c
].map(x
=> x
.toString(36)).join("");
362 // Enpassant part of the FEN string
365 return "-"; //no en-passant
366 return C
.CoordsToSquare(this.epSquare
);
371 ["w","b"].map(c
=> Object
.values(this.reserve
[c
]).join("")).join("")
376 const squares
= Object
.keys(this.ispawn
);
377 if (squares
.length
== 0)
379 return squares
.join(",");
382 // Set flags from fen (castle: white a,h then black a,h)
385 w: [0, 1].map(i
=> parseInt(fenflags
.charAt(i
), 36)),
386 b: [2, 3].map(i
=> parseInt(fenflags
.charAt(i
), 36))
394 this.options
= o
.options
;
395 // Fill missing options (always the case if random challenge)
396 (V
.Options
.select
|| []).concat(V
.Options
.input
|| []).forEach(opt
=> {
397 if (this.options
[opt
.variable
] === undefined)
398 this.options
[opt
.variable
] = opt
.defaut
;
401 // This object will be used only for initial FEN generation
403 this.playerColor
= o
.color
;
404 this.afterPlay
= o
.afterPlay
; //trigger some actions after playing a move
406 // Fen string fully describes the game state
408 o
.fen
= this.genRandInitFen(o
.seed
);
409 this.re_initFromFen(o
.fen
);
411 // Graphical (can use variables defined above)
412 this.containerId
= o
.element
;
413 this.graphicalInit();
416 re_initFromFen(fen
, oldBoard
) {
417 const fenParsed
= this.parseFen(fen
);
418 this.board
= oldBoard
|| this.getBoard(fenParsed
.position
);
419 this.turn
= fenParsed
.turn
;
420 this.movesCount
= parseInt(fenParsed
.movesCount
, 10);
421 this.setOtherVariables(fenParsed
);
424 // Turn position fen into double array ["wb","wp","bk",...]
426 const rows
= position
.split("/");
427 let board
= ArrayFun
.init(this.size
.x
, this.size
.y
, "");
428 for (let i
= 0; i
< rows
.length
; i
++) {
430 for (let indexInRow
= 0; indexInRow
< rows
[i
].length
; indexInRow
++) {
431 const character
= rows
[i
][indexInRow
];
432 const num
= parseInt(character
, 10);
433 // If num is a number, just shift j:
436 // Else: something at position i,j
438 board
[i
][j
++] = this.fen2board(character
);
444 // Some additional variables from FEN (variant dependant)
445 setOtherVariables(fenParsed
) {
446 // Set flags and enpassant:
448 this.setFlags(fenParsed
.flags
);
449 if (this.hasEnpassant
)
450 this.epSquare
= this.getEpSquare(fenParsed
.enpassant
);
452 this.initReserves(fenParsed
.reserve
);
453 if (this.options
["crazyhouse"])
454 this.initIspawn(fenParsed
.ispawn
);
455 if (this.options
["teleport"]) {
456 this.subTurnTeleport
= 1;
457 this.captured
= null;
459 if (this.options
["dark"]) {
460 // Setup enlightened: squares reachable by player side
461 this.enlightened
= ArrayFun
.init(this.size
.x
, this.size
.y
, false);
462 this.updateEnlightened();
464 this.subTurn
= 1; //may be unused
465 if (!this.moveStack
) //avoid resetting (unwanted)
469 updateEnlightened() {
470 this.oldEnlightened
= this.enlightened
;
471 this.enlightened
= ArrayFun
.init(this.size
.x
, this.size
.y
, false);
472 // Add pieces positions + all squares reachable by moves (includes Zen):
473 for (let x
=0; x
<this.size
.x
; x
++) {
474 for (let y
=0; y
<this.size
.y
; y
++) {
475 if (this.board
[x
][y
] != "" && this.getColor(x
, y
) == this.playerColor
)
477 this.enlightened
[x
][y
] = true;
478 this.getPotentialMovesFrom([x
, y
]).forEach(m
=> {
479 this.enlightened
[m
.end
.x
][m
.end
.y
] = true;
485 this.enlightEnpassant();
488 // Include square of the en-passant capturing square:
490 // NOTE: shortcut, pawn has only one attack type, doesn't depend on square
491 const steps
= this.pieces(this.playerColor
)["p"].attack
[0].steps
;
492 for (let step
of steps
) {
493 const x
= this.epSquare
.x
- step
[0],
494 y
= this.getY(this.epSquare
.y
- step
[1]);
496 this.onBoard(x
, y
) &&
497 this.getColor(x
, y
) == this.playerColor
&&
498 this.getPieceType(x
, y
) == "p"
500 this.enlightened
[x
][this.epSquare
.y
] = true;
506 // ordering as in pieces() p,r,n,b,q,k (+ count in base 30 if needed)
507 initReserves(reserveStr
) {
508 const counts
= reserveStr
.split("").map(c
=> parseInt(c
, 30));
509 this.reserve
= { w: {}, b: {} };
510 const pieceName
= ['p', 'r', 'n', 'b', 'q', 'k'];
511 const L
= pieceName
.length
;
512 for (let i
of ArrayFun
.range(2 * L
)) {
514 this.reserve
['w'][pieceName
[i
]] = counts
[i
];
516 this.reserve
['b'][pieceName
[i
-L
]] = counts
[i
];
520 initIspawn(ispawnStr
) {
521 if (ispawnStr
!= "-")
522 this.ispawn
= ArrayFun
.toObject(ispawnStr
.split(","), true);
527 getNbReservePieces(color
) {
529 Object
.values(this.reserve
[color
]).reduce(
530 (oldV
,newV
) => oldV
+ (newV
> 0 ? 1 : 0), 0)
534 getRankInReserve(c
, p
) {
535 const pieces
= Object
.keys(this.pieces());
536 const lastIndex
= pieces
.findIndex(pp
=> pp
== p
)
537 let toTest
= pieces
.slice(0, lastIndex
);
538 return toTest
.reduce(
539 (oldV
,newV
) => oldV
+ (this.reserve
[c
][newV
] > 0 ? 1 : 0), 0);
545 getPieceWidth(rwidth
) {
546 return (rwidth
/ this.size
.y
);
549 getReserveSquareSize(rwidth
, nbR
) {
550 const sqSize
= this.getPieceWidth(rwidth
);
551 return Math
.min(sqSize
, rwidth
/ nbR
);
554 getReserveNumId(color
, piece
) {
555 return `${this.containerId}|rnum-${color}${piece}`;
558 static AddClass_es(piece
, class_es
) {
559 if (!Array
.isArray(class_es
))
560 class_es
= [class_es
];
561 class_es
.forEach(cl
=> {
562 piece
.classList
.add(cl
);
566 static RemoveClass_es(piece
, class_es
) {
567 if (!Array
.isArray(class_es
))
568 class_es
= [class_es
];
569 class_es
.forEach(cl
=> {
570 piece
.classList
.remove(cl
);
575 // NOTE: not window.onresize = this.re_drawBoardElts because scope (this)
576 window
.onresize
= () => this.re_drawBoardElements();
577 this.re_drawBoardElements();
578 this.initMouseEvents();
580 document
.getElementById(this.containerId
).querySelector(".chessboard");
583 re_drawBoardElements() {
584 const board
= this.getSvgChessboard();
585 const oppCol
= C
.GetOppCol(this.playerColor
);
587 document
.getElementById(this.containerId
).querySelector(".chessboard");
588 chessboard
.innerHTML
= "";
589 chessboard
.insertAdjacentHTML('beforeend', board
);
590 // Compare window ratio width / height to aspectRatio:
591 const windowRatio
= window
.innerWidth
/ window
.innerHeight
;
592 let cbWidth
, cbHeight
;
593 const vRatio
= this.size
.ratio
|| 1;
594 if (windowRatio
<= vRatio
) {
595 // Limiting dimension is width:
596 cbWidth
= Math
.min(window
.innerWidth
, 767);
597 cbHeight
= cbWidth
/ vRatio
;
600 // Limiting dimension is height:
601 cbHeight
= Math
.min(window
.innerHeight
, 767);
602 cbWidth
= cbHeight
* vRatio
;
604 if (this.hasReserve
) {
605 const sqSize
= cbWidth
/ this.size
.y
;
606 // NOTE: allocate space for reserves (up/down) even if they are empty
607 // Cannot use getReserveSquareSize() here, but sqSize is an upper bound.
608 if ((window
.innerHeight
- cbHeight
) / 2 < sqSize
+ 5) {
609 cbHeight
= window
.innerHeight
- 2 * (sqSize
+ 5);
610 cbWidth
= cbHeight
* vRatio
;
613 chessboard
.style
.width
= cbWidth
+ "px";
614 chessboard
.style
.height
= cbHeight
+ "px";
615 // Center chessboard:
616 const spaceLeft
= (window
.innerWidth
- cbWidth
) / 2,
617 spaceTop
= (window
.innerHeight
- cbHeight
) / 2;
618 chessboard
.style
.left
= spaceLeft
+ "px";
619 chessboard
.style
.top
= spaceTop
+ "px";
620 // Give sizes instead of recomputing them,
621 // because chessboard might not be drawn yet.
630 // Get SVG board (background, no pieces)
632 const flipped
= (this.playerColor
== 'b');
635 viewBox="0 0 ${10*this.size.y} ${10*this.size.x}"
636 class="chessboard_SVG">`;
637 for (let i
=0; i
< this.size
.x
; i
++) {
638 for (let j
=0; j
< this.size
.y
; j
++) {
639 const ii
= (flipped
? this.size
.x
- 1 - i : i
);
640 const jj
= (flipped
? this.size
.y
- 1 - j : j
);
641 let classes
= this.getSquareColorClass(ii
, jj
);
642 if (this.enlightened
&& !this.enlightened
[ii
][jj
])
643 classes
+= " in-shadow";
644 // NOTE: x / y reversed because coordinates system is reversed.
648 id="${this.coordsToId({x: ii, y: jj})}"
660 // Generally light square bottom-right
661 getSquareColorClass(x
, y
) {
662 return ((x
+y
) % 2 == 0 ? "light-square": "dark-square");
667 // Refreshing: delete old pieces first
668 for (let i
=0; i
<this.size
.x
; i
++) {
669 for (let j
=0; j
<this.size
.y
; j
++) {
670 if (this.g_pieces
[i
][j
]) {
671 this.g_pieces
[i
][j
].remove();
672 this.g_pieces
[i
][j
] = null;
678 this.g_pieces
= ArrayFun
.init(this.size
.x
, this.size
.y
, null);
680 document
.getElementById(this.containerId
).querySelector(".chessboard");
682 r
= chessboard
.getBoundingClientRect();
683 const pieceWidth
= this.getPieceWidth(r
.width
);
684 for (let i
=0; i
< this.size
.x
; i
++) {
685 for (let j
=0; j
< this.size
.y
; j
++) {
686 if (this.board
[i
][j
] != "") {
687 const color
= this.getColor(i
, j
);
688 const piece
= this.getPiece(i
, j
);
689 this.g_pieces
[i
][j
] = document
.createElement("piece");
690 C
.AddClass_es(this.g_pieces
[i
][j
],
691 this.pieces(color
, i
, j
)[piece
]["class"]);
692 this.g_pieces
[i
][j
].classList
.add(C
.GetColorClass(color
));
693 this.g_pieces
[i
][j
].style
.width
= pieceWidth
+ "px";
694 this.g_pieces
[i
][j
].style
.height
= pieceWidth
+ "px";
695 let [ip
, jp
] = this.getPixelPosition(i
, j
, r
);
696 // Translate coordinates to use chessboard as reference:
697 this.g_pieces
[i
][j
].style
.transform
=
698 `translate(${ip - r.x}px,${jp - r.y}px)`;
699 if (this.enlightened
&& !this.enlightened
[i
][j
])
700 this.g_pieces
[i
][j
].classList
.add("hidden");
701 chessboard
.appendChild(this.g_pieces
[i
][j
]);
706 this.re_drawReserve(['w', 'b'], r
);
709 // NOTE: assume this.reserve != null
710 re_drawReserve(colors
, r
) {
712 // Remove (old) reserve pieces
713 for (let c
of colors
) {
714 Object
.keys(this.r_pieces
[c
]).forEach(p
=> {
715 this.r_pieces
[c
][p
].remove();
716 delete this.r_pieces
[c
][p
];
717 const numId
= this.getReserveNumId(c
, p
);
718 document
.getElementById(numId
).remove();
723 this.r_pieces
= { w: {}, b: {} };
724 let container
= document
.getElementById(this.containerId
);
726 r
= container
.querySelector(".chessboard").getBoundingClientRect();
727 for (let c
of colors
) {
728 let reservesDiv
= document
.getElementById("reserves_" + c
);
730 reservesDiv
.remove();
731 if (!this.reserve
[c
])
733 const nbR
= this.getNbReservePieces(c
);
736 const sqResSize
= this.getReserveSquareSize(r
.width
, nbR
);
738 const vShift
= (c
== this.playerColor
? r
.height
+ 5 : -sqResSize
- 5);
739 const [i0
, j0
] = [r
.x
, r
.y
+ vShift
];
740 let rcontainer
= document
.createElement("div");
741 rcontainer
.id
= "reserves_" + c
;
742 rcontainer
.classList
.add("reserves");
743 rcontainer
.style
.left
= i0
+ "px";
744 rcontainer
.style
.top
= j0
+ "px";
745 // NOTE: +1 fix display bug on Firefox at least
746 rcontainer
.style
.width
= (nbR
* sqResSize
+ 1) + "px";
747 rcontainer
.style
.height
= sqResSize
+ "px";
748 container
.appendChild(rcontainer
);
749 for (let p
of Object
.keys(this.reserve
[c
])) {
750 if (this.reserve
[c
][p
] == 0)
752 let r_cell
= document
.createElement("div");
753 r_cell
.id
= this.coordsToId({x: c
, y: p
});
754 r_cell
.classList
.add("reserve-cell");
755 r_cell
.style
.width
= sqResSize
+ "px";
756 r_cell
.style
.height
= sqResSize
+ "px";
757 rcontainer
.appendChild(r_cell
);
758 let piece
= document
.createElement("piece");
759 C
.AddClass_es(piece
, this.pieces(c
, c
, p
)[p
]["class"]);
760 piece
.classList
.add(C
.GetColorClass(c
));
761 piece
.style
.width
= "100%";
762 piece
.style
.height
= "100%";
763 this.r_pieces
[c
][p
] = piece
;
764 r_cell
.appendChild(piece
);
765 let number
= document
.createElement("div");
766 number
.textContent
= this.reserve
[c
][p
];
767 number
.classList
.add("reserve-num");
768 number
.id
= this.getReserveNumId(c
, p
);
769 const fontSize
= "1.3em";
770 number
.style
.fontSize
= fontSize
;
771 number
.style
.fontSize
= fontSize
;
772 r_cell
.appendChild(number
);
778 updateReserve(color
, piece
, count
) {
779 if (this.options
["cannibal"] && C
.CannibalKings
[piece
])
780 piece
= "k"; //capturing cannibal king: back to king form
781 const oldCount
= this.reserve
[color
][piece
];
782 this.reserve
[color
][piece
] = count
;
783 // Redrawing is much easier if count==0
784 if ([oldCount
, count
].includes(0))
785 this.re_drawReserve([color
]);
787 const numId
= this.getReserveNumId(color
, piece
);
788 document
.getElementById(numId
).textContent
= count
;
792 // Apply diff this.enlightened --> oldEnlightened on board
793 graphUpdateEnlightened() {
795 document
.getElementById(this.containerId
).querySelector(".chessboard");
796 const r
= chessboard
.getBoundingClientRect();
797 const pieceWidth
= this.getPieceWidth(r
.width
);
798 for (let x
=0; x
<this.size
.x
; x
++) {
799 for (let y
=0; y
<this.size
.y
; y
++) {
800 if (!this.enlightened
[x
][y
] && this.oldEnlightened
[x
][y
]) {
801 let elt
= document
.getElementById(this.coordsToId({x: x
, y: y
}));
802 elt
.classList
.add("in-shadow");
803 if (this.g_pieces
[x
][y
])
804 this.g_pieces
[x
][y
].classList
.add("hidden");
806 else if (this.enlightened
[x
][y
] && !this.oldEnlightened
[x
][y
]) {
807 let elt
= document
.getElementById(this.coordsToId({x: x
, y: y
}));
808 elt
.classList
.remove("in-shadow");
809 if (this.g_pieces
[x
][y
])
810 this.g_pieces
[x
][y
].classList
.remove("hidden");
816 // Resize board: no need to destroy/recreate pieces
819 document
.getElementById(this.containerId
).querySelector(".chessboard");
820 const r
= chessboard
.getBoundingClientRect();
821 const multFact
= (mode
== "up" ? 1.05 : 0.95);
822 let [newWidth
, newHeight
] = [multFact
* r
.width
, multFact
* r
.height
];
824 const vRatio
= this.size
.ratio
|| 1;
825 if (newWidth
> window
.innerWidth
) {
826 newWidth
= window
.innerWidth
;
827 newHeight
= newWidth
/ vRatio
;
829 if (newHeight
> window
.innerHeight
) {
830 newHeight
= window
.innerHeight
;
831 newWidth
= newHeight
* vRatio
;
833 chessboard
.style
.width
= newWidth
+ "px";
834 chessboard
.style
.height
= newHeight
+ "px";
835 const newX
= (window
.innerWidth
- newWidth
) / 2;
836 chessboard
.style
.left
= newX
+ "px";
837 const newY
= (window
.innerHeight
- newHeight
) / 2;
838 chessboard
.style
.top
= newY
+ "px";
839 const newR
= {x: newX
, y: newY
, width: newWidth
, height: newHeight
};
840 const pieceWidth
= this.getPieceWidth(newWidth
);
841 // NOTE: next "if" for variants which use squares filling
842 // instead of "physical", moving pieces
844 for (let i
=0; i
< this.size
.x
; i
++) {
845 for (let j
=0; j
< this.size
.y
; j
++) {
846 if (this.g_pieces
[i
][j
]) {
847 // NOTE: could also use CSS transform "scale"
848 this.g_pieces
[i
][j
].style
.width
= pieceWidth
+ "px";
849 this.g_pieces
[i
][j
].style
.height
= pieceWidth
+ "px";
850 const [ip
, jp
] = this.getPixelPosition(i
, j
, newR
);
851 // Translate coordinates to use chessboard as reference:
852 this.g_pieces
[i
][j
].style
.transform
=
853 `translate(${ip - newX}px,${jp - newY}px)`;
859 this.rescaleReserve(newR
);
863 for (let c
of ['w','b']) {
864 if (!this.reserve
[c
])
866 const nbR
= this.getNbReservePieces(c
);
869 // Resize container first
870 const sqResSize
= this.getReserveSquareSize(r
.width
, nbR
);
871 const vShift
= (c
== this.playerColor
? r
.height
+ 5 : -sqResSize
- 5);
872 const [i0
, j0
] = [r
.x
, r
.y
+ vShift
];
873 let rcontainer
= document
.getElementById("reserves_" + c
);
874 rcontainer
.style
.left
= i0
+ "px";
875 rcontainer
.style
.top
= j0
+ "px";
876 rcontainer
.style
.width
= (nbR
* sqResSize
+ 1) + "px";
877 rcontainer
.style
.height
= sqResSize
+ "px";
878 // And then reserve cells:
879 const rpieceWidth
= this.getReserveSquareSize(r
.width
, nbR
);
880 Object
.keys(this.reserve
[c
]).forEach(p
=> {
881 if (this.reserve
[c
][p
] == 0)
883 let r_cell
= document
.getElementById(this.coordsToId({x: c
, y: p
}));
884 r_cell
.style
.width
= sqResSize
+ "px";
885 r_cell
.style
.height
= sqResSize
+ "px";
890 // Return the absolute pixel coordinates given current position.
891 // Our coordinate system differs from CSS one (x <--> y).
892 // We return here the CSS coordinates (more useful).
893 getPixelPosition(i
, j
, r
) {
895 return [0, 0]; //piece vanishes
897 if (typeof i
== "string") {
898 // Reserves: need to know the rank of piece
899 const nbR
= this.getNbReservePieces(i
);
900 const rsqSize
= this.getReserveSquareSize(r
.width
, nbR
);
901 x
= this.getRankInReserve(i
, j
) * rsqSize
;
902 y
= (this.playerColor
== i
? y
= r
.height
+ 5 : - 5 - rsqSize
);
905 const sqSize
= r
.width
/ this.size
.y
;
906 const flipped
= (this.playerColor
== 'b');
907 x
= (flipped
? this.size
.y
- 1 - j : j
) * sqSize
;
908 y
= (flipped
? this.size
.x
- 1 - i : i
) * sqSize
;
910 return [r
.x
+ x
, r
.y
+ y
];
914 let container
= document
.getElementById(this.containerId
);
915 let chessboard
= container
.querySelector(".chessboard");
917 const getOffset
= e
=> {
920 return {x: e
.clientX
, y: e
.clientY
};
921 let touchLocation
= null;
922 if (e
.targetTouches
&& e
.targetTouches
.length
>= 1)
923 // Touch screen, dragstart
924 touchLocation
= e
.targetTouches
[0];
925 else if (e
.changedTouches
&& e
.changedTouches
.length
>= 1)
926 // Touch screen, dragend
927 touchLocation
= e
.changedTouches
[0];
929 return {x: touchLocation
.clientX
, y: touchLocation
.clientY
};
930 return {x: 0, y: 0}; //shouldn't reach here =)
933 const centerOnCursor
= (piece
, e
) => {
934 const centerShift
= this.getPieceWidth(r
.width
) / 2;
935 const offset
= getOffset(e
);
936 piece
.style
.left
= (offset
.x
- centerShift
) + "px";
937 piece
.style
.top
= (offset
.y
- centerShift
) + "px";
942 startPiece
, curPiece
= null,
944 const mousedown
= (e
) => {
945 // Disable zoom on smartphones:
946 if (e
.touches
&& e
.touches
.length
> 1)
948 r
= chessboard
.getBoundingClientRect();
949 pieceWidth
= this.getPieceWidth(r
.width
);
950 const cd
= this.idToCoords(e
.target
.id
);
952 const move = this.doClick(cd
);
954 this.buildMoveStack(move, r
);
956 const [x
, y
] = Object
.values(cd
);
957 if (typeof x
!= "number")
958 startPiece
= this.r_pieces
[x
][y
];
960 startPiece
= this.g_pieces
[x
][y
];
961 if (startPiece
&& this.canIplay(x
, y
)) {
964 curPiece
= startPiece
.cloneNode();
965 curPiece
.style
.transform
= "none";
966 curPiece
.style
.zIndex
= 5;
967 curPiece
.style
.width
= pieceWidth
+ "px";
968 curPiece
.style
.height
= pieceWidth
+ "px";
969 centerOnCursor(curPiece
, e
);
970 container
.appendChild(curPiece
);
971 startPiece
.style
.opacity
= "0.4";
972 chessboard
.style
.cursor
= "none";
978 const mousemove
= (e
) => {
981 centerOnCursor(curPiece
, e
);
983 else if (e
.changedTouches
&& e
.changedTouches
.length
>= 1)
984 // Attempt to prevent horizontal swipe...
988 const mouseup
= (e
) => {
991 const [x
, y
] = [start
.x
, start
.y
];
994 chessboard
.style
.cursor
= "pointer";
995 startPiece
.style
.opacity
= "1";
996 const offset
= getOffset(e
);
997 const landingElt
= document
.elementFromPoint(offset
.x
, offset
.y
);
999 (landingElt
? this.idToCoords(landingElt
.id
) : undefined);
1001 // NOTE: clearly suboptimal, but much easier, and not a big deal.
1002 const potentialMoves
= this.getPotentialMovesFrom([x
, y
])
1003 .filter(m
=> m
.end
.x
== cd
.x
&& m
.end
.y
== cd
.y
);
1004 const moves
= this.filterValid(potentialMoves
);
1005 if (moves
.length
>= 2)
1006 this.showChoices(moves
, r
);
1007 else if (moves
.length
== 1)
1008 this.buildMoveStack(moves
[0], r
);
1013 if ('onmousedown' in window
) {
1014 document
.addEventListener("mousedown", mousedown
);
1015 document
.addEventListener("mousemove", mousemove
);
1016 document
.addEventListener("mouseup", mouseup
);
1017 document
.addEventListener("wheel",
1018 (e
) => this.rescale(e
.deltaY
< 0 ? "up" : "down"));
1020 if ('ontouchstart' in window
) {
1021 // https://stackoverflow.com/a/42509310/12660887
1022 document
.addEventListener("touchstart", mousedown
, {passive: false});
1023 document
.addEventListener("touchmove", mousemove
, {passive: false});
1024 document
.addEventListener("touchend", mouseup
, {passive: false});
1026 // TODO: onpointerdown/move/up ? See reveal.js /controllers/touch.js
1029 showChoices(moves
, r
) {
1030 let container
= document
.getElementById(this.containerId
);
1031 let chessboard
= container
.querySelector(".chessboard");
1032 let choices
= document
.createElement("div");
1033 choices
.id
= "choices";
1035 r
= chessboard
.getBoundingClientRect();
1036 choices
.style
.width
= r
.width
+ "px";
1037 choices
.style
.height
= r
.height
+ "px";
1038 choices
.style
.left
= r
.x
+ "px";
1039 choices
.style
.top
= r
.y
+ "px";
1040 chessboard
.style
.opacity
= "0.5";
1041 container
.appendChild(choices
);
1042 const squareWidth
= r
.width
/ this.size
.y
;
1043 const firstUpLeft
= (r
.width
- (moves
.length
* squareWidth
)) / 2;
1044 const firstUpTop
= (r
.height
- squareWidth
) / 2;
1045 const color
= moves
[0].appear
[0].c
;
1046 const callback
= (m
) => {
1047 chessboard
.style
.opacity
= "1";
1048 container
.removeChild(choices
);
1049 this.buildMoveStack(m
, r
);
1051 for (let i
=0; i
< moves
.length
; i
++) {
1052 let choice
= document
.createElement("div");
1053 choice
.classList
.add("choice");
1054 choice
.style
.width
= squareWidth
+ "px";
1055 choice
.style
.height
= squareWidth
+ "px";
1056 choice
.style
.left
= (firstUpLeft
+ i
* squareWidth
) + "px";
1057 choice
.style
.top
= firstUpTop
+ "px";
1058 choice
.style
.backgroundColor
= "lightyellow";
1059 choice
.onclick
= () => callback(moves
[i
]);
1060 const piece
= document
.createElement("piece");
1061 const cdisp
= moves
[i
].choice
|| moves
[i
].appear
[0].p
;
1062 C
.AddClass_es(piece
,
1063 this.pieces(color
, moves
[i
].end
.x
, moves
[i
].end
.y
)[cdisp
]["class"]);
1064 piece
.classList
.add(C
.GetColorClass(color
));
1065 piece
.style
.width
= "100%";
1066 piece
.style
.height
= "100%";
1067 choice
.appendChild(piece
);
1068 choices
.appendChild(choice
);
1079 ratio: 1 //for rectangular board = y / x (optional, 1 = default)
1083 // Color of thing on square (i,j). 'undefined' if square is empty
1085 if (typeof i
== "string")
1086 return i
; //reserves
1087 return this.board
[i
][j
].charAt(0);
1090 static GetColorClass(c
) {
1095 return "other-color"; //unidentified color
1098 // Assume square i,j isn't empty
1100 if (typeof j
== "string")
1101 return j
; //reserves
1102 return this.board
[i
][j
].charAt(1);
1105 // Piece type on square (i,j)
1106 getPieceType(i
, j
, p
) {
1108 p
= this.getPiece(i
, j
);
1109 return C
.CannibalKings
[p
] || p
; //a cannibal king move as...
1112 // Get opponent color
1113 static GetOppCol(color
) {
1114 return (color
== "w" ? "b" : "w");
1117 // Can thing on square1 capture (enemy) thing on square2?
1118 canTake([x1
, y1
], [x2
, y2
]) {
1119 return (this.getColor(x1
, y1
) !== this.getColor(x2
, y2
));
1122 // Is (x,y) on the chessboard?
1124 return (x
>= 0 && x
< this.size
.x
&&
1125 y
>= 0 && y
< this.size
.y
);
1128 // Am I allowed to move thing at square x,y ?
1130 return (this.playerColor
== this.turn
&& this.getColor(x
, y
) == this.turn
);
1133 ////////////////////////
1134 // PIECES SPECIFICATIONS
1136 pieces(color
, x
, y
) {
1137 const pawnShift
= (color
== "w" ? -1 : 1);
1138 // NOTE: jump 2 squares from first rank (pawns can be here sometimes)
1139 const initRank
= ((color
== 'w' && x
>= 6) || (color
== 'b' && x
<= 1));
1145 steps: [[pawnShift
, 0]],
1146 range: (initRank
? 2 : 1)
1151 steps: [[pawnShift
, 1], [pawnShift
, -1]],
1160 {steps: [[0, 1], [0, -1], [1, 0], [-1, 0]]}
1169 [1, 2], [1, -2], [-1, 2], [-1, -2],
1170 [2, 1], [-2, 1], [2, -1], [-2, -1]
1180 {steps: [[1, 1], [1, -1], [-1, 1], [-1, -1]]}
1189 [0, 1], [0, -1], [1, 0], [-1, 0],
1190 [1, 1], [1, -1], [-1, 1], [-1, -1]
1201 [0, 1], [0, -1], [1, 0], [-1, 0],
1202 [1, 1], [1, -1], [-1, 1], [-1, -1]
1209 '!': {"class": "king-pawn", moveas: "p"},
1210 '#': {"class": "king-rook", moveas: "r"},
1211 '$': {"class": "king-knight", moveas: "n"},
1212 '%': {"class": "king-bishop", moveas: "b"},
1213 '*': {"class": "king-queen", moveas: "q"}
1217 ////////////////////
1220 // For Cylinder: get Y coordinate
1222 if (!this.options
["cylinder"])
1224 let res
= y
% this.size
.y
;
1230 // Stop at the first capture found
1231 atLeastOneCapture(color
) {
1232 color
= color
|| this.turn
;
1233 const oppCol
= C
.GetOppCol(color
);
1234 for (let i
= 0; i
< this.size
.x
; i
++) {
1235 for (let j
= 0; j
< this.size
.y
; j
++) {
1236 if (this.board
[i
][j
] != "" && this.getColor(i
, j
) == color
) {
1237 const allSpecs
= this.pieces(color
, i
, j
)
1238 let specs
= allSpecs
[this.getPieceType(i
, j
)];
1240 specs
= allSpecs
[specs
.moveas
];
1241 const attacks
= specs
.attack
|| specs
.moves
;
1242 for (let a
of attacks
) {
1243 outerLoop: for (let step
of a
.steps
) {
1244 let [ii
, jj
] = [i
+ step
[0], this.getY(j
+ step
[1])];
1245 let stepCounter
= 1;
1246 while (this.onBoard(ii
, jj
) && this.board
[ii
][jj
] == "") {
1247 if (a
.range
<= stepCounter
++)
1250 jj
= this.getY(jj
+ step
[1]);
1253 this.onBoard(ii
, jj
) &&
1254 this.getColor(ii
, jj
) == oppCol
&&
1256 [this.getBasicMove([i
, j
], [ii
, jj
])]
1269 getDropMovesFrom([c
, p
]) {
1270 // NOTE: by design, this.reserve[c][p] >= 1 on user click
1271 // (but not necessarily otherwise: atLeastOneMove() etc)
1272 if (this.reserve
[c
][p
] == 0)
1275 for (let i
=0; i
<this.size
.x
; i
++) {
1276 for (let j
=0; j
<this.size
.y
; j
++) {
1278 this.board
[i
][j
] == "" &&
1279 (!this.enlightened
|| this.enlightened
[i
][j
]) &&
1282 (c
== 'w' && i
< this.size
.x
- 1) ||
1288 start: {x: c
, y: p
},
1290 appear: [new PiPo({x: i
, y: j
, c: c
, p: p
})],
1300 // All possible moves from selected square
1301 getPotentialMovesFrom(sq
, color
) {
1302 if (this.subTurnTeleport
== 2)
1304 if (typeof sq
[0] == "string")
1305 return this.getDropMovesFrom(sq
);
1306 if (this.isImmobilized(sq
))
1308 const piece
= this.getPieceType(sq
[0], sq
[1]);
1309 let moves
= this.getPotentialMovesOf(piece
, sq
);
1312 this.hasEnpassant
&&
1315 Array
.prototype.push
.apply(moves
, this.getEnpassantCaptures(sq
));
1320 this.castleFlags
[color
|| this.turn
].some(v
=> v
< this.size
.y
)
1322 Array
.prototype.push
.apply(moves
, this.getCastleMoves(sq
));
1324 return this.postProcessPotentialMoves(moves
);
1327 postProcessPotentialMoves(moves
) {
1328 if (moves
.length
== 0)
1330 const color
= this.getColor(moves
[0].start
.x
, moves
[0].start
.y
);
1331 const oppCol
= C
.GetOppCol(color
);
1333 if (this.options
["capture"] && this.atLeastOneCapture())
1334 moves
= this.capturePostProcess(moves
, oppCol
);
1336 if (this.options
["atomic"])
1337 this.atomicPostProcess(moves
, color
, oppCol
);
1341 this.getPieceType(moves
[0].start
.x
, moves
[0].start
.y
) == "p"
1343 this.pawnPostProcess(moves
, color
, oppCol
);
1347 this.options
["cannibal"] &&
1348 this.options
["rifle"]
1350 // In this case a rifle-capture from last rank may promote a pawn
1351 this.riflePromotePostProcess(moves
, color
);
1357 capturePostProcess(moves
, oppCol
) {
1358 // Filter out non-capturing moves (not using m.vanish because of
1359 // self captures of Recycle and Teleport).
1360 return moves
.filter(m
=> {
1362 this.board
[m
.end
.x
][m
.end
.y
] != "" &&
1363 this.getColor(m
.end
.x
, m
.end
.y
) == oppCol
1368 atomicPostProcess(moves
, color
, oppCol
) {
1369 moves
.forEach(m
=> {
1371 this.board
[m
.end
.x
][m
.end
.y
] != "" &&
1372 this.getColor(m
.end
.x
, m
.end
.y
) == oppCol
1385 let mNext
= new Move({
1391 for (let step
of steps
) {
1392 let x
= m
.end
.x
+ step
[0];
1393 let y
= this.getY(m
.end
.y
+ step
[1]);
1395 this.onBoard(x
, y
) &&
1396 this.board
[x
][y
] != "" &&
1397 (x
!= m
.start
.x
|| y
!= m
.start
.y
) &&
1398 this.getPieceType(x
, y
) != "p"
1402 p: this.getPiece(x
, y
),
1403 c: this.getColor(x
, y
),
1410 if (!this.options
["rifle"]) {
1411 // The moving piece also vanish
1412 mNext
.vanish
.unshift(
1417 p: this.getPiece(m
.start
.x
, m
.start
.y
)
1426 pawnPostProcess(moves
, color
, oppCol
) {
1428 const lastRank
= (color
== "w" ? 0 : this.size
.x
- 1);
1429 const initPiece
= this.getPiece(moves
[0].start
.x
, moves
[0].start
.y
);
1430 moves
.forEach(m
=> {
1431 const [x1
, y1
] = [m
.start
.x
, m
.start
.y
];
1432 const [x2
, y2
] = [m
.end
.x
, m
.end
.y
];
1433 const promotionOk
= (
1435 (!this.options
["rifle"] || this.board
[x2
][y2
] == "")
1438 return; //nothing to do
1439 if (this.options
["pawnfall"]) {
1443 let finalPieces
= ["p"];
1445 this.options
["cannibal"] &&
1446 this.board
[x2
][y2
] != "" &&
1447 this.getColor(x2
, y2
) == oppCol
1449 finalPieces
= [this.getPieceType(x2
, y2
)];
1452 finalPieces
= this.pawnPromotions
;
1453 m
.appear
[0].p
= finalPieces
[0];
1454 if (initPiece
== "!") //cannibal king-pawn
1455 m
.appear
[0].p
= C
.CannibalKingCode
[finalPieces
[0]];
1456 for (let i
=1; i
<finalPieces
.length
; i
++) {
1457 const piece
= finalPieces
[i
];
1460 p: (initPiece
!= "!" ? piece : C
.CannibalKingCode
[piece
])
1462 let newMove
= this.getBasicMove([x1
, y1
], [x2
, y2
], tr
);
1463 moreMoves
.push(newMove
);
1466 Array
.prototype.push
.apply(moves
, moreMoves
);
1469 riflePromotePostProcess(moves
, color
) {
1470 const lastRank
= (color
== "w" ? 0 : this.size
.x
- 1);
1472 moves
.forEach(m
=> {
1474 m
.start
.x
== lastRank
&&
1475 m
.appear
.length
>= 1 &&
1476 m
.appear
[0].p
== "p" &&
1477 m
.appear
[0].x
== m
.start
.x
&&
1478 m
.appear
[0].y
== m
.start
.y
1480 m
.appear
[0].p
= this.pawnPromotions
[0];
1481 for (let i
=1; i
<this.pawnPromotions
.length
; i
++) {
1482 let newMv
= JSON
.parse(JSON
.stringify(m
));
1483 newMv
.appear
[0].p
= this.pawnSpecs
.promotions
[i
];
1484 newMoves
.push(newMv
);
1488 Array
.prototype.push
.apply(moves
, newMoves
);
1491 // NOTE: using special symbols to not interfere with variants' pieces codes
1492 static get CannibalKings() {
1503 static get CannibalKingCode() {
1515 return !!C
.CannibalKings
[symbol
];
1519 // (redefined in Baroque etc, where Madrasi condition doesn't make sense)
1520 isImmobilized([x
, y
]) {
1521 if (!this.options
["madrasi"])
1523 const color
= this.getColor(x
, y
);
1524 const oppCol
= C
.GetOppCol(color
);
1525 const piece
= this.getPieceType(x
, y
); //ok not cannibal king
1526 const allSpecs
= this.pieces(color
, x
, y
);
1527 let stepSpec
= allSpecs
[piece
];
1528 if (stepSpec
.moveas
)
1529 stepSpec
= allSpecs
[stepSpec
.moveas
];
1530 const attacks
= stepSpec
.attack
|| stepSpec
.moves
;
1531 for (let a
of attacks
) {
1532 outerLoop: for (let step
of a
.steps
) {
1533 let [i
, j
] = [x
+ step
[0], y
+ step
[1]];
1534 let stepCounter
= 1;
1535 while (this.onBoard(i
, j
) && this.board
[i
][j
] == "") {
1536 if (a
.range
<= stepCounter
++)
1539 j
= this.getY(j
+ step
[1]);
1542 this.onBoard(i
, j
) &&
1543 this.getColor(i
, j
) == oppCol
&&
1544 this.getPieceType(i
, j
) == piece
1553 canStepOver(i
, j
, p
) {
1554 // In some variants, objects on boards don't stop movement (Chakart)
1555 return this.board
[i
][j
] == "";
1558 // Generic method to find possible moves of "sliding or jumping" pieces
1559 getPotentialMovesOf(piece
, [x
, y
]) {
1560 const color
= this.getColor(x
, y
);
1561 const apparentPiece
= this.getPiece(x
, y
); //how it looks
1562 const allSpecs
= this.pieces(color
, x
, y
);
1563 let stepSpec
= allSpecs
[piece
];
1564 if (stepSpec
.moveas
)
1565 stepSpec
= allSpecs
[stepSpec
.moveas
];
1567 // Next 3 for Cylinder mode:
1572 const addMove
= (start
, end
) => {
1573 let newMove
= this.getBasicMove(start
, end
);
1574 if (segments
.length
> 0) {
1575 newMove
.segments
= JSON
.parse(JSON
.stringify(segments
));
1576 newMove
.segments
.push([[segStart
[0], segStart
[1]], [end
[0], end
[1]]]);
1578 moves
.push(newMove
);
1581 const findAddMoves
= (type
, stepArray
) => {
1582 for (let s
of stepArray
) {
1583 outerLoop: for (let step
of s
.steps
) {
1586 let [i
, j
] = [x
, y
];
1587 let stepCounter
= 0;
1589 this.onBoard(i
, j
) &&
1590 ((i
== x
&& j
== y
) || this.canStepOver(i
, j
, apparentPiece
))
1594 !explored
[i
+ "." + j
] &&
1597 explored
[i
+ "." + j
] = true;
1598 addMove([x
, y
], [i
, j
]);
1600 if (s
.range
<= stepCounter
++)
1602 const oldIJ
= [i
, j
];
1604 j
= this.getY(j
+ step
[1]);
1605 if (Math
.abs(j
- oldIJ
[1]) > 1) {
1606 // Boundary between segments (cylinder mode)
1607 segments
.push([[segStart
[0], segStart
[1]], oldIJ
]);
1611 if (!this.onBoard(i
, j
))
1613 const pieceIJ
= this.getPieceType(i
, j
);
1615 type
!= "moveonly" &&
1616 !explored
[i
+ "." + j
] &&
1618 !this.options
["zen"] ||
1622 this.canTake([x
, y
], [i
, j
]) ||
1624 (this.options
["recycle"] || this.options
["teleport"]) &&
1629 explored
[i
+ "." + j
] = true;
1630 addMove([x
, y
], [i
, j
]);
1636 const specialAttack
= !!stepSpec
.attack
;
1638 findAddMoves("attack", stepSpec
.attack
);
1639 findAddMoves(specialAttack
? "moveonly" : "all", stepSpec
.moves
);
1640 if (this.options
["zen"]) {
1641 Array
.prototype.push
.apply(moves
,
1642 this.findCapturesOn([x
, y
], {zen: true}));
1647 // Search for enemy (or not) pieces attacking [x, y]
1648 findCapturesOn([x
, y
], args
) {
1651 args
.oppCol
= C
.GetOppCol(this.getColor(x
, y
) || this.turn
);
1652 for (let i
=0; i
<this.size
.x
; i
++) {
1653 for (let j
=0; j
<this.size
.y
; j
++) {
1655 this.board
[i
][j
] != "" &&
1656 this.getColor(i
, j
) == args
.oppCol
&&
1657 !this.isImmobilized([i
, j
])
1659 if (args
.zen
&& this.isKing(this.getPiece(i
, j
)))
1660 continue; //king not captured in this way
1661 const apparentPiece
= this.getPiece(i
, j
);
1662 // Quick check: does this potential attacker target x,y ?
1663 if (this.canStepOver(x
, y
, apparentPiece
))
1665 const allSpecs
= this.pieces(args
.oppCol
, i
, j
);
1666 let stepSpec
= allSpecs
[this.getPieceType(i
, j
)];
1667 if (stepSpec
.moveas
)
1668 stepSpec
= allSpecs
[stepSpec
.moveas
];
1669 const attacks
= stepSpec
.attack
|| stepSpec
.moves
;
1670 for (let a
of attacks
) {
1671 for (let s
of a
.steps
) {
1672 // Quick check: if step isn't compatible, don't even try
1673 if (!C
.CompatibleStep([i
, j
], [x
, y
], s
, a
.range
))
1675 // Finally verify that nothing stand in-between
1676 let [ii
, jj
] = [i
+ s
[0], this.getY(j
+ s
[1])];
1677 let stepCounter
= 1;
1679 this.onBoard(ii
, jj
) &&
1680 this.board
[ii
][jj
] == "" &&
1681 (ii
!= x
|| jj
!= y
) //condition to attack empty squares too
1684 jj
= this.getY(jj
+ s
[1]);
1686 if (ii
== x
&& jj
== y
) {
1689 moves
.push(this.getBasicMove([x
, y
], [i
, j
]));
1691 moves
.push(this.getBasicMove([i
, j
], [x
, y
]));
1693 return moves
; //test for underCheck
1703 static CompatibleStep([x1
, y1
], [x2
, y2
], step
, range
) {
1704 const rx
= (x2
- x1
) / step
[0],
1705 ry
= (y2
- y1
) / step
[1];
1707 (!Number
.isFinite(rx
) && !Number
.isNaN(rx
)) ||
1708 (!Number
.isFinite(ry
) && !Number
.isNaN(ry
))
1712 let distance
= (Number
.isNaN(rx
) ? ry : rx
);
1713 // TODO: 1e-7 here is totally arbitrary
1714 if (Math
.abs(distance
- Math
.round(distance
)) > 1e-7)
1716 distance
= Math
.round(distance
); //in case of (numerical...)
1717 if (range
< distance
)
1722 // Build a regular move from its initial and destination squares.
1723 // tr: transformation
1724 getBasicMove([sx
, sy
], [ex
, ey
], tr
) {
1725 const initColor
= this.getColor(sx
, sy
);
1726 const initPiece
= this.getPiece(sx
, sy
);
1727 const destColor
= (this.board
[ex
][ey
] != "" ? this.getColor(ex
, ey
) : "");
1731 start: {x: sx
, y: sy
},
1735 !this.options
["rifle"] ||
1736 this.board
[ex
][ey
] == "" ||
1737 destColor
== initColor
//Recycle, Teleport
1743 c: !!tr
? tr
.c : initColor
,
1744 p: !!tr
? tr
.p : initPiece
1756 if (this.board
[ex
][ey
] != "") {
1761 c: this.getColor(ex
, ey
),
1762 p: this.getPiece(ex
, ey
)
1765 if (this.options
["cannibal"] && destColor
!= initColor
) {
1766 const lastIdx
= mv
.vanish
.length
- 1;
1767 let trPiece
= mv
.vanish
[lastIdx
].p
;
1768 if (this.isKing(this.getPiece(sx
, sy
)))
1769 trPiece
= C
.CannibalKingCode
[trPiece
];
1770 if (mv
.appear
.length
>= 1)
1771 mv
.appear
[0].p
= trPiece
;
1772 else if (this.options
["rifle"]) {
1795 // En-passant square, if any
1796 getEpSquare(moveOrSquare
) {
1797 if (typeof moveOrSquare
=== "string") {
1798 const square
= moveOrSquare
;
1801 return C
.SquareToCoords(square
);
1803 // Argument is a move:
1804 const move = moveOrSquare
;
1805 const s
= move.start
,
1809 Math
.abs(s
.x
- e
.x
) == 2 &&
1810 // Next conditions for variants like Atomic or Rifle, Recycle...
1812 move.appear
.length
> 0 &&
1813 this.getPieceType(0, 0, move.appear
[0].p
) == "p"
1817 move.vanish
.length
> 0 &&
1818 this.getPieceType(0, 0, move.vanish
[0].p
) == "p"
1826 return undefined; //default
1829 // Special case of en-passant captures: treated separately
1830 getEnpassantCaptures([x
, y
]) {
1831 const color
= this.getColor(x
, y
);
1832 const shiftX
= (color
== 'w' ? -1 : 1);
1833 const oppCol
= C
.GetOppCol(color
);
1834 let enpassantMove
= null;
1837 this.epSquare
.x
== x
+ shiftX
&&
1838 Math
.abs(this.getY(this.epSquare
.y
- y
)) == 1 &&
1839 this.getColor(x
, this.epSquare
.y
) == oppCol
//Doublemove guard...
1841 const [epx
, epy
] = [this.epSquare
.x
, this.epSquare
.y
];
1842 this.board
[epx
][epy
] = oppCol
+ "p";
1843 enpassantMove
= this.getBasicMove([x
, y
], [epx
, epy
]);
1844 this.board
[epx
][epy
] = "";
1845 const lastIdx
= enpassantMove
.vanish
.length
- 1; //think Rifle
1846 enpassantMove
.vanish
[lastIdx
].x
= x
;
1848 return !!enpassantMove
? [enpassantMove
] : [];
1851 // "castleInCheck" arg to let some variants castle under check
1852 getCastleMoves([x
, y
], finalSquares
, castleInCheck
, castleWith
) {
1853 const c
= this.getColor(x
, y
);
1856 const oppCol
= C
.GetOppCol(c
);
1860 finalSquares
|| [ [2, 3], [this.size
.y
- 2, this.size
.y
- 3] ];
1861 const castlingKing
= this.getPiece(x
, y
);
1862 castlingCheck: for (
1865 castleSide
++ //large, then small
1867 if (this.castleFlags
[c
][castleSide
] >= this.size
.y
)
1869 // If this code is reached, rook and king are on initial position
1871 // NOTE: in some variants this is not a rook
1872 const rookPos
= this.castleFlags
[c
][castleSide
];
1873 const castlingPiece
= this.getPiece(x
, rookPos
);
1875 this.board
[x
][rookPos
] == "" ||
1876 this.getColor(x
, rookPos
) != c
||
1877 (!!castleWith
&& !castleWith
.includes(castlingPiece
))
1879 // Rook is not here, or changed color (see Benedict)
1882 // Nothing on the path of the king ? (and no checks)
1883 const finDist
= finalSquares
[castleSide
][0] - y
;
1884 let step
= finDist
/ Math
.max(1, Math
.abs(finDist
));
1888 (!castleInCheck
&& this.underCheck([x
, i
], oppCol
)) ||
1890 this.board
[x
][i
] != "" &&
1891 // NOTE: next check is enough, because of chessboard constraints
1892 (this.getColor(x
, i
) != c
|| ![rookPos
, y
].includes(i
))
1895 continue castlingCheck
;
1898 } while (i
!= finalSquares
[castleSide
][0]);
1899 // Nothing on the path to the rook?
1900 step
= (castleSide
== 0 ? -1 : 1);
1901 for (i
= y
+ step
; i
!= rookPos
; i
+= step
) {
1902 if (this.board
[x
][i
] != "")
1903 continue castlingCheck
;
1906 // Nothing on final squares, except maybe king and castling rook?
1907 for (i
= 0; i
< 2; i
++) {
1909 finalSquares
[castleSide
][i
] != rookPos
&&
1910 this.board
[x
][finalSquares
[castleSide
][i
]] != "" &&
1912 finalSquares
[castleSide
][i
] != y
||
1913 this.getColor(x
, finalSquares
[castleSide
][i
]) != c
1916 continue castlingCheck
;
1920 // If this code is reached, castle is valid
1926 y: finalSquares
[castleSide
][0],
1932 y: finalSquares
[castleSide
][1],
1938 // King might be initially disguised (Titan...)
1939 new PiPo({ x: x
, y: y
, p: castlingKing
, c: c
}),
1940 new PiPo({ x: x
, y: rookPos
, p: castlingPiece
, c: c
})
1943 Math
.abs(y
- rookPos
) <= 2
1944 ? {x: x
, y: rookPos
}
1945 : {x: x
, y: y
+ 2 * (castleSide
== 0 ? -1 : 1)}
1953 ////////////////////
1956 // Is (king at) given position under check by "oppCol" ?
1957 underCheck([x
, y
], oppCol
) {
1958 if (this.options
["taking"] || this.options
["dark"])
1961 this.findCapturesOn([x
, y
], {oppCol: oppCol
, one: true}).length
>= 1
1965 // Stop at first king found (TODO: multi-kings)
1966 searchKingPos(color
) {
1967 for (let i
=0; i
< this.size
.x
; i
++) {
1968 for (let j
=0; j
< this.size
.y
; j
++) {
1969 if (this.getColor(i
, j
) == color
&& this.isKing(this.getPiece(i
, j
)))
1973 return [-1, -1]; //king not found
1976 // Some variants (e.g. Refusal) may need to check opponent moves too
1977 filterValid(moves
, color
) {
1978 if (moves
.length
== 0)
1982 const oppCol
= C
.GetOppCol(color
);
1983 if (this.options
["balance"] && [1, 3].includes(this.movesCount
)) {
1984 // Forbid moves either giving check or exploding opponent's king:
1985 const oppKingPos
= this.searchKingPos(oppCol
);
1986 moves
= moves
.filter(m
=> {
1988 m
.vanish
.some(v
=> v
.c
== oppCol
&& v
.p
== "k") &&
1989 m
.appear
.every(a
=> a
.c
!= oppCol
|| a
.p
!= "k")
1992 this.playOnBoard(m
);
1993 const res
= !this.underCheck(oppKingPos
, color
);
1994 this.undoOnBoard(m
);
1998 if (this.options
["taking"] || this.options
["dark"])
2000 const kingPos
= this.searchKingPos(color
);
2001 let filtered
= {}; //avoid re-checking similar moves (promotions...)
2002 return moves
.filter(m
=> {
2003 const key
= m
.start
.x
+ m
.start
.y
+ '.' + m
.end
.x
+ m
.end
.y
;
2004 if (!filtered
[key
]) {
2005 this.playOnBoard(m
);
2006 let square
= kingPos
,
2007 res
= true; //a priori valid
2008 if (m
.vanish
.some(v
=> {
2009 return this.isKing(v
.p
) && v
.c
== color
;
2011 // Search king in appear array:
2013 m
.appear
.findIndex(a
=> {
2014 return this.isKing(a
.p
) && a
.c
== color
;
2016 if (newKingIdx
>= 0)
2017 square
= [m
.appear
[newKingIdx
].x
, m
.appear
[newKingIdx
].y
];
2021 res
&&= !this.underCheck(square
, oppCol
);
2022 this.undoOnBoard(m
);
2023 filtered
[key
] = res
;
2026 return filtered
[key
];
2033 // Aggregate flags into one object
2035 return this.castleFlags
;
2038 // Reverse operation
2039 disaggregateFlags(flags
) {
2040 this.castleFlags
= flags
;
2043 // Apply a move on board
2045 for (let psq
of move.vanish
)
2046 this.board
[psq
.x
][psq
.y
] = "";
2047 for (let psq
of move.appear
)
2048 this.board
[psq
.x
][psq
.y
] = psq
.c
+ psq
.p
;
2050 // Un-apply the played move
2052 for (let psq
of move.appear
)
2053 this.board
[psq
.x
][psq
.y
] = "";
2054 for (let psq
of move.vanish
)
2055 this.board
[psq
.x
][psq
.y
] = psq
.c
+ psq
.p
;
2058 updateCastleFlags(move) {
2059 // Update castling flags if start or arrive from/at rook/king locations
2060 move.appear
.concat(move.vanish
).forEach(psq
=> {
2062 this.board
[psq
.x
][psq
.y
] != "" &&
2063 this.getPieceType(psq
.x
, psq
.y
) == "k"
2065 this.castleFlags
[psq
.c
] = [this.size
.y
, this.size
.y
];
2067 // NOTE: not "else if" because king can capture enemy rook...
2071 else if (psq
.x
== this.size
.x
- 1)
2074 const fidx
= this.castleFlags
[c
].findIndex(f
=> f
== psq
.y
);
2076 this.castleFlags
[c
][fidx
] = this.size
.y
;
2084 // If flags already off, no need to re-check:
2085 Object
.keys(this.castleFlags
).some(c
=> {
2086 return this.castleFlags
[c
].some(val
=> val
< this.size
.y
)})
2088 this.updateCastleFlags(move);
2090 if (this.options
["crazyhouse"]) {
2091 move.vanish
.forEach(v
=> {
2092 const square
= C
.CoordsToSquare({x: v
.x
, y: v
.y
});
2093 if (this.ispawn
[square
])
2094 delete this.ispawn
[square
];
2096 if (move.appear
.length
> 0 && move.vanish
.length
> 0) {
2097 // Assumption: something is moving
2098 const initSquare
= C
.CoordsToSquare(move.start
);
2099 const destSquare
= C
.CoordsToSquare(move.end
);
2101 this.ispawn
[initSquare
] ||
2102 (move.vanish
[0].p
== "p" && move.appear
[0].p
!= "p")
2104 this.ispawn
[destSquare
] = true;
2107 this.ispawn
[destSquare
] &&
2108 this.getColor(move.end
.x
, move.end
.y
) != move.vanish
[0].c
2110 move.vanish
[1].p
= "p";
2111 delete this.ispawn
[destSquare
];
2115 const minSize
= Math
.min(move.appear
.length
, move.vanish
.length
);
2118 // Warning; atomic pawn removal isn't a capture
2119 (!this.options
["atomic"] || !this.rempawn
|| this.movesCount
>= 1)
2121 const color
= this.turn
;
2122 for (let i
=minSize
; i
<move.appear
.length
; i
++) {
2123 // Something appears = dropped on board (some exceptions, Chakart...)
2124 if (move.appear
[i
].c
== color
) {
2125 const piece
= move.appear
[i
].p
;
2126 this.updateReserve(color
, piece
, this.reserve
[color
][piece
] - 1);
2129 for (let i
=minSize
; i
<move.vanish
.length
; i
++) {
2130 // Something vanish: add to reserve except if recycle & opponent
2132 this.options
["crazyhouse"] ||
2133 (this.options
["recycle"] && move.vanish
[i
].c
== color
)
2135 const piece
= move.vanish
[i
].p
;
2136 this.updateReserve(color
, piece
, this.reserve
[color
][piece
] + 1);
2144 if (this.hasEnpassant
)
2145 this.epSquare
= this.getEpSquare(move);
2146 this.playOnBoard(move);
2147 this.postPlay(move);
2151 const color
= this.turn
;
2152 const oppCol
= C
.GetOppCol(color
);
2153 if (this.options
["dark"])
2154 this.updateEnlightened();
2155 if (this.options
["teleport"]) {
2157 this.subTurnTeleport
== 1 &&
2158 move.vanish
.length
> move.appear
.length
&&
2159 move.vanish
[move.vanish
.length
- 1].c
== color
2161 const v
= move.vanish
[move.vanish
.length
- 1];
2162 this.captured
= {x: v
.x
, y: v
.y
, c: v
.c
, p: v
.p
};
2163 this.subTurnTeleport
= 2;
2166 this.subTurnTeleport
= 1;
2167 this.captured
= null;
2173 this.options
["doublemove"] &&
2174 this.movesCount
>= 1 &&
2178 (this.options
["progressive"] && this.subTurn
<= this.movesCount
)
2181 const oppKingPos
= this.searchKingPos(oppCol
);
2183 oppKingPos
[0] >= 0 &&
2185 this.options
["taking"] ||
2186 !this.underCheck(oppKingPos
, color
)
2193 if (this.isLastMove(move)) {
2202 (this.options
["balance"] && ![1, 3].includes(this.movesCount
)) ||
2207 // "Stop at the first move found"
2208 atLeastOneMove(color
) {
2209 color
= color
|| this.turn
;
2210 for (let i
= 0; i
< this.size
.x
; i
++) {
2211 for (let j
= 0; j
< this.size
.y
; j
++) {
2212 if (this.board
[i
][j
] != "" && this.getColor(i
, j
) == color
) {
2213 // NOTE: in fact searching for all potential moves from i,j.
2214 // I don't believe this is an issue, for now at least.
2215 const moves
= this.getPotentialMovesFrom([i
, j
]);
2216 if (moves
.some(m
=> this.filterValid([m
]).length
>= 1))
2221 if (this.hasReserve
&& this.reserve
[color
]) {
2222 for (let p
of Object
.keys(this.reserve
[color
])) {
2223 const moves
= this.getDropMovesFrom([color
, p
]);
2224 if (moves
.some(m
=> this.filterValid([m
]).length
>= 1))
2231 // What is the score ? (Interesting if game is over)
2232 getCurrentScore(move) {
2233 const color
= this.turn
;
2234 const oppCol
= C
.GetOppCol(color
);
2235 const kingPos
= [this.searchKingPos(color
), this.searchKingPos(oppCol
)];
2236 if (kingPos
[0][0] < 0 && kingPos
[1][0] < 0)
2238 if (kingPos
[0][0] < 0)
2239 return (color
== "w" ? "0-1" : "1-0");
2240 if (kingPos
[1][0] < 0)
2241 return (color
== "w" ? "1-0" : "0-1");
2242 if (this.atLeastOneMove())
2244 // No valid move: stalemate or checkmate?
2245 if (!this.underCheck(kingPos
[0], color
))
2248 return (color
== "w" ? "0-1" : "1-0");
2251 playVisual(move, r
) {
2252 move.vanish
.forEach(v
=> {
2253 this.g_pieces
[v
.x
][v
.y
].remove();
2254 this.g_pieces
[v
.x
][v
.y
] = null;
2257 document
.getElementById(this.containerId
).querySelector(".chessboard");
2259 r
= chessboard
.getBoundingClientRect();
2260 const pieceWidth
= this.getPieceWidth(r
.width
);
2261 move.appear
.forEach(a
=> {
2262 this.g_pieces
[a
.x
][a
.y
] = document
.createElement("piece");
2263 C
.AddClass_es(this.g_pieces
[a
.x
][a
.y
],
2264 this.pieces(a
.c
, a
.x
, a
.y
)[a
.p
]["class"]);
2265 this.g_pieces
[a
.x
][a
.y
].classList
.add(C
.GetColorClass(a
.c
));
2266 this.g_pieces
[a
.x
][a
.y
].style
.width
= pieceWidth
+ "px";
2267 this.g_pieces
[a
.x
][a
.y
].style
.height
= pieceWidth
+ "px";
2268 const [ip
, jp
] = this.getPixelPosition(a
.x
, a
.y
, r
);
2269 // Translate coordinates to use chessboard as reference:
2270 this.g_pieces
[a
.x
][a
.y
].style
.transform
=
2271 `translate(${ip - r.x}px,${jp - r.y}px)`;
2272 if (this.enlightened
&& !this.enlightened
[a
.x
][a
.y
])
2273 this.g_pieces
[a
.x
][a
.y
].classList
.add("hidden");
2274 chessboard
.appendChild(this.g_pieces
[a
.x
][a
.y
]);
2276 if (this.options
["dark"])
2277 this.graphUpdateEnlightened();
2280 // TODO: send stack receive stack, or allow incremental? (good/bad points)
2281 buildMoveStack(move, r
) {
2282 this.moveStack
.push(move);
2283 this.computeNextMove(move);
2285 const newTurn
= this.turn
;
2286 if (this.moveStack
.length
== 1)
2287 this.playVisual(move, r
);
2291 board: JSON
.parse(JSON
.stringify(this.board
)) //easier
2293 this.buildMoveStack(move.next
, r
);
2296 if (this.moveStack
.length
== 1) {
2297 // Usual case (one normal move)
2298 this.afterPlay(this.moveStack
, newTurn
, {send: true, res: true});
2302 this.afterPlay(this.moveStack
, newTurn
, {send: true, res: false});
2303 this.re_initFromFen(this.gameState
.fen
, this.gameState
.board
);
2304 this.playReceivedMove(this.moveStack
.slice(1), () => {
2305 this.afterPlay(this.moveStack
, newTurn
, {send: false, res: true});
2312 // Implemented in variants using (automatic) moveStack
2313 computeNextMove(move) {}
2316 // Works for all rectangular boards:
2317 return Math
.sqrt(r
.width
** 2 + r
.height
** 2);
2321 return (typeof x
== "string" ? this.r_pieces : this.g_pieces
)[x
][y
];
2324 animateMoving(start
, end
, drag
, segments
, cb
) {
2325 let initPiece
= this.getDomPiece(start
.x
, start
.y
);
2326 // NOTE: cloning often not required, but light enough, and simpler
2327 let movingPiece
= initPiece
.cloneNode();
2328 initPiece
.style
.opacity
= "0";
2330 document
.getElementById(this.containerId
)
2331 const r
= container
.querySelector(".chessboard").getBoundingClientRect();
2332 if (typeof start
.x
== "string") {
2333 // Need to bound width/height (was 100% for reserve pieces)
2334 const pieceWidth
= this.getPieceWidth(r
.width
);
2335 movingPiece
.style
.width
= pieceWidth
+ "px";
2336 movingPiece
.style
.height
= pieceWidth
+ "px";
2338 const maxDist
= this.getMaxDistance(r
);
2339 const apparentColor
= this.getColor(start
.x
, start
.y
);
2340 const pieces
= this.pieces(apparentColor
, start
.x
, start
.y
);
2342 const startCode
= this.getPiece(start
.x
, start
.y
);
2343 C
.RemoveClass_es(movingPiece
, pieces
[startCode
]["class"]);
2344 C
.AddClass_es(movingPiece
, pieces
[drag
.p
]["class"]);
2345 if (apparentColor
!= drag
.c
) {
2346 movingPiece
.classList
.remove(C
.GetColorClass(apparentColor
));
2347 movingPiece
.classList
.add(C
.GetColorClass(drag
.c
));
2350 container
.appendChild(movingPiece
);
2351 const animateSegment
= (index
, cb
) => {
2352 // NOTE: move.drag could be generalized per-segment (usage?)
2353 const [i1
, j1
] = segments
[index
][0];
2354 const [i2
, j2
] = segments
[index
][1];
2355 const dep
= this.getPixelPosition(i1
, j1
, r
);
2356 const arr
= this.getPixelPosition(i2
, j2
, r
);
2357 movingPiece
.style
.transitionDuration
= "0s";
2358 movingPiece
.style
.transform
= `translate(${dep[0]}px, ${dep[1]}px)`;
2360 Math
.sqrt((arr
[0] - dep
[0]) ** 2 + (arr
[1] - dep
[1]) ** 2);
2361 const duration
= 0.2 + (distance
/ maxDist
) * 0.3;
2362 // TODO: unclear why we need this new delay below:
2364 movingPiece
.style
.transitionDuration
= duration
+ "s";
2365 // movingPiece is child of container: no need to adjust coordinates
2366 movingPiece
.style
.transform
= `translate(${arr[0]}px, ${arr[1]}px)`;
2367 setTimeout(cb
, duration
* 1000);
2371 const animateSegmentCallback
= () => {
2372 if (index
< segments
.length
)
2373 animateSegment(index
++, animateSegmentCallback
);
2375 movingPiece
.remove();
2376 initPiece
.style
.opacity
= "1";
2380 animateSegmentCallback();
2383 // Input array of objects with at least fields x,y (e.g. PiPo)
2384 animateFading(arr
, cb
) {
2385 const animLength
= 350; //TODO: 350ms? More? Less?
2387 let fadingPiece
= this.getDomPiece(v
.x
, v
.y
);
2388 fadingPiece
.style
.transitionDuration
= (animLength
/ 1000) + "s";
2389 fadingPiece
.style
.opacity
= "0";
2391 setTimeout(cb
, animLength
);
2394 animate(move, callback
) {
2395 if (this.noAnimate
|| move.noAnimate
) {
2399 let segments
= move.segments
;
2401 segments
= [ [[move.start
.x
, move.start
.y
], [move.end
.x
, move.end
.y
]] ];
2402 let targetObj
= new TargetObj(callback
);
2403 if (move.start
.x
!= move.end
.x
|| move.start
.y
!= move.end
.y
) {
2405 this.animateMoving(move.start
, move.end
, move.drag
, segments
,
2406 () => targetObj
.increment());
2408 if (move.vanish
.length
> move.appear
.length
) {
2409 const arr
= move.vanish
.slice(move.appear
.length
)
2410 // Ignore disappearing pieces hidden by some appearing ones:
2411 .filter(v
=> move.appear
.every(a
=> a
.x
!= v
.x
|| a
.y
!= v
.y
));
2412 if (arr
.length
> 0) {
2414 this.animateFading(arr
, () => targetObj
.increment());
2418 this.customAnimate(move, segments
, () => targetObj
.increment());
2419 if (targetObj
.target
== 0)
2423 // Potential other animations (e.g. for Suction variant)
2424 customAnimate(move, segments
, cb
) {
2425 return 0; //nb of targets
2428 playReceivedMove(moves
, callback
) {
2429 const launchAnimation
= () => {
2430 const r
= container
.querySelector(".chessboard").getBoundingClientRect();
2431 const animateRec
= i
=> {
2432 this.animate(moves
[i
], () => {
2433 this.play(moves
[i
]);
2434 this.playVisual(moves
[i
], r
);
2435 if (i
< moves
.length
- 1)
2436 setTimeout(() => animateRec(i
+1), 300);
2443 // Delay if user wasn't focused:
2444 const checkDisplayThenAnimate
= (delay
) => {
2445 if (container
.style
.display
== "none") {
2446 alert("New move! Let's go back to game...");
2447 document
.getElementById("gameInfos").style
.display
= "none";
2448 container
.style
.display
= "block";
2449 setTimeout(launchAnimation
, 700);
2452 setTimeout(launchAnimation
, delay
|| 0);
2454 let container
= document
.getElementById(this.containerId
);
2455 if (document
.hidden
) {
2456 document
.onvisibilitychange
= () => {
2457 document
.onvisibilitychange
= undefined;
2458 checkDisplayThenAnimate(700);
2462 checkDisplayThenAnimate();