- // The opponent piece disappears if we take it
- if (this.board[ex][ey] != V.EMPTY) {
- mv.vanish.push(
- new PiPo({
- x: ex,
- y: ey,
- c: this.getColor(ex, ey),
- p: this.board[ex][ey].charAt(1)
- })
- );
- // Pieces are revealed when they capture
- mv.appear[0].p = V.Decode(mv.appear[0].p);
+ // What are the pawn moves from square x,y ?
+ getPotentialPawnMoves([x, y]) {
+ const color = this.turn;
+ let moves = [];
+ const [sizeX, sizeY] = [V.size.x, V.size.y];
+ const shiftX = color == "w" ? -1 : 1;
+ const startRank = color == "w" ? sizeX - 2 : 1;
+ const lastRank = color == "w" ? 0 : sizeX - 1;
+
+ const sq1 = this.getSquareAfter([x,y], [shiftX,0]);
+ if (sq1 && this.board[sq1[0]][y] == V.EMPTY) {
+ // One square forward (cannot be a promotion)
+ moves.push(this.getBasicMove([x, y], [sq1[0], y]));
+ if (x == startRank) {
+ // If two squares after is available, then move is possible
+ const sq2 = this.getSquareAfter([x,y], [2*shiftX,0]);
+ if (sq2 && this.board[sq2[0]][y] == V.EMPTY)
+ // Two squares jump
+ moves.push(this.getBasicMove([x, y], [sq2[0], y]));
+ }
+ }
+ // Captures
+ for (let shiftY of [-1, 1]) {
+ const sq = this.getSquareAfter([x,y], [shiftX,shiftY]);
+ if (
+ !!sq &&
+ this.board[sq[0]][sq[1]] != V.EMPTY &&
+ this.canTake([x, y], [sq[0], sq[1]])
+ ) {
+ const finalPieces = sq[0] == lastRank
+ ? [V.ROOK, V.KNIGHT, V.BISHOP, V.QUEEN]
+ : [V.PAWN];
+ for (let piece of finalPieces) {
+ moves.push(
+ this.getBasicMove([x, y], [sq[0], sq[1]], {
+ c: color,
+ p: piece
+ })
+ );
+ }
+ }