Skip to content

Commit

Permalink
Rename castling flag to castling right
Browse files Browse the repository at this point in the history
This is a more conventional naming as
reported also in:

http://chessprogramming.wikispaces.com/Castling+rights

No functional change.
  • Loading branch information
mcostalba committed Mar 8, 2014
1 parent 13b9e1e commit 1d1b7df
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 55 deletions.
16 changes: 8 additions & 8 deletions src/movegen.cpp
Expand Up @@ -31,18 +31,18 @@
(mlist++)->move = make_move(to - (d), to); }
namespace {

template<CastlingFlag Cf, bool Checks, bool Chess960>
template<CastlingRight Cr, bool Checks, bool Chess960>
ExtMove* generate_castling(const Position& pos, ExtMove* mlist, Color us, const CheckInfo* ci) {

static const bool KingSide = (Cf == WHITE_OO || Cf == BLACK_OO);
static const bool KingSide = (Cr == WHITE_OO || Cr == BLACK_OO);

if (pos.castling_impeded(Cf) || !pos.can_castle(Cf))
if (pos.castling_impeded(Cr) || !pos.can_castle(Cr))
return mlist;

// After castling, the rook and king final positions are the same in Chess960
// as they would be in standard chess.
Square kfrom = pos.king_square(us);
Square rfrom = pos.castling_rook_square(Cf);
Square rfrom = pos.castling_rook_square(Cr);
Square kto = relative_square(us, KingSide ? SQ_G1 : SQ_C1);
Bitboard enemies = pos.pieces(~us);

Expand Down Expand Up @@ -264,13 +264,13 @@ namespace {
{
if (pos.is_chess960())
{
mlist = generate_castling<MakeCastling<Us, KING_SIDE>::flag, Checks, true>(pos, mlist, Us, ci);
mlist = generate_castling<MakeCastling<Us, QUEEN_SIDE>::flag, Checks, true>(pos, mlist, Us, ci);
mlist = generate_castling<MakeCastling<Us, KING_SIDE>::right, Checks, true>(pos, mlist, Us, ci);
mlist = generate_castling<MakeCastling<Us, QUEEN_SIDE>::right, Checks, true>(pos, mlist, Us, ci);
}
else
{
mlist = generate_castling<MakeCastling<Us, KING_SIDE>::flag, Checks, false>(pos, mlist, Us, ci);
mlist = generate_castling<MakeCastling<Us, QUEEN_SIDE>::flag, Checks, false>(pos, mlist, Us, ci);
mlist = generate_castling<MakeCastling<Us, KING_SIDE>::right, Checks, false>(pos, mlist, Us, ci);
mlist = generate_castling<MakeCastling<Us, QUEEN_SIDE>::right, Checks, false>(pos, mlist, Us, ci);
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/pawns.cpp
Expand Up @@ -278,7 +278,7 @@ template<Color Us>
Score Entry::update_safety(const Position& pos, Square ksq) {

kingSquares[Us] = ksq;
castlingFlags[Us] = pos.can_castle(Us);
castlingRights[Us] = pos.can_castle(Us);
minKPdistance[Us] = 0;

Bitboard pawns = pos.pieces(Us, PAWN);
Expand All @@ -291,10 +291,10 @@ Score Entry::update_safety(const Position& pos, Square ksq) {
Value bonus = shelter_storm<Us>(pos, ksq);

// If we can castle use the bonus after the castling if it is bigger
if (pos.can_castle(MakeCastling<Us, KING_SIDE>::flag))
if (pos.can_castle(MakeCastling<Us, KING_SIDE>::right))
bonus = std::max(bonus, shelter_storm<Us>(pos, relative_square(Us, SQ_G1)));

if (pos.can_castle(MakeCastling<Us, QUEEN_SIDE>::flag))
if (pos.can_castle(MakeCastling<Us, QUEEN_SIDE>::right))
bonus = std::max(bonus, shelter_storm<Us>(pos, relative_square(Us, SQ_C1)));

return kingSafety[Us] = make_score(bonus, -16 * minKPdistance[Us]);
Expand Down
4 changes: 2 additions & 2 deletions src/pawns.h
Expand Up @@ -48,7 +48,7 @@ struct Entry {
template<Color Us>
Score king_safety(const Position& pos, Square ksq) {

return kingSquares[Us] == ksq && castlingFlags[Us] == pos.can_castle(Us)
return kingSquares[Us] == ksq && castlingRights[Us] == pos.can_castle(Us)
? kingSafety[Us] : update_safety<Us>(pos, ksq);
}

Expand All @@ -64,7 +64,7 @@ struct Entry {
Bitboard pawnAttacks[COLOR_NB];
Square kingSquares[COLOR_NB];
int minKPdistance[COLOR_NB];
int castlingFlags[COLOR_NB];
int castlingRights[COLOR_NB];
Score value;
int semiopenFiles[COLOR_NB];
Score kingSafety[COLOR_NB];
Expand Down
40 changes: 20 additions & 20 deletions src/position.cpp
Expand Up @@ -47,7 +47,7 @@ namespace Zobrist {

Key psq[COLOR_NB][PIECE_TYPE_NB][SQUARE_NB];
Key enpassant[FILE_NB];
Key castling[CASTLING_FLAG_NB];
Key castling[CASTLING_RIGHT_NB];
Key side;
Key exclusion;
}
Expand Down Expand Up @@ -263,7 +263,7 @@ void Position::set(const string& fenStr, bool isChess960, Thread* th) {
else
continue;

set_castling_flag(c, rsq);
set_castling_right(c, rsq);
}

// 4. En passant square. Ignore if no pawn capture is possible
Expand Down Expand Up @@ -297,30 +297,30 @@ void Position::set(const string& fenStr, bool isChess960, Thread* th) {
}


/// Position::set_castling_flag() is a helper function used to set castling
/// flags given the corresponding color and the rook starting square.
/// Position::set_castling_right() is a helper function used to set castling
/// rights given the corresponding color and the rook starting square.

void Position::set_castling_flag(Color c, Square rfrom) {
void Position::set_castling_right(Color c, Square rfrom) {

Square kfrom = king_square(c);
CastlingSide cs = kfrom < rfrom ? KING_SIDE : QUEEN_SIDE;
CastlingFlag cf = (c | cs);
CastlingRight cr = (c | cs);

st->castlingFlags |= cf;
castlingFlagsMask[kfrom] |= cf;
castlingFlagsMask[rfrom] |= cf;
castlingRookSquare[cf] = rfrom;
st->castlingRights |= cr;
castlingRightsMask[kfrom] |= cr;
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);

for (Square s = std::min(rfrom, rto); s <= std::max(rfrom, rto); ++s)
if (s != kfrom && s != rfrom)
castlingPath[cf] |= s;
castlingPath[cr] |= s;

for (Square s = std::min(kfrom, kto); s <= std::max(kfrom, kto); ++s)
if (s != kfrom && s != rfrom)
castlingPath[cf] |= s;
castlingPath[cr] |= s;
}


Expand Down Expand Up @@ -791,12 +791,12 @@ void Position::do_move(Move m, StateInfo& newSt, const CheckInfo& ci, bool moveI
st->epSquare = SQ_NONE;
}

// Update castling flags if needed
if (st->castlingFlags && (castlingFlagsMask[from] | castlingFlagsMask[to]))
// Update castling rights if needed
if (st->castlingRights && (castlingRightsMask[from] | castlingRightsMask[to]))
{
int cf = castlingFlagsMask[from] | castlingFlagsMask[to];
k ^= Zobrist::castling[st->castlingFlags & cf];
st->castlingFlags &= ~cf;
int cr = castlingRightsMask[from] | castlingRightsMask[to];
k ^= Zobrist::castling[st->castlingRights & cr];
st->castlingRights &= ~cr;
}

// Prefetch TT access as soon as we know the new hash key
Expand Down Expand Up @@ -1128,7 +1128,7 @@ void Position::clear() {

Key Position::compute_key() const {

Key k = Zobrist::castling[st->castlingFlags];
Key k = Zobrist::castling[st->castlingRights];

for (Bitboard b = pieces(); b; )
{
Expand Down Expand Up @@ -1396,9 +1396,9 @@ bool Position::pos_is_ok(int* failedStep) const {
if (!can_castle(c | s))
continue;

if ( (castlingFlagsMask[king_square(c)] & (c | s)) != (c | s)
if ( (castlingRightsMask[king_square(c)] & (c | s)) != (c | s)
|| piece_on(castlingRookSquare[c | s]) != make_piece(c, ROOK)
|| castlingFlagsMask[castlingRookSquare[c | s]] != (c | s))
|| castlingRightsMask[castlingRookSquare[c | s]] != (c | s))
return false;
}

Expand Down
30 changes: 15 additions & 15 deletions src/position.h
Expand Up @@ -51,7 +51,7 @@ struct CheckInfo {
struct StateInfo {
Key pawnKey, materialKey;
Value npMaterial[COLOR_NB];
int castlingFlags, rule50, pliesFromNull;
int castlingRights, rule50, pliesFromNull;
Score psq;
Square epSquare;

Expand Down Expand Up @@ -101,9 +101,9 @@ class Position {

// Castling
int can_castle(Color c) const;
int can_castle(CastlingFlag f) const;
bool castling_impeded(CastlingFlag f) const;
Square castling_rook_square(CastlingFlag f) const;
int can_castle(CastlingRight cr) const;
bool castling_impeded(CastlingRight cr) const;
Square castling_rook_square(CastlingRight cr) const;

// Checking
Bitboard checkers() const;
Expand Down Expand Up @@ -170,7 +170,7 @@ class Position {
private:
// Initialization helpers (used while setting up a position)
void clear();
void set_castling_flag(Color c, Square rfrom);
void set_castling_right(Color c, Square rfrom);

// Helper functions
void do_castling(Square kfrom, Square kto, Square rfrom, Square rto);
Expand All @@ -197,9 +197,9 @@ class Position {
int index[SQUARE_NB];

// Other info
int castlingFlagsMask[SQUARE_NB];
Square castlingRookSquare[CASTLING_FLAG_NB];
Bitboard castlingPath[CASTLING_FLAG_NB];
int castlingRightsMask[SQUARE_NB];
Square castlingRookSquare[CASTLING_RIGHT_NB];
Bitboard castlingPath[CASTLING_RIGHT_NB];
StateInfo startState;
uint64_t nodes;
int gamePly;
Expand Down Expand Up @@ -273,20 +273,20 @@ inline Square Position::king_square(Color c) const {
return pieceList[c][KING][0];
}

inline int Position::can_castle(CastlingFlag f) const {
return st->castlingFlags & f;
inline int Position::can_castle(CastlingRight cr) const {
return st->castlingRights & cr;
}

inline int Position::can_castle(Color c) const {
return st->castlingFlags & ((WHITE_OO | WHITE_OOO) << (2 * c));
return st->castlingRights & ((WHITE_OO | WHITE_OOO) << (2 * c));
}

inline bool Position::castling_impeded(CastlingFlag f) const {
return byTypeBB[ALL_PIECES] & castlingPath[f];
inline bool Position::castling_impeded(CastlingRight cr) const {
return byTypeBB[ALL_PIECES] & castlingPath[cr];
}

inline Square Position::castling_rook_square(CastlingFlag f) const {
return castlingRookSquare[f];
inline Square Position::castling_rook_square(CastlingRight cr) const {
return castlingRookSquare[cr];
}

template<PieceType Pt>
Expand Down
14 changes: 7 additions & 7 deletions src/types.h
Expand Up @@ -124,20 +124,20 @@ enum CastlingSide {
KING_SIDE, QUEEN_SIDE, CASTLING_SIDE_NB = 2
};

enum CastlingFlag { // Defined as in PolyGlot book hash key
enum CastlingRight { // Defined as in PolyGlot book hash key
NO_CASTLING,
WHITE_OO,
WHITE_OOO = WHITE_OO << 1,
BLACK_OO = WHITE_OO << 2,
BLACK_OOO = WHITE_OO << 3,
ANY_CASTLING = WHITE_OO | WHITE_OOO | BLACK_OO | BLACK_OOO,
CASTLING_FLAG_NB = 16
CASTLING_RIGHT_NB = 16
};

template<Color C, CastlingSide S> struct MakeCastling {
static const CastlingFlag
flag = C == WHITE ? S == QUEEN_SIDE ? WHITE_OOO : WHITE_OO
: S == QUEEN_SIDE ? BLACK_OOO : BLACK_OO;
static const CastlingRight
right = C == WHITE ? S == QUEEN_SIDE ? WHITE_OOO : WHITE_OO
: S == QUEEN_SIDE ? BLACK_OOO : BLACK_OO;
};

enum Phase {
Expand Down Expand Up @@ -341,8 +341,8 @@ inline Square operator|(File f, Rank r) {
return Square((r << 3) | f);
}

inline CastlingFlag operator|(Color c, CastlingSide s) {
return CastlingFlag(WHITE_OO << ((s == QUEEN_SIDE) + 2 * c));
inline CastlingRight operator|(Color c, CastlingSide s) {
return CastlingRight(WHITE_OO << ((s == QUEEN_SIDE) + 2 * c));
}

inline Value mate_in(int ply) {
Expand Down

0 comments on commit 1d1b7df

Please sign in to comment.