1
0
Fork 0
mirror of https://github.com/sockspls/badfish synced 2025-05-02 01:29:36 +00:00

Pass CastlingFlag argument only

Instead of Color and CastlingSide. Change functions API
accordingly.

No functional change.
This commit is contained in:
Marco Costalba 2014-03-02 11:53:17 +01:00
parent 3e5470d88f
commit 708cb311a0
5 changed files with 51 additions and 47 deletions

View file

@ -31,23 +31,25 @@
(mlist++)->move = make_move(to - (d), to); } (mlist++)->move = make_move(to - (d), to); }
namespace { namespace {
template<CastlingSide Side, bool Checks, bool Chess960> template<CastlingFlag Cf, bool Checks, bool Chess960>
ExtMove* generate_castling(const Position& pos, ExtMove* mlist, Color us, const CheckInfo* ci) { ExtMove* generate_castling(const Position& pos, ExtMove* mlist, Color us, const CheckInfo* ci) {
if (pos.castling_impeded(us, Side) || !pos.can_castle(make_castling_flag(us, Side))) static const bool KingSide = (Cf == WHITE_OO || Cf == BLACK_OO);
if (pos.castling_impeded(Cf) || !pos.can_castle(Cf))
return mlist; return mlist;
// After castling, the rook and king final positions are the same in Chess960 // After castling, the rook and king final positions are the same in Chess960
// as they would be in standard chess. // as they would be in standard chess.
Square kfrom = pos.king_square(us); Square kfrom = pos.king_square(us);
Square rfrom = pos.castling_rook_square(us, Side); Square rfrom = pos.castling_rook_square(Cf);
Square kto = relative_square(us, Side == KING_SIDE ? SQ_G1 : SQ_C1); Square kto = relative_square(us, KingSide ? SQ_G1 : SQ_C1);
Bitboard enemies = pos.pieces(~us); Bitboard enemies = pos.pieces(~us);
assert(!pos.checkers()); assert(!pos.checkers());
const Square K = Chess960 ? kto > kfrom ? DELTA_W : DELTA_E const Square K = Chess960 ? kto > kfrom ? DELTA_W : DELTA_E
: Side == KING_SIDE ? DELTA_W : DELTA_E; : KingSide ? DELTA_W : DELTA_E;
for (Square s = kto; s != kfrom; s += K) for (Square s = kto; s != kfrom; s += K)
if (pos.attackers_to(s) & enemies) if (pos.attackers_to(s) & enemies)
@ -262,13 +264,13 @@ namespace {
{ {
if (pos.is_chess960()) if (pos.is_chess960())
{ {
mlist = generate_castling< KING_SIDE, Checks, true>(pos, mlist, Us, ci); mlist = generate_castling<MakeCastling<Us, KING_SIDE>::flag, Checks, true>(pos, mlist, Us, ci);
mlist = generate_castling<QUEEN_SIDE, Checks, true>(pos, mlist, Us, ci); mlist = generate_castling<MakeCastling<Us, QUEEN_SIDE>::flag, Checks, true>(pos, mlist, Us, ci);
} }
else else
{ {
mlist = generate_castling< KING_SIDE, Checks, false>(pos, mlist, Us, ci); mlist = generate_castling<MakeCastling<Us, KING_SIDE>::flag, Checks, false>(pos, mlist, Us, ci);
mlist = generate_castling<QUEEN_SIDE, Checks, false>(pos, mlist, Us, ci); mlist = generate_castling<MakeCastling<Us, QUEEN_SIDE>::flag, Checks, false>(pos, mlist, Us, ci);
} }
} }

View file

@ -291,10 +291,10 @@ Score Entry::update_safety(const Position& pos, Square ksq) {
Value bonus = shelter_storm<Us>(pos, ksq); Value bonus = shelter_storm<Us>(pos, ksq);
// If we can castle use the bonus after the castling if it is bigger // If we can castle use the bonus after the castling if it is bigger
if (pos.can_castle(make_castling_flag(Us, KING_SIDE))) if (pos.can_castle(MakeCastling<Us, KING_SIDE>::flag))
bonus = std::max(bonus, shelter_storm<Us>(pos, relative_square(Us, SQ_G1))); bonus = std::max(bonus, shelter_storm<Us>(pos, relative_square(Us, SQ_G1)));
if (pos.can_castle(make_castling_flag(Us, QUEEN_SIDE))) if (pos.can_castle(MakeCastling<Us, QUEEN_SIDE>::flag))
bonus = std::max(bonus, shelter_storm<Us>(pos, relative_square(Us, SQ_C1))); bonus = std::max(bonus, shelter_storm<Us>(pos, relative_square(Us, SQ_C1)));
return kingSafety[Us] = make_score(bonus, -16 * minKPdistance[Us]); return kingSafety[Us] = make_score(bonus, -16 * minKPdistance[Us]);

View file

@ -304,23 +304,23 @@ void Position::set_castling_flag(Color c, Square rfrom) {
Square kfrom = king_square(c); Square kfrom = king_square(c);
CastlingSide cs = kfrom < rfrom ? KING_SIDE : QUEEN_SIDE; CastlingSide cs = kfrom < rfrom ? KING_SIDE : QUEEN_SIDE;
CastlingFlag cf = make_castling_flag(c, cs); CastlingFlag cf = (c | cs);
st->castlingFlags |= cf; st->castlingFlags |= cf;
castlingFlagsMask[kfrom] |= cf; castlingFlagsMask[kfrom] |= cf;
castlingFlagsMask[rfrom] |= cf; castlingFlagsMask[rfrom] |= cf;
castlingRookSquare[c][cs] = rfrom; castlingRookSquare[cf] = rfrom;
Square kto = relative_square(c, cs == KING_SIDE ? SQ_G1 : SQ_C1); 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 rto = relative_square(c, cs == KING_SIDE ? SQ_F1 : SQ_D1);
for (Square s = std::min(rfrom, rto); s <= std::max(rfrom, rto); ++s) for (Square s = std::min(rfrom, rto); s <= std::max(rfrom, rto); ++s)
if (s != kfrom && s != rfrom) if (s != kfrom && s != rfrom)
castlingPath[c][cs] |= s; castlingPath[cf] |= s;
for (Square s = std::min(kfrom, kto); s <= std::max(kfrom, kto); ++s) for (Square s = std::min(kfrom, kto); s <= std::max(kfrom, kto); ++s)
if (s != kfrom && s != rfrom) if (s != kfrom && s != rfrom)
castlingPath[c][cs] |= s; castlingPath[cf] |= s;
} }
@ -353,16 +353,16 @@ const string Position::fen() const {
ss << (sideToMove == WHITE ? " w " : " b "); ss << (sideToMove == WHITE ? " w " : " b ");
if (can_castle(WHITE_OO)) if (can_castle(WHITE_OO))
ss << (chess960 ? to_char(file_of(castling_rook_square(WHITE, KING_SIDE)), false) : 'K'); ss << (chess960 ? to_char(file_of(castling_rook_square(WHITE | KING_SIDE)), false) : 'K');
if (can_castle(WHITE_OOO)) if (can_castle(WHITE_OOO))
ss << (chess960 ? to_char(file_of(castling_rook_square(WHITE, QUEEN_SIDE)), false) : 'Q'); ss << (chess960 ? to_char(file_of(castling_rook_square(WHITE | QUEEN_SIDE)), false) : 'Q');
if (can_castle(BLACK_OO)) if (can_castle(BLACK_OO))
ss << (chess960 ? to_char(file_of(castling_rook_square(BLACK, KING_SIDE)), true) : 'k'); ss << (chess960 ? to_char(file_of(castling_rook_square(BLACK | KING_SIDE)), true) : 'k');
if (can_castle(BLACK_OOO)) if (can_castle(BLACK_OOO))
ss << (chess960 ? to_char(file_of(castling_rook_square(BLACK, QUEEN_SIDE)), true) : 'q'); ss << (chess960 ? to_char(file_of(castling_rook_square(BLACK | QUEEN_SIDE)), true) : 'q');
if (!can_castle(WHITE) && !can_castle(BLACK)) if (!can_castle(WHITE) && !can_castle(BLACK))
ss << '-'; ss << '-';
@ -1393,14 +1393,12 @@ bool Position::pos_is_ok(int* failedStep) const {
for (Color c = WHITE; c <= BLACK; ++c) for (Color c = WHITE; c <= BLACK; ++c)
for (CastlingSide s = KING_SIDE; s <= QUEEN_SIDE; s = CastlingSide(s + 1)) for (CastlingSide s = KING_SIDE; s <= QUEEN_SIDE; s = CastlingSide(s + 1))
{ {
CastlingFlag cf = make_castling_flag(c, s); if (!can_castle(c | s))
if (!can_castle(cf))
continue; continue;
if ( (castlingFlagsMask[king_square(c)] & cf) != cf if ( (castlingFlagsMask[king_square(c)] & (c | s)) != (c | s)
|| piece_on(castlingRookSquare[c][s]) != make_piece(c, ROOK) || piece_on(castlingRookSquare[c | s]) != make_piece(c, ROOK)
|| castlingFlagsMask[castlingRookSquare[c][s]] != cf) || castlingFlagsMask[castlingRookSquare[c | s]] != (c | s))
return false; return false;
} }

View file

@ -100,10 +100,10 @@ public:
template<PieceType Pt> const Square* list(Color c) const; template<PieceType Pt> const Square* list(Color c) const;
// Castling // Castling
int can_castle(CastlingFlag f) const;
int can_castle(Color c) const; int can_castle(Color c) const;
bool castling_impeded(Color c, CastlingSide s) const; int can_castle(CastlingFlag f) const;
Square castling_rook_square(Color c, CastlingSide s) const; bool castling_impeded(CastlingFlag f) const;
Square castling_rook_square(CastlingFlag f) const;
// Checking // Checking
Bitboard checkers() const; Bitboard checkers() const;
@ -198,8 +198,8 @@ private:
// Other info // Other info
int castlingFlagsMask[SQUARE_NB]; int castlingFlagsMask[SQUARE_NB];
Square castlingRookSquare[COLOR_NB][CASTLING_SIDE_NB]; Square castlingRookSquare[CASTLING_FLAG_NB];
Bitboard castlingPath[COLOR_NB][CASTLING_SIDE_NB]; Bitboard castlingPath[CASTLING_FLAG_NB];
StateInfo startState; StateInfo startState;
uint64_t nodes; uint64_t nodes;
int gamePly; int gamePly;
@ -281,12 +281,12 @@ inline int Position::can_castle(Color c) const {
return st->castlingFlags & ((WHITE_OO | WHITE_OOO) << (2 * c)); return st->castlingFlags & ((WHITE_OO | WHITE_OOO) << (2 * c));
} }
inline bool Position::castling_impeded(Color c, CastlingSide s) const { inline bool Position::castling_impeded(CastlingFlag f) const {
return byTypeBB[ALL_PIECES] & castlingPath[c][s]; return byTypeBB[ALL_PIECES] & castlingPath[f];
} }
inline Square Position::castling_rook_square(Color c, CastlingSide s) const { inline Square Position::castling_rook_square(CastlingFlag f) const {
return castlingRookSquare[c][s]; return castlingRookSquare[f];
} }
template<PieceType Pt> template<PieceType Pt>

View file

@ -116,6 +116,14 @@ enum MoveType {
CASTLING = 3 << 14 CASTLING = 3 << 14
}; };
enum Color {
WHITE, BLACK, NO_COLOR, COLOR_NB = 2
};
enum CastlingSide {
KING_SIDE, QUEEN_SIDE, CASTLING_SIDE_NB = 2
};
enum CastlingFlag { // Defined as in PolyGlot book hash key enum CastlingFlag { // Defined as in PolyGlot book hash key
NO_CASTLING, NO_CASTLING,
WHITE_OO, WHITE_OO,
@ -126,10 +134,10 @@ enum CastlingFlag { // Defined as in PolyGlot book hash key
CASTLING_FLAG_NB = 16 CASTLING_FLAG_NB = 16
}; };
enum CastlingSide { template<Color C, CastlingSide S> struct MakeCastling {
KING_SIDE, static const CastlingFlag
QUEEN_SIDE, flag = C == WHITE ? S == QUEEN_SIDE ? WHITE_OOO : WHITE_OO
CASTLING_SIDE_NB = 2 : S == QUEEN_SIDE ? BLACK_OOO : BLACK_OO;
}; };
enum Phase { enum Phase {
@ -187,10 +195,6 @@ enum Piece {
PIECE_NB = 16 PIECE_NB = 16
}; };
enum Color {
WHITE, BLACK, NO_COLOR, COLOR_NB = 2
};
enum Depth { enum Depth {
ONE_PLY = 2, ONE_PLY = 2,
@ -337,6 +341,10 @@ inline Square operator|(File f, Rank r) {
return Square((r << 3) | f); return Square((r << 3) | f);
} }
inline CastlingFlag operator|(Color c, CastlingSide s) {
return CastlingFlag(WHITE_OO << ((s == QUEEN_SIDE) + 2 * c));
}
inline Value mate_in(int ply) { inline Value mate_in(int ply) {
return VALUE_MATE - ply; return VALUE_MATE - ply;
} }
@ -349,10 +357,6 @@ inline Piece make_piece(Color c, PieceType pt) {
return Piece((c << 3) | pt); return Piece((c << 3) | pt);
} }
inline CastlingFlag make_castling_flag(Color c, CastlingSide s) {
return CastlingFlag(WHITE_OO << ((s == QUEEN_SIDE) + 2 * c));
}
inline PieceType type_of(Piece p) { inline PieceType type_of(Piece p) {
return PieceType(p & 7); return PieceType(p & 7);
} }