Some bugs fixes
[xogo.git] / base_rules.js
CommitLineData
41534b92
BA
1import { Random } from "/utils/alea.js";
2import { ArrayFun } from "/utils/array.js";
3import PiPo from "/utils/PiPo.js";
4import Move from "/utils/Move.js";
5
6// NOTE: x coords: top to bottom (white perspective); y: left to right
cc2c7183 7// NOTE: ChessRules is aliased as window.C, and variants as window.V
41534b92
BA
8export default class ChessRules {
9
e5f93427 10 static get Aliases() {
3caec36f 11 return {'C': ChessRules};
e5f93427
BA
12 }
13
41534b92
BA
14 /////////////////////////
15 // VARIANT SPECIFICATIONS
16
17 // Some variants have specific options, like the number of pawns in Monster,
18 // or the board size for Pandemonium.
19 // Users can generally select a randomness level from 0 to 2.
20 static get Options() {
21 return {
41534b92
BA
22 select: [{
23 label: "Randomness",
24 variable: "randomness",
25 defaut: 0,
26 options: [
b4ae3ff6
BA
27 {label: "Deterministic", value: 0},
28 {label: "Symmetric random", value: 1},
29 {label: "Asymmetric random", value: 2}
41534b92
BA
30 ]
31 }],
437dfd42 32 input: [
f8b43ef7
BA
33 {
34 label: "Capture king",
437dfd42
BA
35 variable: "taking",
36 type: "checkbox",
37 defaut: false
f8b43ef7
BA
38 },
39 {
40 label: "Falling pawn",
437dfd42
BA
41 variable: "pawnfall",
42 type: "checkbox",
43 defaut: false
f8b43ef7
BA
44 }
45 ],
41534b92
BA
46 // Game modifiers (using "elementary variants"). Default: false
47 styles: [
48 "atomic",
49 "balance", //takes precedence over doublemove & progressive
50 "cannibal",
51 "capture",
52 "crazyhouse",
53 "cylinder", //ok with all
54 "dark",
55 "doublemove",
56 "madrasi",
57 "progressive", //(natural) priority over doublemove
58 "recycle",
59 "rifle",
60 "teleport",
61 "zen"
62 ]
63 };
64 }
65
c9ab0340
BA
66 get pawnPromotions() {
67 return ['q', 'r', 'n', 'b'];
41534b92
BA
68 }
69
70 // Some variants don't have flags:
71 get hasFlags() {
72 return true;
73 }
74 // Or castle
75 get hasCastle() {
76 return this.hasFlags;
77 }
78
79 // En-passant captures allowed?
80 get hasEnpassant() {
81 return true;
82 }
83
84 get hasReserve() {
85 return (
86 !!this.options["crazyhouse"] ||
87 (!!this.options["recycle"] && !this.options["teleport"])
88 );
89 }
90
91 get noAnimate() {
92 return !!this.options["dark"];
93 }
94
95 // Some variants use click infos:
15106e82
BA
96 doClick(coords) {
97 if (typeof coords.x != "number")
b4ae3ff6 98 return null; //click on reserves
41534b92 99 if (
cc2c7183 100 this.options["teleport"] && this.subTurnTeleport == 2 &&
15106e82 101 this.board[coords.x][coords.y] == ""
41534b92 102 ) {
1a7c0492 103 let res = new Move({
41534b92
BA
104 start: {x: this.captured.x, y: this.captured.y},
105 appear: [
106 new PiPo({
15106e82
BA
107 x: coords.x,
108 y: coords.y,
41534b92
BA
109 c: this.captured.c, //this.turn,
110 p: this.captured.p
111 })
112 ],
1a7c0492 113 vanish: []
41534b92 114 });
1a7c0492
BA
115 res.drag = {c: this.captured.c, p: this.captured.p};
116 return res;
41534b92
BA
117 }
118 return null;
119 }
120
121 ////////////////////
122 // COORDINATES UTILS
123
4bff03f5 124 // 3a --> {x:3, y:10}
41534b92 125 static SquareToCoords(sq) {
15106e82
BA
126 return ArrayFun.toObject(["x", "y"],
127 [0, 1].map(i => parseInt(sq[i], 36)));
41534b92
BA
128 }
129
4bff03f5 130 // {x:11, y:12} --> bc
15106e82
BA
131 static CoordsToSquare(cd) {
132 return Object.values(cd).map(c => c.toString(36)).join("");
41534b92
BA
133 }
134
15106e82
BA
135 coordsToId(cd) {
136 if (typeof cd.x == "number") {
137 return (
138 `${this.containerId}|sq-${cd.x.toString(36)}-${cd.y.toString(36)}`
139 );
140 }
41534b92 141 // Reserve :
15106e82 142 return `${this.containerId}|rsq-${cd.x}-${cd.y}`;
41534b92
BA
143 }
144
145 idToCoords(targetId) {
b4ae3ff6
BA
146 if (!targetId)
147 return null; //outside page, maybe...
41534b92
BA
148 const idParts = targetId.split('|'); //prefix|sq-2-3 (start at 0 => 3,4)
149 if (
150 idParts.length < 2 ||
151 idParts[0] != this.containerId ||
152 !idParts[1].match(/sq-[0-9a-zA-Z]-[0-9a-zA-Z]/)
153 ) {
154 return null;
155 }
156 const squares = idParts[1].split('-');
157 if (squares[0] == "sq")
15106e82
BA
158 return {x: parseInt(squares[1], 36), y: parseInt(squares[2], 36)};
159 // squares[0] == "rsq" : reserve, 'c' + 'p' (letters color & piece)
160 return {x: squares[1], y: squares[2]};
41534b92
BA
161 }
162
163 /////////////
164 // FEN UTILS
165
166 // Turn "wb" into "B" (for FEN)
167 board2fen(b) {
4bff03f5 168 return (b[0] == "w" ? b[1].toUpperCase() : b[1]);
41534b92
BA
169 }
170
171 // Turn "p" into "bp" (for board)
172 fen2board(f) {
4bff03f5 173 return (f.charCodeAt(0) <= 90 ? "w" + f.toLowerCase() : "b" + f);
41534b92
BA
174 }
175
176 // Setup the initial random-or-not (asymmetric-or-not) position
177 genRandInitFen(seed) {
178 Random.setSeed(seed);
179
180 let fen, flags = "0707";
cc2c7183 181 if (!this.options.randomness)
41534b92
BA
182 // Deterministic:
183 fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w 0";
184
185 else {
186 // Randomize
187 let pieces = { w: new Array(8), b: new Array(8) };
188 flags = "";
189 // Shuffle pieces on first (and last rank if randomness == 2)
190 for (let c of ["w", "b"]) {
191 if (c == 'b' && this.options.randomness == 1) {
192 pieces['b'] = pieces['w'];
193 flags += flags;
194 break;
195 }
196
197 let positions = ArrayFun.range(8);
198
199 // Get random squares for bishops
200 let randIndex = 2 * Random.randInt(4);
201 const bishop1Pos = positions[randIndex];
202 // The second bishop must be on a square of different color
203 let randIndex_tmp = 2 * Random.randInt(4) + 1;
204 const bishop2Pos = positions[randIndex_tmp];
205 // Remove chosen squares
206 positions.splice(Math.max(randIndex, randIndex_tmp), 1);
207 positions.splice(Math.min(randIndex, randIndex_tmp), 1);
208
209 // Get random squares for knights
210 randIndex = Random.randInt(6);
211 const knight1Pos = positions[randIndex];
212 positions.splice(randIndex, 1);
213 randIndex = Random.randInt(5);
214 const knight2Pos = positions[randIndex];
215 positions.splice(randIndex, 1);
216
217 // Get random square for queen
218 randIndex = Random.randInt(4);
219 const queenPos = positions[randIndex];
220 positions.splice(randIndex, 1);
221
222 // Rooks and king positions are now fixed,
223 // because of the ordering rook-king-rook
224 const rook1Pos = positions[0];
225 const kingPos = positions[1];
226 const rook2Pos = positions[2];
227
228 // Finally put the shuffled pieces in the board array
229 pieces[c][rook1Pos] = "r";
230 pieces[c][knight1Pos] = "n";
231 pieces[c][bishop1Pos] = "b";
232 pieces[c][queenPos] = "q";
233 pieces[c][kingPos] = "k";
234 pieces[c][bishop2Pos] = "b";
235 pieces[c][knight2Pos] = "n";
236 pieces[c][rook2Pos] = "r";
237 flags += rook1Pos.toString() + rook2Pos.toString();
238 }
239 fen = (
240 pieces["b"].join("") +
241 "/pppppppp/8/8/8/8/PPPPPPPP/" +
242 pieces["w"].join("").toUpperCase() +
243 " w 0"
244 );
245 }
246 // Add turn + flags + enpassant (+ reserve)
247 let parts = [];
b4ae3ff6
BA
248 if (this.hasFlags)
249 parts.push(`"flags":"${flags}"`);
250 if (this.hasEnpassant)
251 parts.push('"enpassant":"-"');
252 if (this.hasReserve)
253 parts.push('"reserve":"000000000000"');
254 if (this.options["crazyhouse"])
255 parts.push('"ispawn":"-"');
256 if (parts.length >= 1)
257 fen += " {" + parts.join(",") + "}";
41534b92
BA
258 return fen;
259 }
260
261 // "Parse" FEN: just return untransformed string data
262 parseFen(fen) {
263 const fenParts = fen.split(" ");
264 let res = {
265 position: fenParts[0],
266 turn: fenParts[1],
267 movesCount: fenParts[2]
268 };
b4ae3ff6
BA
269 if (fenParts.length > 3)
270 res = Object.assign(res, JSON.parse(fenParts[3]));
41534b92
BA
271 return res;
272 }
273
274 // Return current fen (game state)
275 getFen() {
276 let fen = (
15106e82 277 this.getPosition() + " " +
41534b92
BA
278 this.getTurnFen() + " " +
279 this.movesCount
280 );
281 let parts = [];
b4ae3ff6
BA
282 if (this.hasFlags)
283 parts.push(`"flags":"${this.getFlagsFen()}"`);
41534b92
BA
284 if (this.hasEnpassant)
285 parts.push(`"enpassant":"${this.getEnpassantFen()}"`);
b4ae3ff6
BA
286 if (this.hasReserve)
287 parts.push(`"reserve":"${this.getReserveFen()}"`);
41534b92
BA
288 if (this.options["crazyhouse"])
289 parts.push(`"ispawn":"${this.getIspawnFen()}"`);
b4ae3ff6
BA
290 if (parts.length >= 1)
291 fen += " {" + parts.join(",") + "}";
41534b92
BA
292 return fen;
293 }
294
d621e620
BA
295 static FenEmptySquares(count) {
296 // if more than 9 consecutive free spaces, break the integer,
297 // otherwise FEN parsing will fail.
298 if (count <= 9)
299 return count;
300 // Most boards of size < 18:
301 if (count <= 18)
302 return "9" + (count - 9);
303 // Except Gomoku:
304 return "99" + (count - 18);
305 }
306
41534b92 307 // Position part of the FEN string
15106e82 308 getPosition() {
41534b92
BA
309 let position = "";
310 for (let i = 0; i < this.size.y; i++) {
311 let emptyCount = 0;
312 for (let j = 0; j < this.size.x; j++) {
b4ae3ff6
BA
313 if (this.board[i][j] == "")
314 emptyCount++;
41534b92
BA
315 else {
316 if (emptyCount > 0) {
317 // Add empty squares in-between
d621e620 318 position += C.FenEmptySquares(emptyCount);
41534b92
BA
319 emptyCount = 0;
320 }
321 position += this.board2fen(this.board[i][j]);
322 }
323 }
324 if (emptyCount > 0)
325 // "Flush remainder"
d621e620 326 position += C.FenEmptySquares(emptyCount);
b4ae3ff6
BA
327 if (i < this.size.y - 1)
328 position += "/"; //separate rows
41534b92
BA
329 }
330 return position;
331 }
332
333 getTurnFen() {
334 return this.turn;
335 }
336
337 // Flags part of the FEN string
338 getFlagsFen() {
339 return ["w", "b"].map(c => {
15106e82 340 return this.castleFlags[c].map(x => x.toString(36)).join("");
41534b92
BA
341 }).join("");
342 }
343
344 // Enpassant part of the FEN string
345 getEnpassantFen() {
b4ae3ff6
BA
346 if (!this.epSquare)
347 return "-"; //no en-passant
cc2c7183 348 return C.CoordsToSquare(this.epSquare);
41534b92
BA
349 }
350
351 getReserveFen() {
352 return (
353 ["w","b"].map(c => Object.values(this.reserve[c]).join("")).join("")
354 );
355 }
356
357 getIspawnFen() {
15106e82
BA
358 const squares = Object.keys(this.ispawn);
359 if (squares.length == 0)
b4ae3ff6 360 return "-";
15106e82 361 return squares.join(",");
41534b92
BA
362 }
363
364 // Set flags from fen (castle: white a,h then black a,h)
365 setFlags(fenflags) {
366 this.castleFlags = {
15106e82
BA
367 w: [0, 1].map(i => parseInt(fenflags.charAt(i), 36)),
368 b: [2, 3].map(i => parseInt(fenflags.charAt(i), 36))
41534b92
BA
369 };
370 }
371
372 //////////////////
373 // INITIALIZATION
374
41534b92
BA
375 constructor(o) {
376 this.options = o.options;
535c464b
BA
377 // Fill missing options (always the case if random challenge)
378 (V.Options.select || []).concat(V.Options.input || []).forEach(opt => {
379 if (this.options[opt.variable] === undefined)
380 this.options[opt.variable] = opt.defaut;
381 });
41534b92 382 this.playerColor = o.color;
15106e82 383 this.afterPlay = o.afterPlay; //trigger some actions after playing a move
41534b92 384
c9ab0340 385 // Fen string fully describes the game state
b4ae3ff6
BA
386 if (!o.fen)
387 o.fen = this.genRandInitFen(o.seed);
41534b92
BA
388 const fenParsed = this.parseFen(o.fen);
389 this.board = this.getBoard(fenParsed.position);
390 this.turn = fenParsed.turn;
391 this.movesCount = parseInt(fenParsed.movesCount, 10);
392 this.setOtherVariables(fenParsed);
393
394 // Graphical (can use variables defined above)
395 this.containerId = o.element;
396 this.graphicalInit();
397 }
398
399 // Turn position fen into double array ["wb","wp","bk",...]
400 getBoard(position) {
401 const rows = position.split("/");
402 let board = ArrayFun.init(this.size.x, this.size.y, "");
403 for (let i = 0; i < rows.length; i++) {
404 let j = 0;
405 for (let indexInRow = 0; indexInRow < rows[i].length; indexInRow++) {
406 const character = rows[i][indexInRow];
407 const num = parseInt(character, 10);
408 // If num is a number, just shift j:
b4ae3ff6
BA
409 if (!isNaN(num))
410 j += num;
41534b92 411 // Else: something at position i,j
b4ae3ff6
BA
412 else
413 board[i][j++] = this.fen2board(character);
41534b92
BA
414 }
415 }
416 return board;
417 }
418
419 // Some additional variables from FEN (variant dependant)
420 setOtherVariables(fenParsed) {
421 // Set flags and enpassant:
b4ae3ff6
BA
422 if (this.hasFlags)
423 this.setFlags(fenParsed.flags);
41534b92
BA
424 if (this.hasEnpassant)
425 this.epSquare = this.getEpSquare(fenParsed.enpassant);
b4ae3ff6
BA
426 if (this.hasReserve)
427 this.initReserves(fenParsed.reserve);
428 if (this.options["crazyhouse"])
429 this.initIspawn(fenParsed.ispawn);
41534b92 430 this.subTurn = 1; //may be unused
cc2c7183
BA
431 if (this.options["teleport"]) {
432 this.subTurnTeleport = 1;
433 this.captured = null;
434 }
41534b92 435 if (this.options["dark"]) {
41534b92 436 // Setup enlightened: squares reachable by player side
c9ab0340
BA
437 this.enlightened = ArrayFun.init(this.size.x, this.size.y, false);
438 this.updateEnlightened();
41534b92
BA
439 }
440 }
441
c9ab0340
BA
442 updateEnlightened() {
443 this.oldEnlightened = this.enlightened;
444 this.enlightened = ArrayFun.init(this.size.x, this.size.y, false);
41534b92 445 // Add pieces positions + all squares reachable by moves (includes Zen):
41534b92
BA
446 for (let x=0; x<this.size.x; x++) {
447 for (let y=0; y<this.size.y; y++) {
448 if (this.board[x][y] != "" && this.getColor(x, y) == this.playerColor)
449 {
c9ab0340 450 this.enlightened[x][y] = true;
41534b92 451 this.getPotentialMovesFrom([x, y]).forEach(m => {
c9ab0340 452 this.enlightened[m.end.x][m.end.y] = true;
41534b92
BA
453 });
454 }
455 }
456 }
b4ae3ff6 457 if (this.epSquare)
c9ab0340 458 this.enlightEnpassant();
41534b92
BA
459 }
460
c9ab0340
BA
461 // Include square of the en-passant capturing square:
462 enlightEnpassant() {
463 // NOTE: shortcut, pawn has only one attack type, doesn't depend on square
464 const steps = this.pieces(this.playerColor)["p"].attack[0].steps;
41534b92
BA
465 for (let step of steps) {
466 const x = this.epSquare.x - step[0],
d262cff4 467 y = this.getY(this.epSquare.y - step[1]);
41534b92
BA
468 if (
469 this.onBoard(x, y) &&
470 this.getColor(x, y) == this.playerColor &&
cc2c7183 471 this.getPieceType(x, y) == "p"
41534b92 472 ) {
c9ab0340 473 this.enlightened[x][this.epSquare.y] = true;
41534b92
BA
474 break;
475 }
476 }
477 }
478
c9ab0340 479 // ordering as in pieces() p,r,n,b,q,k (+ count in base 30 if needed)
41534b92
BA
480 initReserves(reserveStr) {
481 const counts = reserveStr.split("").map(c => parseInt(c, 30));
482 this.reserve = { w: {}, b: {} };
c9ab0340
BA
483 const pieceName = ['p', 'r', 'n', 'b', 'q', 'k'];
484 const L = pieceName.length;
485 for (let i of ArrayFun.range(2 * L)) {
486 if (i < L)
b4ae3ff6
BA
487 this.reserve['w'][pieceName[i]] = counts[i];
488 else
c9ab0340 489 this.reserve['b'][pieceName[i-L]] = counts[i];
41534b92
BA
490 }
491 }
492
493 initIspawn(ispawnStr) {
15106e82
BA
494 if (ispawnStr != "-")
495 this.ispawn = ArrayFun.toObject(ispawnStr.split(","), true);
b4ae3ff6
BA
496 else
497 this.ispawn = {};
41534b92
BA
498 }
499
500 getNbReservePieces(color) {
501 return (
502 Object.values(this.reserve[color]).reduce(
503 (oldV,newV) => oldV + (newV > 0 ? 1 : 0), 0)
504 );
505 }
506
15106e82
BA
507 getRankInReserve(c, p) {
508 const pieces = Object.keys(this.pieces());
509 const lastIndex = pieces.findIndex(pp => pp == p)
510 let toTest = pieces.slice(0, lastIndex);
511 return toTest.reduce(
512 (oldV,newV) => oldV + (this.reserve[c][newV] > 0 ? 1 : 0), 0);
513 }
514
41534b92
BA
515 //////////////
516 // VISUAL PART
517
518 getPieceWidth(rwidth) {
519 return (rwidth / this.size.y);
520 }
521
41534b92 522 getReserveSquareSize(rwidth, nbR) {
15106e82 523 const sqSize = this.getPieceWidth(rwidth);
41534b92
BA
524 return Math.min(sqSize, rwidth / nbR);
525 }
526
527 getReserveNumId(color, piece) {
528 return `${this.containerId}|rnum-${color}${piece}`;
529 }
530
531 graphicalInit() {
532 // NOTE: not window.onresize = this.re_drawBoardElts because scope (this)
533 window.onresize = () => this.re_drawBoardElements();
534 this.re_drawBoardElements();
535 this.initMouseEvents();
3c61449b
BA
536 const chessboard =
537 document.getElementById(this.containerId).querySelector(".chessboard");
41534b92
BA
538 }
539
540 re_drawBoardElements() {
541 const board = this.getSvgChessboard();
cc2c7183 542 const oppCol = C.GetOppCol(this.playerColor);
3c61449b
BA
543 let chessboard =
544 document.getElementById(this.containerId).querySelector(".chessboard");
545 chessboard.innerHTML = "";
546 chessboard.insertAdjacentHTML('beforeend', board);
41534b92
BA
547 // Compare window ratio width / height to aspectRatio:
548 const windowRatio = window.innerWidth / window.innerHeight;
549 let cbWidth, cbHeight;
15106e82 550 if (windowRatio <= this.size.ratio) {
41534b92
BA
551 // Limiting dimension is width:
552 cbWidth = Math.min(window.innerWidth, 767);
15106e82 553 cbHeight = cbWidth / this.size.ratio;
41534b92
BA
554 }
555 else {
556 // Limiting dimension is height:
557 cbHeight = Math.min(window.innerHeight, 767);
15106e82 558 cbWidth = cbHeight * this.size.ratio;
41534b92 559 }
1a7c0492 560 if (this.hasReserve) {
41534b92
BA
561 const sqSize = cbWidth / this.size.y;
562 // NOTE: allocate space for reserves (up/down) even if they are empty
15106e82 563 // Cannot use getReserveSquareSize() here, but sqSize is an upper bound.
41534b92
BA
564 if ((window.innerHeight - cbHeight) / 2 < sqSize + 5) {
565 cbHeight = window.innerHeight - 2 * (sqSize + 5);
15106e82 566 cbWidth = cbHeight * this.size.ratio;
41534b92
BA
567 }
568 }
3c61449b
BA
569 chessboard.style.width = cbWidth + "px";
570 chessboard.style.height = cbHeight + "px";
41534b92
BA
571 // Center chessboard:
572 const spaceLeft = (window.innerWidth - cbWidth) / 2,
573 spaceTop = (window.innerHeight - cbHeight) / 2;
3c61449b
BA
574 chessboard.style.left = spaceLeft + "px";
575 chessboard.style.top = spaceTop + "px";
41534b92
BA
576 // Give sizes instead of recomputing them,
577 // because chessboard might not be drawn yet.
578 this.setupPieces({
579 width: cbWidth,
580 height: cbHeight,
581 x: spaceLeft,
582 y: spaceTop
583 });
584 }
585
586 // Get SVG board (background, no pieces)
587 getSvgChessboard() {
41534b92
BA
588 const flipped = (this.playerColor == 'b');
589 let board = `
590 <svg
591 viewBox="0 0 80 80"
535c464b 592 class="chessboard_SVG">`;
728cb1e3
BA
593 for (let i=0; i < this.size.x; i++) {
594 for (let j=0; j < this.size.y; j++) {
41534b92
BA
595 const ii = (flipped ? this.size.x - 1 - i : i);
596 const jj = (flipped ? this.size.y - 1 - j : j);
c7bf7b1b
BA
597 let classes = this.getSquareColorClass(ii, jj);
598 if (this.enlightened && !this.enlightened[ii][jj])
599 classes += " in-shadow";
41534b92 600 // NOTE: x / y reversed because coordinates system is reversed.
535c464b
BA
601 board += `
602 <rect
603 class="${classes}"
604 id="${this.coordsToId({x: ii, y: jj})}"
605 width="10"
606 height="10"
607 x="${10*j}"
608 y="${10*i}"
609 />`;
41534b92
BA
610 }
611 }
535c464b 612 board += "</svg>";
41534b92
BA
613 return board;
614 }
615
cc2c7183 616 // Generally light square bottom-right
15106e82
BA
617 getSquareColorClass(x, y) {
618 return ((x+y) % 2 == 0 ? "light-square": "dark-square");
41534b92
BA
619 }
620
621 setupPieces(r) {
622 if (this.g_pieces) {
623 // Refreshing: delete old pieces first
624 for (let i=0; i<this.size.x; i++) {
625 for (let j=0; j<this.size.y; j++) {
626 if (this.g_pieces[i][j]) {
627 this.g_pieces[i][j].remove();
628 this.g_pieces[i][j] = null;
629 }
630 }
631 }
632 }
b4ae3ff6
BA
633 else
634 this.g_pieces = ArrayFun.init(this.size.x, this.size.y, null);
3c61449b
BA
635 let chessboard =
636 document.getElementById(this.containerId).querySelector(".chessboard");
b4ae3ff6
BA
637 if (!r)
638 r = chessboard.getBoundingClientRect();
41534b92
BA
639 const pieceWidth = this.getPieceWidth(r.width);
640 for (let i=0; i < this.size.x; i++) {
641 for (let j=0; j < this.size.y; j++) {
c9ab0340 642 if (this.board[i][j] != "") {
41534b92 643 const color = this.getColor(i, j);
cc2c7183 644 const piece = this.getPiece(i, j);
41534b92
BA
645 this.g_pieces[i][j] = document.createElement("piece");
646 this.g_pieces[i][j].classList.add(this.pieces()[piece]["class"]);
15106e82 647 this.g_pieces[i][j].classList.add(C.GetColorClass(color));
41534b92
BA
648 this.g_pieces[i][j].style.width = pieceWidth + "px";
649 this.g_pieces[i][j].style.height = pieceWidth + "px";
9db5050a
BA
650 let [ip, jp] = this.getPixelPosition(i, j, r);
651 // Translate coordinates to use chessboard as reference:
652 this.g_pieces[i][j].style.transform =
653 `translate(${ip - r.x}px,${jp - r.y}px)`;
c9ab0340
BA
654 if (this.enlightened && !this.enlightened[i][j])
655 this.g_pieces[i][j].classList.add("hidden");
3c61449b 656 chessboard.appendChild(this.g_pieces[i][j]);
41534b92
BA
657 }
658 }
659 }
1a7c0492 660 if (this.hasReserve)
b4ae3ff6 661 this.re_drawReserve(['w', 'b'], r);
41534b92
BA
662 }
663
664 // NOTE: assume !!this.reserve
665 re_drawReserve(colors, r) {
666 if (this.r_pieces) {
667 // Remove (old) reserve pieces
668 for (let c of colors) {
b4ae3ff6
BA
669 if (!this.reserve[c])
670 continue;
41534b92
BA
671 Object.keys(this.reserve[c]).forEach(p => {
672 if (this.r_pieces[c][p]) {
673 this.r_pieces[c][p].remove();
674 delete this.r_pieces[c][p];
675 const numId = this.getReserveNumId(c, p);
676 document.getElementById(numId).remove();
677 }
678 });
679 let reservesDiv = document.getElementById("reserves_" + c);
b4ae3ff6
BA
680 if (reservesDiv)
681 reservesDiv.remove();
41534b92
BA
682 }
683 }
b4ae3ff6 684 else
9db5050a
BA
685 this.r_pieces = { w: {}, b: {} };
686 let container = document.getElementById(this.containerId);
b4ae3ff6 687 if (!r)
9db5050a 688 r = container.querySelector(".chessboard").getBoundingClientRect();
41534b92 689 for (let c of colors) {
b4ae3ff6
BA
690 if (!this.reserve[c])
691 continue;
41534b92 692 const nbR = this.getNbReservePieces(c);
b4ae3ff6
BA
693 if (nbR == 0)
694 continue;
41534b92
BA
695 const sqResSize = this.getReserveSquareSize(r.width, nbR);
696 let ridx = 0;
697 const vShift = (c == this.playerColor ? r.height + 5 : -sqResSize - 5);
698 const [i0, j0] = [r.x, r.y + vShift];
699 let rcontainer = document.createElement("div");
700 rcontainer.id = "reserves_" + c;
701 rcontainer.classList.add("reserves");
702 rcontainer.style.left = i0 + "px";
703 rcontainer.style.top = j0 + "px";
1aa9054d
BA
704 // NOTE: +1 fix display bug on Firefox at least
705 rcontainer.style.width = (nbR * sqResSize + 1) + "px";
41534b92 706 rcontainer.style.height = sqResSize + "px";
9db5050a 707 container.appendChild(rcontainer);
41534b92 708 for (let p of Object.keys(this.reserve[c])) {
b4ae3ff6
BA
709 if (this.reserve[c][p] == 0)
710 continue;
41534b92 711 let r_cell = document.createElement("div");
15106e82 712 r_cell.id = this.coordsToId({x: c, y: p});
41534b92 713 r_cell.classList.add("reserve-cell");
1aa9054d
BA
714 r_cell.style.width = sqResSize + "px";
715 r_cell.style.height = sqResSize + "px";
41534b92
BA
716 rcontainer.appendChild(r_cell);
717 let piece = document.createElement("piece");
c9ab0340 718 const pieceSpec = this.pieces()[p];
41534b92 719 piece.classList.add(pieceSpec["class"]);
15106e82 720 piece.classList.add(C.GetColorClass(c));
41534b92
BA
721 piece.style.width = "100%";
722 piece.style.height = "100%";
723 this.r_pieces[c][p] = piece;
724 r_cell.appendChild(piece);
725 let number = document.createElement("div");
726 number.textContent = this.reserve[c][p];
727 number.classList.add("reserve-num");
728 number.id = this.getReserveNumId(c, p);
729 const fontSize = "1.3em";
730 number.style.fontSize = fontSize;
731 number.style.fontSize = fontSize;
732 r_cell.appendChild(number);
733 ridx++;
734 }
735 }
736 }
737
738 updateReserve(color, piece, count) {
55a15dcb 739 if (this.options["cannibal"] && C.CannibalKings[piece])
cc2c7183 740 piece = "k"; //capturing cannibal king: back to king form
41534b92
BA
741 const oldCount = this.reserve[color][piece];
742 this.reserve[color][piece] = count;
743 // Redrawing is much easier if count==0
b4ae3ff6
BA
744 if ([oldCount, count].includes(0))
745 this.re_drawReserve([color]);
41534b92
BA
746 else {
747 const numId = this.getReserveNumId(color, piece);
748 document.getElementById(numId).textContent = count;
749 }
750 }
751
15106e82
BA
752 // Apply diff this.enlightened --> oldEnlightened on board
753 graphUpdateEnlightened() {
754 let chessboard =
755 document.getElementById(this.containerId).querySelector(".chessboard");
756 const r = chessboard.getBoundingClientRect();
757 const pieceWidth = this.getPieceWidth(r.width);
758 for (let x=0; x<this.size.x; x++) {
759 for (let y=0; y<this.size.y; y++) {
760 if (!this.enlightened[x][y] && this.oldEnlightened[x][y]) {
6997e386 761 let elt = document.getElementById(this.coordsToId({x: x, y: y}));
15106e82
BA
762 elt.classList.add("in-shadow");
763 if (this.g_pieces[x][y])
764 this.g_pieces[x][y].classList.add("hidden");
765 }
766 else if (this.enlightened[x][y] && !this.oldEnlightened[x][y]) {
6997e386 767 let elt = document.getElementById(this.coordsToId({x: x, y: y}));
15106e82
BA
768 elt.classList.remove("in-shadow");
769 if (this.g_pieces[x][y])
770 this.g_pieces[x][y].classList.remove("hidden");
771 }
772 }
773 }
774 }
775
c4e9bb92
BA
776 // Resize board: no need to destroy/recreate pieces
777 rescale(mode) {
778 let chessboard =
779 document.getElementById(this.containerId).querySelector(".chessboard");
780 const r = chessboard.getBoundingClientRect();
781 const multFact = (mode == "up" ? 1.05 : 0.95);
782 let [newWidth, newHeight] = [multFact * r.width, multFact * r.height];
535c464b 783 // Stay in window:
c4e9bb92 784 if (newWidth > window.innerWidth) {
535c464b 785 newWidth = window.innerWidth;
c4e9bb92
BA
786 newHeight = newWidth / this.size.ratio;
787 }
788 if (newHeight > window.innerHeight) {
535c464b 789 newHeight = window.innerHeight;
c4e9bb92
BA
790 newWidth = newHeight * this.size.ratio;
791 }
535c464b
BA
792 chessboard.style.width = newWidth + "px";
793 chessboard.style.height = newHeight + "px";
41534b92 794 const newX = (window.innerWidth - newWidth) / 2;
3c61449b 795 chessboard.style.left = newX + "px";
41534b92 796 const newY = (window.innerHeight - newHeight) / 2;
3c61449b 797 chessboard.style.top = newY + "px";
9db5050a 798 const newR = {x: newX, y: newY, width: newWidth, height: newHeight};
c4e9bb92 799 const pieceWidth = this.getPieceWidth(newWidth);
d621e620
BA
800 // NOTE: next "if" for variants which use squares filling
801 // instead of "physical", moving pieces
802 if (this.g_pieces) {
c4e9bb92
BA
803 for (let i=0; i < this.size.x; i++) {
804 for (let j=0; j < this.size.y; j++) {
805 if (this.g_pieces[i][j]) {
d621e620 806 // NOTE: could also use CSS transform "scale"
c4e9bb92
BA
807 this.g_pieces[i][j].style.width = pieceWidth + "px";
808 this.g_pieces[i][j].style.height = pieceWidth + "px";
809 const [ip, jp] = this.getPixelPosition(i, j, newR);
d621e620 810 // Translate coordinates to use chessboard as reference:
c4e9bb92 811 this.g_pieces[i][j].style.transform =
d621e620
BA
812 `translate(${ip - newX}px,${jp - newY}px)`;
813 }
41534b92
BA
814 }
815 }
816 }
c4e9bb92
BA
817 if (this.hasReserve)
818 this.rescaleReserve(newR);
41534b92
BA
819 }
820
821 rescaleReserve(r) {
41534b92 822 for (let c of ['w','b']) {
b4ae3ff6
BA
823 if (!this.reserve[c])
824 continue;
41534b92 825 const nbR = this.getNbReservePieces(c);
b4ae3ff6
BA
826 if (nbR == 0)
827 continue;
41534b92
BA
828 // Resize container first
829 const sqResSize = this.getReserveSquareSize(r.width, nbR);
830 const vShift = (c == this.playerColor ? r.height + 5 : -sqResSize - 5);
831 const [i0, j0] = [r.x, r.y + vShift];
832 let rcontainer = document.getElementById("reserves_" + c);
833 rcontainer.style.left = i0 + "px";
834 rcontainer.style.top = j0 + "px";
1aa9054d 835 rcontainer.style.width = (nbR * sqResSize + 1) + "px";
41534b92
BA
836 rcontainer.style.height = sqResSize + "px";
837 // And then reserve cells:
838 const rpieceWidth = this.getReserveSquareSize(r.width, nbR);
839 Object.keys(this.reserve[c]).forEach(p => {
b4ae3ff6
BA
840 if (this.reserve[c][p] == 0)
841 return;
15106e82 842 let r_cell = document.getElementById(this.coordsToId({x: c, y: p}));
1aa9054d
BA
843 r_cell.style.width = sqResSize + "px";
844 r_cell.style.height = sqResSize + "px";
41534b92
BA
845 });
846 }
847 }
848
9db5050a 849 // Return the absolute pixel coordinates given current position.
41534b92
BA
850 // Our coordinate system differs from CSS one (x <--> y).
851 // We return here the CSS coordinates (more useful).
852 getPixelPosition(i, j, r) {
b4ae3ff6
BA
853 if (i < 0 || j < 0)
854 return [0, 0]; //piece vanishes
15106e82
BA
855 let x, y;
856 if (typeof i == "string") {
857 // Reserves: need to know the rank of piece
858 const nbR = this.getNbReservePieces(i);
859 const rsqSize = this.getReserveSquareSize(r.width, nbR);
860 x = this.getRankInReserve(i, j) * rsqSize;
861 y = (this.playerColor == i ? y = r.height + 5 : - 5 - rsqSize);
862 }
863 else {
864 const sqSize = r.width / this.size.y;
865 const flipped = (this.playerColor == 'b');
866 x = (flipped ? this.size.y - 1 - j : j) * sqSize;
867 y = (flipped ? this.size.x - 1 - i : i) * sqSize;
868 }
9db5050a 869 return [r.x + x, r.y + y];
41534b92
BA
870 }
871
872 initMouseEvents() {
9db5050a
BA
873 let container = document.getElementById(this.containerId);
874 let chessboard = container.querySelector(".chessboard");
41534b92
BA
875
876 const getOffset = e => {
3c61449b
BA
877 if (e.clientX)
878 // Mouse
879 return {x: e.clientX, y: e.clientY};
41534b92
BA
880 let touchLocation = null;
881 if (e.targetTouches && e.targetTouches.length >= 1)
882 // Touch screen, dragstart
883 touchLocation = e.targetTouches[0];
884 else if (e.changedTouches && e.changedTouches.length >= 1)
885 // Touch screen, dragend
886 touchLocation = e.changedTouches[0];
887 if (touchLocation)
11625344 888 return {x: touchLocation.clientX, y: touchLocation.clientY};
57b8015b 889 return {x: 0, y: 0}; //shouldn't reach here =)
41534b92
BA
890 }
891
892 const centerOnCursor = (piece, e) => {
15106e82 893 const centerShift = this.getPieceWidth(r.width) / 2;
41534b92 894 const offset = getOffset(e);
9db5050a
BA
895 piece.style.left = (offset.x - centerShift) + "px";
896 piece.style.top = (offset.y - centerShift) + "px";
41534b92
BA
897 }
898
899 let start = null,
900 r = null,
901 startPiece, curPiece = null,
15106e82 902 pieceWidth;
41534b92 903 const mousedown = (e) => {
cb17fed8 904 // Disable zoom on smartphones:
b4ae3ff6
BA
905 if (e.touches && e.touches.length > 1)
906 e.preventDefault();
3c61449b 907 r = chessboard.getBoundingClientRect();
15106e82
BA
908 pieceWidth = this.getPieceWidth(r.width);
909 const cd = this.idToCoords(e.target.id);
910 if (cd) {
911 const move = this.doClick(cd);
b4ae3ff6
BA
912 if (move)
913 this.playPlusVisual(move);
41534b92 914 else {
15106e82
BA
915 const [x, y] = Object.values(cd);
916 if (typeof x != "number")
917 startPiece = this.r_pieces[x][y];
918 else
919 startPiece = this.g_pieces[x][y];
920 if (startPiece && this.canIplay(x, y)) {
41534b92 921 e.preventDefault();
15106e82 922 start = cd;
41534b92
BA
923 curPiece = startPiece.cloneNode();
924 curPiece.style.transform = "none";
925 curPiece.style.zIndex = 5;
15106e82
BA
926 curPiece.style.width = pieceWidth + "px";
927 curPiece.style.height = pieceWidth + "px";
41534b92 928 centerOnCursor(curPiece, e);
9db5050a 929 container.appendChild(curPiece);
41534b92 930 startPiece.style.opacity = "0.4";
3c61449b 931 chessboard.style.cursor = "none";
41534b92
BA
932 }
933 }
934 }
935 };
936
937 const mousemove = (e) => {
938 if (start) {
939 e.preventDefault();
940 centerOnCursor(curPiece, e);
941 }
11625344
BA
942 else if (e.changedTouches && e.changedTouches.length >= 1)
943 // Attempt to prevent horizontal swipe...
944 e.preventDefault();
41534b92
BA
945 };
946
947 const mouseup = (e) => {
b4ae3ff6
BA
948 if (!start)
949 return;
41534b92
BA
950 const [x, y] = [start.x, start.y];
951 start = null;
952 e.preventDefault();
3c61449b 953 chessboard.style.cursor = "pointer";
41534b92
BA
954 startPiece.style.opacity = "1";
955 const offset = getOffset(e);
956 const landingElt = document.elementFromPoint(offset.x, offset.y);
15106e82
BA
957 const cd =
958 (landingElt ? this.idToCoords(landingElt.id) : undefined);
959 if (cd) {
41534b92
BA
960 // NOTE: clearly suboptimal, but much easier, and not a big deal.
961 const potentialMoves = this.getPotentialMovesFrom([x, y])
15106e82 962 .filter(m => m.end.x == cd.x && m.end.y == cd.y);
41534b92 963 const moves = this.filterValid(potentialMoves);
b4ae3ff6
BA
964 if (moves.length >= 2)
965 this.showChoices(moves, r);
966 else if (moves.length == 1)
967 this.playPlusVisual(moves[0], r);
41534b92
BA
968 }
969 curPiece.remove();
970 };
971
972 if ('onmousedown' in window) {
973 document.addEventListener("mousedown", mousedown);
974 document.addEventListener("mousemove", mousemove);
975 document.addEventListener("mouseup", mouseup);
437dfd42
BA
976 document.addEventListener("wheel",
977 (e) => this.rescale(e.deltaY < 0 ? "up" : "down"));
41534b92
BA
978 }
979 if ('ontouchstart' in window) {
cb17fed8
BA
980 // https://stackoverflow.com/a/42509310/12660887
981 document.addEventListener("touchstart", mousedown, {passive: false});
982 document.addEventListener("touchmove", mousemove, {passive: false});
983 document.addEventListener("touchend", mouseup, {passive: false});
41534b92 984 }
11625344 985 // TODO: onpointerdown/move/up ? See reveal.js /controllers/touch.js
41534b92
BA
986 }
987
988 showChoices(moves, r) {
989 let container = document.getElementById(this.containerId);
3c61449b 990 let chessboard = container.querySelector(".chessboard");
41534b92
BA
991 let choices = document.createElement("div");
992 choices.id = "choices";
993 choices.style.width = r.width + "px";
994 choices.style.height = r.height + "px";
995 choices.style.left = r.x + "px";
996 choices.style.top = r.y + "px";
3c61449b
BA
997 chessboard.style.opacity = "0.5";
998 container.appendChild(choices);
15106e82 999 const squareWidth = r.width / this.size.y;
41534b92
BA
1000 const firstUpLeft = (r.width - (moves.length * squareWidth)) / 2;
1001 const firstUpTop = (r.height - squareWidth) / 2;
1002 const color = moves[0].appear[0].c;
1003 const callback = (m) => {
3c61449b
BA
1004 chessboard.style.opacity = "1";
1005 container.removeChild(choices);
41534b92
BA
1006 this.playPlusVisual(m, r);
1007 }
1008 for (let i=0; i < moves.length; i++) {
1009 let choice = document.createElement("div");
1010 choice.classList.add("choice");
1011 choice.style.width = squareWidth + "px";
1012 choice.style.height = squareWidth + "px";
1013 choice.style.left = (firstUpLeft + i * squareWidth) + "px";
1014 choice.style.top = firstUpTop + "px";
1015 choice.style.backgroundColor = "lightyellow";
1016 choice.onclick = () => callback(moves[i]);
1017 const piece = document.createElement("piece");
c9ab0340 1018 const pieceSpec = this.pieces()[moves[i].appear[0].p];
41534b92 1019 piece.classList.add(pieceSpec["class"]);
15106e82 1020 piece.classList.add(C.GetColorClass(color));
41534b92
BA
1021 piece.style.width = "100%";
1022 piece.style.height = "100%";
1023 choice.appendChild(piece);
1024 choices.appendChild(choice);
1025 }
1026 }
1027
1028 //////////////
1029 // BASIC UTILS
1030
1031 get size() {
15106e82
BA
1032 return {
1033 x: 8,
1034 y: 8,
1035 ratio: 1 //for rectangular board = y / x
1036 };
41534b92
BA
1037 }
1038
1039 // Color of thing on square (i,j). 'undefined' if square is empty
1040 getColor(i, j) {
15106e82
BA
1041 if (typeof i == "string")
1042 return i; //reserves
41534b92
BA
1043 return this.board[i][j].charAt(0);
1044 }
1045
15106e82
BA
1046 static GetColorClass(c) {
1047 return (c == 'w' ? "white" : "black");
1048 }
1049
cc2c7183 1050 // Assume square i,j isn't empty
41534b92 1051 getPiece(i, j) {
15106e82
BA
1052 if (typeof j == "string")
1053 return j; //reserves
41534b92
BA
1054 return this.board[i][j].charAt(1);
1055 }
1056
cc2c7183
BA
1057 // Piece type on square (i,j)
1058 getPieceType(i, j) {
6997e386 1059 const p = this.getPiece(i, j);
cc2c7183
BA
1060 return C.CannibalKings[p] || p; //a cannibal king move as...
1061 }
1062
41534b92
BA
1063 // Get opponent color
1064 static GetOppCol(color) {
1065 return (color == "w" ? "b" : "w");
1066 }
1067
c9ab0340 1068 // Can thing on square1 capture (no return) thing on square2?
41534b92 1069 canTake([x1, y1], [x2, y2]) {
c9ab0340 1070 return (this.getColor(x1, y1) !== this.getColor(x2, y2));
41534b92
BA
1071 }
1072
1073 // Is (x,y) on the chessboard?
1074 onBoard(x, y) {
b99ce1fb
BA
1075 return (x >= 0 && x < this.size.x &&
1076 y >= 0 && y < this.size.y);
41534b92
BA
1077 }
1078
15106e82 1079 // Am I allowed to move thing at square x,y ?
41534b92 1080 canIplay(x, y) {
0c44c676 1081 return (this.playerColor == this.turn && this.getColor(x, y) == this.turn);
41534b92
BA
1082 }
1083
1084 ////////////////////////
1085 // PIECES SPECIFICATIONS
1086
c9ab0340 1087 pieces(color, x, y) {
41534b92 1088 const pawnShift = (color == "w" ? -1 : 1);
9db5050a
BA
1089 // NOTE: jump 2 squares from first rank (pawns can be here sometimes)
1090 const initRank = ((color == 'w' && x >= 6) || (color == 'b' && x <= 1));
41534b92
BA
1091 return {
1092 'p': {
1093 "class": "pawn",
c9ab0340
BA
1094 moves: [
1095 {
1096 steps: [[pawnShift, 0]],
1097 range: (initRank ? 2 : 1)
1098 }
1099 ],
1100 attack: [
1101 {
1102 steps: [[pawnShift, 1], [pawnShift, -1]],
1103 range: 1
1104 }
1105 ]
41534b92
BA
1106 },
1107 // rook
1108 'r': {
1109 "class": "rook",
c9ab0340
BA
1110 moves: [
1111 {steps: [[0, 1], [0, -1], [1, 0], [-1, 0]]}
1112 ]
41534b92
BA
1113 },
1114 // knight
1115 'n': {
1116 "class": "knight",
c9ab0340
BA
1117 moves: [
1118 {
1119 steps: [
1120 [1, 2], [1, -2], [-1, 2], [-1, -2],
1121 [2, 1], [-2, 1], [2, -1], [-2, -1]
1122 ],
1123 range: 1
1124 }
1125 ]
41534b92
BA
1126 },
1127 // bishop
1128 'b': {
1129 "class": "bishop",
c9ab0340
BA
1130 moves: [
1131 {steps: [[1, 1], [1, -1], [-1, 1], [-1, -1]]}
1132 ]
41534b92
BA
1133 },
1134 // queen
1135 'q': {
1136 "class": "queen",
c9ab0340
BA
1137 moves: [
1138 {
1139 steps: [
1140 [0, 1], [0, -1], [1, 0], [-1, 0],
1141 [1, 1], [1, -1], [-1, 1], [-1, -1]
1142 ]
1143 }
41534b92
BA
1144 ]
1145 },
1146 // king
1147 'k': {
1148 "class": "king",
c9ab0340
BA
1149 moves: [
1150 {
1151 steps: [
1152 [0, 1], [0, -1], [1, 0], [-1, 0],
1153 [1, 1], [1, -1], [-1, 1], [-1, -1]
1154 ],
1155 range: 1
1156 }
1157 ]
cc2c7183
BA
1158 },
1159 // Cannibal kings:
c9ab0340
BA
1160 '!': {"class": "king-pawn", moveas: "p"},
1161 '#': {"class": "king-rook", moveas: "r"},
1162 '$': {"class": "king-knight", moveas: "n"},
1163 '%': {"class": "king-bishop", moveas: "b"},
1164 '*': {"class": "king-queen", moveas: "q"}
41534b92
BA
1165 };
1166 }
1167
41534b92
BA
1168 ////////////////////
1169 // MOVES GENERATION
1170
adf7c659
BA
1171 // For Cylinder: get Y coordinate
1172 getY(y) {
b4ae3ff6
BA
1173 if (!this.options["cylinder"])
1174 return y;
41534b92 1175 let res = y % this.size.y;
b4ae3ff6 1176 if (res < 0)
adf7c659 1177 res += this.size.y;
41534b92
BA
1178 return res;
1179 }
1180
1181 // Stop at the first capture found
1182 atLeastOneCapture(color) {
1183 color = color || this.turn;
cc2c7183 1184 const oppCol = C.GetOppCol(color);
41534b92
BA
1185 for (let i = 0; i < this.size.x; i++) {
1186 for (let j = 0; j < this.size.y; j++) {
1187 if (this.board[i][j] != "" && this.getColor(i, j) == color) {
c9ab0340
BA
1188 const allSpecs = this.pieces(color, i, j)
1189 let specs = allSpecs[this.getPieceType(i, j)];
1190 const attacks = specs.attack || specs.moves;
1191 for (let a of attacks) {
1192 outerLoop: for (let step of a.steps) {
d262cff4 1193 let [ii, jj] = [i + step[0], this.getY(j + step[1])];
c9ab0340
BA
1194 let stepCounter = 1;
1195 while (this.onBoard(ii, jj) && this.board[ii][jj] == "") {
1196 if (a.range <= stepCounter++)
1197 continue outerLoop;
1198 ii += step[0];
d262cff4 1199 jj = this.getY(jj + step[1]);
c9ab0340
BA
1200 }
1201 if (
1202 this.onBoard(ii, jj) &&
1203 this.getColor(ii, jj) == oppCol &&
1204 this.filterValid(
1205 [this.getBasicMove([i, j], [ii, jj])]
1206 ).length >= 1
1207 ) {
1208 return true;
1209 }
41534b92
BA
1210 }
1211 }
1212 }
1213 }
1214 }
1215 return false;
1216 }
1217
1218 getDropMovesFrom([c, p]) {
1219 // NOTE: by design, this.reserve[c][p] >= 1 on user click
1a7c0492 1220 // (but not necessarily otherwise: atLeastOneMove() etc)
b4ae3ff6
BA
1221 if (this.reserve[c][p] == 0)
1222 return [];
41534b92
BA
1223 let moves = [];
1224 for (let i=0; i<this.size.x; i++) {
1225 for (let j=0; j<this.size.y; j++) {
41534b92
BA
1226 if (
1227 this.board[i][j] == "" &&
c9ab0340 1228 (!this.enlightened || this.enlightened[i][j]) &&
41534b92 1229 (
cc2c7183 1230 p != "p" ||
41534b92
BA
1231 (c == 'w' && i < this.size.x - 1) ||
1232 (c == 'b' && i > 0)
1233 )
1234 ) {
1235 moves.push(
1236 new Move({
1237 start: {x: c, y: p},
1238 end: {x: i, y: j},
1239 appear: [new PiPo({x: i, y: j, c: c, p: p})],
1240 vanish: []
1241 })
1242 );
1243 }
1244 }
1245 }
1246 return moves;
1247 }
1248
1249 // All possible moves from selected square
c7bf7b1b 1250 getPotentialMovesFrom(sq, color) {
8b301184
BA
1251 if (this.subTurnTeleport == 2)
1252 return [];
b4ae3ff6
BA
1253 if (typeof sq[0] == "string")
1254 return this.getDropMovesFrom(sq);
57b8015b 1255 if (this.isImmobilized(sq))
b4ae3ff6 1256 return [];
cc2c7183 1257 const piece = this.getPieceType(sq[0], sq[1]);
c9ab0340
BA
1258 let moves = this.getPotentialMovesOf(piece, sq);
1259 if (
1260 piece == "p" &&
1261 this.hasEnpassant &&
1262 this.epSquare
1263 ) {
1264 Array.prototype.push.apply(moves, this.getEnpassantCaptures(sq));
1265 }
41534b92 1266 if (
cc2c7183 1267 piece == "k" &&
41534b92
BA
1268 this.hasCastle &&
1269 this.castleFlags[color || this.turn].some(v => v < this.size.y)
1270 ) {
1271 Array.prototype.push.apply(moves, this.getCastleMoves(sq));
1272 }
1273 return this.postProcessPotentialMoves(moves);
1274 }
1275
1276 postProcessPotentialMoves(moves) {
b4ae3ff6
BA
1277 if (moves.length == 0)
1278 return [];
41534b92 1279 const color = this.getColor(moves[0].start.x, moves[0].start.y);
cc2c7183 1280 const oppCol = C.GetOppCol(color);
41534b92 1281
57b8015b
BA
1282 if (this.options["capture"] && this.atLeastOneCapture())
1283 moves = this.capturePostProcess(moves, oppCol);
41534b92 1284
57b8015b
BA
1285 if (this.options["atomic"])
1286 this.atomicPostProcess(moves, oppCol);
cc2c7183 1287
c9ab0340
BA
1288 if (
1289 moves.length > 0 &&
1290 this.getPieceType(moves[0].start.x, moves[0].start.y) == "p"
1291 ) {
57b8015b 1292 this.pawnPostProcess(moves, color, oppCol);
c9ab0340
BA
1293 }
1294
cc2c7183
BA
1295 if (
1296 this.options["cannibal"] &&
57b8015b 1297 this.options["rifle"]
cc2c7183
BA
1298 ) {
1299 // In this case a rifle-capture from last rank may promote a pawn
9db5050a 1300 this.riflePromotePostProcess(moves, color);
57b8015b
BA
1301 }
1302
1303 return moves;
1304 }
1305
1306 capturePostProcess(moves, oppCol) {
1307 // Filter out non-capturing moves (not using m.vanish because of
1308 // self captures of Recycle and Teleport).
1309 return moves.filter(m => {
1310 return (
1311 this.board[m.end.x][m.end.y] != "" &&
1312 this.getColor(m.end.x, m.end.y) == oppCol
1313 );
1314 });
1315 }
1316
1317 atomicPostProcess(moves, oppCol) {
1318 moves.forEach(m => {
1319 if (
1320 this.board[m.end.x][m.end.y] != "" &&
1321 this.getColor(m.end.x, m.end.y) == oppCol
1322 ) {
1323 // Explosion!
1324 let steps = [
1325 [-1, -1],
1326 [-1, 0],
1327 [-1, 1],
1328 [0, -1],
1329 [0, 1],
1330 [1, -1],
1331 [1, 0],
1332 [1, 1]
1333 ];
1334 for (let step of steps) {
1335 let x = m.end.x + step[0];
d262cff4 1336 let y = this.getY(m.end.y + step[1]);
57b8015b
BA
1337 if (
1338 this.onBoard(x, y) &&
1339 this.board[x][y] != "" &&
1340 this.getPieceType(x, y) != "p"
1341 ) {
1342 m.vanish.push(
1343 new PiPo({
1344 p: this.getPiece(x, y),
1345 c: this.getColor(x, y),
1346 x: x,
1347 y: y
1348 })
1349 );
1350 }
1351 }
1352 if (!this.options["rifle"])
0c44c676 1353 m.appear.pop(); //nothing appears
57b8015b
BA
1354 }
1355 });
1356 }
1357
1358 pawnPostProcess(moves, color, oppCol) {
1359 let moreMoves = [];
1360 const lastRank = (color == "w" ? 0 : this.size.x - 1);
1361 const initPiece = this.getPiece(moves[0].start.x, moves[0].start.y);
1362 moves.forEach(m => {
57b8015b
BA
1363 const [x1, y1] = [m.start.x, m.start.y];
1364 const [x2, y2] = [m.end.x, m.end.y];
1365 const promotionOk = (
1366 x2 == lastRank &&
1367 (!this.options["rifle"] || this.board[x2][y2] == "")
1368 );
1369 if (!promotionOk)
1370 return; //nothing to do
8cc2f6d0
BA
1371 if (this.options["pawnfall"]) {
1372 m.appear.shift();
8cc2f6d0
BA
1373 return;
1374 }
99ea2453
BA
1375 let finalPieces = ["p"];
1376 if (
1377 this.options["cannibal"] &&
1378 this.board[x2][y2] != "" &&
1379 this.getColor(x2, y2) == oppCol
1380 ) {
1381 finalPieces = [this.getPieceType(x2, y2)];
1382 }
1383 else
1384 finalPieces = this.pawnPromotions;
57b8015b
BA
1385 m.appear[0].p = finalPieces[0];
1386 if (initPiece == "!") //cannibal king-pawn
1387 m.appear[0].p = C.CannibalKingCode[finalPieces[0]];
1388 for (let i=1; i<finalPieces.length; i++) {
1389 const piece = finalPieces[i];
99ea2453
BA
1390 const tr = {
1391 c: color,
1392 p: (initPiece != "!" ? piece : C.CannibalKingCode[piece])
1393 };
57b8015b 1394 let newMove = this.getBasicMove([x1, y1], [x2, y2], tr);
57b8015b
BA
1395 moreMoves.push(newMove);
1396 }
1397 });
1398 Array.prototype.push.apply(moves, moreMoves);
1399 }
cc2c7183 1400
9db5050a 1401 riflePromotePostProcess(moves, color) {
57b8015b
BA
1402 const lastRank = (color == "w" ? 0 : this.size.x - 1);
1403 let newMoves = [];
1404 moves.forEach(m => {
1405 if (
1406 m.start.x == lastRank &&
1407 m.appear.length >= 1 &&
1408 m.appear[0].p == "p" &&
1409 m.appear[0].x == m.start.x &&
1410 m.appear[0].y == m.start.y
1411 ) {
57b8015b
BA
1412 m.appear[0].p = this.pawnPromotions[0];
1413 for (let i=1; i<this.pawnPromotions.length; i++) {
1414 let newMv = JSON.parse(JSON.stringify(m));
1415 newMv.appear[0].p = this.pawnSpecs.promotions[i];
1416 newMoves.push(newMv);
1417 }
1418 }
1419 });
1420 Array.prototype.push.apply(moves, newMoves);
41534b92
BA
1421 }
1422
b99ce1fb 1423 // NOTE: using special symbols to not interfere with variants' pieces codes
cc2c7183
BA
1424 static get CannibalKings() {
1425 return {
b99ce1fb
BA
1426 "!": "p",
1427 "#": "r",
1428 "$": "n",
1429 "%": "b",
6997e386
BA
1430 "*": "q",
1431 "k": "k"
cc2c7183
BA
1432 };
1433 }
1434
1435 static get CannibalKingCode() {
1436 return {
b99ce1fb
BA
1437 "p": "!",
1438 "r": "#",
1439 "n": "$",
1440 "b": "%",
1441 "q": "*",
cc2c7183
BA
1442 "k": "k"
1443 };
1444 }
1445
1446 isKing(symbol) {
6997e386 1447 return !!C.CannibalKings[symbol];
cc2c7183
BA
1448 }
1449
41534b92
BA
1450 // For Madrasi:
1451 // (redefined in Baroque etc, where Madrasi condition doesn't make sense)
1452 isImmobilized([x, y]) {
57b8015b
BA
1453 if (!this.options["madrasi"])
1454 return false;
41534b92 1455 const color = this.getColor(x, y);
cc2c7183 1456 const oppCol = C.GetOppCol(color);
c9ab0340 1457 const piece = this.getPieceType(x, y); //ok not cannibal king
57b8015b 1458 const stepSpec = this.pieces(color, x, y)[piece];
c9ab0340
BA
1459 const attacks = stepSpec.attack || stepSpec.moves;
1460 for (let a of attacks) {
1461 outerLoop: for (let step of a.steps) {
1462 let [i, j] = [x + step[0], y + step[1]];
1463 let stepCounter = 1;
1464 while (this.onBoard(i, j) && this.board[i][j] == "") {
1465 if (a.range <= stepCounter++)
1466 continue outerLoop;
1467 i += step[0];
d262cff4 1468 j = this.getY(j + step[1]);
c9ab0340
BA
1469 }
1470 if (
1471 this.onBoard(i, j) &&
1472 this.getColor(i, j) == oppCol &&
1473 this.getPieceType(i, j) == piece
1474 ) {
1475 return true;
1476 }
41534b92
BA
1477 }
1478 }
1479 return false;
1480 }
1481
1482 // Generic method to find possible moves of "sliding or jumping" pieces
1483 getPotentialMovesOf(piece, [x, y]) {
1484 const color = this.getColor(x, y);
c9ab0340 1485 const stepSpec = this.pieces(color, x, y)[piece];
41534b92 1486 let moves = [];
adf7c659
BA
1487 // Next 3 for Cylinder mode:
1488 let explored = {};
1489 let segments = [];
1490 let segStart = [];
1491
1492 const addMove = (start, end) => {
1493 let newMove = this.getBasicMove(start, end);
1494 if (segments.length > 0) {
1495 newMove.segments = JSON.parse(JSON.stringify(segments));
1496 newMove.segments.push([[segStart[0], segStart[1]], [end[0], end[1]]]);
1497 }
1498 moves.push(newMove);
1499 };
c9ab0340
BA
1500
1501 const findAddMoves = (type, stepArray) => {
1502 for (let s of stepArray) {
1503 outerLoop: for (let step of s.steps) {
adf7c659
BA
1504 segments = [];
1505 segStart = [x, y];
d262cff4
BA
1506 let [i, j] = [x, y];
1507 let stepCounter = 0;
1508 while (
1509 this.onBoard(i, j) &&
1510 (this.board[i][j] == "" || (i == x && j == y))
1511 ) {
1512 if (
1513 type != "attack" &&
1514 !explored[i + "." + j] &&
1515 (i != x || j != y)
1516 ) {
c9ab0340 1517 explored[i + "." + j] = true;
adf7c659 1518 addMove([x, y], [i, j]);
c9ab0340
BA
1519 }
1520 if (s.range <= stepCounter++)
1521 continue outerLoop;
d262cff4 1522 const oldIJ = [i, j];
c9ab0340 1523 i += step[0];
adf7c659
BA
1524 j = this.getY(j + step[1]);
1525 if (Math.abs(j - oldIJ[1]) > 1) {
d262cff4 1526 // Boundary between segments (cylinder mode)
adf7c659
BA
1527 segments.push([[segStart[0], segStart[1]], oldIJ]);
1528 segStart = [i, j];
d262cff4 1529 }
c9ab0340
BA
1530 }
1531 if (!this.onBoard(i, j))
1532 continue;
1533 const pieceIJ = this.getPieceType(i, j);
1534 if (
1535 type != "moveonly" &&
1536 !explored[i + "." + j] &&
1537 (
1538 !this.options["zen"] ||
1539 pieceIJ == "k"
1540 ) &&
1541 (
1542 this.canTake([x, y], [i, j]) ||
1543 (
1544 (this.options["recycle"] || this.options["teleport"]) &&
1545 pieceIJ != "k"
1546 )
1547 )
1548 ) {
1549 explored[i + "." + j] = true;
adf7c659 1550 addMove([x, y], [i, j]);
c9ab0340
BA
1551 }
1552 }
41534b92 1553 }
c9ab0340
BA
1554 };
1555
1556 const specialAttack = !!stepSpec.attack;
1557 if (specialAttack)
1558 findAddMoves("attack", stepSpec.attack);
1559 findAddMoves(specialAttack ? "moveonly" : "all", stepSpec.moves);
082e639a
BA
1560 if (this.options["zen"]) {
1561 Array.prototype.push.apply(moves,
1562 this.findCapturesOn([x, y], {zen: true}));
1563 }
41534b92
BA
1564 return moves;
1565 }
1566
082e639a
BA
1567 // Search for enemy (or not) pieces attacking [x, y]
1568 findCapturesOn([x, y], args) {
41534b92 1569 let moves = [];
082e639a
BA
1570 if (!args.oppCol)
1571 args.oppCol = C.GetOppCol(this.getColor(x, y) || this.turn);
c9ab0340
BA
1572 for (let i=0; i<this.size.x; i++) {
1573 for (let j=0; j<this.size.y; j++) {
57b8015b
BA
1574 if (
1575 this.board[i][j] != "" &&
082e639a 1576 this.getColor(i, j) == args.oppCol &&
57b8015b
BA
1577 !this.isImmobilized([i, j])
1578 ) {
082e639a 1579 if (args.zen && this.isKing(this.getPiece(i, j)))
c9ab0340 1580 continue; //king not captured in this way
082e639a
BA
1581 const stepSpec =
1582 this.pieces(args.oppCol, i, j)[this.getPieceType(i, j)];
c9ab0340
BA
1583 const attacks = stepSpec.attack || stepSpec.moves;
1584 for (let a of attacks) {
1585 for (let s of a.steps) {
1586 // Quick check: if step isn't compatible, don't even try
57b8015b 1587 if (!C.CompatibleStep([i, j], [x, y], s, a.range))
c9ab0340
BA
1588 continue;
1589 // Finally verify that nothing stand in-between
d262cff4 1590 let [ii, jj] = [i + s[0], this.getY(j + s[1])];
c9ab0340 1591 let stepCounter = 1;
082e639a
BA
1592 while (
1593 this.onBoard(ii, jj) &&
1594 this.board[ii][jj] == "" &&
1595 (ii != x || jj != y) //condition to attack empty squares too
1596 ) {
c9ab0340 1597 ii += s[0];
d262cff4 1598 jj = this.getY(jj + s[1]);
c9ab0340
BA
1599 }
1600 if (ii == x && jj == y) {
082e639a
BA
1601 if (args.zen)
1602 // Reverse capture:
1603 moves.push(this.getBasicMove([x, y], [i, j]));
1604 else
1605 moves.push(this.getBasicMove([i, j], [x, y]));
1606 if (args.one)
c9ab0340
BA
1607 return moves; //test for underCheck
1608 }
1609 }
1610 }
41534b92 1611 }
c9ab0340
BA
1612 }
1613 }
41534b92
BA
1614 return moves;
1615 }
1616
57b8015b
BA
1617 static CompatibleStep([x1, y1], [x2, y2], step, range) {
1618 const rx = (x2 - x1) / step[0],
1619 ry = (y2 - y1) / step[1];
1620 if (
1621 (!Number.isFinite(rx) && !Number.isNaN(rx)) ||
1622 (!Number.isFinite(ry) && !Number.isNaN(ry))
1623 ) {
1624 return false;
1625 }
1626 let distance = (Number.isNaN(rx) ? ry : rx);
1627 // TODO: 1e-7 here is totally arbitrary
1628 if (Math.abs(distance - Math.round(distance)) > 1e-7)
1629 return false;
1630 distance = Math.round(distance); //in case of (numerical...)
1631 if (range < distance)
1632 return false;
1633 return true;
1634 }
1635
41534b92
BA
1636 // Build a regular move from its initial and destination squares.
1637 // tr: transformation
1638 getBasicMove([sx, sy], [ex, ey], tr) {
1639 const initColor = this.getColor(sx, sy);
cc2c7183 1640 const initPiece = this.getPiece(sx, sy);
41534b92
BA
1641 const destColor = (this.board[ex][ey] != "" ? this.getColor(ex, ey) : "");
1642 let mv = new Move({
1643 appear: [],
1644 vanish: [],
15106e82
BA
1645 start: {x: sx, y: sy},
1646 end: {x: ex, y: ey}
41534b92
BA
1647 });
1648 if (
1649 !this.options["rifle"] ||
1650 this.board[ex][ey] == "" ||
1651 destColor == initColor //Recycle, Teleport
1652 ) {
1653 mv.appear = [
1654 new PiPo({
1655 x: ex,
1656 y: ey,
1657 c: !!tr ? tr.c : initColor,
1658 p: !!tr ? tr.p : initPiece
1659 })
1660 ];
1661 mv.vanish = [
1662 new PiPo({
1663 x: sx,
1664 y: sy,
1665 c: initColor,
1666 p: initPiece
1667 })
1668 ];
1669 }
1670 if (this.board[ex][ey] != "") {
1671 mv.vanish.push(
1672 new PiPo({
1673 x: ex,
1674 y: ey,
1675 c: this.getColor(ex, ey),
cc2c7183 1676 p: this.getPiece(ex, ey)
41534b92
BA
1677 })
1678 );
41534b92
BA
1679 if (this.options["cannibal"] && destColor != initColor) {
1680 const lastIdx = mv.vanish.length - 1;
cc2c7183
BA
1681 let trPiece = mv.vanish[lastIdx].p;
1682 if (this.isKing(this.getPiece(sx, sy)))
1683 trPiece = C.CannibalKingCode[trPiece];
b4ae3ff6
BA
1684 if (mv.appear.length >= 1)
1685 mv.appear[0].p = trPiece;
41534b92
BA
1686 else if (this.options["rifle"]) {
1687 mv.appear.unshift(
1688 new PiPo({
1689 x: sx,
1690 y: sy,
1691 c: initColor,
cc2c7183 1692 p: trPiece
41534b92
BA
1693 })
1694 );
1695 mv.vanish.unshift(
1696 new PiPo({
1697 x: sx,
1698 y: sy,
1699 c: initColor,
1700 p: initPiece
1701 })
1702 );
1703 }
1704 }
1705 }
1706 return mv;
1707 }
1708
1709 // En-passant square, if any
1710 getEpSquare(moveOrSquare) {
1711 if (typeof moveOrSquare === "string") {
1712 const square = moveOrSquare;
b4ae3ff6
BA
1713 if (square == "-")
1714 return undefined;
cc2c7183 1715 return C.SquareToCoords(square);
41534b92
BA
1716 }
1717 // Argument is a move:
1718 const move = moveOrSquare;
1719 const s = move.start,
1720 e = move.end;
1721 if (
1722 s.y == e.y &&
1723 Math.abs(s.x - e.x) == 2 &&
1724 // Next conditions for variants like Atomic or Rifle, Recycle...
cc2c7183
BA
1725 (move.appear.length > 0 && move.appear[0].p == "p") &&
1726 (move.vanish.length > 0 && move.vanish[0].p == "p")
41534b92
BA
1727 ) {
1728 return {
1729 x: (s.x + e.x) / 2,
1730 y: s.y
1731 };
1732 }
1733 return undefined; //default
1734 }
1735
1736 // Special case of en-passant captures: treated separately
c9ab0340 1737 getEnpassantCaptures([x, y]) {
41534b92 1738 const color = this.getColor(x, y);
c9ab0340 1739 const shiftX = (color == 'w' ? -1 : 1);
cc2c7183 1740 const oppCol = C.GetOppCol(color);
41534b92
BA
1741 let enpassantMove = null;
1742 if (
1743 !!this.epSquare &&
1744 this.epSquare.x == x + shiftX &&
d262cff4 1745 Math.abs(this.getY(this.epSquare.y - y)) == 1 &&
41534b92
BA
1746 this.getColor(x, this.epSquare.y) == oppCol //Doublemove guard...
1747 ) {
1748 const [epx, epy] = [this.epSquare.x, this.epSquare.y];
1749 this.board[epx][epy] = oppCol + "p";
1750 enpassantMove = this.getBasicMove([x, y], [epx, epy]);
1751 this.board[epx][epy] = "";
1752 const lastIdx = enpassantMove.vanish.length - 1; //think Rifle
1753 enpassantMove.vanish[lastIdx].x = x;
1754 }
1755 return !!enpassantMove ? [enpassantMove] : [];
1756 }
1757
41534b92
BA
1758 // "castleInCheck" arg to let some variants castle under check
1759 getCastleMoves([x, y], finalSquares, castleInCheck, castleWith) {
1760 const c = this.getColor(x, y);
1761
1762 // Castling ?
cc2c7183 1763 const oppCol = C.GetOppCol(c);
41534b92
BA
1764 let moves = [];
1765 // King, then rook:
1766 finalSquares =
1767 finalSquares || [ [2, 3], [this.size.y - 2, this.size.y - 3] ];
cc2c7183 1768 const castlingKing = this.getPiece(x, y);
41534b92
BA
1769 castlingCheck: for (
1770 let castleSide = 0;
1771 castleSide < 2;
1772 castleSide++ //large, then small
1773 ) {
b4ae3ff6
BA
1774 if (this.castleFlags[c][castleSide] >= this.size.y)
1775 continue;
41534b92
BA
1776 // If this code is reached, rook and king are on initial position
1777
1778 // NOTE: in some variants this is not a rook
1779 const rookPos = this.castleFlags[c][castleSide];
cc2c7183 1780 const castlingPiece = this.getPiece(x, rookPos);
41534b92
BA
1781 if (
1782 this.board[x][rookPos] == "" ||
1783 this.getColor(x, rookPos) != c ||
1784 (!!castleWith && !castleWith.includes(castlingPiece))
1785 ) {
1786 // Rook is not here, or changed color (see Benedict)
1787 continue;
1788 }
1789 // Nothing on the path of the king ? (and no checks)
1790 const finDist = finalSquares[castleSide][0] - y;
1791 let step = finDist / Math.max(1, Math.abs(finDist));
1792 let i = y;
1793 do {
1794 if (
1795 (!castleInCheck && this.underCheck([x, i], oppCol)) ||
1796 (
1797 this.board[x][i] != "" &&
1798 // NOTE: next check is enough, because of chessboard constraints
1799 (this.getColor(x, i) != c || ![rookPos, y].includes(i))
1800 )
1801 ) {
1802 continue castlingCheck;
1803 }
1804 i += step;
1805 } while (i != finalSquares[castleSide][0]);
1806 // Nothing on the path to the rook?
1807 step = (castleSide == 0 ? -1 : 1);
1808 for (i = y + step; i != rookPos; i += step) {
b4ae3ff6
BA
1809 if (this.board[x][i] != "")
1810 continue castlingCheck;
41534b92
BA
1811 }
1812
1813 // Nothing on final squares, except maybe king and castling rook?
1814 for (i = 0; i < 2; i++) {
1815 if (
1816 finalSquares[castleSide][i] != rookPos &&
1817 this.board[x][finalSquares[castleSide][i]] != "" &&
1818 (
1819 finalSquares[castleSide][i] != y ||
1820 this.getColor(x, finalSquares[castleSide][i]) != c
1821 )
1822 ) {
1823 continue castlingCheck;
1824 }
1825 }
1826
1827 // If this code is reached, castle is valid
1828 moves.push(
1829 new Move({
1830 appear: [
1831 new PiPo({
1832 x: x,
1833 y: finalSquares[castleSide][0],
1834 p: castlingKing,
1835 c: c
1836 }),
1837 new PiPo({
1838 x: x,
1839 y: finalSquares[castleSide][1],
1840 p: castlingPiece,
1841 c: c
1842 })
1843 ],
1844 vanish: [
1845 // King might be initially disguised (Titan...)
1846 new PiPo({ x: x, y: y, p: castlingKing, c: c }),
1847 new PiPo({ x: x, y: rookPos, p: castlingPiece, c: c })
1848 ],
1849 end:
1850 Math.abs(y - rookPos) <= 2
c9ab0340
BA
1851 ? {x: x, y: rookPos}
1852 : {x: x, y: y + 2 * (castleSide == 0 ? -1 : 1)}
41534b92
BA
1853 })
1854 );
1855 }
1856
1857 return moves;
1858 }
1859
1860 ////////////////////
1861 // MOVES VALIDATION
1862
082e639a
BA
1863 // Is (king at) given position under check by "oppCol" ?
1864 underCheck([x, y], oppCol) {
b4ae3ff6
BA
1865 if (this.options["taking"] || this.options["dark"])
1866 return false;
082e639a
BA
1867 return (
1868 this.findCapturesOn([x, y], {oppCol: oppCol, one: true}).length >= 1
1869 );
41534b92
BA
1870 }
1871
1872 // Stop at first king found (TODO: multi-kings)
1873 searchKingPos(color) {
1874 for (let i=0; i < this.size.x; i++) {
1875 for (let j=0; j < this.size.y; j++) {
cc2c7183
BA
1876 if (this.getColor(i, j) == color && this.isKing(this.getPiece(i, j)))
1877 return [i, j];
41534b92
BA
1878 }
1879 }
1880 return [-1, -1]; //king not found
1881 }
1882
1883 filterValid(moves) {
b4ae3ff6
BA
1884 if (moves.length == 0)
1885 return [];
41534b92 1886 const color = this.turn;
cc2c7183 1887 const oppCol = C.GetOppCol(color);
41534b92
BA
1888 if (this.options["balance"] && [1, 3].includes(this.movesCount)) {
1889 // Forbid moves either giving check or exploding opponent's king:
1890 const oppKingPos = this.searchKingPos(oppCol);
1891 moves = moves.filter(m => {
1892 if (
cc2c7183
BA
1893 m.vanish.some(v => v.c == oppCol && v.p == "k") &&
1894 m.appear.every(a => a.c != oppCol || a.p != "k")
41534b92
BA
1895 )
1896 return false;
1897 this.playOnBoard(m);
1898 const res = !this.underCheck(oppKingPos, color);
1899 this.undoOnBoard(m);
1900 return res;
1901 });
1902 }
b4ae3ff6
BA
1903 if (this.options["taking"] || this.options["dark"])
1904 return moves;
41534b92
BA
1905 const kingPos = this.searchKingPos(color);
1906 let filtered = {}; //avoid re-checking similar moves (promotions...)
1907 return moves.filter(m => {
1908 const key = m.start.x + m.start.y + '.' + m.end.x + m.end.y;
1909 if (!filtered[key]) {
1910 this.playOnBoard(m);
1911 let square = kingPos,
1912 res = true; //a priori valid
cc2c7183 1913 if (m.vanish.some(v => {
6997e386 1914 return C.CannibalKings[v.p] && v.c == color;
cc2c7183 1915 })) {
41534b92
BA
1916 // Search king in appear array:
1917 const newKingIdx =
cc2c7183 1918 m.appear.findIndex(a => {
6997e386 1919 return C.CannibalKings[a.p] && a.c == color;
cc2c7183 1920 });
41534b92
BA
1921 if (newKingIdx >= 0)
1922 square = [m.appear[newKingIdx].x, m.appear[newKingIdx].y];
b4ae3ff6
BA
1923 else
1924 res = false;
41534b92
BA
1925 }
1926 res &&= !this.underCheck(square, oppCol);
1927 this.undoOnBoard(m);
1928 filtered[key] = res;
1929 return res;
1930 }
1931 return filtered[key];
1932 });
1933 }
1934
1935 /////////////////
1936 // MOVES PLAYING
1937
1938 // Aggregate flags into one object
1939 aggregateFlags() {
1940 return this.castleFlags;
1941 }
1942
1943 // Reverse operation
1944 disaggregateFlags(flags) {
1945 this.castleFlags = flags;
1946 }
1947
1948 // Apply a move on board
1949 playOnBoard(move) {
6997e386
BA
1950 for (let psq of move.vanish)
1951 this.board[psq.x][psq.y] = "";
1952 for (let psq of move.appear)
1953 this.board[psq.x][psq.y] = psq.c + psq.p;
41534b92
BA
1954 }
1955 // Un-apply the played move
1956 undoOnBoard(move) {
6997e386
BA
1957 for (let psq of move.appear)
1958 this.board[psq.x][psq.y] = "";
1959 for (let psq of move.vanish)
1960 this.board[psq.x][psq.y] = psq.c + psq.p;
41534b92
BA
1961 }
1962
1963 updateCastleFlags(move) {
1964 // Update castling flags if start or arrive from/at rook/king locations
1965 move.appear.concat(move.vanish).forEach(psq => {
1966 if (
1967 this.board[psq.x][psq.y] != "" &&
cc2c7183 1968 this.getPieceType(psq.x, psq.y) == "k"
41534b92
BA
1969 ) {
1970 this.castleFlags[psq.c] = [this.size.y, this.size.y];
1971 }
1972 // NOTE: not "else if" because king can capture enemy rook...
cc2c7183 1973 let c = "";
b4ae3ff6
BA
1974 if (psq.x == 0)
1975 c = "b";
1976 else if (psq.x == this.size.x - 1)
1977 c = "w";
cc2c7183 1978 if (c != "") {
41534b92 1979 const fidx = this.castleFlags[c].findIndex(f => f == psq.y);
b4ae3ff6
BA
1980 if (fidx >= 0)
1981 this.castleFlags[c][fidx] = this.size.y;
41534b92
BA
1982 }
1983 });
1984 }
1985
1986 prePlay(move) {
1987 if (
99ea2453
BA
1988 this.hasCastle &&
1989 // If flags already off, no need to re-check:
1990 Object.keys(this.castleFlags).some(c => {
1991 return this.castleFlags[c].some(val => val < this.size.y)})
41534b92 1992 ) {
99ea2453
BA
1993 this.updateCastleFlags(move);
1994 }
1995 if (this.options["crazyhouse"]) {
1996 move.vanish.forEach(v => {
1997 const square = C.CoordsToSquare({x: v.x, y: v.y});
1998 if (this.ispawn[square])
1999 delete this.ispawn[square];
2000 });
2001 if (move.appear.length > 0 && move.vanish.length > 0) {
2002 // Assumption: something is moving
2003 const initSquare = C.CoordsToSquare(move.start);
f429756d 2004 const destSquare = C.CoordsToSquare(move.end);
99ea2453
BA
2005 if (
2006 this.ispawn[initSquare] ||
2007 (move.vanish[0].p == "p" && move.appear[0].p != "p")
41534b92 2008 ) {
f429756d
BA
2009 this.ispawn[destSquare] = true;
2010 }
2011 else if (
2012 this.ispawn[destSquare] &&
2013 this.getColor(move.end.x, move.end.y) != move.vanish[0].c
2014 ) {
2015 move.vanish[1].p = "p";
2016 delete this.ispawn[destSquare];
41534b92
BA
2017 }
2018 }
2019 }
2020 const minSize = Math.min(move.appear.length, move.vanish.length);
0c44c676
BA
2021 if (
2022 this.hasReserve &&
2023 // Warning; atomic pawn removal isn't a capture
2024 (!this.options["atomic"] || !this.rempawn || this.movesCount >= 1)
2025 ) {
41534b92
BA
2026 const color = this.turn;
2027 for (let i=minSize; i<move.appear.length; i++) {
2028 // Something appears = dropped on board (some exceptions, Chakart...)
0c44c676
BA
2029 if (move.appear[i].c == color) {
2030 const piece = move.appear[i].p;
2031 this.updateReserve(color, piece, this.reserve[color][piece] - 1);
2032 }
41534b92
BA
2033 }
2034 for (let i=minSize; i<move.vanish.length; i++) {
2035 // Something vanish: add to reserve except if recycle & opponent
0c44c676
BA
2036 if (
2037 this.options["crazyhouse"] ||
2038 (this.options["recycle"] && move.vanish[i].c == color)
2039 ) {
2040 const piece = move.vanish[i].p;
41534b92 2041 this.updateReserve(color, piece, this.reserve[color][piece] + 1);
0c44c676 2042 }
41534b92
BA
2043 }
2044 }
2045 }
2046
2047 play(move) {
2048 this.prePlay(move);
b4ae3ff6
BA
2049 if (this.hasEnpassant)
2050 this.epSquare = this.getEpSquare(move);
41534b92
BA
2051 this.playOnBoard(move);
2052 this.postPlay(move);
2053 }
2054
2055 postPlay(move) {
2056 const color = this.turn;
cc2c7183 2057 const oppCol = C.GetOppCol(color);
b4ae3ff6 2058 if (this.options["dark"])
c9ab0340 2059 this.updateEnlightened();
41534b92
BA
2060 if (this.options["teleport"]) {
2061 if (
cc2c7183 2062 this.subTurnTeleport == 1 &&
41534b92
BA
2063 move.vanish.length > move.appear.length &&
2064 move.vanish[move.vanish.length - 1].c == color
2065 ) {
2066 const v = move.vanish[move.vanish.length - 1];
2067 this.captured = {x: v.x, y: v.y, c: v.c, p: v.p};
cc2c7183 2068 this.subTurnTeleport = 2;
41534b92
BA
2069 return;
2070 }
cc2c7183 2071 this.subTurnTeleport = 1;
41534b92
BA
2072 this.captured = null;
2073 }
2074 if (this.options["balance"]) {
b4ae3ff6
BA
2075 if (![1, 3].includes(this.movesCount))
2076 this.turn = oppCol;
41534b92
BA
2077 }
2078 else {
2079 if (
2080 (
2081 this.options["doublemove"] &&
2082 this.movesCount >= 1 &&
2083 this.subTurn == 1
2084 ) ||
2085 (this.options["progressive"] && this.subTurn <= this.movesCount)
2086 ) {
2087 const oppKingPos = this.searchKingPos(oppCol);
6f74b81a
BA
2088 if (
2089 oppKingPos[0] >= 0 &&
2090 (
2091 this.options["taking"] ||
2092 !this.underCheck(oppKingPos, color)
2093 )
2094 ) {
41534b92
BA
2095 this.subTurn++;
2096 return;
2097 }
2098 }
2099 this.turn = oppCol;
2100 }
2101 this.movesCount++;
2102 this.subTurn = 1;
2103 }
2104
2105 // "Stop at the first move found"
2106 atLeastOneMove(color) {
2107 color = color || this.turn;
2108 for (let i = 0; i < this.size.x; i++) {
2109 for (let j = 0; j < this.size.y; j++) {
2110 if (this.board[i][j] != "" && this.getColor(i, j) == color) {
cc2c7183
BA
2111 // NOTE: in fact searching for all potential moves from i,j.
2112 // I don't believe this is an issue, for now at least.
41534b92 2113 const moves = this.getPotentialMovesFrom([i, j]);
b4ae3ff6
BA
2114 if (moves.some(m => this.filterValid([m]).length >= 1))
2115 return true;
41534b92
BA
2116 }
2117 }
2118 }
2119 if (this.hasReserve && this.reserve[color]) {
2120 for (let p of Object.keys(this.reserve[color])) {
2121 const moves = this.getDropMovesFrom([color, p]);
b4ae3ff6
BA
2122 if (moves.some(m => this.filterValid([m]).length >= 1))
2123 return true;
41534b92
BA
2124 }
2125 }
2126 return false;
2127 }
2128
2129 // What is the score ? (Interesting if game is over)
2130 getCurrentScore(move) {
2131 const color = this.turn;
cc2c7183 2132 const oppCol = C.GetOppCol(color);
41534b92 2133 const kingPos = [this.searchKingPos(color), this.searchKingPos(oppCol)];
b4ae3ff6
BA
2134 if (kingPos[0][0] < 0 && kingPos[1][0] < 0)
2135 return "1/2";
2136 if (kingPos[0][0] < 0)
2137 return (color == "w" ? "0-1" : "1-0");
2138 if (kingPos[1][0] < 0)
2139 return (color == "w" ? "1-0" : "0-1");
2140 if (this.atLeastOneMove())
2141 return "*";
41534b92 2142 // No valid move: stalemate or checkmate?
c9ab0340 2143 if (!this.underCheck(kingPos[0], color))
b4ae3ff6 2144 return "1/2";
41534b92
BA
2145 // OK, checkmate
2146 return (color == "w" ? "0-1" : "1-0");
2147 }
2148
41534b92
BA
2149 playVisual(move, r) {
2150 move.vanish.forEach(v => {
f77da909 2151 this.g_pieces[v.x][v.y].remove();
c9ab0340 2152 this.g_pieces[v.x][v.y] = null;
41534b92 2153 });
3c61449b
BA
2154 let chessboard =
2155 document.getElementById(this.containerId).querySelector(".chessboard");
b4ae3ff6
BA
2156 if (!r)
2157 r = chessboard.getBoundingClientRect();
41534b92
BA
2158 const pieceWidth = this.getPieceWidth(r.width);
2159 move.appear.forEach(a => {
41534b92
BA
2160 this.g_pieces[a.x][a.y] = document.createElement("piece");
2161 this.g_pieces[a.x][a.y].classList.add(this.pieces()[a.p]["class"]);
2162 this.g_pieces[a.x][a.y].classList.add(a.c == "w" ? "white" : "black");
2163 this.g_pieces[a.x][a.y].style.width = pieceWidth + "px";
2164 this.g_pieces[a.x][a.y].style.height = pieceWidth + "px";
2165 const [ip, jp] = this.getPixelPosition(a.x, a.y, r);
9db5050a
BA
2166 // Translate coordinates to use chessboard as reference:
2167 this.g_pieces[a.x][a.y].style.transform =
2168 `translate(${ip - r.x}px,${jp - r.y}px)`;
c9ab0340
BA
2169 if (this.enlightened && !this.enlightened[a.x][a.y])
2170 this.g_pieces[a.x][a.y].classList.add("hidden");
3c61449b 2171 chessboard.appendChild(this.g_pieces[a.x][a.y]);
41534b92 2172 });
c9ab0340
BA
2173 if (this.options["dark"])
2174 this.graphUpdateEnlightened();
41534b92
BA
2175 }
2176
2177 playPlusVisual(move, r) {
41534b92 2178 this.play(move);
c9ab0340 2179 this.playVisual(move, r);
41534b92
BA
2180 this.afterPlay(move); //user method
2181 }
2182
15106e82
BA
2183 getMaxDistance(rwidth) {
2184 // Works for all rectangular boards:
2185 return Math.sqrt(rwidth ** 2 + (rwidth / this.size.ratio) ** 2);
2186 }
2187
2188 getDomPiece(x, y) {
2189 return (typeof x == "string" ? this.r_pieces : this.g_pieces)[x][y];
41534b92
BA
2190 }
2191
2192 animate(move, callback) {
15106e82 2193 if (this.noAnimate || move.noAnimate) {
e8b85c86
BA
2194 callback();
2195 return;
2196 }
9db5050a 2197 let initPiece = this.getDomPiece(move.start.x, move.start.y);
9db5050a
BA
2198 // NOTE: cloning generally not required, but light enough, and simpler
2199 let movingPiece = initPiece.cloneNode();
2200 initPiece.style.opacity = "0";
2201 let container =
2202 document.getElementById(this.containerId)
2203 const r = container.querySelector(".chessboard").getBoundingClientRect();
082e639a
BA
2204 if (typeof move.start.x == "string") {
2205 // Need to bound width/height (was 100% for reserve pieces)
2206 const pieceWidth = this.getPieceWidth(r.width);
2207 movingPiece.style.width = pieceWidth + "px";
2208 movingPiece.style.height = pieceWidth + "px";
2209 }
15106e82 2210 const maxDist = this.getMaxDistance(r.width);
9db5050a 2211 const pieces = this.pieces();
15106e82 2212 if (move.drag) {
15106e82
BA
2213 const startCode = this.getPiece(move.start.x, move.start.y);
2214 movingPiece.classList.remove(pieces[startCode]["class"]);
2215 movingPiece.classList.add(pieces[move.drag.p]["class"]);
2216 const apparentColor = this.getColor(move.start.x, move.start.y);
2217 if (apparentColor != move.drag.c) {
2218 movingPiece.classList.remove(C.GetColorClass(apparentColor));
2219 movingPiece.classList.add(C.GetColorClass(move.drag.c));
41534b92 2220 }
41534b92 2221 }
9db5050a 2222 container.appendChild(movingPiece);
15106e82 2223 const animateSegment = (index, cb) => {
9db5050a 2224 // NOTE: move.drag could be generalized per-segment (usage?)
15106e82
BA
2225 const [i1, j1] = move.segments[index][0];
2226 const [i2, j2] = move.segments[index][1];
2227 const dep = this.getPixelPosition(i1, j1, r);
2228 const arr = this.getPixelPosition(i2, j2, r);
9db5050a
BA
2229 movingPiece.style.transitionDuration = "0s";
2230 movingPiece.style.transform = `translate(${dep[0]}px, ${dep[1]}px)`;
15106e82
BA
2231 const distance =
2232 Math.sqrt((arr[0] - dep[0]) ** 2 + (arr[1] - dep[1]) ** 2);
2233 const duration = 0.2 + (distance / maxDist) * 0.3;
9db5050a
BA
2234 // TODO: unclear why we need this new delay below:
2235 setTimeout(() => {
2236 movingPiece.style.transitionDuration = duration + "s";
adf7c659 2237 // movingPiece is child of container: no need to adjust coordinates
9db5050a
BA
2238 movingPiece.style.transform = `translate(${arr[0]}px, ${arr[1]}px)`;
2239 setTimeout(cb, duration * 1000);
2240 }, 50);
15106e82 2241 };
635418a5
BA
2242 if (!move.segments) {
2243 move.segments = [
2244 [[move.start.x, move.start.y], [move.end.x, move.end.y]]
2245 ];
2246 }
15106e82 2247 let index = 0;
635418a5 2248 const animateSegmentCallback = () => {
15106e82 2249 if (index < move.segments.length)
635418a5 2250 animateSegment(index++, animateSegmentCallback);
15106e82 2251 else {
9db5050a
BA
2252 movingPiece.remove();
2253 initPiece.style.opacity = "1";
41534b92 2254 callback();
15106e82 2255 }
635418a5
BA
2256 };
2257 animateSegmentCallback();
41534b92
BA
2258 }
2259
2260 playReceivedMove(moves, callback) {
21e8e712 2261 const launchAnimation = () => {
3c61449b 2262 const r = container.querySelector(".chessboard").getBoundingClientRect();
21e8e712
BA
2263 const animateRec = i => {
2264 this.animate(moves[i], () => {
21e8e712 2265 this.play(moves[i]);
57b8015b 2266 this.playVisual(moves[i], r);
b4ae3ff6
BA
2267 if (i < moves.length - 1)
2268 setTimeout(() => animateRec(i+1), 300);
2269 else
2270 callback();
21e8e712
BA
2271 });
2272 };
2273 animateRec(0);
2274 };
e081c5eb
BA
2275 // Delay if user wasn't focused:
2276 const checkDisplayThenAnimate = (delay) => {
3c61449b 2277 if (container.style.display == "none") {
21e8e712
BA
2278 alert("New move! Let's go back to game...");
2279 document.getElementById("gameInfos").style.display = "none";
3c61449b 2280 container.style.display = "block";
21e8e712
BA
2281 setTimeout(launchAnimation, 700);
2282 }
b4ae3ff6
BA
2283 else
2284 setTimeout(launchAnimation, delay || 0);
21e8e712 2285 };
3c61449b 2286 let container = document.getElementById(this.containerId);
016306e3
BA
2287 if (document.hidden) {
2288 document.onvisibilitychange = () => {
2289 document.onvisibilitychange = undefined;
e081c5eb 2290 checkDisplayThenAnimate(700);
fd31883b 2291 };
fd31883b 2292 }
b4ae3ff6
BA
2293 else
2294 checkDisplayThenAnimate();
41534b92
BA
2295 }
2296
2297};