Dec 16, 2006

[Guide] FEN Notation

FEN Notation basics: A FEN-notation is a single string that holds all the information about a position. That is piece placement, side to move, castling rights, en passant square, number of half-moves (moves by one side) since last capture or pawn move (to keep track of 50 move rule), and total number of full moves (moves by both sides). In that order.

The starting position is described with FEN-notation as:
rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
And here's how to read it:

We start with the black pieces (8th rank, in lower-case for black), then '/' to move to the next rank (the 7th), there we have the black pawns, then next rank where we have no pieces so we put the number 8 (as in eight empty squares).

Then comes 3 more empty ranks, then the white pawns and pieces (in upper-case for white).

Next character 'w' determines it's white turn to move.

KQkq is for castling rights (K - white can castle kingside, Q - white can castle queenside, and same for black in lower-case). If noone can castle we put a dash '-'.

Then comes the en passant square, since there is no en passants available in the starting position we have '-' there. There can only be one en passant square in any given position (there's only one pawn move at a time that can result in the square).

Then comes the half-moves since last capture or pawn move, and finally the total of full-moves (starting at 1 and incremented every time black moves).

A few examples:

k7/8/8/4N3/8/8/8/3K4 b - - 13 56
Notice how neither side has castling rights. The black king is placed in the corner so we write 'k7' for black king and 7 empty squares, while the white knight is in the middle of the rank so we write '4N3' for four empty squares, white knight, and 3 empty squares.




rnbqkbnr/pp2pppp/8/2ppP3/8/8/PPPP1PPP/RNBQKBNR w KQkq d6 0 3
The black pawn has just moved two squares, so there is an en passant available on d6.










Don't mind the formatting, I'm sure it looks horrible on some web browsers. (I noticed the last characters on the second FEN example got cut off on mine)

Why do we want it, and why now?: We will need an easy way of seeing what our engine is doing. Sure we could write some ASCII presentation of the board, or even a graphical interface (I will not touch that subject here), but why spend the time when we can create a very easy and effective feature, that we for sure will be using later anyways.

At this stage we have no (easy) way of seeing how the application perceives the board. A FEN-output feature easily solves that.

For development purposes later on we will insert different positions using FEN, and then analyze them with the engine.

If we use a third-party program with its own graphical interface (any that can read/write FEN will do), we have an easy way of writing our own FENs and also a way of looking at how our program perceives the board at the moment.

This was a little bit about FEN Notation. Now let's try to implement it in our Board-class.

(screenshots are taken from Blitzin 2.5, available at Internet Chess Club)

3 comments:

greg_b said...

Thanks for the help on understanding FEN notation. This is one of the very few websites I've found that makes sense of it :) ... It's quite simple really, but easy to misinterpret! Keep the blog going - it's very useful ... (I'll hopefully be using FEN tags with my 3rd party PGN viewer ... )

Anonymous said...

hello,
It made a lot of sense.
Thank you for your help!

ASKural

Jabid Haab said...

Fenally.. I fend it