dojotrades.com

Turn a position on the board into FEN, rank by rank

You have a position on a wooden board or on paper, and you want it as a line of text that you can store or paste elsewhere. Writing the FEN yourself is quick once you follow a fixed routine.

You will work through one position rank by rank, check the total of each rank as you go and then add the five short fields that come after the pieces.

Get the board facing the right way

Before writing anything, make sure you know where a1 is. From White's side it is the dark corner on the near left, and rank 8 is the far row. If you are copying a printed picture, check its edge labels, since some are drawn from Black's side. The guide on reading a diagram covers that check.

FEN is written from White's point of view, whoever is to move. You begin at rank 8 and work down to rank 1. Inside each rank you go from the a-file to the h-file, which is left to right as White sees it.

Use uppercase for White (K, Q, R, B, N and P) and lowercase for Black (k, q, r, b, n and p). Here the pawn does get a letter, unlike in written moves. For the purpose of each field, read what FEN is.

White king on g1, rook on d1, bishop on g2, knight on d4 and pawns on a2, b3, f2, g3 and h2; black king on g8, rooks on a8 and f8 and pawns on a7, b7, c6, f7, g7 and h7.
The position you will turn into text, with Black to move.Open this position in the Board builderr4rk1/pp3ppp/2p5/8/3N4/1P4P1/P4PBP/3R2K1 b - - 0 22

Write one rank at a time

Run a finger along the rank, starting at the a-file. When you meet a piece, write its letter. When you meet empty squares, count how many lie together and write that number as a single digit. A run of empty squares ends only at a piece or at the edge of the board, so two digits never stand side by side.

The worked position, one rank per row
RankFrom the a-file to the h-fileYou writeTotal
8rook, four empty, rook, king, one emptyr4rk11+4+1+1+1 = 8
7two pawns, three empty, three pawnspp3ppp2+3+3 = 8
6two empty, pawn, five empty2p52+1+5 = 8
5eight empty88
4three empty, knight, four empty3N43+1+4 = 8
3one empty, pawn, four empty, pawn, one empty1P4P11+1+4+1+1 = 8
2pawn, four empty, pawn, bishop, pawnP4PBP1+4+1+1+1 = 8
1three empty, rook, two empty, king, one empty3R2K13+1+2+1+1 = 8

Look at rank 8 slowly. The black rook on a8 gives the letter r. The squares b8 to e8 are empty, which gives 4. Then come a rook, a king and one last empty square on h8, so the group ends with 1. That final digit is easy to forget.

It helps to write the eight groups on eight separate lines first, labeled from 8 down to 1. A wrong total is then easy to trace before anything is joined.

The same position with the second rank highlighted: white pawns on a2, f2 and h2 and a white bishop on g2.
Rank 2, marked: a pawn, four empty squares, then pawn, bishop, pawn gives P4PBP.Black to move. Open this position in the Board builderr4rk1/pp3ppp/2p5/8/3N4/1P4P1/P4PBP/3R2K1 b - - 0 22

Join the eight groups with slashes, from the rank 8 group to the rank 1 group, with no spaces and no slash at the end.

Field 1 of the worked position

r4rk1/pp3ppp/2p5/8/3N4/1P4P1/P4PBP/3R2K1

Check that every rank adds to eight

Add up each group before moving on. A letter counts as one square and a digit counts as its number. Any total other than eight means that a square was skipped or counted twice, and the pieces after that point will be shifted.

  • There are eight groups, so there are seven slashes.
  • Each group totals eight.
  • The whole field has exactly one K and one k.
  • The first and last groups contain no P and no p.

Next, compare piece counts. The worked position has nine white pieces and pawns and nine black ones, so your field should hold nine uppercase and nine lowercase letters.

Fill in the last five fields

Leave a space after the pieces, then add the other fields in this order, with one space between them.

  1. Side to move: write w or b.
  2. Castling: write K, Q, k and q, keeping that order, for each right that remains. Write a hyphen if none remain.
  3. En passant: if the last move was a pawn advancing two squares, write the square it skipped. Otherwise write a hyphen.
  4. Halfmove clock: write the number of half moves since the last capture or pawn move.
  5. Move number: write the number of the move now being played.

For the worked position, Black is to move, neither side can castle and no pawn has just advanced two squares. Say the game is at move 22 and White's last move was a single pawn step, so the clock reads 0.

The complete string

r4rk1/pp3ppp/2p5/8/3N4/1P4P1/P4PBP/3R2K1 b - - 0 22

Do not guess castling rights from the picture alone. A king and rook standing on their starting squares may have moved and come back. If the history is unknown, decide what your exercise needs and write that.

A second example with castling and en passant

In the next position White has a king and one rook at home, Black has a king and both rooks, and a pawn has just advanced two squares.

White king on e1, rook on h1 and pawns on c4, f2, g2 and h2; black king on e8, rooks on a8 and h8 and pawns on f7, g7 and h7.
White has just moved a pawn from c2 to c4. Black is to move.Black to move. Open this position in the Board builderr3k2r/5ppp/8/8/2P5/8/5PPP/4K2R b Kkq c3 0 18
The six fields of the second example
FieldYou writeReason
Piecesr3k2r/5ppp/8/8/2P5/8/5PPP/4K2Rrank 8 down to rank 1
Side to movebBlack moves next
CastlingKkqWhite kingside only, Black both sides
En passantc3the square the pawn skipped
Halfmove clock0the last move was a pawn move
Move number18Black's half of move 18 is next

Build the piece field exactly as before. Its rank 4 group is 2P5: two empty squares, the pawn on c4 and five empty squares. The castling field has no Q because White has no rook left on a1.

No black pawn can capture on c3 here. The PGN standard still has you record the square after a two-square advance. Some programs write a hyphen when no capture exists, so do not be surprised to see either.

Common mistakes while writing

  • Starting at rank 1. The ranks come out in reverse order.
  • Reading from Black's left. Files run from a to h even if you sit behind the black pieces.
  • Splitting one gap into two digits, such as 31 where 4 is meant.
  • Dropping a closing digit. Empty squares at the end of a rank count too.
  • Wrong case. A lowercase letter is a black piece.
  • Stopping after the pieces. Write all six fields.

Final check

  • Paste the string into the Board builder and compare it square by square with the original.
  • Flip the view if the original was drawn from Black's side.
  • Confirm the side to move, castling rights and en passant square.
  • Save the position with a title that you will recognize.

If the string will not load, the guide to rejected FENs helps you find the broken part. If you are preparing exercises for a group, check each string before you move on to printing the diagrams.

Common questions

Where do I start when writing a FEN?

At the a-file of rank 8, as White sees the board. Work along that rank, then move down to the next one.

How do I write empty squares in FEN?

Count the empty squares that lie together on one rank and write the number as a single digit from 1 to 8.

What should I write if I do not know the move number?

A common choice is 0 for the halfmove clock and 1 for the move number. The pieces and side to move are unaffected.

Are there spaces between the ranks?

No. Ranks are joined by slashes only. Spaces are used to separate the six fields.

Sources

Keep studying

All guides on reading and writing positions