dojotrades.com

Why your FEN will not load, and how to repair it

A FEN that will not load is often wrong in one small place. A digit is off by one, a slash has gone missing or a later field no longer agrees with the pieces.

You will learn to test a string in a fixed order: first its shape, then whether the position it describes could exist. The main faults come with a broken example that shows you where to look.

Start from a string that works

The broken examples here are damaged copies of one good string, so you can compare them character by character.

Correct string

2r3k1/pp3ppp/8/8/8/2N5/PP3PPP/4R1K1 w - - 0 1

White king on g1, rook on e1, knight on c3 and pawns on a2, b2, f2, g2 and h2; black king on g8, rook on c8 and pawns on a7, b7, f7, g7 and h7.
The correct position behind the examples, with White to move.Open this position in the Board builder2r3k1/pp3ppp/8/8/8/2N5/PP3PPP/4R1K1 w - - 0 1

Faults come in two kinds. A shape fault means that the text cannot be read as a board at all. A position fault means that the text reads cleanly but describes something the rules do not allow. Look for them in that order. If the fields are new to you, first read what each FEN field means.

Faults in the piece field

Broken piece fields. Each string in the first column is rejected.
Broken string, do not useWhat is wrongRepair
2r3k1/pp3ppp/8/8/8/2N4/PP3PPP/4R1K1 w - - 0 1rank 3 adds up to 7change 2N4 to 2N5
2r3k1/pp3ppp/8/8/2N5/PP3PPP/4R1K1 w - - 0 1only seven ranksrestore the missing empty rank
2r3k1/pp3ppp/8/8/44/2N5/PP3PPP/4R1K1 w - - 0 1two digits in a rowchange 44 to 8
2r3k1/pp3ppp/8/8/8/2H5/PP3PPP/4R1K1 w - - 0 1H is not a piece letterchange H to N

To find a bad rank, split the field at each slash and total every group, adding one for each piece letter and the stated number for each digit. Each group must come to eight. The first group is rank 8 and the last is rank 1, so the sixth group is rank 3.

A rank that is too short or too long points to a miscount. Look for a dropped piece or an empty-square digit that is off by one. A rank can also total eight and still be wrong, with a piece one square to the side; only a comparison with the board reveals that.

Count the groups as well as their contents. Seven groups can mean that one slash and its rank vanished together, which happens easily in a run of empty ranks. Nine points to an extra slash or a rank typed twice.

The only digits allowed are 1 to 8. A zero or a 9 breaks the field. So does a space inside it, because the space is read as the start of the next field. The piece letters are k, q, r, b, n and p, in either case.

Missing or badly formed fields

A full FEN has six fields. The next string is deliberately broken: the letter for the side to move has been lost.

Broken example: a field is missing

2r3k1/pp3ppp/8/8/8/2N5/PP3PPP/4R1K1 - - 0 1

  1. Five fields remain instead of six.
  2. The first hyphen now sits where w or b belongs.
  3. Put the w back after the piece field to repair it.
The accepted form of each later field
FieldAccepted form
Side to movew or b
Castlingletters from KQkq in that order, or a hyphen
En passanta square on rank 3 or 6, or a hyphen
Halfmove clocka whole number, 0 or higher
Move numbera whole number, 1 or higher

Some programs are lenient: they fill in missing counters or tolerate letters out of order. Others refuse the string. Six fields in the standard form suit both kinds.

Stray text picked up while copying breaks a string as well. Watch for quotation marks around it, a label such as FEN: in front of it or a full stop after the last number. Copy only the six fields and the spaces between them.

Impossible positions: kings, pawns and check

A string can have a perfect shape and still describe a board that no game could produce. Strict tools reject it, and the Board builder warns you about impossible positions.

  • Kings. Each side has exactly one, and the two kings can never stand on neighboring squares.
  • Pawns on end ranks. No pawn can be on rank 1 or rank 8. Pawns start one rank in, move only forward and are replaced when they reach the far end.
  • Too many units. A side cannot have more than eight pawns, or more than sixteen pieces and pawns in all.
  • The wrong side in check. If White is to move, Black cannot be in check, since Black has just moved and may not leave the king attacked.
