+/// Position::set_castling_rights() sets castling parameters castling avaiability.
+/// This function is compatible with 3 standards: Normal FEN standard, Shredder-FEN
+/// that uses the letters of the columns on which the rooks began the game instead
+/// of KQkq and also X-FEN standard that, in case of Chess960, if an inner Rook is
+/// associated with the castling right, the traditional castling tag will be replaced
+/// by the file letter of the involved rook as for the Shredder-FEN.
+
+void Position::set_castling_rights(char token) {
+
+ Color c = islower(token) ? BLACK : WHITE;
+
+ Square sqA = relative_square(c, SQ_A1);
+ Square sqH = relative_square(c, SQ_H1);
+
+ initialKFile = square_file(king_square(c));
+
+ if (toupper(token) == 'K')
+ {
+ for (Square sq = sqH; sq >= sqA; sq--)
+ if (piece_on(sq) == make_piece(c, ROOK))
+ {
+ set_castle_kingside(c);
+ initialKRFile = square_file(sq);
+ break;
+ }
+ }
+ else if (toupper(token) == 'Q')
+ {
+ for (Square sq = sqA; sq <= sqH; sq++)
+ if (piece_on(sq) == make_piece(c, ROOK))
+ {
+ set_castle_queenside(c);
+ initialQRFile = square_file(sq);
+ break;
+ }
+ }
+ else if (toupper(token) >= 'A' && toupper(token) <= 'H')
+ {
+ File rookFile = File(toupper(token) - 'A');
+
+ if (rookFile < initialKFile)
+ {
+ set_castle_queenside(c);
+ initialQRFile = rookFile;
+ }
+ else
+ {
+ set_castle_kingside(c);
+ initialKRFile = rookFile;
+ }
+ }
+}
+
+
+/// Position::to_fen() returns a FEN representation of the position. In case
+/// of Chess960 the Shredder-FEN notation is used. Mainly a debugging function.