mirror of
https://github.com/sockspls/badfish
synced 2025-04-30 16:53:09 +00:00
Rename CASTLE to CASTLING
It is call 'castling move', not 'castle move' Noticed while reading DiscoCheck sources. No functional change.
This commit is contained in:
parent
dd4e5db2be
commit
f99cb3dc27
10 changed files with 101 additions and 101 deletions
|
@ -49,7 +49,7 @@ namespace {
|
||||||
Key PolyGlotRandoms[781];
|
Key PolyGlotRandoms[781];
|
||||||
struct {
|
struct {
|
||||||
Key psq[12][64]; // [piece][square]
|
Key psq[12][64]; // [piece][square]
|
||||||
Key castle[4]; // [castle right]
|
Key castling[4]; // [castling flag]
|
||||||
Key enpassant[8]; // [file]
|
Key enpassant[8]; // [file]
|
||||||
Key turn;
|
Key turn;
|
||||||
} Zobrist;
|
} Zobrist;
|
||||||
|
@ -332,10 +332,10 @@ namespace {
|
||||||
key ^= PG.Zobrist.psq[2 * (type_of(p) - 1) + (color_of(p) == WHITE)][s];
|
key ^= PG.Zobrist.psq[2 * (type_of(p) - 1) + (color_of(p) == WHITE)][s];
|
||||||
}
|
}
|
||||||
|
|
||||||
b = pos.can_castle(ALL_CASTLES);
|
b = pos.can_castle(ANY_CASTLING);
|
||||||
|
|
||||||
while (b)
|
while (b)
|
||||||
key ^= PG.Zobrist.castle[pop_lsb(&b)];
|
key ^= PG.Zobrist.castling[pop_lsb(&b)];
|
||||||
|
|
||||||
if (pos.ep_square() != SQ_NONE)
|
if (pos.ep_square() != SQ_NONE)
|
||||||
key ^= PG.Zobrist.enpassant[file_of(pos.ep_square())];
|
key ^= PG.Zobrist.enpassant[file_of(pos.ep_square())];
|
||||||
|
|
|
@ -569,7 +569,7 @@ Value do_evaluate(const Position& pos) {
|
||||||
Square ksq = pos.king_square(Us);
|
Square ksq = pos.king_square(Us);
|
||||||
|
|
||||||
// Penalize rooks which are trapped inside a king. Penalize more if
|
// Penalize rooks which are trapped inside a king. Penalize more if
|
||||||
// king has lost right to castle.
|
// king has lost castling availability.
|
||||||
if ( ((file_of(ksq) < FILE_E) == (file_of(s) < file_of(ksq)))
|
if ( ((file_of(ksq) < FILE_E) == (file_of(s) < file_of(ksq)))
|
||||||
&& (rank_of(ksq) == rank_of(s) || relative_rank(Us, ksq) == RANK_1)
|
&& (rank_of(ksq) == rank_of(s) || relative_rank(Us, ksq) == RANK_1)
|
||||||
&& !ei.pi->semiopen_on_side(Us, file_of(ksq), file_of(ksq) < FILE_E))
|
&& !ei.pi->semiopen_on_side(Us, file_of(ksq), file_of(ksq) < FILE_E))
|
||||||
|
|
|
@ -32,15 +32,15 @@
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
template<CastlingSide Side, bool Checks, bool Chess960>
|
template<CastlingSide Side, bool Checks, bool Chess960>
|
||||||
ExtMove* generate_castle(const Position& pos, ExtMove* mlist, Color us) {
|
ExtMove* generate_castling(const Position& pos, ExtMove* mlist, Color us) {
|
||||||
|
|
||||||
if (pos.castle_impeded(us, Side) || !pos.can_castle(make_castle_right(us, Side)))
|
if (pos.castling_impeded(us, Side) || !pos.can_castle(make_castling_flag(us, Side)))
|
||||||
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.castle_rook_square(us, Side);
|
Square rfrom = pos.castling_rook_square(us, Side);
|
||||||
Square kto = relative_square(us, Side == KING_SIDE ? SQ_G1 : SQ_C1);
|
Square kto = relative_square(us, Side == KING_SIDE ? SQ_G1 : SQ_C1);
|
||||||
Bitboard enemies = pos.pieces(~us);
|
Bitboard enemies = pos.pieces(~us);
|
||||||
|
|
||||||
|
@ -59,7 +59,7 @@ namespace {
|
||||||
if (Chess960 && (attacks_bb<ROOK>(kto, pos.pieces() ^ rfrom) & pos.pieces(~us, ROOK, QUEEN)))
|
if (Chess960 && (attacks_bb<ROOK>(kto, pos.pieces() ^ rfrom) & pos.pieces(~us, ROOK, QUEEN)))
|
||||||
return mlist;
|
return mlist;
|
||||||
|
|
||||||
(mlist++)->move = make<CASTLE>(kfrom, rfrom);
|
(mlist++)->move = make<CASTLING>(kfrom, rfrom);
|
||||||
|
|
||||||
if (Checks && !pos.gives_check((mlist - 1)->move, CheckInfo(pos)))
|
if (Checks && !pos.gives_check((mlist - 1)->move, CheckInfo(pos)))
|
||||||
--mlist;
|
--mlist;
|
||||||
|
@ -260,13 +260,13 @@ namespace {
|
||||||
{
|
{
|
||||||
if (pos.is_chess960())
|
if (pos.is_chess960())
|
||||||
{
|
{
|
||||||
mlist = generate_castle< KING_SIDE, Checks, true>(pos, mlist, Us);
|
mlist = generate_castling< KING_SIDE, Checks, true>(pos, mlist, Us);
|
||||||
mlist = generate_castle<QUEEN_SIDE, Checks, true>(pos, mlist, Us);
|
mlist = generate_castling<QUEEN_SIDE, Checks, true>(pos, mlist, Us);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
mlist = generate_castle< KING_SIDE, Checks, false>(pos, mlist, Us);
|
mlist = generate_castling< KING_SIDE, Checks, false>(pos, mlist, Us);
|
||||||
mlist = generate_castle<QUEEN_SIDE, Checks, false>(pos, mlist, Us);
|
mlist = generate_castling<QUEEN_SIDE, Checks, false>(pos, mlist, Us);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -56,7 +56,7 @@ string score_to_uci(Value v, Value alpha, Value beta) {
|
||||||
/// move_to_uci() converts a move to a string in coordinate notation
|
/// move_to_uci() converts a move to a string in coordinate notation
|
||||||
/// (g1f3, a7a8q, etc.). The only special case is castling moves, where we print
|
/// (g1f3, a7a8q, etc.). The only special case is castling moves, where we print
|
||||||
/// in the e1g1 notation in normal chess mode, and in e1h1 notation in chess960
|
/// in the e1g1 notation in normal chess mode, and in e1h1 notation in chess960
|
||||||
/// mode. Internally castle moves are always coded as "king captures rook".
|
/// mode. Internally castling moves are always encoded as "king captures rook".
|
||||||
|
|
||||||
const string move_to_uci(Move m, bool chess960) {
|
const string move_to_uci(Move m, bool chess960) {
|
||||||
|
|
||||||
|
@ -69,7 +69,7 @@ const string move_to_uci(Move m, bool chess960) {
|
||||||
if (m == MOVE_NULL)
|
if (m == MOVE_NULL)
|
||||||
return "0000";
|
return "0000";
|
||||||
|
|
||||||
if (type_of(m) == CASTLE && !chess960)
|
if (type_of(m) == CASTLING && !chess960)
|
||||||
to = (to > from ? FILE_G : FILE_C) | rank_of(from);
|
to = (to > from ? FILE_G : FILE_C) | rank_of(from);
|
||||||
|
|
||||||
string move = square_to_string(from) + square_to_string(to);
|
string move = square_to_string(from) + square_to_string(to);
|
||||||
|
@ -118,7 +118,7 @@ const string move_to_san(Position& pos, Move m) {
|
||||||
Piece pc = pos.piece_on(from);
|
Piece pc = pos.piece_on(from);
|
||||||
PieceType pt = type_of(pc);
|
PieceType pt = type_of(pc);
|
||||||
|
|
||||||
if (type_of(m) == CASTLE)
|
if (type_of(m) == CASTLING)
|
||||||
san = to > from ? "O-O" : "O-O-O";
|
san = to > from ? "O-O" : "O-O-O";
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
|
@ -259,7 +259,7 @@ template<Color Us>
|
||||||
Score Entry::update_safety(const Position& pos, Square ksq) {
|
Score Entry::update_safety(const Position& pos, Square ksq) {
|
||||||
|
|
||||||
kingSquares[Us] = ksq;
|
kingSquares[Us] = ksq;
|
||||||
castleRights[Us] = pos.can_castle(Us);
|
castlingFlags[Us] = pos.can_castle(Us);
|
||||||
minKPdistance[Us] = 0;
|
minKPdistance[Us] = 0;
|
||||||
|
|
||||||
Bitboard pawns = pos.pieces(Us, PAWN);
|
Bitboard pawns = pos.pieces(Us, PAWN);
|
||||||
|
@ -271,11 +271,11 @@ 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 castle if it is bigger
|
// If we can castle use the bonus after the castling if it is bigger
|
||||||
if (pos.can_castle(make_castle_right(Us, KING_SIDE)))
|
if (pos.can_castle(make_castling_flag(Us, KING_SIDE)))
|
||||||
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_castle_right(Us, QUEEN_SIDE)))
|
if (pos.can_castle(make_castling_flag(Us, QUEEN_SIDE)))
|
||||||
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]);
|
||||||
|
|
|
@ -48,7 +48,7 @@ struct Entry {
|
||||||
template<Color Us>
|
template<Color Us>
|
||||||
Score king_safety(const Position& pos, Square ksq) {
|
Score king_safety(const Position& pos, Square ksq) {
|
||||||
|
|
||||||
return kingSquares[Us] == ksq && castleRights[Us] == pos.can_castle(Us)
|
return kingSquares[Us] == ksq && castlingFlags[Us] == pos.can_castle(Us)
|
||||||
? kingSafety[Us] : update_safety<Us>(pos, ksq);
|
? kingSafety[Us] : update_safety<Us>(pos, ksq);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -64,7 +64,7 @@ struct Entry {
|
||||||
Bitboard pawnAttacks[COLOR_NB];
|
Bitboard pawnAttacks[COLOR_NB];
|
||||||
Square kingSquares[COLOR_NB];
|
Square kingSquares[COLOR_NB];
|
||||||
int minKPdistance[COLOR_NB];
|
int minKPdistance[COLOR_NB];
|
||||||
int castleRights[COLOR_NB];
|
int castlingFlags[COLOR_NB];
|
||||||
Score value;
|
Score value;
|
||||||
int semiopenFiles[COLOR_NB];
|
int semiopenFiles[COLOR_NB];
|
||||||
Score kingSafety[COLOR_NB];
|
Score kingSafety[COLOR_NB];
|
||||||
|
|
102
src/position.cpp
102
src/position.cpp
|
@ -47,7 +47,7 @@ namespace Zobrist {
|
||||||
|
|
||||||
Key psq[COLOR_NB][PIECE_TYPE_NB][SQUARE_NB];
|
Key psq[COLOR_NB][PIECE_TYPE_NB][SQUARE_NB];
|
||||||
Key enpassant[FILE_NB];
|
Key enpassant[FILE_NB];
|
||||||
Key castle[CASTLE_RIGHT_NB];
|
Key castling[CASTLING_FLAG_NB];
|
||||||
Key side;
|
Key side;
|
||||||
Key exclusion;
|
Key exclusion;
|
||||||
}
|
}
|
||||||
|
@ -125,13 +125,13 @@ void Position::init() {
|
||||||
for (File f = FILE_A; f <= FILE_H; ++f)
|
for (File f = FILE_A; f <= FILE_H; ++f)
|
||||||
Zobrist::enpassant[f] = rk.rand<Key>();
|
Zobrist::enpassant[f] = rk.rand<Key>();
|
||||||
|
|
||||||
for (int cr = CASTLES_NONE; cr <= ALL_CASTLES; ++cr)
|
for (int cf = NO_CASTLING; cf <= ANY_CASTLING; ++cf)
|
||||||
{
|
{
|
||||||
Bitboard b = cr;
|
Bitboard b = cf;
|
||||||
while (b)
|
while (b)
|
||||||
{
|
{
|
||||||
Key k = Zobrist::castle[1ULL << pop_lsb(&b)];
|
Key k = Zobrist::castling[1ULL << pop_lsb(&b)];
|
||||||
Zobrist::castle[cr] ^= k ? k : rk.rand<Key>();
|
Zobrist::castling[cf] ^= k ? k : rk.rand<Key>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -263,7 +263,7 @@ void Position::set(const string& fenStr, bool isChess960, Thread* th) {
|
||||||
else
|
else
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
set_castle_right(c, rsq);
|
set_castling_flag(c, rsq);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 4. En passant square. Ignore if no pawn capture is possible
|
// 4. En passant square. Ignore if no pawn capture is possible
|
||||||
|
@ -297,30 +297,30 @@ void Position::set(const string& fenStr, bool isChess960, Thread* th) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Position::set_castle_right() is an helper function used to set castling
|
/// Position::set_castling_flag() is an helper function used to set castling
|
||||||
/// rights given the corresponding color and the rook starting square.
|
/// flags given the corresponding color and the rook starting square.
|
||||||
|
|
||||||
void Position::set_castle_right(Color c, Square rfrom) {
|
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;
|
||||||
CastleRight cr = make_castle_right(c, cs);
|
CastlingFlag cf = make_castling_flag(c, cs);
|
||||||
|
|
||||||
st->castleRights |= cr;
|
st->castlingFlags |= cf;
|
||||||
castleRightsMask[kfrom] |= cr;
|
castlingFlagsMask[kfrom] |= cf;
|
||||||
castleRightsMask[rfrom] |= cr;
|
castlingFlagsMask[rfrom] |= cf;
|
||||||
castleRookSquare[c][cs] = rfrom;
|
castlingRookSquare[c][cs] = 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)
|
||||||
castlePath[c][cs] |= s;
|
castlingPath[c][cs] |= 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)
|
||||||
castlePath[c][cs] |= s;
|
castlingPath[c][cs] |= s;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -357,18 +357,18 @@ 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 ? file_to_char(file_of(castle_rook_square(WHITE, KING_SIDE)), false) : 'K');
|
ss << (chess960 ? file_to_char(file_of(castling_rook_square(WHITE, KING_SIDE)), false) : 'K');
|
||||||
|
|
||||||
if (can_castle(WHITE_OOO))
|
if (can_castle(WHITE_OOO))
|
||||||
ss << (chess960 ? file_to_char(file_of(castle_rook_square(WHITE, QUEEN_SIDE)), false) : 'Q');
|
ss << (chess960 ? file_to_char(file_of(castling_rook_square(WHITE, QUEEN_SIDE)), false) : 'Q');
|
||||||
|
|
||||||
if (can_castle(BLACK_OO))
|
if (can_castle(BLACK_OO))
|
||||||
ss << (chess960 ? file_to_char(file_of(castle_rook_square(BLACK, KING_SIDE)), true) : 'k');
|
ss << (chess960 ? file_to_char(file_of(castling_rook_square(BLACK, KING_SIDE)), true) : 'k');
|
||||||
|
|
||||||
if (can_castle(BLACK_OOO))
|
if (can_castle(BLACK_OOO))
|
||||||
ss << (chess960 ? file_to_char(file_of(castle_rook_square(BLACK, QUEEN_SIDE)), true) : 'q');
|
ss << (chess960 ? file_to_char(file_of(castling_rook_square(BLACK, QUEEN_SIDE)), true) : 'q');
|
||||||
|
|
||||||
if (st->castleRights == CASTLES_NONE)
|
if (st->castlingFlags == NO_CASTLING)
|
||||||
ss << '-';
|
ss << '-';
|
||||||
|
|
||||||
ss << (ep_square() == SQ_NONE ? " - " : " " + square_to_string(ep_square()) + " ")
|
ss << (ep_square() == SQ_NONE ? " - " : " " + square_to_string(ep_square()) + " ")
|
||||||
|
@ -489,7 +489,7 @@ bool Position::legal(Move m, Bitboard pinned) const {
|
||||||
// square is attacked by the opponent. Castling moves are checked
|
// square is attacked by the opponent. Castling moves are checked
|
||||||
// for legality during move generation.
|
// for legality during move generation.
|
||||||
if (type_of(piece_on(from)) == KING)
|
if (type_of(piece_on(from)) == KING)
|
||||||
return type_of(m) == CASTLE || !(attackers_to(to_sq(m)) & pieces(~us));
|
return type_of(m) == CASTLING || !(attackers_to(to_sq(m)) & pieces(~us));
|
||||||
|
|
||||||
// A non-king move is legal if and only if it is not pinned or it
|
// A non-king move is legal if and only if it is not pinned or it
|
||||||
// is moving along the ray towards or away from the king.
|
// is moving along the ray towards or away from the king.
|
||||||
|
@ -663,10 +663,10 @@ bool Position::gives_check(Move m, const CheckInfo& ci) const {
|
||||||
return (attacks_bb< ROOK>(ksq, b) & pieces(us, QUEEN, ROOK))
|
return (attacks_bb< ROOK>(ksq, b) & pieces(us, QUEEN, ROOK))
|
||||||
| (attacks_bb<BISHOP>(ksq, b) & pieces(us, QUEEN, BISHOP));
|
| (attacks_bb<BISHOP>(ksq, b) & pieces(us, QUEEN, BISHOP));
|
||||||
}
|
}
|
||||||
case CASTLE:
|
case CASTLING:
|
||||||
{
|
{
|
||||||
Square kfrom = from;
|
Square kfrom = from;
|
||||||
Square rfrom = to; // 'King captures the rook' notation
|
Square rfrom = to; // Castling is encoded as 'King captures the rook'
|
||||||
Square kto = relative_square(us, rfrom > kfrom ? SQ_G1 : SQ_C1);
|
Square kto = relative_square(us, rfrom > kfrom ? SQ_G1 : SQ_C1);
|
||||||
Square rto = relative_square(us, rfrom > kfrom ? SQ_F1 : SQ_D1);
|
Square rto = relative_square(us, rfrom > kfrom ? SQ_F1 : SQ_D1);
|
||||||
|
|
||||||
|
@ -724,20 +724,20 @@ void Position::do_move(Move m, StateInfo& newSt, const CheckInfo& ci, bool moveI
|
||||||
PieceType captured = type_of(m) == ENPASSANT ? PAWN : type_of(piece_on(to));
|
PieceType captured = type_of(m) == ENPASSANT ? PAWN : type_of(piece_on(to));
|
||||||
|
|
||||||
assert(color_of(pc) == us);
|
assert(color_of(pc) == us);
|
||||||
assert(piece_on(to) == NO_PIECE || color_of(piece_on(to)) == them || type_of(m) == CASTLE);
|
assert(piece_on(to) == NO_PIECE || color_of(piece_on(to)) == them || type_of(m) == CASTLING);
|
||||||
assert(captured != KING);
|
assert(captured != KING);
|
||||||
|
|
||||||
if (type_of(m) == CASTLE)
|
if (type_of(m) == CASTLING)
|
||||||
{
|
{
|
||||||
assert(pc == make_piece(us, KING));
|
assert(pc == make_piece(us, KING));
|
||||||
|
|
||||||
bool kingSide = to > from;
|
bool kingSide = to > from;
|
||||||
Square rfrom = to; // Castle is encoded as "king captures friendly rook"
|
Square rfrom = to; // Castling is encoded as "king captures friendly rook"
|
||||||
Square rto = relative_square(us, kingSide ? SQ_F1 : SQ_D1);
|
Square rto = relative_square(us, kingSide ? SQ_F1 : SQ_D1);
|
||||||
to = relative_square(us, kingSide ? SQ_G1 : SQ_C1);
|
to = relative_square(us, kingSide ? SQ_G1 : SQ_C1);
|
||||||
captured = NO_PIECE_TYPE;
|
captured = NO_PIECE_TYPE;
|
||||||
|
|
||||||
do_castle(from, to, rfrom, rto);
|
do_castling(from, to, rfrom, rto);
|
||||||
|
|
||||||
st->psq += psq[us][ROOK][rto] - psq[us][ROOK][rfrom];
|
st->psq += psq[us][ROOK][rto] - psq[us][ROOK][rfrom];
|
||||||
k ^= Zobrist::psq[us][ROOK][rfrom] ^ Zobrist::psq[us][ROOK][rto];
|
k ^= Zobrist::psq[us][ROOK][rfrom] ^ Zobrist::psq[us][ROOK][rto];
|
||||||
|
@ -794,19 +794,19 @@ void Position::do_move(Move m, StateInfo& newSt, const CheckInfo& ci, bool moveI
|
||||||
st->epSquare = SQ_NONE;
|
st->epSquare = SQ_NONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update castle rights if needed
|
// Update castling flags if needed
|
||||||
if (st->castleRights && (castleRightsMask[from] | castleRightsMask[to]))
|
if (st->castlingFlags && (castlingFlagsMask[from] | castlingFlagsMask[to]))
|
||||||
{
|
{
|
||||||
int cr = castleRightsMask[from] | castleRightsMask[to];
|
int cf = castlingFlagsMask[from] | castlingFlagsMask[to];
|
||||||
k ^= Zobrist::castle[st->castleRights & cr];
|
k ^= Zobrist::castling[st->castlingFlags & cf];
|
||||||
st->castleRights &= ~cr;
|
st->castlingFlags &= ~cf;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prefetch TT access as soon as we know the new hash key
|
// Prefetch TT access as soon as we know the new hash key
|
||||||
prefetch((char*)TT.first_entry(k));
|
prefetch((char*)TT.first_entry(k));
|
||||||
|
|
||||||
// Move the piece. The tricky Chess960 castle is handled earlier
|
// Move the piece. The tricky Chess960 castling is handled earlier
|
||||||
if (type_of(m) != CASTLE)
|
if (type_of(m) != CASTLING)
|
||||||
move_piece(from, to, us, pt);
|
move_piece(from, to, us, pt);
|
||||||
|
|
||||||
// If the moving piece is a pawn do some special extra work
|
// If the moving piece is a pawn do some special extra work
|
||||||
|
@ -907,7 +907,7 @@ void Position::undo_move(Move m) {
|
||||||
PieceType pt = type_of(piece_on(to));
|
PieceType pt = type_of(piece_on(to));
|
||||||
PieceType captured = st->capturedType;
|
PieceType captured = st->capturedType;
|
||||||
|
|
||||||
assert(empty(from) || type_of(m) == CASTLE);
|
assert(empty(from) || type_of(m) == CASTLING);
|
||||||
assert(captured != KING);
|
assert(captured != KING);
|
||||||
|
|
||||||
if (type_of(m) == PROMOTION)
|
if (type_of(m) == PROMOTION)
|
||||||
|
@ -923,15 +923,15 @@ void Position::undo_move(Move m) {
|
||||||
pt = PAWN;
|
pt = PAWN;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (type_of(m) == CASTLE)
|
if (type_of(m) == CASTLING)
|
||||||
{
|
{
|
||||||
bool kingSide = to > from;
|
bool kingSide = to > from;
|
||||||
Square rfrom = to; // Castle is encoded as "king captures friendly rook"
|
Square rfrom = to; // Castling is encoded as "king captures friendly rook"
|
||||||
Square rto = relative_square(us, kingSide ? SQ_F1 : SQ_D1);
|
Square rto = relative_square(us, kingSide ? SQ_F1 : SQ_D1);
|
||||||
to = relative_square(us, kingSide ? SQ_G1 : SQ_C1);
|
to = relative_square(us, kingSide ? SQ_G1 : SQ_C1);
|
||||||
captured = NO_PIECE_TYPE;
|
captured = NO_PIECE_TYPE;
|
||||||
pt = KING;
|
pt = KING;
|
||||||
do_castle(to, from, rto, rfrom);
|
do_castling(to, from, rto, rfrom);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
move_piece(to, from, us, pt); // Put the piece back at the source square
|
move_piece(to, from, us, pt); // Put the piece back at the source square
|
||||||
|
@ -961,10 +961,10 @@ void Position::undo_move(Move m) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Position::do_castle() is a helper used to do/undo a castling move. This
|
/// Position::do_castling() is a helper used to do/undo a castling move. This
|
||||||
/// is a bit tricky, especially in Chess960.
|
/// is a bit tricky, especially in Chess960.
|
||||||
|
|
||||||
void Position::do_castle(Square kfrom, Square kto, Square rfrom, Square rto) {
|
void Position::do_castling(Square kfrom, Square kto, Square rfrom, Square rto) {
|
||||||
|
|
||||||
// Remove both pieces first since squares could overlap in Chess960
|
// Remove both pieces first since squares could overlap in Chess960
|
||||||
remove_piece(kfrom, sideToMove, KING);
|
remove_piece(kfrom, sideToMove, KING);
|
||||||
|
@ -1048,10 +1048,10 @@ int Position::see(Move m, int asymmThreshold) const {
|
||||||
stm = color_of(piece_on(from));
|
stm = color_of(piece_on(from));
|
||||||
occupied = pieces() ^ from;
|
occupied = pieces() ^ from;
|
||||||
|
|
||||||
// Castle moves are implemented as king capturing the rook so cannot be
|
// Castling moves are implemented as king capturing the rook so cannot be
|
||||||
// handled correctly. Simply return 0 that is always the correct value
|
// handled correctly. Simply return 0 that is always the correct value
|
||||||
// unless in the rare case the rook ends up under attack.
|
// unless in the rare case the rook ends up under attack.
|
||||||
if (type_of(m) == CASTLE)
|
if (type_of(m) == CASTLING)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
if (type_of(m) == ENPASSANT)
|
if (type_of(m) == ENPASSANT)
|
||||||
|
@ -1139,7 +1139,7 @@ void Position::clear() {
|
||||||
|
|
||||||
Key Position::compute_key() const {
|
Key Position::compute_key() const {
|
||||||
|
|
||||||
Key k = Zobrist::castle[st->castleRights];
|
Key k = Zobrist::castling[st->castlingFlags];
|
||||||
|
|
||||||
for (Bitboard b = pieces(); b; )
|
for (Bitboard b = pieces(); b; )
|
||||||
{
|
{
|
||||||
|
@ -1326,7 +1326,7 @@ bool Position::pos_is_ok(int* failedStep) const {
|
||||||
const bool debugNonPawnMaterial = all || false;
|
const bool debugNonPawnMaterial = all || false;
|
||||||
const bool debugPieceCounts = all || false;
|
const bool debugPieceCounts = all || false;
|
||||||
const bool debugPieceList = all || false;
|
const bool debugPieceList = all || false;
|
||||||
const bool debugCastleSquares = all || false;
|
const bool debugCastlingSquares = all || false;
|
||||||
|
|
||||||
*step = 1;
|
*step = 1;
|
||||||
|
|
||||||
|
@ -1410,18 +1410,18 @@ bool Position::pos_is_ok(int* failedStep) const {
|
||||||
|| index[pieceList[c][pt][i]] != i)
|
|| index[pieceList[c][pt][i]] != i)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if ((*step)++, debugCastleSquares)
|
if ((*step)++, debugCastlingSquares)
|
||||||
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))
|
||||||
{
|
{
|
||||||
CastleRight cr = make_castle_right(c, s);
|
CastlingFlag cf = make_castling_flag(c, s);
|
||||||
|
|
||||||
if (!can_castle(cr))
|
if (!can_castle(cf))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if ( (castleRightsMask[king_square(c)] & cr) != cr
|
if ( (castlingFlagsMask[king_square(c)] & cf) != cf
|
||||||
|| piece_on(castleRookSquare[c][s]) != make_piece(c, ROOK)
|
|| piece_on(castlingRookSquare[c][s]) != make_piece(c, ROOK)
|
||||||
|| castleRightsMask[castleRookSquare[c][s]] != cr)
|
|| castlingFlagsMask[castlingRookSquare[c][s]] != cf)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -51,7 +51,7 @@ struct CheckInfo {
|
||||||
struct StateInfo {
|
struct StateInfo {
|
||||||
Key pawnKey, materialKey;
|
Key pawnKey, materialKey;
|
||||||
Value npMaterial[COLOR_NB];
|
Value npMaterial[COLOR_NB];
|
||||||
int castleRights, rule50, pliesFromNull;
|
int castlingFlags, rule50, pliesFromNull;
|
||||||
Score psq;
|
Score psq;
|
||||||
Square epSquare;
|
Square epSquare;
|
||||||
|
|
||||||
|
@ -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(CastleRight f) const;
|
int can_castle(CastlingFlag f) const;
|
||||||
int can_castle(Color c) const;
|
int can_castle(Color c) const;
|
||||||
bool castle_impeded(Color c, CastlingSide s) const;
|
bool castling_impeded(Color c, CastlingSide s) const;
|
||||||
Square castle_rook_square(Color c, CastlingSide s) const;
|
Square castling_rook_square(Color c, CastlingSide s) const;
|
||||||
|
|
||||||
// Checking
|
// Checking
|
||||||
Bitboard checkers() const;
|
Bitboard checkers() const;
|
||||||
|
@ -170,10 +170,10 @@ public:
|
||||||
private:
|
private:
|
||||||
// Initialization helpers (used while setting up a position)
|
// Initialization helpers (used while setting up a position)
|
||||||
void clear();
|
void clear();
|
||||||
void set_castle_right(Color c, Square rfrom);
|
void set_castling_flag(Color c, Square rfrom);
|
||||||
|
|
||||||
// Helper functions
|
// Helper functions
|
||||||
void do_castle(Square kfrom, Square kto, Square rfrom, Square rto);
|
void do_castling(Square kfrom, Square kto, Square rfrom, Square rto);
|
||||||
Bitboard hidden_checkers(Square ksq, Color c, Color toMove) const;
|
Bitboard hidden_checkers(Square ksq, Color c, Color toMove) const;
|
||||||
void put_piece(Square s, Color c, PieceType pt);
|
void put_piece(Square s, Color c, PieceType pt);
|
||||||
void remove_piece(Square s, Color c, PieceType pt);
|
void remove_piece(Square s, Color c, PieceType pt);
|
||||||
|
@ -197,9 +197,9 @@ private:
|
||||||
int index[SQUARE_NB];
|
int index[SQUARE_NB];
|
||||||
|
|
||||||
// Other info
|
// Other info
|
||||||
int castleRightsMask[SQUARE_NB];
|
int castlingFlagsMask[SQUARE_NB];
|
||||||
Square castleRookSquare[COLOR_NB][CASTLING_SIDE_NB];
|
Square castlingRookSquare[COLOR_NB][CASTLING_SIDE_NB];
|
||||||
Bitboard castlePath[COLOR_NB][CASTLING_SIDE_NB];
|
Bitboard castlingPath[COLOR_NB][CASTLING_SIDE_NB];
|
||||||
StateInfo startState;
|
StateInfo startState;
|
||||||
int64_t nodes;
|
int64_t nodes;
|
||||||
int gamePly;
|
int gamePly;
|
||||||
|
@ -273,20 +273,20 @@ inline Square Position::king_square(Color c) const {
|
||||||
return pieceList[c][KING][0];
|
return pieceList[c][KING][0];
|
||||||
}
|
}
|
||||||
|
|
||||||
inline int Position::can_castle(CastleRight f) const {
|
inline int Position::can_castle(CastlingFlag f) const {
|
||||||
return st->castleRights & f;
|
return st->castlingFlags & f;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline int Position::can_castle(Color c) const {
|
inline int Position::can_castle(Color c) const {
|
||||||
return st->castleRights & ((WHITE_OO | WHITE_OOO) << (2 * c));
|
return st->castlingFlags & ((WHITE_OO | WHITE_OOO) << (2 * c));
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool Position::castle_impeded(Color c, CastlingSide s) const {
|
inline bool Position::castling_impeded(Color c, CastlingSide s) const {
|
||||||
return byTypeBB[ALL_PIECES] & castlePath[c][s];
|
return byTypeBB[ALL_PIECES] & castlingPath[c][s];
|
||||||
}
|
}
|
||||||
|
|
||||||
inline Square Position::castle_rook_square(Color c, CastlingSide s) const {
|
inline Square Position::castling_rook_square(Color c, CastlingSide s) const {
|
||||||
return castleRookSquare[c][s];
|
return castlingRookSquare[c][s];
|
||||||
}
|
}
|
||||||
|
|
||||||
template<PieceType Pt>
|
template<PieceType Pt>
|
||||||
|
@ -380,14 +380,14 @@ inline bool Position::is_chess960() const {
|
||||||
inline bool Position::capture_or_promotion(Move m) const {
|
inline bool Position::capture_or_promotion(Move m) const {
|
||||||
|
|
||||||
assert(is_ok(m));
|
assert(is_ok(m));
|
||||||
return type_of(m) ? type_of(m) != CASTLE : !empty(to_sq(m));
|
return type_of(m) ? type_of(m) != CASTLING : !empty(to_sq(m));
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool Position::capture(Move m) const {
|
inline bool Position::capture(Move m) const {
|
||||||
|
|
||||||
// Note that castle is coded as "king captures the rook"
|
// Note that castling is encoded as "king captures the rook"
|
||||||
assert(is_ok(m));
|
assert(is_ok(m));
|
||||||
return (!empty(to_sq(m)) && type_of(m) != CASTLE) || type_of(m) == ENPASSANT;
|
return (!empty(to_sq(m)) && type_of(m) != CASTLING) || type_of(m) == ENPASSANT;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline PieceType Position::captured_piece_type() const {
|
inline PieceType Position::captured_piece_type() const {
|
||||||
|
|
|
@ -807,7 +807,7 @@ moves_loop: // When in check and at SpNode search starts from here
|
||||||
givesCheck = pos.gives_check(move, ci);
|
givesCheck = pos.gives_check(move, ci);
|
||||||
dangerous = givesCheck
|
dangerous = givesCheck
|
||||||
|| pos.passed_pawn_push(move)
|
|| pos.passed_pawn_push(move)
|
||||||
|| type_of(move) == CASTLE;
|
|| type_of(move) == CASTLING;
|
||||||
|
|
||||||
// Step 12. Extend checks
|
// Step 12. Extend checks
|
||||||
if (givesCheck && pos.see_sign(move) >= 0)
|
if (givesCheck && pos.see_sign(move) >= 0)
|
||||||
|
@ -1332,7 +1332,7 @@ moves_loop: // When in check and at SpNode search starts from here
|
||||||
assert(is_ok(first));
|
assert(is_ok(first));
|
||||||
assert(is_ok(second));
|
assert(is_ok(second));
|
||||||
assert(color_of(pos.piece_on(from_sq(second))) == ~pos.side_to_move());
|
assert(color_of(pos.piece_on(from_sq(second))) == ~pos.side_to_move());
|
||||||
assert(type_of(first) == CASTLE || color_of(pos.piece_on(to_sq(first))) == ~pos.side_to_move());
|
assert(type_of(first) == CASTLING || color_of(pos.piece_on(to_sq(first))) == ~pos.side_to_move());
|
||||||
|
|
||||||
Square m1from = from_sq(first);
|
Square m1from = from_sq(first);
|
||||||
Square m2from = from_sq(second);
|
Square m2from = from_sq(second);
|
||||||
|
|
16
src/types.h
16
src/types.h
|
@ -97,7 +97,7 @@ const int MAX_PLY_PLUS_6 = MAX_PLY + 6;
|
||||||
/// bit 0- 5: destination square (from 0 to 63)
|
/// bit 0- 5: destination square (from 0 to 63)
|
||||||
/// bit 6-11: origin square (from 0 to 63)
|
/// bit 6-11: origin square (from 0 to 63)
|
||||||
/// bit 12-13: promotion piece type - 2 (from KNIGHT-2 to QUEEN-2)
|
/// bit 12-13: promotion piece type - 2 (from KNIGHT-2 to QUEEN-2)
|
||||||
/// bit 14-15: special move flag: promotion (1), en passant (2), castle (3)
|
/// bit 14-15: special move flag: promotion (1), en passant (2), castling (3)
|
||||||
///
|
///
|
||||||
/// Special cases are MOVE_NONE and MOVE_NULL. We can sneak these in because in
|
/// Special cases are MOVE_NONE and MOVE_NULL. We can sneak these in because in
|
||||||
/// any normal move destination square is always different from origin square
|
/// any normal move destination square is always different from origin square
|
||||||
|
@ -112,17 +112,17 @@ enum MoveType {
|
||||||
NORMAL,
|
NORMAL,
|
||||||
PROMOTION = 1 << 14,
|
PROMOTION = 1 << 14,
|
||||||
ENPASSANT = 2 << 14,
|
ENPASSANT = 2 << 14,
|
||||||
CASTLE = 3 << 14
|
CASTLING = 3 << 14
|
||||||
};
|
};
|
||||||
|
|
||||||
enum CastleRight { // Defined as in PolyGlot book hash key
|
enum CastlingFlag { // Defined as in PolyGlot book hash key
|
||||||
CASTLES_NONE,
|
NO_CASTLING,
|
||||||
WHITE_OO,
|
WHITE_OO,
|
||||||
WHITE_OOO = WHITE_OO << 1,
|
WHITE_OOO = WHITE_OO << 1,
|
||||||
BLACK_OO = WHITE_OO << 2,
|
BLACK_OO = WHITE_OO << 2,
|
||||||
BLACK_OOO = WHITE_OO << 3,
|
BLACK_OOO = WHITE_OO << 3,
|
||||||
ALL_CASTLES = WHITE_OO | WHITE_OOO | BLACK_OO | BLACK_OOO,
|
ANY_CASTLING = WHITE_OO | WHITE_OOO | BLACK_OO | BLACK_OOO,
|
||||||
CASTLE_RIGHT_NB = 16
|
CASTLING_FLAG_NB = 16
|
||||||
};
|
};
|
||||||
|
|
||||||
enum CastlingSide {
|
enum CastlingSide {
|
||||||
|
@ -346,8 +346,8 @@ inline Piece make_piece(Color c, PieceType pt) {
|
||||||
return Piece((c << 3) | pt);
|
return Piece((c << 3) | pt);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline CastleRight make_castle_right(Color c, CastlingSide s) {
|
inline CastlingFlag make_castling_flag(Color c, CastlingSide s) {
|
||||||
return CastleRight(WHITE_OO << ((s == QUEEN_SIDE) + 2 * c));
|
return CastlingFlag(WHITE_OO << ((s == QUEEN_SIDE) + 2 * c));
|
||||||
}
|
}
|
||||||
|
|
||||||
inline PieceType type_of(Piece p) {
|
inline PieceType type_of(Piece p) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue