thomasahle / sunfish

Sunfish: a Python Chess Engine in 111 lines of code
https://www.chessprogramming.org/Sunfish
Other
2.95k stars 543 forks source link

illegal move and a potential solution #91

Closed kerbals-repository closed 1 year ago

kerbals-repository commented 2 years ago

Sunfish claims that Qf2 is the best move in this position, although it is illegal.

r3n3/7Q/4pkp1/3p2P1/1p1P4/1P3PK1/r3q3/8 b - - 0 63

I therefore thought about changing the can_kill_king function to

`def can_kill_king(pos):

If we just checked for opponent moves capturing the king, we would miss

# captures in case of illegal castling.
return any(pos.value(m) >= sunfish.MATE_LOWER for m in pos.gen_moves()) or any(pos.board[b]=='k' for a,b in pos.gen_moves())

`

As far as I can see the function works as intended, but the pv function doesn't return any moves in some cases. So, it might be preferable if we also check the returned pv; if it's empty, we can use the prior pv (assuming it will return a pv at depth 1)

thomasahle commented 2 years ago

Let me just add a picture of the situation for those who don't speak fluent FEN ☺️ ![Uploading Screenshot_20220728-101819.png…]()

thomasahle commented 1 year ago

This is fixed in the latest sunfish:

~/repos/sunfish (nnue)$ ./sunfish.py
position fen r3n3/7Q/4pkp1/3p2P1/1p1P4/1P3PK1/r3q3/8 b - - 0 63
go
info depth 1 score cp 51 time 2 nodes 6 nps 2813 pv f6g5
info depth 1 score cp 140 time 3 nodes 8 nps 2557 pv f6g5
info depth 1 score cp 98 time 18 nodes 46 nps 2574 pv f6g5
info depth 1 score cp 115 time 19 nodes 49 nps 2579 pv f6g5
info depth 1 score cp 107 time 28 nodes 71 nps 2524 pv f6g5
info depth 2 score cp 112 time 43 nodes 102 nps 2390 pv f6g5