Skip to content

Don't allow king to castle through or out of check#17

Open
CompuGeniusCode wants to merge 1 commit intoemymin:mainfrom
CompuGeniusCode:patch-1
Open

Don't allow king to castle through or out of check#17
CompuGeniusCode wants to merge 1 commit intoemymin:mainfrom
CompuGeniusCode:patch-1

Conversation

@CompuGeniusCode
Copy link

@CompuGeniusCode CompuGeniusCode commented Feb 21, 2025

No description provided.

@CompuGeniusCode CompuGeniusCode changed the title Don't allow king to castle through or out of check and adjust en passant logic Don't allow king to castle through or out of check Feb 21, 2025
@emymin
Copy link
Owner

emymin commented Feb 22, 2025

Thank you for the contribution!
There seems to be a bug generating legal moves for the king when placed under check while the conditions for castling are satisfied

rnb1kbnr/ppp3p1/8/7p/8/8/PPPPqPPP/RNB1K2R w KQkq - 0 1
This position is registered as a checkmate, however the king is able to capture the queen and exit check.
rnb1kbnr/pppp3p/6p1/8/8/7N/PPPPqPPP/RNBQK2R w KQkq - 0 1
Trying a similar position, it seems that indeed the fact that the king could capture the queen is not considered

1nbqkbnr/1ppppppp/8/p6Q/4r3/N6N/PPPP1PPP/R1B1K2R w KQka - 0 1
In this position, the king should be able to move left or right by one square to exit check, however oddly enough the board registers the moves king to C4 and king to G4 instead (which are very much illegal)

@CompuGeniusCode
Copy link
Author

CompuGeniusCode commented Feb 22, 2025

Were these bugs not there before my edit? I definitely remember coming across an issue where a king couldn't kill a queen that was attacking it.

@emymin
Copy link
Owner

emymin commented Feb 23, 2025

It's possible there might be similar issues like that, the move generation really needs some proper automated testing.

However in the positions I mentioned, the current version behaves correctly (with the exception of also including castling as a valid move of course).

@CompuGeniusCode
Copy link
Author

CompuGeniusCode commented Feb 23, 2025

Hmm. I'll keep trying. I haven't looked yet - is there a way to start the board in a certain FEN besides standard starting pos for faster testing? I'm not up for creating automated testing rn, but st least that would make it faster.

@emymin
Copy link
Owner

emymin commented Feb 23, 2025

The Board has a method to load FEN strings which it uses to lay out the board when starting a game, that part is also fairly untested but I think loading mid-game positions should be fine!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants