Required fields in Forsyth-Edwards Notation (FEN)

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

niel5946
Posts: 174
Joined: Thu Nov 26, 2020 10:06 am
Full name: Niels Abildskov

Required fields in Forsyth-Edwards Notation (FEN)

Post by niel5946 »

Hi everyone :)
I have a small question regarding which of the "fields" are required for a FEN to be valid. As this Wikipedia page, explains, there can be a maximum of six fields - which is just space-separated substrings :D - which are the following (with the starting FEN as an example):
  • Piece placement: rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR
  • Side to move: w or b
  • Castling rights: K and Q for white king and queen-side respectively, and lowercase for black. "-" for none
  • En passant square: The en-passant square in algebraic notation. "-" for none.
  • Half-move clock: The amount of half-moves since last capture or pawn move.
  • Full-move clock: The amount of full moves (incremented every time black moves) in the game.
What I am confused about is the two last points... The CPW perft results page doesn't denote them in some of its FENs, so I am wondering whether or not they are even required for a FEN to be valid? On one side, they don't provide any information that is critical to recreating a position from the FEN (it would - perhaps not with the half-move clock - be evaluated the exact same regardless of their values), but on the other hand, the Wikipedia page I mentioned above, doesn't say anything about this...
I know this is a simple question, but I want to be 100% sure since my FEN-parser, in the rewrite of Loki, will be much more strict about which FENs are valid and which are not, so it'll throw an exception and perhaps stop the game if it gets a FEN it deems invalid.
Author of Loki, a C++ work in progress.
Code | Releases | Progress Log |
User avatar
j.t.
Posts: 239
Joined: Wed Jun 16, 2021 2:08 am
Location: Berlin
Full name: Jost Triller

Re: Required fields in Forsyth-Edwards Notation (FEN)

Post by j.t. »

There are so many places that assume FENs without the last two parts (move numbers) to be correct, that I decided to just default them to 0 if they aren't sent.
User avatar
Ajedrecista
Posts: 1972
Joined: Wed Jul 13, 2011 9:04 pm
Location: Madrid, Spain.

Re: Required fields in Forsyth-Edwards Notation (FEN).

Post by Ajedrecista »

Hello Niels:

I would take a look to PGN standards. There are various sources, I hope that without differences among them:

https://ia902908.us.archive.org/26/item ... -03-12.txt
https://www.thechessdrum.net/PGN_Reference.txt
https://www.chessclub.com/help/PGN-spec

I think that this exact question is asked once in a while and there is not a definitive answer. Could you raise a warning if the last two fields are missing without invalidating the full FEN for this only issue, or is it nor practical? OTOH, the halfmove clock field is important for the 50-move rule, so IMHO the last two fields should appear, but it is only my opinion, which is unimportant.

------------

Replying to Jost, I agree that a number of places accept FEN strings without the last two fields, but I think that the default should be 0 1 —like it is found in many compositions— instead of 0 0, just taking a glance into the paragraph 16.1.3.6 'Fullmove number', which says the following (bold and underline added by me):
16.1.3.6: Fullmove number

The sixth and last field is a positive integer that gives the fullmove number.
This will have the value "1" for the first move of a game for both White and
Black. It is incremented by one immediately after each move by Black.
So 0 should not be allowed. OTOH we know that appending 0 1 is a workaround and for example the fullmove number field can be fooled in a simple position like the set up of the Grünfeld Defence, which is:

rnbqkb1r/ppp1pp1p/5np1/3p4/2PP4/2N5/PP2PPPP/R1BQKBNR w KQkq - 0 4

And could be found as:

rnbqkb1r/ppp1pp1p/5np1/3p4/2PP4/2N5/PP2PPPP/R1BQKBNR w KQkq - 0 1

The importance of the fullmove number field roots in time controls, i.e. know where is the position within the game and be able to set the correct amount of remaining time in an adjourned game with repeating time controls. I know that this argument is a bit elaborated and reinforces my POV of 'better to have all the fields correct than missing some', but invalidate the full FEN for only missing those two fields seems excessive to me.

Regards from Spain.

Ajedrecista.
abulmo2
Posts: 433
Joined: Fri Dec 16, 2016 11:04 am
Location: France
Full name: Richard Delorme

Re: Required fields in Forsyth-Edwards Notation (FEN)

Post by abulmo2 »

The problem is that they are two standards. The FEN requires the last two fields with the fifty-move counter and the move counter. However these two fields are missing (or differently described) in the Extended Position Description (EPD). So if you want the same code to read both you have to make the two last fields of the FEN optional and replace them with default value.
Richard Delorme
niel5946
Posts: 174
Joined: Thu Nov 26, 2020 10:06 am
Full name: Niels Abildskov

Re: Required fields in Forsyth-Edwards Notation (FEN)

Post by niel5946 »

Thank you all for your answers :D
I have taken your advice and made it optional to read the move counters. It works by separating the FEN by spaces and if there are more than four elements in that list, the last two are assumed to be move counters and read like that.
When I generate a FEN, i only add them if one of them are more than zero.
Author of Loki, a C++ work in progress.
Code | Releases | Progress Log |