- const getMoveHash = (move) => {
- return (
- "m" + move.start.x + move.start.y +
- move.end.x + move.end.y +
- // Also use m.appear[0].p for pawn promotions
- move.appear[0].p
- );
- };
- myMoves.forEach(m => moveSet[getMoveHash(m)] = true);
- // 1) Generate all opponent's moves
- this.turn = V.GetOppCol(color);
- const oppMoves = super.getAllValidMoves();
- this.turn = color;
- // 2) Play each opponent's move, and see if captures are possible:
- // --> capturing moving unit only (otherwise some issues)
- oppMoves.forEach(m => {
- V.PlayOnBoard(this.board, m);
- // Can I take on [m.end.x, m.end.y] ?
- // If yes and not already in list, add it (without the capturing part)
- let capturingMoves = this.getCaptures(m.end.x, m.end.y);
- capturingMoves.forEach(cm => {
- const cmHash = getMoveHash(cm);
- if (!moveSet[cmHash]) {
- // The captured unit hasn't moved yet, so temporarily cancel capture
- cm.vanish.pop();
- // If m is itself a capturing move: then replace by self-capture
- if (m.vanish.length == 2) cm.vanish.push(m.vanish[1]);
- myMoves.push(cm);
- moveSet[cmHash] = true;
- }
- });
- V.UndoOnBoard(this.board, m);
+ oppCaptureMoves.forEach(m => {
+ // If another opponent capture with same endpoint already processed, skip:
+ const mHash = "m" + m.end.x + m.end.y;
+ if (!moveSet[mHash]) {
+ moveSet[mHash] = true;
+ // Just make enemy piece disappear, to clear potential path:
+ const justDisappear = {
+ appear: [],
+ vanish: [m.vanish[0]]
+ };
+ V.PlayOnBoard(this.board, justDisappear);
+ // Can I take on [m.end.x, m.end.y] ? If yes, add to list:
+ this.getCaptures(m.end.x, m.end.y, color).forEach(cm => myMoves.push(cm));
+ V.UndoOnBoard(this.board, justDisappear);
+ }