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? Jun 17, 2014 at 18:17
  • Yeah I am writing code. 4K3 is also valid and there are not 4 spaces.
    – Niklas
    Jun 17, 2014 at 18:25
  • 1
    Sure there are; I carefully did not say "four characters". You just have to treat numerals and alphabetical characters differently. Jun 17, 2014 at 18:27
  • 4
    If you're writing code, then I suppose this is a question for stackoverflow.com Jun 17, 2014 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. Jun 17, 2014 at 18:29

1 Answer 1


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? Jun 17, 2014 at 21:57
  • Yup I did. I updated my post as I wrote a handy function.
    – Niklas
    Jun 18, 2014 at 6:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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