Answers.Body: <p>Using the <a href="https://en.wikipedia.org/wiki/Forsyth%E2%80%93Edwards_Notation#Definition" rel="nofollow noreferrer">definition of FEN</a> it is relatively straightforward to compute an upper bound:</p>
  
  <ol>
  <li>piece placement: 64 (pieces/squares) + 7 (slashes)</li>
  <li>color: 1 (w/b)</li>
  <li>castling: 4 (KQkq)</li>
  <li>en passant: 2 (e3)</li>
  <li>halfmove clock: 3 (100)</li>
  <li>fullmove number: 4 (considering that maximum game length &lt; 9999 due to the 50-move rule)</li>
  </ol>
  
  <p>spaces between fields: 5</p>
  
  <p>So in total we get as an upper bound:
  64+7+1+4+2+3+4+5 = <strong>90</strong></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-06-14T10:37:09.693Z
Answers.LastActivityDate: 2020-06-14T12:38:43.620Z
Answers.LastEditDate: 2020-06-14T12:38:43.620Z
Answers.UserDisplayName: Fabian Fichter
Answers.Body: <p>The FEN consists of six parts (see <a href="https://en.wikipedia.org/wiki/Forsyth%E2%80%93Edwards_Notation" rel="noreferrer">definition of FEN here</a>):</p>
  
  <ol>
  <li>piece placement: at most 64 characters, one for each square plus 7 times "/" to delimit ranks = 71 characters</li>
  <li>active color: one character ("w" or "b")</li>
  <li>castling: at most 4 characters</li>
  <li>en passant: one or two characters</li>
  <li>half move clock</li>
  <li>full move number</li>
  </ol>
  
  <p>In addition there are 5 space characters between them.</p>
  
  <p>So in total I count a maximum of: 71+1+1+1+4+1+2+1+x+1+y = 83 + x + y</p>
  
  <p>x and y are the number of characters to represent half moves since the last capture/pawn move and the total number of moves respectively.</p>
  
  <p>In most realistic games you'd have x=3 (after 50 moves = 100 half moves a draw can be claimed after 75 moves = 150 half moves it is a draw) and y=3 (allowing games up to 999 moves long. If you want to be on the safe side, taking x=3 (no pawn move or capture for 999 half moves max) and y=4 (games up to 9999 moves long).</p>
  
  <p>This would leave you with a string of 90 characters, but read on...</p>
  
  <hr>
  
  <h2>Additional consideration 1 (en passant)</h2>
  
  <p>The first part of the FEN string can be 64+7 characters long. One possible position is:</p>
  
  <pre><code>b1q1k1n1/p1p1p1r1/1r1b1n1p/1p1p1p1P/1P1P1Pp1/P1P1P1P1/R1R1K1B1/N1Q1B1N1 w - f3 0 1
  </code></pre>
  
  <p>Note in particular, that it is possible to have en passant capture in such setup (thanks Fabian Fichter for pointing this out), i.e. to have a 2 characters en passant string and 64+7 characters for the piece placement.</p>
  
  <h2>Additional consideration 2 (castling)</h2>
  
  <p>Any of the four castlings is described by one character (to be compared with one character if no castling is possible).</p>
  
  <p>In order for castling to be possible, neither king nor the respective rook can have moved. For queenside castling this does not create any problem, because e1 and a1 (e8 and a8) are squares of the same color and you can place pieces alternatingly similar to the example above.</p>
  
  <p>However regarding kingside castling, if e1 and h1 (or e8 and h8) are occupied, this will break the structure and shorten the 1st part of the FEN string, by 1 for each color's kingside castling.</p>
  
  <h2>Additional consideration 3 (en passant and half move clock)</h2>
  
  <p>[Again due to Fabian Fichter]:</p>
  
  <p>En passant is only possible if the last move was a pawn move. Any pawn move however resets the half move counter (5th part of FEN). If you compare one extra character for en passant (2 chars vs just "-") with two extra characters for the half move counter (3 digit number vs "0") the conclusion is, that in the longest FEN, en passant is not possible.</p>
  
  <p>This reduces the string by 1.</p>
  
  <h2>Additional consideration 4 (50 or 75 move rule)</h2>
  
  <p>The game is drawn after 50 moves (if claimed) or at most 75 moves without a capture or pawn move. Captures shorten the 1st part of the FEN by 1 char, so clearly you don't gain anything with regard to the last part of the FEN string by extending the game via captures.</p>
  
  <p>The maximum number of pawn moves (for a full board with 64+7 string for the first part of the FEN) should be 24, e.g. by moving every second pawn 1 square ahead and the other pawns two squares in two moves (=2*(4+8)=24). So the pawn moves can extend your game to 24*50=1200 or 24*75=1800 moves.</p>
  
  <p>This shows that it is possible to have a 4 digit move counter without a forced draw and also that it is not possible to have 5 digit move numbers because the game would be drawn before. (even if captures are included you would only gain another 62*75=4650 moves, leaving you well below the limit of 9999 moves).</p>
  
  <hr>
  
  <p>So the total maximum number should be 90 - 
   2 (for castling) -1 (en passant vs half move counter) = <strong>87 characters.</strong></p>
  
  <p>Note that all this holds for regular chess games. If you need to support any non-standard variants you might have to reconsider these numbers.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-06-14T11:15:11.223Z
