lichess.org
Donate

developer question!

Hi,

I'm a JavaScript developer looking to improve my skills developing with React.js by building my own chess app with React.
One thing I cannot figure out how to write the code for is castling.

In my research thus far I have found little help on this matter. If anyone here is a developer with ideas or thoughts OR if anyone knows how to reach out to the lichess developers for their thoughts I am ALL EARS.

Thank you,

SL
Maybe just keep 6 boolean values to see if the king and the rooks have already moved. And each turn (or maybe only if the player attempts to castle) check:
- if the king and the rook have already moves (boolean values) and
- if there are any pieces between the king and the rook and
- if an opponent piece is currently giving check or can attack the squares that your king goes through
@ethchess9394 Ok, I'm not a javascript programmer but I am a python programmer. If I had to code it, I would write something like this:
Trigger of the request to castle (this may be moving the king two spaces or moving the rook onto the king)
Checking legality of castling (can't castle through check, in check, or if the king or rook have already moved)
If function to separate between kingside castling and queenside castling.
Kingside castling: move the king to g1/g8 and the rook to f1/f8
Queenside castling: move the king to c1/c8 and the rook to d1/d8

This is just an outline of what I would write, you can apply this to any programming language.
You can do with just four boolean values, similar to how a FEN-string encodes the game state: a boolean indicating whether each castling option is still possible in the future or not. (If a rook moves then that eliminates one castling opportunity, for now and for later, while a king move eliminates both options for that side.)

The FEN format tells you exactly what you need to track in a position to be able to tell which are the legal moves in a given position. This means you will have to keep track of castling options as well as en passant options. (And if you want to be precise, you should count half-moves since the last capture or pawn move as well for the 50 move rule.)

en.wikipedia.org/wiki/Forsyth%E2%80%93Edwards_Notation
@thijscom , @boilingFrog , @Repiked , @Anton1000

Thank you all for your thorough and thoughtful contributions! I will probably attempt to implement more than one of your suggestions to see which works best (or which one I'm most capable of executing at my skill level!)

Thanks again!

SL

This topic has been archived and can no longer be replied to.