- // Consider all potential promotions.
- // NOTE: "promotions" arg = special override for Hiddenqueen variant
- addPawnMoves([x1, y1], [x2, y2], moves, promotions) {
- let finalPieces = ["p"];
- const color = this.getColor(x1, y1);
- const oppCol = C.GetOppCol(color);
- const lastRank = (color == "w" ? 0 : this.size.x - 1);
- const promotionOk =
- x2 == lastRank && (!this.options["rifle"] || this.board[x2][y2] == "");
- if (promotionOk && !this.options["pawnfall"]) {
- if (
- this.options["cannibal"] &&
- this.board[x2][y2] != "" &&
- this.getColor(x2, y2) == oppCol
- ) {
- finalPieces = [this.getPieceType(x2, y2)];
- }
- else if (promotions)
- finalPieces = promotions;
- else if (this.pawnSpecs.promotions)
- finalPieces = this.pawnSpecs.promotions;
- }
- for (let piece of finalPieces) {
- const tr = !this.options["pawnfall"] && piece != "p"
- ? { c: color, p: piece }
- : null;
- let newMove = this.getBasicMove([x1, y1], [x2, y2], tr);
- if (promotionOk && this.options["pawnfall"]) {
- newMove.appear.shift();
- newMove.pawnfall = true; //required in prePlay()
- }
- moves.push(newMove);
- }
- }
-
- // What are the pawn moves from square x,y ?
- getPotentialPawnMoves([x, y], promotions) {
- const color = this.getColor(x, y); //this.turn doesn't work for Dark mode
- const [sizeX, sizeY] = [this.size.x, this.size.y];
- const pawnShiftX = this.pawnSpecs.directions[color];
- const firstRank = (color == "w" ? sizeX - 1 : 0);
- const forward = (color == 'w' ? -1 : 1);
-
- // 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] == "") {
- // One square forward (or backward)
- this.addPawnMoves([x, y], [x + shiftX, y], moves, promotions);
- // Next condition because pawns on 1st rank can generally jump
- if (
- this.pawnSpecs.twoSquares &&
- (
- (
- color == 'w' &&
- x >= this.size.x - 1 - this.pawnSpecs.initShift['w']
- )
- ||
- (color == 'b' && x <= this.pawnSpecs.initShift['b'])
- )
- ) {
- if (
- shiftX == forward &&
- this.board[x + 2 * shiftX][y] == ""
- ) {
- // Two squares jump
- moves.push(this.getBasicMove([x, y], [x + 2 * shiftX, y]));
- if (
- this.pawnSpecs.threeSquares &&
- this.board[x + 3 * shiftX, y] == ""
- ) {
- // Three squares jump
- moves.push(this.getBasicMove([x, y], [x + 3 * shiftX, y]));
- }
- }
- }
- }
- // Captures
- if (this.pawnSpecs.canCapture) {
- for (let shiftY of [-1, 1]) {
- const yCoord = this.computeY(y + shiftY);
- if (yCoord >= 0 && yCoord < sizeY) {
- if (
- this.board[x + shiftX][yCoord] != "" &&
- this.canTake([x, y], [x + shiftX, yCoord]) &&
- (
- !this.options["zen"] ||
- this.getPieceType(x + shiftX, yCoord) == "k"
- )
- ) {
- this.addPawnMoves(
- [x, y], [x + shiftX, yCoord],
- moves, promotions
- );
- }
- if (
- this.pawnSpecs.captureBackward && shiftX == forward &&
- x - shiftX >= 0 && x - shiftX < this.size.x &&
- this.board[x - shiftX][yCoord] != "" &&
- this.canTake([x, y], [x - shiftX, yCoord]) &&
- (
- !this.options["zen"] ||
- this.getPieceType(x + shiftX, yCoord) == "k"
- )
- ) {
- this.addPawnMoves(
- [x, y], [x - shiftX, yCoord],
- moves, promotions
- );
- }
- }
- }
- }
- }
- return moves;
- }
-
- let pMoves = getPawnMoves(pawnShiftX);
- if (this.pawnSpecs.bidirectional)
- pMoves = pMoves.concat(getPawnMoves(-pawnShiftX));
-
- if (this.hasEnpassant) {
- // NOTE: backward en-passant captures are not considered
- // because no rules define them (for now).
- Array.prototype.push.apply(
- pMoves,
- this.getEnpassantCaptures([x, y], pawnShiftX)
- );
- }
-
- if (this.options["zen"])
- Array.prototype.push.apply(pMoves, this.getZenCaptures(x, y));
-
- return pMoves;
- }
-