Skip to content

12. Castling and En passant

phu54321 edited this page Feb 10, 2018 · 9 revisions

Table of Contents generated with DocToc

The material used in this chapter can be downloaded at here.

In this chapter, we implement castling and En passant rules.

Castling En passant

Updating LMTN (Last moved turn number)

On the last chapter, we introduced a concept of object to store LTMN for each chess cells. We created the lastMovedTurn member, but we haven't stored proper values here. To use this field, we first have to put a correct number to lastMovedTurn member.

LTMN, by definition, is the turn count when the piece have moved the last. To put proper LTMN value there, we should

  1. Count 'turn number' properly.
  2. Store LTMN for each piece moves.

The code in afterTriggerExec implements piece moving. Since we should update turn number by every piece moves, afterTriggerExec function would be a great place to count turn number and update LTMNs.

/* Skipping */

var beforeUnitPlayer, beforeUnitType;
var beforeUnitX, beforeUnitY = -1, -1;

//// NEW
var currentTurn = 1;  // New
//// NEW

function afterTriggerExec() {
    const unitType, selX, selY = sel.getSelectedUnit();
    if (unitType != -1) {
        if(unitType == $U('Zerg Scourge')) {
            piece.removePiece(selX, selY);
            piece.placePiece(selX, selY, beforeUnitType, beforeUnitPlayer);

//// NEW
            tile.tile(selX, selY).lastMovedTurn = currentTurn;
            currentTurn++;
//// NEW

}}} /* Skipping */

Put simply, we created currentTurn variable to store the current turn number, and as we place the pieces we update their lastMovedTurn number. The expression tile.tile(selX, selY).lastMovedTurn = currentTurn; may seem complex, but you can paraphrase this expression to this code.

// Create a piece on (selX, selY)
const piece = tile.tile(selX, selY);   // Get the created piece object.
piece.lastMovedTurn = currentTurn;  // Set its LMTN to currentTurn

So we have valid LMTN value for each pieces. Let's jump on implementing castling.

Castling

To perform a castling, we move the king pieces by two cells right or left, and then we move the rook right next to the king, on the opposite direction the king has moved to. So it's the king that first moves on the castling. So wee need to modify king.eps to perform castling. king.eps currently looks like this.

function getPossibleDestination(player, x, y) {
    const dxTable = [-1, 0, 1, -1, 1, -1, 0, 1];
    const dyTable = [-1, -1, -1, 0, 0, 1, 1, 1];
    for(var direction = 0 ; direction < 8 ; direction++) {
        const dx, dy = dxTable[direction], dyTable[direction];
        if (common.canPieceGoTo(player, x + dx, y + dy)) {
            common.placeScourge(player, x + dx, y + dy);
        }
    }
}

Remember three conditions for castling?

  1. Both the king piece and the rook piece shouldn't have moved before.
  2. There are no pieces between two pieces.
  3. King is not currently in check, and king won't be checked after the castling.
    • Any squares the king would pass during the castling is not being attacked by opponent

We won't think about the third condition for now. Just check the first and the second one. The first condition can be checked with if (kingPiece.lastMovedTurn == 0 && rookPiece.lastMovedTurn == 0), where const kingPiece = tile.tile(x, y);. We can check the second condition and get what rookPiece is with a simple for loop, like this.

// Going to the left side
for(var x1 = x - 1 ; x1 < 8 ; x1--) {
    const piece = tile.tile(x1, y);
    if(piece) {
        if(x1 == 0 && piece.unitType == $U('Rook') && piece.player == player) {
            // rookPiece is piece.
            break;
        }
        else break;  // Something is between rook and king, or rook is not at x1=0.
    }
}

// Going to the right side
for(var x1 = x + 1 ; x1 < 8 ; x1++) {
    const piece = tile.tile(x1, y);
    if(piece) {
        if(x1 == 7 && piece.unitType == $U('Rook') && piece.player == player) {
            // rookPiece is piece.
            break;
        }
        else break;  // Something is between rook and king, or rook is not at x1=7.
    }
}

Two for loops look similar, so we merge them.