Answers.Text: Is it really possible for a legal FEN to have all 64 piece placement characters used? The obvious idea to achieve that is to place all the pieces in a checkerboard pattern, but that causes a problem with wrong-colored bishops.
Answers.UserDisplayName: the default.
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-06-14T11:25:51.527Z
Answers.Text: Yes you can. You can place pieces in two checkerboard patterns, one on the light squares and one on the dark squares. If you have the separation horizontally you will have two adjacent empty squares *vertically* which does not show up in the FEN (because it goes row by row)
Answers.UserDisplayName: user1583209
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-06-14T15:03:00.817Z
Answers.Text: 1p1pP1p1 also has 8 characters with 4 pieces, so adjacent pawns with en passant should be possible.
Answers.UserDisplayName: Fabian Fichter
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-06-14T15:08:30.547Z
Answers.Text: @FabianFichter Good point. Will modify.  Still, castling will lower the number.
Answers.UserDisplayName: user1583209
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-06-14T15:29:01.707Z
Answers.Text: However, there is another consideration that completely excludes en passant: The last move before en passant must be a pawn move, so the half-move clock would be reset. Therefore 87 should be the maximum (chess960 might allow 89), e.g., r1b1k2r/1n1q1p1p/p1p1p1p1/1p1pP1b1/1N1P1P1P/P1P1Q1P1/1P1nN1B1/R1B1K2R w KQkq - 100 1000
Answers.UserDisplayName: Fabian Fichter
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-06-14T10:41:06.303Z
Answers.LastActivityDate: 2020-06-15T15:29:12.933Z
Answers.LastEditDate: 2020-06-15T15:29:12.933Z
Answers.UserDisplayName: user1583209
Body: <p>What is the longest possible FEN? The reason for that question is, that  I am <a href="https://github.com/mliebelt/PgnViewerJS" rel="noreferrer">designing a PGN viewer</a> (again), and I have noticed that the FEN is dependent on the width of the board (the FEN test area is placed below). So the following number of lines are necessary:</p>
  
  <ul>
  <li>FEN width > 460: 1 line</li>
  <li>FEN width > 240: 2 lines</li>
  <li>FEN width &lt; 240: 3 lines or more</li>
  </ul>
  
  <p>But is that sufficient as a heuristic? If I would known how many characters a FEN string has as maximum, I could compute it.</p>
  
  <p>Addition: I did some experiments with games, and did not found FENs longer than 72 characters. So the current answers are of course sufficient, but could someone provide a (legal) position that comes near those 90 characters?</p>
  
