Answers.Body: <p>Thanks to Henry for the tip. Here is my solution.</p>
  
  <pre><code>var kingOnRightField = function (groundRow, king) {
      var index = 0;
  
      for (var i = 0; i &lt; groundRow.length; i++) {
          var number = parseInt(groundRow[i]);
          index += isNaN(number) ? 1 : number;
  
          if (index == 5 &amp;&amp; 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');
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-17T21:57:10.693Z
Answers.Text: Have you tested the code with different types of FEN?
Answers.UserDisplayName: NM Wesley Falcao
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-18T06:24:18.263Z
Answers.Text: Yup I did. I updated my post as I wrote a handy function.
Answers.UserDisplayName: Niklas
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-17T18:49:36.673Z
Answers.LastActivityDate: 2014-06-18T06:23:51.133Z
Answers.LastEditDate: 2014-06-18T06:23:51.133Z
Answers.UserDisplayName: Niklas
Body: <p>Is there some easy way to check whether the black king is on e8 or the white king is on e1, when using fen?</p>
  <blockquote>
  <p>Here the black and white king are on d1/8 <code>r2k3r/3q4/8/8/8/8/8/R1QK2RR</code></p>
  <p>Here both are on the right field <code>r3k2r/3q4/8/8/8/8/8/R1Q1K1RR</code></p>
  </blockquote>
  <p>The problem is that there is really no regularity, to check by indexing whether the King is on e1/e8.</p>
  <blockquote>
  <p>The fen above, could also be <code>rb2k2r/3q4/8/8/8/8/8/R1Q1K1RR</code> and would still be valid.</p>
  </blockquote>
  <p>Checking for the rocks are pretty easy, since they have to be at the beginning or the end of the fen.</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-06-17T18:17:04.780Z
Comments.Text: Sure, "are there exactly four spaces accounted for before the K". What is the context here? Are you writing code, or what?
Comments.UserDisplayName: Henry Keiter
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-06-17T18:25:08.710Z
Comments.Text: Yeah I am writing code. `4K3` is also valid and there are not 4 spaces.
Comments.UserDisplayName: Niklas
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-06-17T18:27:26.807Z
Comments.Text: Sure there are; I carefully did not say "four characters". You just have to treat numerals and alphabetical characters differently.
Comments.UserDisplayName: Henry Keiter
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-06-17T18:27:35.073Z
Comments.Text: If you're writing code, then I suppose this is a question for stackoverflow.com
Comments.UserDisplayName: NM Wesley Falcao
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-06-17T18:29:18.737Z
Comments.Text: Also, a full [FEN record](http://en.wikipedia.org/wiki/Forsyth%E2%80%93Edwards_Notation) 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.
Comments.UserDisplayName: Henry Keiter
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-06-17T18:33:18.290Z
Comments.Text: @HenryKeiter I am aware of that, but I wanna let the user create a position and set whether white/black is allowed to castle or not. Afterwards I wanna check if the rocks and the king are in the right position for that.
Comments.UserDisplayName: Niklas
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-06-17T18:36:26.563Z
Comments.Text: Not very difficult to code this yourself. You're really looking for the last row, so 1) get the last index of / . 2) get the remaining substring (e.g. R1Q1K1RR). 3. Check if this string contains K. 4) Iterate over this substring - count = 0; foreach(character in string) { if (c == 'K') { count = count + 1; break;  } else { if (c is number) { count = count + number} else { count = count + 1 } } Now, if count is 5, then the king is on e1.
Comments.UserDisplayName: NM Wesley Falcao
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-06-21T04:46:48.520Z
Comments.Text: 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).
Comments.UserDisplayName: maxwell
Tags: <castling><fen>
Title: Fen - Check if King is on e1/e8
ThreadText: # Fen - Check if King is on e1/e8
  
  (Asked by: username_0 on 2014-06-17)
  
  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.
  
  **Tags:** castling, fen
  
  > **Comments:**
  > **username_1**: Sure, "are there exactly four spaces accounted for before the K". What is the context here? Are you writing code, or what?  
  > **username_0**: Yeah I am writing code. `4K3` is also valid and there are not 4 spaces.  
  > **username_1**: Sure there are; I carefully did not say "four characters". You just have to treat numerals and alphabetical characters differently.  
  > **username_2**: If you're writing code, then I suppose this is a question for stackoverflow.com  
  > **username_1**: Also, a full [FEN record](http://en.wikipedia.org/wiki/Forsyth%E2%80%93Edwards_Notation) 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.  
  > **username_0**: @HenryKeiter I am aware of that, but I wanna let the user create a position and set whether white/black is allowed to castle or not. Afterwards I wanna check if the rocks and the king are in the right position for that.  
  > **username_2**: Not very difficult to code this yourself. You're really looking for the last row, so 1) get the last index of / . 2) get the remaining substring (e.g. R1Q1K1RR). 3. Check if this string contains K. 4) Iterate over this substring - count = 0; foreach(character in string) { if (c == 'K') { count = count + 1; break;  } else { if (c is number) { count = count + number} else { count = count + 1 } } Now, if count is 5, then the king is on e1.  
  > **username_3**: 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).  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2014-06-17. Score: 2 | Accepted answer
  
  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');
  
  > **Comments:**
  > **username_2**: Have you tested the code with different types of FEN?  
  > **username_0**: Yup I did. I updated my post as I wrote a handy function.  
  
  ---
  
  