// Going to the left side
const dxTable = [-1, 1];  // dx
const rookXTable = [0, 7];  //  corresponding rook's expected column.
for(var i = 0 ; i < 2 ; i++) {
    const dx = dxTable[i];
    for(var x1 = x + dx ; x1 < 8 ; x1 += dx) {
        const piece = tile.tile(x1, y);
        if(piece) {
            if(x1 == rookXTable[i] && piece.unitType == $U('Rook') && piece.player == player) {
                // rookPiece is piece.
                if (piece.lastMovedTurn == 0) {
                    // We can perform a castling
                    break;
                }
            }
            else break;  // Something is between rook and king, or rook is not at x1=0.
        }
    }
}

To summarize, king.eps now looks like this.

import tile;
import loc;
import pieceRule.common;

function getPossibleDestination(player, x, y) {
    const dxTable = [-1, 0, 1, -1, 1, -1, 0, 1];
    const dyTable = [-1, -1, -1, 0, 0, 1, 1, 1];
    for(var direction = 0 ; direction < 8 ; direction++) {
        const dx, dy = dxTable[direction], dyTable[direction];
        if (common.canPieceGoTo(player, x + dx, y + dy)) {
            common.placeScourge(player, x + dx, y + dy);
        }
    }

    const kingPiece = tile.tile(x, y);
    if (kingPiece.lastMovedTurn == 0) {
        const dxTable2 = [-1, 1];  // dx
        const rookXTable = [0, 7];  //  corresponding rook's expected column.
        for(var i = 0 ; i < 2 ; i++) {
            const dx = dxTable2[i];
            for(var x1 = x + dx ; x1 < 8 ; x1 += dx) {
                const piece = tile.tile(x1, y);
                if(piece) {
                    if(x1 == rookXTable[i] && piece.unitType == $U('Rook') && piece.player == player && piece.lastMovedTurn == 0) {
                        common.placeScourge(player, x + dx * 2, y);
                    }
                    else break;
                }
            }
        }
    }
}

Done that, we now have this. I moved the right rook back and forth to update its LMTN. We now know the directions where a king can perform castling to.

Castling availability

Unlike any other movement in chess, you move two pieces during the castling. Piece moving is handled on afterTriggerExec on main.eps. We have to modify this logic a bit, too. This modification is dead simple.

        if(unitType == $U('Zerg Scourge')) {
            piece.removePiece(selX, selY);
            piece.placePiece(selX, selY, beforeUnitType, beforeUnitPlayer);
            tile.tile(selX, selY).lastMovedTurn = currentTurn;

            // Special rule for castling
            if(beforeUnitType == $('King') && beforeUnitY == selY) {
                const deltaX = selX - beforeUnitX;  // X change of the king piece.
                if(deltaX == -2 || deltaX == 2) {  // Castling was performed.
                    // Also move rook.
                    const rookBeforeX = (deltaX == -2) ? 0 : 7;
                    const rookAfterX = selX + (deltaX == -2) ? 1 : -1;
                    piece.removePiece(rookBeforeX, selY);
                    piece.placePiece(rookAfterX, selY, $U('Rook'), beforeUnitPlayer);
                    tile.tile(rookAfterX, selY).lastMovedTurn = currentTurn;

                }
            }

            currentTurn++;  // We update currentTurn after processing castling rule.

            piece.removePiece(beforeUnitX, beforeUnitY);
            RemoveUnit('Zerg Scourge', Force3);


            beforeUnitX = -1;
            beforeUnitY = -1;
        }

And now we have a castling!

Castling performed

En passant

En passant rule has two conditions.

  1. Captured pawn has just moved two squares.
  2. Capturing pawn is right next to the captured pawn.

These two conditions are somewhat hard to express directly, so I'll modify them a bit.

  1. Captured pawn has just moved. (capturedPiece.lastMovedTurn - currentTurn - 1)
  2. Captured pawn is two rows front of its initial position. capturedPiece.y = (3 for P7, 4 for P8)
  3. Capturing pawn is right next to the captured pawn. capturingPiece.y == capturedPiece.y && capturingPiece - capturedPiece.y == 1 or -1