Tags: <pgn><records><mathematics><fen>
Title: Longest possible FEN
ThreadText: # Longest possible FEN
  
  (Asked by: username_0 on 2020-06-14)
  
  What is the longest possible FEN? The reason for that question is, that I am [designing a PGN viewer](https://github.com/username_0/PgnViewerJS) (again), and I have noticed that the FEN is dependent on the width of the board (the FEN test area is placed below). So the following number of lines are necessary:
  
  * FEN width > 460: 1 line
  * FEN width > 240: 2 lines
  * FEN width < 240: 3 lines or more
  
  But is that sufficient as a heuristic? If I would known how many characters a FEN string has as maximum, I could compute it.
  
  Addition: I did some experiments with games, and did not found FENs longer than 72 characters. So the current answers are of course sufficient, but could someone provide a (legal) position that comes near those 90 characters?
  
  **Tags:** pgn, records, mathematics, fen
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2020-06-14. Score: 13 | Accepted answer
  
  The FEN consists of six parts (see [definition of FEN here](https://en.wikipedia.org/wiki/Forsyth%E2%80%93Edwards_Notation)):
  
  1. piece placement: at most 64 characters, one for each square plus 7 times "/" to delimit ranks = 71 characters
  2. active color: one character ("w" or "b")
  3. castling: at most 4 characters
  4. en passant: one or two characters
  5. half move clock
  6. full move number
  
  In addition there are 5 space characters between them.
  
  So in total I count a maximum of: 71+1+1+1+4+1+2+1+x+1+y = 83 + x + y
  
  x and y are the number of characters to represent half moves since the last capture/pawn move and the total number of moves respectively.
  
  In most realistic games you'd have x=3 (after 50 moves = 100 half moves a draw can be claimed after 75 moves = 150 half moves it is a draw) and y=3 (allowing games up to 999 moves long. If you want to be on the safe side, taking x=3 (no pawn move or capture for 999 half moves max) and y=4 (games up to 9999 moves long).
  
  This would leave you with a string of 90 characters, but read on...
  
  ---
  
  ## Additional consideration 1 (en passant)
  
  The first part of the FEN string can be 64+7 characters long. One possible position is:
  
  ```
  b1q1k1n1/p1p1p1r1/1r1b1n1p/1p1p1p1P/1P1P1Pp1/P1P1P1P1/R1R1K1B1/N1Q1B1N1 w - f3 0 1
  
  ```
  
  Note in particular, that it is possible to have en passant capture in such setup (thanks username_1 username_1 for pointing this out), i.e. to have a 2 characters en passant string and 64+7 characters for the piece placement.
  
  ## Additional consideration 2 (castling)
  
  Any of the four castlings is described by one character (to be compared with one character if no castling is possible).
  
  In order for castling to be possible, neither king nor the respective rook can have moved. For queenside castling this does not create any problem, because e1 and a1 (e8 and a8) are squares of the same color and you can place pieces alternatingly similar to the example above.
  
  However regarding kingside castling, if e1 and h1 (or e8 and h8) are occupied, this will break the structure and shorten the 1st part of the FEN string, by 1 for each color's kingside castling.
  
  ## Additional consideration 3 (en passant and half move clock)
  
  [Again due to username_1 username_1]:
  
  En passant is only possible if the last move was a pawn move. Any pawn move however resets the half move counter (5th part of FEN). If you compare one extra character for en passant (2 chars vs just "-") with two extra characters for the half move counter (3 digit number vs "0") the conclusion is, that in the longest FEN, en passant is not possible.
  
  This reduces the string by 1.
  
  ## Additional consideration 4 (50 or 75 move rule)
  
  The game is drawn after 50 moves (if claimed) or at most 75 moves without a capture or pawn move. Captures shorten the 1st part of the FEN by 1 char, so clearly you don't gain anything with regard to the last part of the FEN string by extending the game via captures.
  
  The maximum number of pawn moves (for a full board with 64+7 string for the first part of the FEN) should be 24, e.g. by moving every second pawn 1 square ahead and the other pawns two squares in two moves (=2\*(4+8)=24). So the pawn moves can extend your game to 24\*50=1200 or 24\*75=1800 moves.
  
  This shows that it is possible to have a 4 digit move counter without a forced draw and also that it is not possible to have 5 digit move numbers because the game would be drawn before. (even if captures are included you would only gain another 62\*75=4650 moves, leaving you well below the limit of 9999 moves).
  
  ---
  
  So the total maximum number should be 90 -
  2 (for castling) -1 (en passant vs half move counter) = **87 characters.**
  
  Note that all this holds for regular chess games. If you need to support any non-standard variants you might have to reconsider these numbers.
  
  > **Comments:**
  > **username_3**: Is it really possible for a legal FEN to have all 64 piece placement characters used? The obvious idea to achieve that is to place all the pieces in a checkerboard pattern, but that causes a problem with wrong-colored bishops.  
  > **username_2**: Yes you can. You can place pieces in two checkerboard patterns, one on the light squares and one on the dark squares. If you have the separation horizontally you will have two adjacent empty squares *vertically* which does not show up in the FEN (because it goes row by row)  
  > **username_1**: 1p1pP1p1 also has 8 characters with 4 pieces, so adjacent pawns with en passant should be possible.  
  > **username_2**: @username_1 Good point. Will modify.  Still, castling will lower the number.  
  > **username_1**: However, there is another consideration that completely excludes en passant: The last move before en passant must be a pawn move, so the half-move clock would be reset. Therefore 87 should be the maximum (chess960 might allow 89), e.g., r1b1k2r/1n1q1p1p/p1p1p1p1/1p1pP1b1/1N1P1P1P/P1P1Q1P1/1P1nN1B1/R1B1K2R w KQkq - 100 1000  
  
  ---
  
  ### Answer by username_1 on 2020-06-14. Score: 6
  
  Using the [definition of FEN](https://en.wikipedia.org/wiki/Forsyth%E2%80%93Edwards_Notation#Definition) it is relatively straightforward to compute an upper bound:
  
  1. piece placement: 64 (pieces/squares) + 7 (slashes)
  2. color: 1 (w/b)
  3. castling: 4 (KQkq)
  4. en passant: 2 (e3)
  5. halfmove clock: 3 (100)
  6. fullmove number: 4 (considering that maximum game length < 9999 due to the 50-move rule)
  
  spaces between fields: 5
  
  So in total we get as an upper bound:
  64+7+1+4+2+3+4+5 = **90**
  
  
  ---
  
  