- // Captures
- for (let shiftY of [-1, 1]) {
- if (
- y + shiftY >= 0 &&
- y + shiftY < sizeY &&
- this.board[x + shiftX][y + shiftY] != V.EMPTY &&
- this.canTake([x, y], [x + shiftX, y + shiftY])
- ) {
- for (let piece of finalPieces) {
- moves.push(
- this.getBasicMove([x, y], [x + shiftX, y + shiftY], {
- c: color,
- p: piece
- })
- );
+ for (let piece of finalPieces) {
+ moves.push(
+ this.getBasicMove([x1, y1], [x2, y2], {
+ c: color,
+ p: piece
+ })
+ );
+ }
+ }
+
+ // Pawn movements in shiftX direction:
+ const getPawnMoves = (shiftX) => {
+ let moves = [];
+ // NOTE: next condition is generally true (no pawn on last rank)
+ if (x + shiftX >= 0 && x + shiftX < sizeX) {
+ if (this.board[x + shiftX][y] == V.EMPTY) {
+ // One square forward
+ addMoves([x, y], [x + shiftX, y], moves);
+ // Next condition because pawns on 1st rank can generally jump
+ if (
+ V.PawnSpecs.twoSquares &&
+ [startRank, firstRank].includes(x) &&
+ this.board[x + 2 * shiftX][y] == V.EMPTY
+ ) {
+ // Two squares jump
+ moves.push(this.getBasicMove([x, y], [x + 2 * shiftX, y]));
+ }
+ }
+ // Captures
+ if (V.PawnSpecs.canCapture) {
+ for (let shiftY of [-1, 1]) {
+ if (
+ y + shiftY >= 0 &&
+ y + shiftY < sizeY
+ ) {
+ if (
+ this.board[x + shiftX][y + shiftY] != V.EMPTY &&
+ this.canTake([x, y], [x + shiftX, y + shiftY])
+ ) {
+ addMoves([x, y], [x + shiftX, y + shiftY], moves);
+ }
+ if (
+ V.PawnSpecs.captureBackward &&
+ x - shiftX >= 0 && x - shiftX < V.size.x &&
+ this.board[x - shiftX][y + shiftY] != V.EMPTY &&
+ this.canTake([x, y], [x - shiftX, y + shiftY])
+ ) {
+ addMoves([x, y], [x + shiftX, y + shiftY], moves);
+ }
+ }