Rooks, Files, and Ranks

Details

Author:Mark
Rating:
Difficulty:Beginner
Date:1st February 2021
Description:An introduction on how to use open files to send rooks into enemy territory, and how to use the 7th rank to eat up the enemy pawns.

Code

//Please feel welcome to copy this code for your own creations.

let steps = [];
let targetMove = "";

steps.push(function () {
    fromFen("2r3rk/pp3pp1/8/1p5p/4P3/P4P2/1P2R1PP/3R2K1 w - - 0 1");

    info("A file is called 'open' if it contains no pawns on it.\
    <br>Click on an open file to continue.");

    onPieceClicked(function (squareName) {
        return false;
    });

    onSquareClicked(function (squareName) {
        if (squareName.startsWith("c") || squareName.startsWith("d")) {
            highlightSquareGreen(squareName);
            setTimeout(unhighlightAll, 250);
            nextDelayed();
        } else {
            highlightSquareRed(squareName);
        }
    });
});

steps.push(function () {
    info("A file is called 'closed' if it contains pawns from both players.\
    <br>Click on a closed file to continue.");

    onSquareClicked(function (squareName) {
        switch (squareName.substr(0, 1)) {
            case "a":
            case "b":
            case "f":
            case "g":
            case "h":
                highlightSquareGreen(squareName);
                setTimeout(unhighlightAll, 250);
                nextDelayed();
                break;
            default:
                highlightSquareRed(squareName);
        }
    });
});

steps.push(function () {
    info("A file is called 'half-open' if it contains a pawn from only one player.\
    <br>Click on a half-open file to continue.");

    onSquareClicked(function (squareName) {
        if (squareName.startsWith("e")) {
            highlightSquareGreen(squareName);
            setTimeout(unhighlightAll, 250);
            nextDelayed();
        } else {
            highlightSquareRed(squareName);
        }
    });
});

steps.push(function () {
    info("A white rook enjoys being on rank 7 (black likes rank 2). This is because:\
    <ul>\
    <li>There are usually many pawns that the rook can take.</li>\
    <li>The opponent's king is trapped on the back rank</li>\
    </ul>\
    Play <b>Rd7</b> to continue.");

    targetMove = "Rd7";
    setupPlayFunctions();
});

steps.push(function() {
    info("White has forked the pawns on the b and f files.\
    play <b>Rgf8</b> to protect one of them.");
    targetMove = "Rgf8";
});

steps.push(function() {
    info("White is free to take the pawn on b7.\
    Play <b>Rxb7</b> to continue.");
    targetMove = "Rxb7";
});

steps.push(function() {
    info("Correct!");
    showNextButton(true);
});

steps.push(function() {
    fromFen("1r3kr1/1p1R1p2/p5p1/1p5p/8/P4P2/1P2R1PP/6K1 w - - 0 1");
    info("If you think having one rook on the 7th rank is good, try having two of them.");
    targetMove = "Ree7";
});

steps.push(function() {
    info("Blacks best option here is <b>Rg7</b>");
    targetMove = "Rg7";
});

steps.push(function() {
    info("White is free to play <b>Rxb7</b>");
    targetMove = "Rxb7";
});

steps.push(function() {
    info("Having two rooks on the 7th rank is often called 'pigs on the 7th'.");
    showNextButton(true);
});

steps.push(function() {
    fromFen("r2r2k1/5pp1/2b4p/1p6/2pP1PQ1/6P1/PP5P/KR2R3 b - - 0 1");
    blackAtTop(false);
    info("See if you can clear a half-open file here to achieve checkmate");
    targetMove = "Rxa2+";
});

steps.push(function() {
    setTimeout(function() {
        performMove("Kxa2");
        nextDelayed(true);
    }, 100);
});

steps.push(function() {
    targetMove = "Ra8#";
});

steps.push(function () {
    info("Checkmate!");
    complete();
});

function setupPlayFunctions() {
    onPieceClicked(function (squareName) {
        if (typeof targetMove == "string") {
            if (squareName == sourceOfSan(targetMove)) {
                return true;
            }
        } else if (Array.isArray(targetMove)) {
            for (let temp of targetMove) {
                if (squareName == sourceOfSan(temp)) {
                    return true;
                }
            }
        } else {
            throw new Error();
        }
        if (targetMove == "") {
            return false;
        }

        if (squareName == sourceOfSan(targetMove)) {
            return true;
        }

        highlightSquareRed(squareName);
        return false;
    });

    onMoveAttempted(function (san) {
        if (typeof targetMove == "string") {
            if (san == targetMove) {
                setTimeout(next, 50);
                return true;
            }
        } else if (Array.isArray(targetMove)) {
            for (let temp of targetMove) {
                if (san == temp) {
                    setTimeout(next, 50);
                    return true;
                }
            }
        } else {
            throw new Error();
        }

        return false;
    });

    onSquareClicked(function (squareName) { });
}

onNextClicked(function () {
    showNextButton(false);
    next();
});

function nextDelayed() {
    setTimeout(next, 400);
}

function next() {
    if (steps.length > 0) {
        //remove the first function from the array and call it
        steps.shift()();
    }
}

next();