+/// 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.
+
+bool Position::set_castling_rights(char token) {
+
+ Color c = token >= 'a' ? BLACK : WHITE;
+ Square sqA = (c == WHITE ? SQ_A1 : SQ_A8);
+ Square sqH = (c == WHITE ? SQ_H1 : SQ_H8);
+ Piece rook = (c == WHITE ? WR : BR);
+
+ initialKFile = square_file(king_square(c));
+ token = char(toupper(token));
+
+ if (token == 'K')
+ {
+ for (Square sq = sqH; sq >= sqA; sq--)
+ if (piece_on(sq) == rook)
+ {
+ set_castle_kingside(c);
+ initialKRFile = square_file(sq);
+ break;
+ }
+ }
+ else if (token == 'Q')
+ {
+ for (Square sq = sqA; sq <= sqH; sq++)
+ if (piece_on(sq) == rook)
+ {
+ set_castle_queenside(c);
+ initialQRFile = square_file(sq);
+ break;
+ }
+ }
+ else if (token >= 'A' && token <= 'H')
+ {
+ File rookFile = File(token - 'A') + FILE_A;
+ if (rookFile < initialKFile)
+ {
+ set_castle_queenside(c);
+ initialQRFile = rookFile;
+ }
+ else
+ {
+ set_castle_kingside(c);
+ initialKRFile = rookFile;
+ }
+ }
+ else
+ return token == '-';
+
+ return true;
+}
+
+
+/// Position::to_fen() returns a FEN representation of the position. In case
+/// of Chess960 the Shredder-FEN notation is used. Mainly a debugging function.