From edacbb84c2fd788b48e6e13d361fc21f101ecac3 Mon Sep 17 00:00:00 2001 From: Fabian Fichter Date: Sat, 9 Feb 2019 23:23:57 +0100 Subject: [PATCH] Improve generalization of castling No functional change. --- src/position.cpp | 12 +++++++----- src/types.h | 8 ++------ 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/src/position.cpp b/src/position.cpp index f96a66a..646dd2d 100644 --- a/src/position.cpp +++ b/src/position.cpp @@ -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(c) ? square(c) : make_square(FILE_E, c == WHITE ? RANK_1 : RANK_8); + Square kfrom = count(c) ? square(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(~sideToMove)) && (attacks_bb(rto, (pieces() ^ kfrom ^ rfrom) | rto | kto) & square(~sideToMove)); diff --git a/src/types.h b/src/types.h index 29757fc..bae7c99 100644 --- a/src/types.h +++ b/src/types.h @@ -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) { -- 1.7.0.4