Longest possible FEN What is the longest possible FEN? The reason for that question is, that  I am designing a PGN viewer (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 &lt; 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?
The FEN consists of six parts (see definition of FEN here):


piece placement: at most 64 characters, one for each square plus 7 times "/" to delimit ranks = 71 characters
active color: one character ("w" or "b")
castling: at most 4 characters
en passant: one or two characters
half move clock
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 Fabian Fichter 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 Fabian Fichter]:

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.