Tell me more ×
Chess Stack Exchange is a question and answer site for serious players and enthusiasts of chess. It's 100% free, no registration required.

I am doing a personal project, where at one point I need to validate the FEN position, I started with some basics checks, such as check if there are kings and making sure there aren't any extra rows or columns, and that kind of things.

But what other checks should I do to completely make sure a FEN is legal?

share|improve this question

2 Answers

up vote 4 down vote accepted

Here is a well organized list that should validate 99.99%+ of common positions:

Board:

  • There are exactly 8 cols
  • The row sum of the empty squares and pieces add to 8

Kings:

  • See if there is exactly one w_king and one b_king
  • Make sure kings are separated 1 square apart

Checks:

  • Non-active color is not in check
  • Active color is checked less than 3 times; in case of 2 that it is never pawn+(pawn, bishop, knight), bishop+bishop, knight+knight

Pawns:

  • There are no more than 8 pawns from each color
  • There aren't any pawns in first or last rows
  • In case of en passant square; see if it was legally created (e.g it must be on the x3 or x6 row, there must be a pawn (from the correct color) in front of it, and the en passant square and the one behind it are empty)
  • Prevent having more promoted pieces than missing pawns (e.g extra_pieces = Math.max(0, num_queens-1) + Math.max(0, num_rooks-2)... and then extra_pieces <= (8-num_pawns)), also you should do special calculations for bishops, If you have two (or more) bishops from the same square color, these can only be created through pawn promotion and you should include this information to the formula above somehow
  • The pawn formation is possible to reach (e.g in case of multiple pawns in a single col, there must be enough enemy pieces missing to make that formation), here are some useful rules:
    1. it is impossible to have more than 6 pawns in a single column (because point NÂș2)
    2. it is impossible to have more than 5 pawns in a or h columns
    3. the minimum number of enemy missing pieces to reach a multiple pawn in a single col B to G 2=1, 3=2, 4=4, 5=6, 6=9 ___ A and H 2=1, 3=3, 4=6, 5=10, 6=impossible, for example, if you see 5 pawns in A or H, the other player must be missing at least 10 pieces from his 15 captureable pieces
    4. if there are white pawns in a2 and a3, there can't legally be one in b2, and this idea can be further expanded to cover more possibilities

Castling:

  • If the king or rooks are not in their starting position; the castling ability for that side is lost (in the case of king, both are lost)

Bishops:

  • Look for bishops trapped with pawns in the first and last row, these positions are tricky, they must be surrendered by 3 pawns, not because you see a trapped bishop by 2 pawns means it is illegal (a pawn under-promoting to a bishop could have done this), in the case of an under-promotion, remember to check for missing pawns

Other:

  • Make sure the FEN contains all the parts that are needed (e.g active color, castling ability, en passant square, etc)
  • If there are non-jumpers enemy pieces in between the king and rook and there are still some pawns without moving; check if these enemy pieces could have legally gotten in there. Also, ask yourself: was the king or rook needed to move to generate that position? (if yes, we need to make sure we have the castling abilities correctly)

Note: there is no need to make the 'players should not have more than 16 pieces' check because the points 'no more than 8 pawns' + 'prevent extra promoted pieces' + the 'exactly one king' should already cover this point

share|improve this answer
1  
You can also check the pawn structure, for example white pawns could never be on a2, a3, and b2; there is no way a pawn could be on both a3 and b2. – Akavall Nov 3 '12 at 14:19
\s*([rnbqkpRNBQKP1-8]+\/){7}([rnbqkpRNBQKP1-8]+)\s[bw-]\s(([a-hkqA-HKQ]{1,4})|(-))\s(([a-h][36])|(-))\s\d+\s\d+\s*

Here's a regular expression that I use to ensure that a FEN string is actually valid. It doesn't do any testing for a legal/illegal position, but it's a good starting point.

share|improve this answer
I think the active color is a must (you are allowing -) and half/full clocks are sometimes optional I think. Also I didn't understand the a-h part on castling ability, I rewrote it to /^\s*([rnbqkpRNBQKP1-8]+\/){7}([rnbqkpRNBQKP1-8]+)\s[bw]\s(-|K?Q?k?q?)\s(-|[a-h‌​][36])/ – ajax333221 Mar 7 at 23:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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