Improve generalization of castling
authorFabian Fichter <ianfab@users.noreply.github.com>
Sat, 9 Feb 2019 22:23:57 +0000 (23:23 +0100)
committerFabian Fichter <ianfab@users.noreply.github.com>
Sat, 9 Feb 2019 22:23:57 +0000 (23:23 +0100)
No functional change.

src/position.cpp
src/types.h

index f96a66a..646dd2d 100644 (file)
@@ -448,7 +448,7 @@ Position& Position::set(const Variant* v, const string& fenStr, bool isChess960,
 
 void Position::set_castling_right(Color c, Square rfrom) {
 
-  Square kfrom = count<KING>(c) ? square<KING>(c) : make_square(FILE_E, c == WHITE ? RANK_1 : RANK_8);
+  Square kfrom = count<KING>(c) ? square<KING>(c) : make_square(FILE_E, c == WHITE ? RANK_1 : max_rank());
   CastlingSide cs = kfrom < rfrom ? KING_SIDE : QUEEN_SIDE;
   CastlingRight cr = (c | cs);
 
@@ -457,8 +457,9 @@ void Position::set_castling_right(Color c, Square rfrom) {
   castlingRightsMask[rfrom] |= cr;
   castlingRookSquare[cr] = rfrom;
 
-  Square kto = relative_square(c, cs == KING_SIDE ? SQ_G1 : SQ_C1);
-  Square rto = relative_square(c, cs == KING_SIDE ? SQ_F1 : SQ_D1);
+  Square kto = make_square(cs == KING_SIDE ? castling_kingside_file() : castling_queenside_file(),
+                           relative_rank(c, RANK_1, max_rank()));
+  Square rto = kto + (cs == KING_SIDE ? WEST : EAST);
 
   for (Square s = std::min(rfrom, rto); s <= std::max(rfrom, rto); ++s)
       if (s != kfrom && s != rfrom)
@@ -952,8 +953,9 @@ bool Position::gives_check(Move m) const {
   {
       Square kfrom = from;
       Square rfrom = to; // Castling is encoded as 'King captures the rook'
-      Square kto = relative_square(sideToMove, rfrom > kfrom ? SQ_G1 : SQ_C1);
-      Square rto = relative_square(sideToMove, rfrom > kfrom ? SQ_F1 : SQ_D1);
+      Square kto = make_square(rfrom > kfrom ? castling_kingside_file() : castling_queenside_file(),
+                              relative_rank(sideToMove, RANK_1, max_rank()));
+      Square rto = kto + (rfrom > kfrom ? WEST : EAST);
 
       return   (PseudoAttacks[sideToMove][ROOK][rto] & square<KING>(~sideToMove))
             && (attacks_bb<ROOK>(rto, (pieces() ^ kfrom ^ rfrom) | rto | kto) & square<KING>(~sideToMove));
index 29757fc..bae7c99 100644 (file)
@@ -520,12 +520,8 @@ constexpr Rank relative_rank(Color c, Square s, Rank maxRank = RANK_8) {
   return relative_rank(c, rank_of(s), maxRank);
 }
 
-constexpr Square relative_square(Color c, Square s) {
-#ifdef LARGEBOARDS
-  return make_square(file_of(s), relative_rank(c, s));
-#else
-  return Square(s ^ (c * 56));
-#endif
+constexpr Square relative_square(Color c, Square s, Rank maxRank = RANK_8) {
+  return make_square(file_of(s), relative_rank(c, s, maxRank));
 }
 
 inline bool opposite_colors(Square s1, Square s2) {