- // TODO: either add a "blackMove' field in FEN (bof...),
- // or write an helper function to detect from diff positions,
- // which piece moved (if not disappeared!), which moves are valid.
- // + do not forget pass move (king 2 king): always possible at stage 2.
- return [];
+ // Diff current and old board to know which pieces have moved,
+ // and to deduce possible moves at stage 2.
+ const L = this.initfenStack.length;
+ let initBoard = V.GetBoard(this.initfenStack[L-1]);
+ let appeared = [];
+ const c = this.turn;
+ const oppCol = V.GetOppCol(c);
+ for (let i=0; i<8; i++) {
+ for (let j=0; j<8; j++) {
+ if (this.board[i][j] != initBoard[i][j]) {
+ if (this.board[i][j] != V.EMPTY) {
+ const color = this.board[i][j].charAt(0);
+ appeared.push({ c: color, x: i, y: j });
+ // Pawns capture in diagonal => the following fix.
+ // (Other way would be to redefine getPotentialPawnMoves()...)
+ if (color == oppCol) initBoard[i][j] = this.board[i][j];
+ }
+ }
+ }
+ }
+ const saveBoard = this.board;
+ this.board = initBoard;
+ const movesInit = super.getPotentialMovesFrom([x, y]);
+ this.board = saveBoard;
+ const target = appeared.find(a => a.c == oppCol) || { x: -1, y: -1 };
+ let movesNow = super.getPotentialMovesFrom([x, y]).filter(m => {
+ return (
+ m.end.x == target.x &&
+ m.end.y == target.y &&
+ movesInit.some(mi => mi.end.x == m.end.x && mi.end.y == m.end.y)
+ );
+ });
+ const passTarget =
+ (x != this.kingPos[c][0] || y != this.kingPos[c][0]) ? c : oppCol;
+ movesNow.push(
+ new Move({
+ start: { x: x, y: y },
+ end: {
+ x: this.kingPos[passTarget][0],
+ y: this.kingPos[passTarget][1]
+ },
+ appear: [],
+ vanish: []
+ })
+ );
+ return movesNow;