Is there some easy way to check whether the black king is on e8 or the white king is on e1, when using fen?

Here the black and white king are on d1/8 r2k3r/3q4/8/8/8/8/8/R1QK2RR

Here both are on the right field r3k2r/3q4/8/8/8/8/8/R1Q1K1RR

The problem is that there is really no regularity, to check by indexing whether the King is on e1/e8.

The fen above, could also be rb2k2r/3q4/8/8/8/8/8/R1Q1K1RR and would still be valid.

Checking for the rocks are pretty easy, since they have to be at the beginning or the end of the fen.

  • 1
    Sure, "are there exactly four spaces accounted for before the K". What is the context here? Are you writing code, or what? – Henry Keiter Jun 17 '14 at 18:17
  • Yeah I am writing code. 4K3 is also valid and there are not 4 spaces. – Niklas Jun 17 '14 at 18:25
  • 1
    Sure there are; I carefully did not say "four characters". You just have to treat numerals and alphabetical characters differently. – Henry Keiter Jun 17 '14 at 18:27
  • 4
    If you're writing code, then I suppose this is a question for stackoverflow.com – Wes Jun 17 '14 at 18:27
  • 1
    Also, a full FEN record includes castling availability information, which appears to be what you're actually looking for in the first place. After all, the current placement of Kings and Rooks is not sufficient for determining if a player can castle. – Henry Keiter Jun 17 '14 at 18:29

Thanks to Henry for the tip. Here is my solution.

var kingOnRightField = function (groundRow, king) {
    var index = 0;

    for (var i = 0; i < groundRow.length; i++) {
        var number = parseInt(groundRow[i]);
        index += isNaN(number) ? 1 : number;

        if (index == 5 && groundRow[i] == king) return true;

    return false;

var fen = "r3k2r/3q4/8/8/8/8/8/R1Q1K1RR";

var blackGroundRow = fen.split("/")[0];
var whiteGroundRow = fen.split("/")[7];

var blackKingOnRightField = kingOnRightField(blackGroundRow, 'k');
var whiteKingOnRightField = kingOnRightField(whiteGroundRow, 'K');
  • Have you tested the code with different types of FEN? – Wes Jun 17 '14 at 21:57
  • Yup I did. I updated my post as I wrote a handy function. – Niklas Jun 18 '14 at 6:24

To add to Henry's answer this is the proper nomenclature in a FEN file for castling availability:

If neither side can castle, this is "-". Otherwise, this has one or more letters: "K" (White can castle kingside), "Q" (White can castle queenside), "k" (Black can castle kingside), and/or "q" (Black can castle queenside).

  • I already do know that, but if somebody is setting up a position on the board with spare pieces, there is no complete FEN string available. So one has to be generated. And for the castling part, I have to double check if the rocks and the kings are on their right fields. – Niklas Jun 21 '14 at 8:22
  • Ah I see, but in such an instance there is no way to discern the intent of the author's position - whether or not he envisioned the king or rook moving in the development of this position. – maxwell Jun 21 '14 at 17:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.