Strings with a good shape and an impossible position
Broken string, do not useWhy it is impossible
2r3k1/pp3ppp/8/8/8/2K5/PP3PPP/4R1K1 w - - 0 1two white kings, because the knight was typed as K
2r1P1k1/pp3ppp/8/8/8/2N5/PP3PPP/4R1K1 w - - 0 1a white pawn has been placed on rank 8
4R1k1/pp3ppp/8/8/8/2N5/PP3PPP/6K1 w - - 0 1Black is in check but it is White's turn

The third fault can be nothing more than a wrong second field. Change w to b and the same pieces form a legal position: Black is to move, is in check and is in fact checkmated. The guide to checkmate and stalemate shows how to confirm that.

White rook on e8 checking the black king on g8, which is shut in by its pawns on f7, g7 and h7; white knight on c3 and white king on g1.
Legal once the second field reads b: the rook on e8 has given checkmate.Black to move. Open this position in the Board builder4R1k1/pp3ppp/8/8/8/2N5/PP3PPP/6K1 b - - 0 1

Castling and en passant that do not match

The third and fourth fields make statements about the pieces, and a reader can test them against the board.

  • A castling letter needs the king and that rook on their starting squares: e1 with h1 or a1 for White, e8 with h8 or a8 for Black.
  • An en passant square must be empty, with the pawn that skipped it just beyond and the pawn's starting square empty too. It lies on rank 6 when White is to move and on rank 3 when Black is to move.
Fields that contradict the pieces
Broken string, do not useWhy it does not matchRepair
2r3k1/pp3ppp/8/8/8/2N5/PP3PPP/4R1K1 w K - 0 1White's king has left e1 and h1 holds no rookreplace K with a hyphen
2r3k1/pp3ppp/8/8/8/2N5/PP3PPP/4R1K1 w - d6 0 1d5 holds no black pawnreplace d6 with a hyphen
Black pawn on d5 beside a white pawn on e5; white king on e1 and black king on e8; the empty square d6 is marked.
A square that does match: the pawn has just come from d7 to d5, so d6 is correct.White to move. Open this position in the Board builder4k3/8/8/3pP3/8/8/8/4K3 w - d6 0 2
White king on e1 with rooks on a1 and h1; black king on e8 with rooks on a8 and h8.
All four castling letters fit: both kings and the four rooks are at home.White to move. Open this position in the Board builderr3k2r/8/8/8/8/8/8/R3K2R w KQkq - 0 1

These two faults often appear after editing. If you move a king or a rook, remove the matching right. If you rearrange pawns, check that the en passant square still fits. The rules behind both are set out in castling, en passant and promotion.

Common mistakes and a repair routine

Test a rejected string in this order

  • Count the fields: six, separated by spaces.
  • Count the rank groups: eight.
  • Total each group and look for two digits side by side.
  • Find exactly one K and one k in the first field.
  • Look for pawns in the first and last groups.
  • Ask whose turn it is and who is in check.
  • Compare castling letters and the en passant square with the pieces.

The usual causes are simple. Copying by hand drops a slash. Moving a letter without updating the digits changes a rank total. Changing the pieces without updating the later fields leaves old information behind.

Repair one fault at a time and test again, since a string can hold several. If it is badly damaged, rebuild it from the board with the rank by rank method. Passing these tests does not prove that a real game could reach the position. It only shows that the obvious contradictions are gone.

Common questions

Why does my FEN have a rank of the wrong length?

A group between two slashes does not add up to eight squares. Total the letters and digits group by group.

Can a FEN have two kings of one color?

No. Each side needs exactly one king, so such a position is impossible.

Why is my FEN rejected because of a check?

The side that is not to move cannot be in check. Look at the second field and at the pieces aimed at that king.

Will a FEN load without the last two numbers?

Some programs accept it and fill them in, while others refuse it. Writing both numbers is the safer choice.

Sources

Keep studying

All guides on reading and writing positions