How do I know if a position is legal?

Sort:
ajax333221

I am looking for all the things that I need to test when validating a FEN position for a project I am doing. So far, I have recolected these:

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
  • (not really necessary as we will make sure non-active color is not in check) 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)
  • (not necessary because the points 'no more than 8 pawns' + 'prevent extra promoted pieces' + the 'exactly one king' already cover this point) Players should not have more than 16 pieces
Razdomillie

Great, what's the point though? Better yet, what is the practical application of this information?

Now I'm going to get back to the incredibly practical task of moving little shapes around on a checkered board.

chessgdt

for pawn plus other, what about this:



ajax333221

@chessgdt thanks, you can't be more right

rooperi
Whaleeyeman wrote:

What's the point of knowing this?  Do you often play chess just based on reading FEN strings?

If you do, damn, you must be good! 

That's like "blindfold chess" taken to extremes... someone reads off a FEN string to you, you visualise the board, and shoot off your responsive FEN!

I often do useless things, because it's challenging.

I made an Excel spreadsheet that can play video poker (I also used to work for a casino, Sol Kerzner was my boss, he went to bahamas, maybe you heard of him).

I was thinking of making a spreadsheet that can generate a list of all legal moves from a FEN. Why? I dunno, it's just something interesting to try....

rooperi

Well, I worked for him a couple of levels removed, lol

ajax333221

to the ones saying why I need this, I am writting an application that hopefully will be PGN reader with the possibility to make moves yourself to study variations and stuff like that, as rooperi said, it is interesting. I know there are millions of similar stuff out there, but I like reinveinting wheels, specially challenging ones

waffllemaster

Yeah, sounds like a program.  Amazing how what can be so intuitive to us takes so much effort to explain to a computer.

BigDoggProblem
ajax333221 wrote:

I am looking for all the things that I need to see when validating a FEN position. So far, I have discovered these:

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+(other), bishop+bishop, knight+knight, rook+rook

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 ), 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:it is impossible to have more than 6 pawns in a single column (because point Nº2) it is impossible to have more than 5 pawns in a or h columns 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 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

Writing a program to determine if positions are legal is a difficult task. The only one I am aware of is "Retractor", written by a couple of college students.

The sane approach is to code only the more obvious forms of illegal position [impossible checks, pawns on 1st or 8th ranks, as you were saying] and let the user check the rest.

BigDoggProblem

A picture is worth a thousand a words. Here is what you face if you make a comprehensive test for legality:

(Composer: H. Goldsteen)

BlonderWoman

 

BlonderWoman

White to move (and mate). 

Was wandeting about the legality of this puzzle i composed. Thats how i got here happy.png great string. I wish i could program... maybe should learn!

ajax333221
BlonderWoman escribió:

White to move (and mate). 

Was wandeting about the legality of this puzzle i composed. Thats how i got here  great string. I wish i could program... maybe should learn!

Hehe that position can surely be legally played (as black king could been in F6 for example) and I don't see many pawns in the same column or anything out of the ordinary, you can easily come with a game and start taking out pieces with the queens and then go there

Rocky64

Such a list is interesting but it only shows some basic properties of legal/illegal positions. For a program to be able to determine the legality of any position, that would be a major AI project, maybe as hard as solving chess itself!

BTW, 6 pawns on the a- or h-file is possible. It requires 15 captures, i.e. a lone king remaining. For example, if White has 6 pawns on the a-file, that only means most of Black's K-side pawns had promoted before getting captured by a white one. 

nicky79

Another kinda interesting question: given a legal chess position, is there an algorithm to determine the shortest possible a game which reaches that position?

 

Checkmate6659

This is a very complex position, and your algorithm fails at determining its legality.

Note that White can take en passant on c6, and Black can castle queenside.

If this position was legal, Black's last move would have been c7-c5. There are 2 possibilities:

1) if the Queen came to a4 first, and then the pawn, Black must have blocked the check, as both moving the king and capturing the queen would have revoked Black's queenside castle right. Therefore, the pawn must have blocked the check, and it can only do so from c6, which means the necessary c7-c5 push is impossible.

2) If the pawn came to b5 first, and then the queen, there was no check, but Black must have moved something. Note that because black still has all his pieces, the queen couldn't have captured the piece that just moved. The king and the two rooks could not have moved, because otherwise the castling right would have been revoked. Black's other pieces could not have moved as well, because they are blocked by eachother and the pawns (the black rook on h4 is actually blocked by the white rook on h5). The pawns next to the pieces also couldn't have moved (or captured) to their current position, because all the squares right behind them are occupied. Therefore, only the c pawn could have moved before, and so it would have been forced to leave the 7th rank, and that also renders the necessary c7-c5 pawn push impossible.

Therefore, this position is actually illegal.

 

boulayo

You wrote: <it is impossible to have more than 5 pawns in a or h columns>. This "game" proves that's wrong

iwanttobeme
boulayo wrote:

You wrote: <it is impossible to have more than 5 pawns in a or h columns>. This "game" proves that's wrong

um "more than 5 pawns" means 6 or more pawns...

jojo0101011