6f408bb134c507d6a4dd19a1bdfb96a250ebce1c
1 import { ChessRules
, PiPo
, Move
} from "@/base_rules";
2 import { randInt
} from "@/utils/alea";
3 import { ArrayFun
} from "@/utils/array";
5 export class OtageRules
extends ChessRules
{
7 static get IMAGE_EXTENSION() {
11 // Hostage / Capturer combinations
12 // + letter among a, b, v, w to indicate colors + config:
13 // a: black first, black controls
14 // b: white first, black controls
15 // v: black first, white controls
16 // w: white first, white controls
44 if (ChessRules
.PIECES
.includes(b
[1])) return ChessRules
.board2fen(b
);
45 // Show symbol first (no collisions)
50 if (f
.length
== 1) return ChessRules
.fen2board(f
);
51 return f
[1] + f
[0]; //"color" first
54 static IsGoodPosition(position
) {
55 if (position
.length
== 0) return false;
56 const rows
= position
.split("/");
57 if (rows
.length
!= V
.size
.x
) return false;
58 let kings
= { 'k': 0, 'K': 0 };
59 for (let row
of rows
) {
61 for (let i
= 0; i
< row
.length
; i
++) {
62 const lowR
= row
[i
].toLowerCase();
63 const readNext
= !(ChessRules
.PIECES
.includes(lowR
));
64 if (!!(lowR
.match(/[a-z_]/))) {
66 if (lowR
== 'k') kings
[row
[i
]]++;
68 const up
= this.getUnionPieces(row
[++i
], lowR
);
69 if (up
.w
== V
.KING
) kings
['K']++;
70 // NOTE: not "else if" because two kings might be in union
71 if (up
.b
== V
.KING
) kings
['k']++;
75 const num
= parseInt(row
[i
], 10);
76 if (isNaN(num
) || num
<= 0) return false;
80 if (sumElts
!= V
.size
.y
) return false;
82 // Both kings should be on board. Exactly one per color.
83 if (Object
.values(kings
).some(v
=> v
!= 1)) return false;
87 static GetBoard(position
) {
88 const rows
= position
.split("/");
89 let board
= ArrayFun
.init(V
.size
.x
, V
.size
.y
, "");
90 for (let i
= 0; i
< rows
.length
; i
++) {
92 for (let indexInRow
= 0; indexInRow
< rows
[i
].length
; indexInRow
++) {
93 const character
= rows
[i
][indexInRow
];
94 const num
= parseInt(character
, 10);
95 // If num is a number, just shift j:
96 if (!isNaN(num
)) j
+= num
;
98 // Something at position i,j
99 const lowC
= character
.toLowerCase();
100 if (ChessRules
.PIECES
.includes(lowC
))
101 board
[i
][j
++] = V
.fen2board(character
);
103 board
[i
][j
++] = V
.fen2board(lowC
+ rows
[i
][++indexInRow
]);
115 if (ChessRules
.PIECES
.includes(m
.appear
[0].p
)) return super.getPPpath(m
);
116 // For an "union", show only relevant piece:
117 // The color must be deduced from the move: reaching final rank of who?
118 const color
= (m
.appear
[0].x
== 0 ? 'w' : 'b');
119 const up
= this.getUnionPieces(m
.appear
[0].c
, m
.appear
[0].p
);
120 return "Pacosako/" + color
+ up
[color
];
123 canTake([x1
, y1
], [x2
, y2
]) {
124 const p1
= this.board
[x1
][y1
].charAt(1);
125 if (!(ChessRules
.PIECES
.includes(p1
))) return false;
126 const p2
= this.board
[x2
][y2
].charAt(1);
127 if (!(ChessRules
.PIECES
.includes(p2
))) return true;
128 const c1
= this.board
[x1
][y1
].charAt(0);
129 const c2
= this.board
[x2
][y2
].charAt(0);
133 canIplay(side
, [x
, y
]) {
134 const c
= this.board
[x
][y
].charAt(0);
135 const compSide
= (side
== 'w' ? 'v' : 'a');
136 return (this.turn
== side
&& [side
, compSide
].includes(c
));
140 this.kingPos
= { w: [-1, -1], b: [-1, -1] };
141 const fenRows
= V
.ParseFen(fen
).position
.split("/");
142 const startRow
= { 'w': V
.size
.x
- 1, 'b': 0 };
143 for (let i
= 0; i
< fenRows
.length
; i
++) {
145 for (let j
= 0; j
< fenRows
[i
].length
; j
++) {
146 const c
= fenRows
[i
].charAt(j
);
147 const lowR
= c
.toLowerCase();
148 const readNext
= !(ChessRules
.PIECES
.includes(lowR
));
149 if (!!(lowR
.match(/[a-z_]/))) {
150 if (lowR
== 'k') this.kingPos
[c
== 'k' ? 'b' : 'w'] = [i
, k
];
152 const up
= this.getUnionPieces(fenRows
[i
][++j
], lowR
);
153 if (up
.w
== V
.KING
) this.kingPos
['w'] = [i
, k
];
154 if (up
.b
== V
.KING
) this.kingPos
['b'] = [i
, k
];
158 const num
= parseInt(fenRows
[i
].charAt(j
), 10);
159 if (!isNaN(num
)) k
+= num
- 1;
166 setOtherVariables(fen
) {
167 super.setOtherVariables(fen
);
168 // Stack of "last move" only for intermediate chaining
169 this.lastMoveEnd
= [null];
170 this.repetitions
= [];
173 static IsGoodFlags(flags
) {
174 // 4 for castle + 16 for pawns
175 return !!flags
.match(/^[a-z]{4,4}[01]{16,16}$/);
179 super.setFlags(fenflags
); //castleFlags
181 w: [...Array(8)], //pawns can move 2 squares?
184 const flags
= fenflags
.substr(4); //skip first 4 letters, for castle
185 for (let c
of ["w", "b"]) {
186 for (let i
= 0; i
< 8; i
++)
187 this.pawnFlags
[c
][i
] = flags
.charAt((c
== "w" ? 0 : 8) + i
) == "1";
192 return [this.castleFlags
, this.pawnFlags
];
195 disaggregateFlags(flags
) {
196 this.castleFlags
= flags
[0];
197 this.pawnFlags
= flags
[1];
200 static GenRandInitFen(randomness
) {
201 // Add 16 pawns flags:
202 return ChessRules
.GenRandInitFen(randomness
)
203 .slice(0, -2) + "1111111111111111 -";
207 let fen
= super.getFlagsFen();
209 for (let c
of ["w", "b"])
210 for (let i
= 0; i
< 8; i
++) fen
+= (this.pawnFlags
[c
][i
] ? "1" : "0");
215 const p
= this.board
[i
][j
].charAt(1);
216 if (ChessRules
.PIECES
.includes(p
)) return p
;
217 const c
= this.board
[i
][j
].charAt(0);
218 const idx
= (['a', 'w'].includes(c
) ? 0 : 1);
219 return V
.UNIONS
[p
][idx
];
222 getUnionPieces(color
, code
) {
223 const pieces
= V
.UNIONS
[code
];
225 w: pieces
[ ['b', 'w'].includes(color
) ? 0 : 1 ],
226 b: pieces
[ ['a', 'v'].includes(color
) ? 0 : 1 ]
230 // p1: white piece, p2: black piece, capturer: (temporary) owner
231 getUnionCode(p1
, p2
, capturer
) {
233 Object
.values(V
.UNIONS
).findIndex(v
=> v
[0] == p1
&& v
[1] == p2
)
236 if (capturer
== 'w') c
= (uIdx
>= 0 ? 'w' : 'v');
237 else c
= (uIdx
>= 0 ? 'b' : 'a');
240 Object
.values(V
.UNIONS
).findIndex(v
=> v
[0] == p2
&& v
[1] == p1
)
243 return { c: c
, p: Object
.keys(V
.UNIONS
)[uIdx
] };
246 getBasicMove([sx
, sy
], [ex
, ey
], tr
) {
247 const L
= this.lastMoveEnd
.length
;
248 const lm
= this.lastMoveEnd
[L
-1];
249 const piece
= (!!lm
? lm
.p : null);
250 const initColor
= (!!piece
? this.turn : this.board
[sx
][sy
].charAt(0));
251 const initPiece
= (piece
|| this.board
[sx
][sy
].charAt(1));
253 const oppCol
= V
.GetOppCol(c
);
254 if (!!tr
&& !(ChessRules
.PIECES
.includes(initPiece
))) {
255 // Transformation computed without taking union into account
256 const up
= this.getUnionPieces(initColor
, initPiece
);
257 let args
= [tr
.p
, up
[oppCol
]];
258 if (['a', 'v'].includes(initColor
)) args
= args
.reverse();
259 const capturer
= (['a', 'b'].includes(initColor
) ? 'b' : 'w');
260 const cp
= this.getUnionCode(args
[0], args
[1], capturer
);
265 // - union to free square (other cases are illegal: return null)
266 // - normal piece to free square,
267 // to enemy normal piece, or
268 // to union (releasing our piece)
270 start: { x: sx
, y: sy
},
271 end: { x: ex
, y: ey
},
284 // Treat free square cases first:
285 if (this.board
[ex
][ey
] == V
.EMPTY
) {
290 c: !!tr
? tr
.c : initColor
,
291 p: !!tr
? tr
.p : initPiece
296 // Now the two cases with union / release:
297 const destColor
= this.board
[ex
][ey
].charAt(0);
298 const destPiece
= this.board
[ex
][ey
].charAt(1);
307 if (ChessRules
.PIECES
.includes(destPiece
)) {
308 // Normal piece: just create union
309 let args
= [!!tr
? tr
.p : initPiece
, destPiece
];
310 if (c
== 'b') args
= args
.reverse();
311 const cp
= this.getUnionCode(args
[0], args
[1], c
);
322 // Releasing a piece in an union: keep track of released piece
323 const up
= this.getUnionPieces(destColor
, destPiece
);
324 let args
= [!!tr
? tr
.p : initPiece
, up
[oppCol
]];
325 if (c
== 'b') args
= args
.reverse();
326 const cp
= this.getUnionCode(args
[0], args
[1], c
);
335 mv
.end
.released
= up
[c
];
339 // noCastle arg: when detecting king attacks
340 getPotentialMovesFrom([x
, y
], noCastle
) {
341 const L
= this.lastMoveEnd
.length
;
342 const lm
= this.lastMoveEnd
[L
-1];
343 if (!!lm
&& (x
!= lm
.x
|| y
!= lm
.y
)) return [];
344 const piece
= (!!lm
? lm
.p : this.getPiece(x
, y
));
346 var saveSquare
= this.board
[x
][y
];
347 this.board
[x
][y
] = this.turn
+ piece
;
351 switch (piece
|| this.getPiece(x
, y
)) {
353 const firstRank
= (c
== 'w' ? 7 : 0);
354 baseMoves
= this.getPotentialPawnMoves([x
, y
]).filter(m
=> {
355 // Skip forbidden 2-squares jumps (except from first rank)
356 // Also skip unions capturing en-passant (not allowed).
359 m
.start
.x
== firstRank
||
360 Math
.abs(m
.end
.x
- m
.start
.x
) == 1 ||
361 this.pawnFlags
[c
][m
.start
.y
]
365 this.board
[x
][y
].charAt(1) == V
.PAWN
||
373 baseMoves
= this.getPotentialRookMoves([x
, y
]);
376 baseMoves
= this.getPotentialKnightMoves([x
, y
]);
379 baseMoves
= this.getPotentialBishopMoves([x
, y
]);
382 baseMoves
= this.getPotentialQueenMoves([x
, y
]);
385 baseMoves
= this.getSlideNJumpMoves(
387 V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]),
390 if (!noCastle
&& this.castleFlags
[this.turn
].some(v
=> v
< V
.size
.y
))
391 baseMoves
= baseMoves
.concat(this.getCastleMoves([x
, y
]));
394 // When a pawn in an union reaches final rank with a non-standard
395 // promotion move: apply promotion anyway
397 const oppCol
= V
.GetOppCol(c
);
398 const oppLastRank
= (c
== 'w' ? 7 : 0);
399 baseMoves
.forEach(m
=> {
401 m
.end
.x
== oppLastRank
&&
402 ['c', 'd', 'e', 'f', 'g'].includes(m
.appear
[0].p
)
404 // Move to first rank, which is last rank for opponent's pawn.
405 // => Show promotion choices.
406 // Find our piece in union (not a pawn)
407 const up
= this.getUnionPieces(m
.appear
[0].c
, m
.appear
[0].p
);
408 // merge with all potential promotion pieces + push (loop)
409 for (let promotionPiece
of [V
.ROOK
, V
.KNIGHT
, V
.BISHOP
, V
.QUEEN
]) {
410 let args
= [up
[c
], promotionPiece
];
411 if (c
== 'b') args
= args
.reverse();
412 const cp
= this.getUnionCode(args
[0], args
[1], c
);
413 let cpMove
= JSON
.parse(JSON
.stringify(m
));
414 cpMove
.appear
[0].c
= cp
.c
;
415 cpMove
.appear
[0].p
= cp
.p
;
421 m
.vanish
.length
> 0 &&
422 m
.vanish
[0].p
== V
.PAWN
&&
423 m
.start
.y
!= m
.end
.y
&&
424 this.board
[m
.end
.x
][m
.end
.y
] == V
.EMPTY
427 // No en-passant inside a chaining
429 // Fix en-passant capture: union type, maybe released piece too
430 const cs
= [m
.end
.x
+ (c
== 'w' ? 1 : -1), m
.end
.y
];
431 const code
= this.board
[cs
[0]][cs
[1]].charAt(1);
432 if (code
== V
.PAWN
) {
433 // Simple en-passant capture (usual: just form union)
438 // An union pawn + something just moved two squares
439 const color
= this.board
[cs
[0]][cs
[1]].charAt(0);
440 const up
= this.getUnionPieces(color
, code
);
441 m
.end
.released
= up
[c
];
442 let args
= [V
.PAWN
, up
[oppCol
]];
443 if (c
== 'b') args
= args
.reverse();
444 const cp
= this.getUnionCode(args
[0], args
[1], c
);
445 m
.appear
[0].c
= cp
.c
;
446 m
.appear
[0].p
= cp
.p
;
452 if (!!lm
) this.board
[x
][y
] = saveSquare
;
456 getEpSquare(moveOrSquare
) {
457 if (typeof moveOrSquare
=== "string") {
458 const square
= moveOrSquare
;
459 if (square
== "-") return undefined;
460 return V
.SquareToCoords(square
);
462 const move = moveOrSquare
;
463 const s
= move.start
,
467 Math
.abs(s
.x
- e
.x
) == 2 &&
468 this.getPiece(s
.x
, s
.y
) == V
.PAWN
478 getCastleMoves([x
, y
]) {
480 const accepted
= (c
== 'w' ? ['v', 'w'] : ['a', 'b']);
481 const oppCol
= V
.GetOppCol(c
);
483 const finalSquares
= [ [2, 3], [6, 5] ];
484 castlingCheck: for (let castleSide
= 0; castleSide
< 2; castleSide
++) {
485 if (this.castleFlags
[c
][castleSide
] >= 8) continue;
486 const rookPos
= this.castleFlags
[c
][castleSide
];
487 const castlingColor
= this.board
[x
][rookPos
].charAt(0);
488 const castlingPiece
= this.board
[x
][rookPos
].charAt(1);
490 // Nothing on the path of the king ?
491 const finDist
= finalSquares
[castleSide
][0] - y
;
492 let step
= finDist
/ Math
.max(1, Math
.abs(finDist
));
494 let kingSquares
= [y
];
497 this.board
[x
][i
] != V
.EMPTY
&&
498 !accepted
.includes(this.getColor(x
, i
))
500 continue castlingCheck
;
504 } while (i
!= finalSquares
[castleSide
][0]);
505 // No checks on the path of the king ?
506 if (this.isAttacked(kingSquares
, oppCol
)) continue castlingCheck
;
508 // Nothing on the path to the rook?
509 step
= castleSide
== 0 ? -1 : 1;
510 for (i
= y
+ step
; i
!= rookPos
; i
+= step
) {
511 if (this.board
[x
][i
] != V
.EMPTY
) continue castlingCheck
;
514 // Nothing on final squares, except maybe king and castling rook?
515 for (i
= 0; i
< 2; i
++) {
517 finalSquares
[castleSide
][i
] != rookPos
&&
518 this.board
[x
][finalSquares
[castleSide
][i
]] != V
.EMPTY
&&
520 finalSquares
[castleSide
][i
] != y
||
521 // TODO: next test seems superflu
522 !accepted
.includes(this.getColor(x
, finalSquares
[castleSide
][i
]))
525 continue castlingCheck
;
534 y: finalSquares
[castleSide
][0],
540 y: finalSquares
[castleSide
][1],
546 // King might be initially disguised (Titan...)
547 new PiPo({ x: x
, y: y
, p: V
.KING
, c: c
}),
548 new PiPo({ x: x
, y: rookPos
, p: castlingPiece
, c: castlingColor
})
551 Math
.abs(y
- rookPos
) <= 2
552 ? { x: x
, y: rookPos
}
553 : { x: x
, y: y
+ 2 * (castleSide
== 0 ? -1 : 1) }
561 getEnpassantCaptures(sq
, shiftX
) {
562 // HACK: when artificially change turn, do not consider en-passant
563 const mcMod2
= this.movesCount
% 2;
565 if ((c
== 'w' && mcMod2
== 1) || (c
== 'b' && mcMod2
== 0)) return [];
566 return super.getEnpassantCaptures(sq
, shiftX
);
569 isAttacked_aux(files
, color
, positions
, fromSquare
, released
) {
570 // "positions" = array of FENs to detect infinite loops. Example:
571 // r1q1k2r/p1Pb1ppp/5n2/1f1p4/AV5P/P1eDP3/3B1PP1/R3K1NR,
572 // Bxd2 Bxc3 Bxb4 Bxc3 Bxb4 etc.
573 const newPos
= { fen: super.getBaseFen(), piece: released
};
574 if (positions
.some(p
=> p
.piece
== newPos
.piece
&& p
.fen
== newPos
.fen
))
575 // Start of an infinite loop: exit
577 positions
.push(newPos
);
578 const rank
= (color
== 'w' ? 0 : 7);
579 const moves
= this.getPotentialMovesFrom(fromSquare
);
580 if (moves
.some(m
=> m
.end
.x
== rank
&& files
.includes(m
.end
.y
)))
583 for (let m
of moves
) {
584 if (!!m
.end
.released
) {
585 // Turn won't change since !!m.released
587 const res
= this.isAttacked_aux(
588 files
, color
, positions
, [m
.end
.x
, m
.end
.y
], m
.end
.released
);
590 if (res
) return true;
596 isAttacked(files
, color
) {
597 const rank
= (color
== 'w' ? 0 : 7);
598 // Since it's too difficult (impossible?) to search from the square itself,
599 // let's adopt a suboptimal but working strategy: find all attacks.
601 // Artificial turn change is required:
604 outerLoop: for (let i
=0; i
<8; i
++) {
605 for (let j
=0; j
<8; j
++) {
606 // Attacks must start from a normal piece, not an union.
607 // Therefore, the following test is correct.
609 this.board
[i
][j
] != V
.EMPTY
&&
610 [V
.KING
, V
.PAWN
, V
.ROOK
, V
.KNIGHT
, V
.BISHOP
, V
.QUEEN
].includes(
611 this.board
[i
][j
].charAt(1)) &&
612 this.board
[i
][j
].charAt(0) == color
615 const moves
= this.getPotentialMovesFrom([i
, j
], "noCastle");
616 if (moves
.some(m
=> m
.end
.x
== rank
&& files
.includes(m
.end
.y
))) {
620 for (let m
of moves
) {
621 if (!!m
.end
.released
) {
622 // Turn won't change since !!m.released
625 res
= this.isAttacked_aux(
626 files
, color
, positions
, [m
.end
.x
, m
.end
.y
], m
.end
.released
);
628 if (res
) break outerLoop
;
638 // Do not consider checks, except to forbid castling
644 if (moves
.length
== 0) return [];
645 return moves
.filter(m
=> {
646 if (!m
.end
.released
) return true;
647 // Check for repetitions:
648 V
.PlayOnBoard(this.board
, m
);
649 const newState
= { piece: m
.end
.released
, position: this.getBaseFen() };
651 this.repetitions
.some(r
=> {
653 r
.piece
== newState
.piece
&&
654 r
.position
== newState
.position
657 V
.UndoOnBoard(this.board
, m
);
662 updateCastleFlags(move, piece
) {
664 const firstRank
= (c
== "w" ? 7 : 0);
665 if (piece
== V
.KING
&& move.appear
.length
> 0)
666 this.castleFlags
[c
] = [V
.size
.y
, V
.size
.y
];
668 move.start
.x
== firstRank
&&
669 this.castleFlags
[c
].includes(move.start
.y
)
671 const flagIdx
= (move.start
.y
== this.castleFlags
[c
][0] ? 0 : 1);
672 this.castleFlags
[c
][flagIdx
] = V
.size
.y
;
675 move.end
.x
== firstRank
&&
676 this.castleFlags
[c
].includes(move.end
.y
)
678 // Move to our rook: necessary normal piece, to union, releasing
679 // (or the rook was moved before!)
680 const flagIdx
= (move.end
.y
== this.castleFlags
[c
][0] ? 0 : 1);
681 this.castleFlags
[c
][flagIdx
] = V
.size
.y
;
686 // Easier before move is played in this case (flags are saved)
688 const L
= this.lastMoveEnd
.length
;
689 const lm
= this.lastMoveEnd
[L
-1];
693 this.getPiece(move.vanish
[0].x
, move.vanish
[0].y
);
695 this.kingPos
[c
] = [move.appear
[0].x
, move.appear
[0].y
];
696 this.updateCastleFlags(move, piece
);
697 const pawnFirstRank
= (c
== 'w' ? 6 : 1);
699 move.start
.x
== pawnFirstRank
&&
701 Math
.abs(move.end
.x
- move.start
.x
) == 2
703 // This move turns off a 2-squares pawn flag
704 this.pawnFlags
[c
][move.start
.y
] = false;
709 move.flags
= JSON
.stringify(this.aggregateFlags());
711 this.epSquares
.push(this.getEpSquare(move));
712 // Check if the move is the last of the turn: all cases except releases
713 if (!move.end
.released
) {
714 // No more union releases available
715 this.turn
= V
.GetOppCol(this.turn
);
717 this.lastMoveEnd
.push(null);
720 this.lastMoveEnd
.push(Object
.assign({ p: move.end
.released
}, move.end
));
721 V
.PlayOnBoard(this.board
, move);
722 if (!move.end
.released
) this.repetitions
= [];
724 this.repetitions
.push(
726 piece: move.end
.released
,
727 position: this.getBaseFen()
734 this.epSquares
.pop();
735 this.disaggregateFlags(JSON
.parse(move.flags
));
736 V
.UndoOnBoard(this.board
, move);
737 this.lastMoveEnd
.pop();
738 if (!move.end
.released
) {
739 this.turn
= V
.GetOppCol(this.turn
);
742 if (!!move.end
.released
) this.repetitions
.pop();
747 if (this.getPiece(move.start
.x
, move.start
.y
) == V
.KING
)
748 this.kingPos
[this.turn
] = [move.start
.x
, move.start
.y
];
752 // Check kings: if one is captured, the side lost
753 for (let c
of ['w', 'b']) {
754 const kp
= this.kingPos
[c
];
755 const cell
= this.board
[kp
[0]][kp
[1]];
759 (c
== 'w' && ['a', 'b'].includes(cell
[0])) ||
760 (c
== 'b' && ['v', 'w'].includes(cell
[0]))
764 return (c
== 'w' ? "0-1" : "1-0");
771 let initMoves
= this.getAllValidMoves();
772 if (initMoves
.length
== 0) return null;
773 // Loop until valid move is found (no blocked pawn released...)
775 let moves
= JSON
.parse(JSON
.stringify(initMoves
));
778 // Just play random moves (for now at least. TODO?)
779 while (moves
.length
> 0) {
780 mv
= moves
[randInt(moves
.length
)];
783 if (!!mv
.end
.released
)
784 // A piece was just released from an union
785 moves
= this.getPotentialMovesFrom([mv
.end
.x
, mv
.end
.y
]);
788 for (let i
= mvArray
.length
- 1; i
>= 0; i
--) this.undo(mvArray
[i
]);
789 if (!mv
.end
.released
) return (mvArray
.length
> 1 ? mvArray : mvArray
[0]);
791 return null; //never reached
794 // NOTE: evalPosition() is wrong, but unused since bot plays at random
797 if (move.appear
.length
== 2 && move.appear
[0].p
== V
.KING
)
798 return (move.end
.y
< move.start
.y
? "0-0-0" : "0-0");
801 const L
= this.lastMoveEnd
.length
;
802 const lm
= this.lastMoveEnd
[L
-1];
804 if (!lm
&& move.vanish
.length
== 0)
805 // When importing a game, the info move.released is lost
806 piece
= move.appear
[0].p
;
807 else piece
= (!!lm
? lm
.p : move.vanish
[0].p
);
808 if (!(ChessRules
.PIECES
.includes(piece
))) {
809 // Decode (moving) union
810 const up
= this.getUnionPieces(
811 move.vanish
.length
> 0 ? move.vanish
[0].c : move.appear
[0].c
, piece
);
815 // Basic move notation:
816 let notation
= piece
.toUpperCase();
818 this.board
[move.end
.x
][move.end
.y
] != V
.EMPTY
||
819 (piece
== V
.PAWN
&& move.start
.y
!= move.end
.y
)
823 const finalSquare
= V
.CoordsToSquare(move.end
);
824 notation
+= finalSquare
;
826 // Add potential promotion indications:
827 const firstLastRank
= (c
== 'w' ? [7, 0] : [0, 7]);
828 if (move.end
.x
== firstLastRank
[1] && piece
== V
.PAWN
) {
830 if (ChessRules
.PIECES
.includes(move.appear
[0].p
))
831 notation
+= move.appear
[0].p
.toUpperCase();
833 const up
= this.getUnionPieces(move.appear
[0].c
, move.appear
[0].p
);
834 notation
+= up
[c
].toUpperCase();
838 move.end
.x
== firstLastRank
[0] &&
839 move.vanish
.length
> 0 &&
840 ['c', 'd', 'e', 'f', 'g'].includes(move.vanish
[0].p
)
842 // We promoted an opponent's pawn
843 const oppCol
= V
.GetOppCol(c
);
844 const up
= this.getUnionPieces(move.appear
[0].c
, move.appear
[0].p
);
845 notation
+= "=" + up
[oppCol
].toUpperCase();