mirror of
https://github.com/sockspls/badfish
synced 2025-05-01 17:19:36 +00:00
Add generation of castling checks
When we generate checks one case is missing: generation of castling moves that give check to the opponent king. This is a very rare case but anyway it is a case and we can do this without slowing down the common case of no castling checks. So this is the patch. Signed-off-by: Marco Costalba <mcostalba@gmail.com>
This commit is contained in:
parent
cc76951483
commit
03211296f1
1 changed files with 80 additions and 52 deletions
|
@ -35,8 +35,14 @@
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
// Function
|
enum CastlingSide {
|
||||||
MoveStack* generate_castle_moves(const Position&, MoveStack*);
|
KING_SIDE = 1,
|
||||||
|
QUEEN_SIDE = 2
|
||||||
|
};
|
||||||
|
|
||||||
|
// Functions
|
||||||
|
MoveStack* generate_castle_moves(const Position&, MoveStack*, unsigned = (KING_SIDE | QUEEN_SIDE));
|
||||||
|
bool castling_is_check(const Position& pos, CastlingSide side);
|
||||||
|
|
||||||
// Template generate_pawn_captures() with specializations
|
// Template generate_pawn_captures() with specializations
|
||||||
template<Color, Color, Bitboard, SquareDelta, SquareDelta, SquareDelta>
|
template<Color, Color, Bitboard, SquareDelta, SquareDelta, SquareDelta>
|
||||||
|
@ -165,8 +171,7 @@ int generate_noncaptures(const Position& pos, MoveStack* mlist) {
|
||||||
|
|
||||||
|
|
||||||
/// generate_checks() generates all pseudo-legal non-capturing, non-promoting
|
/// generate_checks() generates all pseudo-legal non-capturing, non-promoting
|
||||||
/// checks, except castling moves (will add this later). It returns the
|
/// checks. It returns the number of generated moves.
|
||||||
/// number of generated moves.
|
|
||||||
|
|
||||||
int generate_checks(const Position& pos, MoveStack* mlist, Bitboard dc) {
|
int generate_checks(const Position& pos, MoveStack* mlist, Bitboard dc) {
|
||||||
|
|
||||||
|
@ -207,7 +212,16 @@ int generate_checks(const Position& pos, MoveStack* mlist, Bitboard dc) {
|
||||||
// Hopefully we always have a king ;-)
|
// Hopefully we always have a king ;-)
|
||||||
mlist = generate_piece_checks_king(pos, pos.king_square(us), dc, ksq, mlist);
|
mlist = generate_piece_checks_king(pos, pos.king_square(us), dc, ksq, mlist);
|
||||||
|
|
||||||
// TODO: Castling moves!
|
// Castling moves that give check. Very rare but nice to have!
|
||||||
|
if ( pos.can_castle_queenside(us)
|
||||||
|
&& (square_rank(ksq) == square_rank(pos.king_square(us)) || square_file(ksq) == FILE_D)
|
||||||
|
&& castling_is_check(pos, QUEEN_SIDE))
|
||||||
|
mlist = generate_castle_moves(pos, mlist, QUEEN_SIDE);
|
||||||
|
|
||||||
|
if ( pos.can_castle_kingside(us)
|
||||||
|
&& (square_rank(ksq) == square_rank(pos.king_square(us)) || square_file(ksq) == FILE_F)
|
||||||
|
&& castling_is_check(pos, KING_SIDE))
|
||||||
|
mlist = generate_castle_moves(pos, mlist, KING_SIDE);
|
||||||
|
|
||||||
return int(mlist - mlist_start);
|
return int(mlist - mlist_start);
|
||||||
}
|
}
|
||||||
|
@ -879,20 +893,17 @@ namespace {
|
||||||
return mlist;
|
return mlist;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
MoveStack* generate_castle_moves(const Position& pos, MoveStack* mlist, unsigned side) {
|
||||||
MoveStack* generate_castle_moves(const Position& pos, MoveStack* mlist) {
|
|
||||||
|
|
||||||
Color us = pos.side_to_move();
|
Color us = pos.side_to_move();
|
||||||
|
|
||||||
if (pos.can_castle(us))
|
if (pos.can_castle_kingside(us) && (side & KING_SIDE))
|
||||||
{
|
{
|
||||||
Color them = opposite_color(us);
|
Color them = opposite_color(us);
|
||||||
Square ksq = pos.king_square(us);
|
Square ksq = pos.king_square(us);
|
||||||
|
|
||||||
assert(pos.piece_on(ksq) == king_of_color(us));
|
assert(pos.piece_on(ksq) == king_of_color(us));
|
||||||
|
|
||||||
if (pos.can_castle_kingside(us))
|
|
||||||
{
|
|
||||||
Square rsq = pos.initial_kr_square(us);
|
Square rsq = pos.initial_kr_square(us);
|
||||||
Square g1 = relative_square(us, SQ_G1);
|
Square g1 = relative_square(us, SQ_G1);
|
||||||
Square f1 = relative_square(us, SQ_F1);
|
Square f1 = relative_square(us, SQ_F1);
|
||||||
|
@ -914,8 +925,13 @@ namespace {
|
||||||
(*mlist++).move = make_castle_move(ksq, rsq);
|
(*mlist++).move = make_castle_move(ksq, rsq);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pos.can_castle_queenside(us))
|
if (pos.can_castle_queenside(us) && (side & QUEEN_SIDE))
|
||||||
{
|
{
|
||||||
|
Color them = opposite_color(us);
|
||||||
|
Square ksq = pos.king_square(us);
|
||||||
|
|
||||||
|
assert(pos.piece_on(ksq) == king_of_color(us));
|
||||||
|
|
||||||
Square rsq = pos.initial_qr_square(us);
|
Square rsq = pos.initial_qr_square(us);
|
||||||
Square c1 = relative_square(us, SQ_C1);
|
Square c1 = relative_square(us, SQ_C1);
|
||||||
Square d1 = relative_square(us, SQ_D1);
|
Square d1 = relative_square(us, SQ_D1);
|
||||||
|
@ -941,8 +957,20 @@ namespace {
|
||||||
if (!illegal)
|
if (!illegal)
|
||||||
(*mlist++).move = make_castle_move(ksq, rsq);
|
(*mlist++).move = make_castle_move(ksq, rsq);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return mlist;
|
return mlist;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool castling_is_check(const Position& pos, CastlingSide side) {
|
||||||
|
|
||||||
|
// After castling opponent king is attacked by the castled rook?
|
||||||
|
File rookFile = (side == QUEEN_SIDE ? FILE_D : FILE_F);
|
||||||
|
Color us = pos.side_to_move();
|
||||||
|
Square ksq = pos.king_square(us);
|
||||||
|
Bitboard occ = pos.occupied_squares(), oppKingBB = EmptyBoardBB;
|
||||||
|
|
||||||
|
set_bit(&oppKingBB, pos.king_square(opposite_color(us)));
|
||||||
|
clear_bit(&occ, ksq); // Remove our king from the board
|
||||||
|
Square rsq = make_square(rookFile, square_rank(ksq));
|
||||||
|
return (rook_attacks_bb(rsq, occ) & oppKingBB);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue