9

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 < 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?

2 Answers 2

12

The FEN consists of six parts (see definition of FEN here):

  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 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.

5
  • 2
    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. Jun 14, 2020 at 11:15
  • 1
    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) Jun 14, 2020 at 11:25
  • 1
    1p1pP1p1 also has 8 characters with 4 pieces, so adjacent pawns with en passant should be possible. Jun 14, 2020 at 15:03
  • 1
    @FabianFichter Good point. Will modify. Still, castling will lower the number. Jun 14, 2020 at 15:08
  • 3
    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 Jun 14, 2020 at 15:29
6

Using the definition of FEN 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

Your Answer

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

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