mirror of
https://github.com/sockspls/badfish
synced 2025-04-30 16:53:09 +00:00
Fix crash under Chess 960
We have a crash with this position: rkqbnnbr/pppppppp/8/8/8/8/PPPPPPPP/RKQBNNBR w HAha - What happens is that even if we are castling QUEEN_SIDE, in this case we have kfrom (B8) < kto (C8) so the loop that checks for attackers runs forever leading to a crash. The fix is to check for (kto > kfrom) instead of Side == KING_SIDE, but this is slower in the normal case of ortodhox chess, so rewrite generate_castle() to handle the chess960 case as a template parameter and allow the compiler to optimize out the comparison in case of normal chess. Reported by Ray Banks.
This commit is contained in:
parent
9ce7469846
commit
22a5f91aa7
1 changed files with 17 additions and 7 deletions
|
@ -31,7 +31,7 @@
|
||||||
(*mlist++).move = make_move(to - (d), to); }
|
(*mlist++).move = make_move(to - (d), to); }
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
template<CastlingSide Side, bool Checks>
|
template<CastlingSide Side, bool Checks, bool Chess960>
|
||||||
MoveStack* generate_castle(const Position& pos, MoveStack* mlist, Color us) {
|
MoveStack* generate_castle(const Position& pos, MoveStack* mlist, Color us) {
|
||||||
|
|
||||||
if (pos.castle_impeded(us, Side) || !pos.can_castle(make_castle_right(us, Side)))
|
if (pos.castle_impeded(us, Side) || !pos.can_castle(make_castle_right(us, Side)))
|
||||||
|
@ -46,15 +46,17 @@ namespace {
|
||||||
|
|
||||||
assert(!pos.in_check());
|
assert(!pos.in_check());
|
||||||
|
|
||||||
for (Square s = kto; s != kfrom; s += (Square)(Side == KING_SIDE ? -1 : 1))
|
const int K = Chess960 ? kto > kfrom ? -1 : 1
|
||||||
|
: Side == KING_SIDE ? -1 : 1;
|
||||||
|
|
||||||
|
for (Square s = kto; s != kfrom; s += (Square)K)
|
||||||
if (pos.attackers_to(s) & enemies)
|
if (pos.attackers_to(s) & enemies)
|
||||||
return mlist;
|
return mlist;
|
||||||
|
|
||||||
// Because we generate only legal castling moves we need to verify that
|
// Because we generate only legal castling moves we need to verify that
|
||||||
// when moving the castling rook we do not discover some hidden checker.
|
// when moving the castling rook we do not discover some hidden checker.
|
||||||
// For instance an enemy queen in SQ_A1 when castling rook is in SQ_B1.
|
// For instance an enemy queen in SQ_A1 when castling rook is in SQ_B1.
|
||||||
if ( pos.is_chess960()
|
if (Chess960 && (pos.attackers_to(kto, pos.pieces() ^ rfrom) & enemies))
|
||||||
&& (pos.attackers_to(kto, pos.pieces() ^ rfrom) & enemies))
|
|
||||||
return mlist;
|
return mlist;
|
||||||
|
|
||||||
(*mlist++).move = make<CASTLE>(kfrom, rfrom);
|
(*mlist++).move = make<CASTLE>(kfrom, rfrom);
|
||||||
|
@ -273,8 +275,16 @@ namespace {
|
||||||
|
|
||||||
if (Type != CAPTURES && Type != EVASIONS && pos.can_castle(us))
|
if (Type != CAPTURES && Type != EVASIONS && pos.can_castle(us))
|
||||||
{
|
{
|
||||||
mlist = generate_castle<KING_SIDE, Type == QUIET_CHECKS>(pos, mlist, us);
|
if (pos.is_chess960())
|
||||||
mlist = generate_castle<QUEEN_SIDE, Type == QUIET_CHECKS>(pos, mlist, us);
|
{
|
||||||
|
mlist = generate_castle<KING_SIDE, Type == QUIET_CHECKS, true>(pos, mlist, us);
|
||||||
|
mlist = generate_castle<QUEEN_SIDE, Type == QUIET_CHECKS, true>(pos, mlist, us);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
mlist = generate_castle<KING_SIDE, Type == QUIET_CHECKS, false>(pos, mlist, us);
|
||||||
|
mlist = generate_castle<QUEEN_SIDE, Type == QUIET_CHECKS, false>(pos, mlist, us);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return mlist;
|
return mlist;
|
||||||
|
|
Loading…
Add table
Reference in a new issue