2008-08-31 23:59:13 -06:00
|
|
|
/*
|
2008-10-19 10:56:28 -06:00
|
|
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
2009-05-07 06:54:40 -06:00
|
|
|
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
2011-12-30 05:24:41 -07:00
|
|
|
Copyright (C) 2008-2012 Marco Costalba, Joona Kiiski, Tord Romstad
|
2008-08-31 23:59:13 -06:00
|
|
|
|
2008-10-19 10:56:28 -06:00
|
|
|
Stockfish is free software: you can redistribute it and/or modify
|
2008-08-31 23:59:13 -06:00
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
2008-10-20 13:57:13 -06:00
|
|
|
|
2008-10-19 10:56:28 -06:00
|
|
|
Stockfish is distributed in the hope that it will be useful,
|
2008-08-31 23:59:13 -06:00
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2008-10-20 13:57:13 -06:00
|
|
|
|
2008-08-31 23:59:13 -06:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <cassert>
|
2011-10-30 22:38:44 -06:00
|
|
|
#include <algorithm>
|
2008-08-31 23:59:13 -06:00
|
|
|
|
2009-05-21 10:08:34 -06:00
|
|
|
#include "bitcount.h"
|
2008-08-31 23:59:13 -06:00
|
|
|
#include "movegen.h"
|
2011-07-16 15:59:14 -06:00
|
|
|
#include "position.h"
|
2008-08-31 23:59:13 -06:00
|
|
|
|
2008-10-23 02:42:14 -06:00
|
|
|
// Simple macro to wrap a very common while loop, no facny, no flexibility,
|
|
|
|
// hardcoded list name 'mlist' and from square 'from'.
|
|
|
|
#define SERIALIZE_MOVES(b) while (b) (*mlist++).move = make_move(from, pop_1st_bit(&b))
|
2008-08-31 23:59:13 -06:00
|
|
|
|
2009-07-14 02:07:30 -06:00
|
|
|
// Version used for pawns, where the 'from' square is given as a delta from the 'to' square
|
|
|
|
#define SERIALIZE_MOVES_D(b, d) while (b) { to = pop_1st_bit(&b); (*mlist++).move = make_move(to + (d), to); }
|
|
|
|
|
2008-08-31 23:59:13 -06:00
|
|
|
namespace {
|
2008-10-18 09:37:49 -06:00
|
|
|
|
2009-02-05 07:06:41 -07:00
|
|
|
enum CastlingSide {
|
2009-02-05 08:08:00 -07:00
|
|
|
KING_SIDE,
|
|
|
|
QUEEN_SIDE
|
2009-02-05 07:06:41 -07:00
|
|
|
};
|
|
|
|
|
2011-04-13 03:54:41 -06:00
|
|
|
template<CastlingSide>
|
2011-01-07 07:17:28 -07:00
|
|
|
MoveStack* generate_castle_moves(const Position&, MoveStack*, Color us);
|
2009-02-10 09:31:07 -07:00
|
|
|
|
2011-04-13 03:54:41 -06:00
|
|
|
template<Color, MoveType>
|
2009-11-03 04:29:07 -07:00
|
|
|
MoveStack* generate_pawn_moves(const Position&, MoveStack*, Bitboard, Square);
|
2009-02-10 09:44:55 -07:00
|
|
|
|
2011-04-13 03:54:41 -06:00
|
|
|
template<PieceType Pt>
|
2011-01-05 01:49:58 -07:00
|
|
|
inline MoveStack* generate_discovered_checks(const Position& pos, MoveStack* mlist, Square from) {
|
|
|
|
|
2011-12-04 02:53:40 -07:00
|
|
|
assert(Pt != QUEEN && Pt != PAWN);
|
2011-01-05 01:49:58 -07:00
|
|
|
|
2011-04-13 03:54:41 -06:00
|
|
|
Bitboard b = pos.attacks_from<Pt>(from) & pos.empty_squares();
|
2011-07-15 03:19:49 -06:00
|
|
|
|
2011-04-13 03:54:41 -06:00
|
|
|
if (Pt == KING)
|
2011-10-02 01:33:40 -06:00
|
|
|
b &= ~QueenPseudoAttacks[pos.king_square(flip(pos.side_to_move()))];
|
2011-07-15 03:19:49 -06:00
|
|
|
|
2011-01-05 01:49:58 -07:00
|
|
|
SERIALIZE_MOVES(b);
|
|
|
|
return mlist;
|
|
|
|
}
|
|
|
|
|
2011-04-13 03:54:41 -06:00
|
|
|
template<PieceType Pt>
|
2011-01-05 01:49:58 -07:00
|
|
|
inline MoveStack* generate_direct_checks(const Position& pos, MoveStack* mlist, Color us,
|
|
|
|
Bitboard dc, Square ksq) {
|
2011-12-04 02:53:40 -07:00
|
|
|
assert(Pt != KING && Pt != PAWN);
|
2011-01-05 01:49:58 -07:00
|
|
|
|
|
|
|
Bitboard checkSqs, b;
|
|
|
|
Square from;
|
2011-06-28 08:16:43 -06:00
|
|
|
const Square* pl = pos.piece_list(us, Pt);
|
2011-01-05 01:49:58 -07:00
|
|
|
|
2011-06-28 08:16:43 -06:00
|
|
|
if ((from = *pl++) == SQ_NONE)
|
2011-01-05 01:49:58 -07:00
|
|
|
return mlist;
|
|
|
|
|
2011-04-13 03:54:41 -06:00
|
|
|
checkSqs = pos.attacks_from<Pt>(ksq) & pos.empty_squares();
|
2011-01-05 01:49:58 -07:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2011-04-13 03:54:41 -06:00
|
|
|
if ( (Pt == QUEEN && !(QueenPseudoAttacks[from] & checkSqs))
|
|
|
|
|| (Pt == ROOK && !(RookPseudoAttacks[from] & checkSqs))
|
|
|
|
|| (Pt == BISHOP && !(BishopPseudoAttacks[from] & checkSqs)))
|
2011-01-05 01:49:58 -07:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (dc && bit_is_set(dc, from))
|
|
|
|
continue;
|
|
|
|
|
2011-04-13 03:54:41 -06:00
|
|
|
b = pos.attacks_from<Pt>(from) & checkSqs;
|
2011-01-05 01:49:58 -07:00
|
|
|
SERIALIZE_MOVES(b);
|
|
|
|
|
2011-06-28 08:16:43 -06:00
|
|
|
} while ((from = *pl++) != SQ_NONE);
|
2011-01-05 01:49:58 -07:00
|
|
|
|
|
|
|
return mlist;
|
|
|
|
}
|
2009-02-10 09:44:55 -07:00
|
|
|
|
2008-10-20 16:10:27 -06:00
|
|
|
template<>
|
2011-01-07 08:33:15 -07:00
|
|
|
FORCE_INLINE MoveStack* generate_direct_checks<PAWN>(const Position& p, MoveStack* m, Color us, Bitboard dc, Square ksq) {
|
2011-01-05 01:49:58 -07:00
|
|
|
|
2011-01-05 11:47:01 -07:00
|
|
|
return (us == WHITE ? generate_pawn_moves<WHITE, MV_CHECK>(p, m, dc, ksq)
|
|
|
|
: generate_pawn_moves<BLACK, MV_CHECK>(p, m, dc, ksq));
|
2011-01-05 01:49:58 -07:00
|
|
|
}
|
2008-10-19 05:43:09 -06:00
|
|
|
|
2011-04-13 03:54:41 -06:00
|
|
|
template<PieceType Pt, MoveType Type>
|
2011-01-07 08:33:15 -07:00
|
|
|
FORCE_INLINE MoveStack* generate_piece_moves(const Position& p, MoveStack* m, Color us, Bitboard t) {
|
2009-02-10 09:31:07 -07:00
|
|
|
|
2011-04-13 03:54:41 -06:00
|
|
|
assert(Pt == PAWN);
|
2009-11-03 04:29:07 -07:00
|
|
|
return (us == WHITE ? generate_pawn_moves<WHITE, Type>(p, m, t, SQ_NONE)
|
|
|
|
: generate_pawn_moves<BLACK, Type>(p, m, t, SQ_NONE));
|
2009-02-10 09:31:07 -07:00
|
|
|
}
|
|
|
|
|
2011-04-13 03:54:41 -06:00
|
|
|
template<PieceType Pt>
|
2011-01-07 08:33:15 -07:00
|
|
|
FORCE_INLINE MoveStack* generate_piece_moves(const Position& pos, MoveStack* mlist, Color us, Bitboard target) {
|
2009-11-01 02:40:14 -07:00
|
|
|
|
2011-01-05 01:49:58 -07:00
|
|
|
Bitboard b;
|
|
|
|
Square from;
|
2011-06-28 08:16:43 -06:00
|
|
|
const Square* pl = pos.piece_list(us, Pt);
|
2011-01-05 01:49:58 -07:00
|
|
|
|
2011-06-28 08:16:43 -06:00
|
|
|
if (*pl != SQ_NONE)
|
2011-01-05 01:49:58 -07:00
|
|
|
{
|
|
|
|
do {
|
2011-06-28 08:16:43 -06:00
|
|
|
from = *pl;
|
2011-04-13 03:54:41 -06:00
|
|
|
b = pos.attacks_from<Pt>(from) & target;
|
2011-01-05 01:49:58 -07:00
|
|
|
SERIALIZE_MOVES(b);
|
2011-06-28 08:16:43 -06:00
|
|
|
} while (*++pl != SQ_NONE);
|
2011-01-05 01:49:58 -07:00
|
|
|
}
|
|
|
|
return mlist;
|
|
|
|
}
|
2009-09-23 04:29:10 -06:00
|
|
|
|
|
|
|
template<>
|
2011-01-07 08:33:15 -07:00
|
|
|
FORCE_INLINE MoveStack* generate_piece_moves<KING>(const Position& pos, MoveStack* mlist, Color us, Bitboard target) {
|
2009-09-23 04:29:10 -06:00
|
|
|
|
2011-01-05 01:49:58 -07:00
|
|
|
Bitboard b;
|
|
|
|
Square from = pos.king_square(us);
|
|
|
|
|
|
|
|
b = pos.attacks_from<KING>(from) & target;
|
|
|
|
SERIALIZE_MOVES(b);
|
|
|
|
return mlist;
|
2009-09-23 04:29:10 -06:00
|
|
|
}
|
2011-01-05 01:49:58 -07:00
|
|
|
|
2008-08-31 23:59:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-05 11:47:01 -07:00
|
|
|
/// generate<MV_CAPTURE> generates all pseudo-legal captures and queen
|
2009-09-20 01:43:25 -06:00
|
|
|
/// promotions. Returns a pointer to the end of the move list.
|
2011-01-05 11:47:01 -07:00
|
|
|
///
|
|
|
|
/// generate<MV_NON_CAPTURE> generates all pseudo-legal non-captures and
|
2009-09-20 01:43:25 -06:00
|
|
|
/// underpromotions. Returns a pointer to the end of the move list.
|
2011-01-05 11:47:01 -07:00
|
|
|
///
|
|
|
|
/// generate<MV_NON_EVASION> generates all pseudo-legal captures and
|
2010-08-06 05:01:21 -06:00
|
|
|
/// non-captures. Returns a pointer to the end of the move list.
|
2011-01-05 11:47:01 -07:00
|
|
|
|
2011-01-07 08:33:15 -07:00
|
|
|
template<MoveType Type>
|
2011-01-05 11:47:01 -07:00
|
|
|
MoveStack* generate(const Position& pos, MoveStack* mlist) {
|
2010-08-06 05:01:21 -06:00
|
|
|
|
2012-01-03 07:16:01 -07:00
|
|
|
assert(Type == MV_CAPTURE || Type == MV_NON_CAPTURE || Type == MV_NON_EVASION);
|
2011-04-29 08:26:48 -06:00
|
|
|
assert(!pos.in_check());
|
2010-08-06 05:01:21 -06:00
|
|
|
|
|
|
|
Color us = pos.side_to_move();
|
2011-01-05 11:47:01 -07:00
|
|
|
Bitboard target;
|
2010-08-06 05:01:21 -06:00
|
|
|
|
2012-01-03 07:16:01 -07:00
|
|
|
if (Type == MV_CAPTURE)
|
2011-10-02 01:33:40 -06:00
|
|
|
target = pos.pieces(flip(us));
|
2012-01-03 07:16:01 -07:00
|
|
|
|
2011-01-07 08:33:15 -07:00
|
|
|
else if (Type == MV_NON_CAPTURE)
|
2011-01-05 11:47:01 -07:00
|
|
|
target = pos.empty_squares();
|
2010-08-06 05:01:21 -06:00
|
|
|
|
2012-01-03 07:16:01 -07:00
|
|
|
else if (Type == MV_NON_EVASION)
|
|
|
|
target = pos.pieces(flip(us)) | pos.empty_squares();
|
2010-08-06 05:01:21 -06:00
|
|
|
|
2012-01-03 07:16:01 -07:00
|
|
|
mlist = generate_piece_moves<PAWN, Type>(pos, mlist, us, target);
|
2010-08-06 05:01:21 -06:00
|
|
|
mlist = generate_piece_moves<KNIGHT>(pos, mlist, us, target);
|
|
|
|
mlist = generate_piece_moves<BISHOP>(pos, mlist, us, target);
|
|
|
|
mlist = generate_piece_moves<ROOK>(pos, mlist, us, target);
|
|
|
|
mlist = generate_piece_moves<QUEEN>(pos, mlist, us, target);
|
|
|
|
mlist = generate_piece_moves<KING>(pos, mlist, us, target);
|
2011-01-05 11:47:01 -07:00
|
|
|
|
2011-06-27 09:06:15 -06:00
|
|
|
if (Type != MV_CAPTURE && pos.can_castle(us))
|
2011-01-05 11:47:01 -07:00
|
|
|
{
|
2011-06-27 09:06:15 -06:00
|
|
|
if (pos.can_castle(us == WHITE ? WHITE_OO : BLACK_OO))
|
2011-01-07 07:17:28 -07:00
|
|
|
mlist = generate_castle_moves<KING_SIDE>(pos, mlist, us);
|
|
|
|
|
2011-06-27 09:06:15 -06:00
|
|
|
if (pos.can_castle(us == WHITE ? WHITE_OOO : BLACK_OOO))
|
2011-01-07 07:17:28 -07:00
|
|
|
mlist = generate_castle_moves<QUEEN_SIDE>(pos, mlist, us);
|
2011-01-05 11:47:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return mlist;
|
2010-08-06 05:01:21 -06:00
|
|
|
}
|
|
|
|
|
2011-04-13 03:54:41 -06:00
|
|
|
// Explicit template instantiations
|
2011-01-05 11:47:01 -07:00
|
|
|
template MoveStack* generate<MV_CAPTURE>(const Position& pos, MoveStack* mlist);
|
|
|
|
template MoveStack* generate<MV_NON_CAPTURE>(const Position& pos, MoveStack* mlist);
|
|
|
|
template MoveStack* generate<MV_NON_EVASION>(const Position& pos, MoveStack* mlist);
|
|
|
|
|
2010-08-06 05:01:21 -06:00
|
|
|
|
2011-12-04 02:53:40 -07:00
|
|
|
/// generate<MV_NON_CAPTURE_CHECK> generates all pseudo-legal non-captures and knight
|
2009-09-21 11:07:11 -06:00
|
|
|
/// underpromotions that give check. Returns a pointer to the end of the move list.
|
2011-01-05 11:00:30 -07:00
|
|
|
template<>
|
2011-01-05 11:47:01 -07:00
|
|
|
MoveStack* generate<MV_NON_CAPTURE_CHECK>(const Position& pos, MoveStack* mlist) {
|
2008-10-17 07:11:19 -06:00
|
|
|
|
2011-04-29 08:26:48 -06:00
|
|
|
assert(!pos.in_check());
|
2008-10-17 07:11:19 -06:00
|
|
|
|
2009-11-06 06:33:34 -07:00
|
|
|
Bitboard b, dc;
|
|
|
|
Square from;
|
2008-10-18 07:36:58 -06:00
|
|
|
Color us = pos.side_to_move();
|
2011-10-02 01:33:40 -06:00
|
|
|
Square ksq = pos.king_square(flip(us));
|
2008-08-31 23:59:13 -06:00
|
|
|
|
2011-10-02 01:33:40 -06:00
|
|
|
assert(pos.piece_on(ksq) == make_piece(flip(us), KING));
|
2008-08-31 23:59:13 -06:00
|
|
|
|
2009-11-01 02:40:14 -07:00
|
|
|
// Discovered non-capture checks
|
2011-07-16 02:56:34 -06:00
|
|
|
b = dc = pos.discovered_check_candidates();
|
2009-11-06 06:33:34 -07:00
|
|
|
|
2009-11-01 02:40:14 -07:00
|
|
|
while (b)
|
|
|
|
{
|
2009-11-06 06:33:34 -07:00
|
|
|
from = pop_1st_bit(&b);
|
2011-10-02 01:33:40 -06:00
|
|
|
switch (type_of(pos.piece_on(from)))
|
2009-11-01 02:40:14 -07:00
|
|
|
{
|
|
|
|
case PAWN: /* Will be generated togheter with pawns direct checks */ break;
|
2009-11-03 04:29:07 -07:00
|
|
|
case KNIGHT: mlist = generate_discovered_checks<KNIGHT>(pos, mlist, from); break;
|
|
|
|
case BISHOP: mlist = generate_discovered_checks<BISHOP>(pos, mlist, from); break;
|
|
|
|
case ROOK: mlist = generate_discovered_checks<ROOK>(pos, mlist, from); break;
|
|
|
|
case KING: mlist = generate_discovered_checks<KING>(pos, mlist, from); break;
|
2009-11-01 02:40:14 -07:00
|
|
|
default: assert(false); break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Direct non-capture checks
|
|
|
|
mlist = generate_direct_checks<PAWN>(pos, mlist, us, dc, ksq);
|
|
|
|
mlist = generate_direct_checks<KNIGHT>(pos, mlist, us, dc, ksq);
|
|
|
|
mlist = generate_direct_checks<BISHOP>(pos, mlist, us, dc, ksq);
|
|
|
|
mlist = generate_direct_checks<ROOK>(pos, mlist, us, dc, ksq);
|
2009-11-01 09:19:04 -07:00
|
|
|
return generate_direct_checks<QUEEN>(pos, mlist, us, dc, ksq);
|
2008-08-31 23:59:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-12-04 02:53:40 -07:00
|
|
|
/// generate<MV_EVASION> generates all pseudo-legal check evasions when the side
|
|
|
|
/// to move is in check. Returns a pointer to the end of the move list.
|
2011-01-05 11:00:30 -07:00
|
|
|
template<>
|
2011-01-05 11:47:01 -07:00
|
|
|
MoveStack* generate<MV_EVASION>(const Position& pos, MoveStack* mlist) {
|
2008-10-17 07:11:19 -06:00
|
|
|
|
2011-04-29 08:26:48 -06:00
|
|
|
assert(pos.in_check());
|
2008-10-17 07:11:19 -06:00
|
|
|
|
2009-11-06 06:33:34 -07:00
|
|
|
Bitboard b, target;
|
2009-11-03 04:29:07 -07:00
|
|
|
Square from, checksq;
|
2009-11-02 10:27:26 -07:00
|
|
|
int checkersCnt = 0;
|
2008-10-19 01:27:24 -06:00
|
|
|
Color us = pos.side_to_move();
|
|
|
|
Square ksq = pos.king_square(us);
|
2009-09-21 00:55:26 -06:00
|
|
|
Bitboard checkers = pos.checkers();
|
2011-12-03 03:56:11 -07:00
|
|
|
Bitboard sliderAttacks = 0;
|
2008-08-31 23:59:13 -06:00
|
|
|
|
2011-02-23 06:25:46 -07:00
|
|
|
assert(pos.piece_on(ksq) == make_piece(us, KING));
|
2009-11-02 10:27:26 -07:00
|
|
|
assert(checkers);
|
2008-08-31 23:59:13 -06:00
|
|
|
|
2009-09-21 11:07:11 -06:00
|
|
|
// Find squares attacked by slider checkers, we will remove
|
2009-11-02 10:27:26 -07:00
|
|
|
// them from the king evasions set so to early skip known
|
|
|
|
// illegal moves and avoid an useless legality check later.
|
|
|
|
b = checkers;
|
|
|
|
do
|
2009-02-07 05:11:52 -07:00
|
|
|
{
|
2009-11-02 10:27:26 -07:00
|
|
|
checkersCnt++;
|
|
|
|
checksq = pop_1st_bit(&b);
|
2009-02-07 05:11:52 -07:00
|
|
|
|
2011-10-02 01:33:40 -06:00
|
|
|
assert(color_of(pos.piece_on(checksq)) == flip(us));
|
2009-11-02 10:27:26 -07:00
|
|
|
|
2011-10-02 01:33:40 -06:00
|
|
|
switch (type_of(pos.piece_on(checksq)))
|
2009-11-02 10:27:26 -07:00
|
|
|
{
|
|
|
|
case BISHOP: sliderAttacks |= BishopPseudoAttacks[checksq]; break;
|
|
|
|
case ROOK: sliderAttacks |= RookPseudoAttacks[checksq]; break;
|
|
|
|
case QUEEN:
|
2011-06-02 05:14:44 -06:00
|
|
|
// If queen and king are far we can safely remove all the squares attacked
|
|
|
|
// in the other direction becuase are not reachable by the king anyway.
|
|
|
|
if (squares_between(ksq, checksq) || (RookPseudoAttacks[checksq] & (1ULL << ksq)))
|
|
|
|
sliderAttacks |= QueenPseudoAttacks[checksq];
|
|
|
|
|
|
|
|
// Otherwise, if king and queen are adjacent and on a diagonal line, we need to
|
|
|
|
// use real rook attacks to check if king is safe to move in the other direction.
|
|
|
|
// For example: king in B2, queen in A1 a knight in B1, and we can safely move to C1.
|
2009-11-02 10:27:26 -07:00
|
|
|
else
|
|
|
|
sliderAttacks |= BishopPseudoAttacks[checksq] | pos.attacks_from<ROOK>(checksq);
|
2011-06-02 05:14:44 -06:00
|
|
|
|
2009-11-02 10:27:26 -07:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (b);
|
2009-02-07 05:11:52 -07:00
|
|
|
|
2009-09-21 11:07:11 -06:00
|
|
|
// Generate evasions for king, capture and non capture moves
|
2011-06-28 07:36:39 -06:00
|
|
|
b = pos.attacks_from<KING>(ksq) & ~pos.pieces(us) & ~sliderAttacks;
|
2009-11-02 10:27:26 -07:00
|
|
|
from = ksq;
|
|
|
|
SERIALIZE_MOVES(b);
|
2008-08-31 23:59:13 -06:00
|
|
|
|
2009-11-02 10:27:26 -07:00
|
|
|
// Generate evasions for other pieces only if not double check
|
|
|
|
if (checkersCnt > 1)
|
2009-11-02 08:33:54 -07:00
|
|
|
return mlist;
|
2008-08-31 23:59:13 -06:00
|
|
|
|
2009-11-03 04:29:07 -07:00
|
|
|
// Find squares where a blocking evasion or a capture of the
|
|
|
|
// checker piece is possible.
|
2009-11-06 06:33:34 -07:00
|
|
|
target = squares_between(checksq, ksq) | checkers;
|
2009-11-02 08:33:54 -07:00
|
|
|
|
2011-01-05 11:47:01 -07:00
|
|
|
mlist = generate_piece_moves<PAWN, MV_EVASION>(pos, mlist, us, target);
|
2009-11-02 10:27:26 -07:00
|
|
|
mlist = generate_piece_moves<KNIGHT>(pos, mlist, us, target);
|
|
|
|
mlist = generate_piece_moves<BISHOP>(pos, mlist, us, target);
|
|
|
|
mlist = generate_piece_moves<ROOK>(pos, mlist, us, target);
|
2009-11-03 04:29:07 -07:00
|
|
|
return generate_piece_moves<QUEEN>(pos, mlist, us, target);
|
2008-08-31 23:59:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-24 01:07:20 -06:00
|
|
|
/// generate<MV_LEGAL> computes a complete list of legal moves in the current position
|
2011-01-05 11:47:01 -07:00
|
|
|
|
|
|
|
template<>
|
|
|
|
MoveStack* generate<MV_LEGAL>(const Position& pos, MoveStack* mlist) {
|
2008-10-17 07:11:19 -06:00
|
|
|
|
2009-11-06 06:33:34 -07:00
|
|
|
MoveStack *last, *cur = mlist;
|
2011-07-16 02:56:34 -06:00
|
|
|
Bitboard pinned = pos.pinned_pieces();
|
2009-03-02 08:20:00 -07:00
|
|
|
|
2011-05-24 01:07:20 -06:00
|
|
|
last = pos.in_check() ? generate<MV_EVASION>(pos, mlist)
|
|
|
|
: generate<MV_NON_EVASION>(pos, mlist);
|
2008-08-31 23:59:13 -06:00
|
|
|
|
2008-10-22 16:18:11 -06:00
|
|
|
// Remove illegal moves from the list
|
2009-11-06 06:33:34 -07:00
|
|
|
while (cur != last)
|
2011-04-13 03:54:41 -06:00
|
|
|
if (!pos.pl_move_is_legal(cur->move, pinned))
|
2009-08-28 00:57:52 -06:00
|
|
|
cur->move = (--last)->move;
|
2011-04-13 03:54:41 -06:00
|
|
|
else
|
|
|
|
cur++;
|
2009-11-06 06:33:34 -07:00
|
|
|
|
2009-08-28 00:57:52 -06:00
|
|
|
return last;
|
2008-08-31 23:59:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2011-02-23 02:03:30 -07:00
|
|
|
template<Square Delta>
|
2009-09-23 04:29:10 -06:00
|
|
|
inline Bitboard move_pawns(Bitboard p) {
|
|
|
|
|
2010-11-15 08:27:05 -07:00
|
|
|
return Delta == DELTA_N ? p << 8 : Delta == DELTA_S ? p >> 8 :
|
|
|
|
Delta == DELTA_NE ? p << 9 : Delta == DELTA_SE ? p >> 7 :
|
|
|
|
Delta == DELTA_NW ? p << 7 : Delta == DELTA_SW ? p >> 9 : p;
|
2009-09-23 04:29:10 -06:00
|
|
|
}
|
|
|
|
|
2012-01-03 07:16:01 -07:00
|
|
|
template<Square Delta>
|
2010-11-15 08:27:05 -07:00
|
|
|
inline MoveStack* generate_pawn_captures(MoveStack* mlist, Bitboard pawns, Bitboard target) {
|
2009-05-07 07:32:28 -06:00
|
|
|
|
2010-11-15 08:27:05 -07:00
|
|
|
const Bitboard TFileABB = (Delta == DELTA_NE || Delta == DELTA_SE ? FileABB : FileHBB);
|
2008-10-18 02:57:27 -06:00
|
|
|
|
2010-11-16 00:51:07 -07:00
|
|
|
Bitboard b;
|
2008-10-23 02:42:14 -06:00
|
|
|
Square to;
|
2008-08-31 23:59:13 -06:00
|
|
|
|
2009-05-07 07:32:28 -06:00
|
|
|
// Captures in the a1-h8 (a8-h1 for black) diagonal or in the h1-a8 (h8-a1 for black)
|
2010-11-16 01:42:12 -07:00
|
|
|
b = move_pawns<Delta>(pawns) & target & ~TFileABB;
|
|
|
|
SERIALIZE_MOVES_D(b, -Delta);
|
2010-11-16 00:51:07 -07:00
|
|
|
return mlist;
|
|
|
|
}
|
|
|
|
|
2011-07-15 03:19:49 -06:00
|
|
|
template<MoveType Type, Square Delta>
|
2010-11-16 01:42:12 -07:00
|
|
|
inline MoveStack* generate_promotions(const Position& pos, MoveStack* mlist, Bitboard pawnsOn7, Bitboard target) {
|
2010-11-16 00:51:07 -07:00
|
|
|
|
|
|
|
const Bitboard TFileABB = (Delta == DELTA_NE || Delta == DELTA_SE ? FileABB : FileHBB);
|
|
|
|
|
|
|
|
Bitboard b;
|
|
|
|
Square to;
|
2008-08-31 23:59:13 -06:00
|
|
|
|
2010-11-16 01:42:12 -07:00
|
|
|
// Promotions and under-promotions, both captures and non-captures
|
|
|
|
b = move_pawns<Delta>(pawnsOn7) & target;
|
2010-11-16 00:51:07 -07:00
|
|
|
|
|
|
|
if (Delta != DELTA_N && Delta != DELTA_S)
|
|
|
|
b &= ~TFileABB;
|
|
|
|
|
|
|
|
while (b)
|
2008-10-18 02:57:27 -06:00
|
|
|
{
|
2010-11-16 00:51:07 -07:00
|
|
|
to = pop_1st_bit(&b);
|
|
|
|
|
2012-01-03 07:16:01 -07:00
|
|
|
if (Type == MV_CAPTURE || Type == MV_EVASION || Type == MV_NON_EVASION)
|
2012-01-01 08:00:00 -07:00
|
|
|
(*mlist++).move = make_promotion(to - Delta, to, QUEEN);
|
2010-11-16 00:51:07 -07:00
|
|
|
|
2012-01-03 07:16:01 -07:00
|
|
|
if (Type == MV_NON_CAPTURE || Type == MV_EVASION || Type == MV_NON_EVASION)
|
2009-07-14 00:53:22 -06:00
|
|
|
{
|
2012-01-01 08:00:00 -07:00
|
|
|
(*mlist++).move = make_promotion(to - Delta, to, ROOK);
|
|
|
|
(*mlist++).move = make_promotion(to - Delta, to, BISHOP);
|
|
|
|
(*mlist++).move = make_promotion(to - Delta, to, KNIGHT);
|
2009-07-14 00:53:22 -06:00
|
|
|
}
|
2008-08-31 23:59:13 -06:00
|
|
|
|
2010-11-16 00:51:07 -07:00
|
|
|
// This is the only possible under promotion that can give a check
|
|
|
|
// not already included in the queen-promotion.
|
2011-01-05 11:47:01 -07:00
|
|
|
if ( Type == MV_CHECK
|
2011-07-15 05:20:31 -06:00
|
|
|
&& bit_is_set(pos.attacks_from<KNIGHT>(to), pos.king_square(Delta > 0 ? BLACK : WHITE)))
|
2012-01-01 08:00:00 -07:00
|
|
|
(*mlist++).move = make_promotion(to - Delta, to, KNIGHT);
|
2010-11-16 01:42:12 -07:00
|
|
|
else (void)pos; // Silence a warning under MSVC
|
2010-11-16 00:51:07 -07:00
|
|
|
}
|
2009-05-07 07:32:28 -06:00
|
|
|
return mlist;
|
|
|
|
}
|
2008-08-31 23:59:13 -06:00
|
|
|
|
2009-09-23 03:47:11 -06:00
|
|
|
template<Color Us, MoveType Type>
|
2009-11-03 04:29:07 -07:00
|
|
|
MoveStack* generate_pawn_moves(const Position& pos, MoveStack* mlist, Bitboard target, Square ksq) {
|
2008-08-31 23:59:13 -06:00
|
|
|
|
2010-11-16 01:42:12 -07:00
|
|
|
// Calculate our parametrized parameters at compile time, named
|
|
|
|
// according to the point of view of white side.
|
2011-02-23 02:03:30 -07:00
|
|
|
const Color Them = (Us == WHITE ? BLACK : WHITE);
|
|
|
|
const Bitboard TRank7BB = (Us == WHITE ? Rank7BB : Rank2BB);
|
|
|
|
const Bitboard TRank3BB = (Us == WHITE ? Rank3BB : Rank6BB);
|
2011-07-15 03:19:49 -06:00
|
|
|
const Square UP = (Us == WHITE ? DELTA_N : DELTA_S);
|
2011-07-15 03:40:09 -06:00
|
|
|
const Square RIGHT_UP = (Us == WHITE ? DELTA_NE : DELTA_SW);
|
|
|
|
const Square LEFT_UP = (Us == WHITE ? DELTA_NW : DELTA_SE);
|
2009-05-07 07:32:28 -06:00
|
|
|
|
2008-10-23 02:42:14 -06:00
|
|
|
Square to;
|
2010-11-16 01:42:12 -07:00
|
|
|
Bitboard b1, b2, dc1, dc2, pawnPushes, emptySquares;
|
2009-11-03 04:29:07 -07:00
|
|
|
Bitboard pawns = pos.pieces(PAWN, Us);
|
2010-11-16 01:42:12 -07:00
|
|
|
Bitboard pawnsOn7 = pawns & TRank7BB;
|
2011-06-28 07:36:39 -06:00
|
|
|
Bitboard enemyPieces = (Type == MV_CAPTURE ? target : pos.pieces(Them));
|
2009-09-23 04:18:04 -06:00
|
|
|
|
2010-11-16 01:42:12 -07:00
|
|
|
// Pre-calculate pawn pushes before changing emptySquares definition
|
2011-01-05 11:47:01 -07:00
|
|
|
if (Type != MV_CAPTURE)
|
2009-09-23 04:18:04 -06:00
|
|
|
{
|
2011-01-05 11:47:01 -07:00
|
|
|
emptySquares = (Type == MV_NON_CAPTURE ? target : pos.empty_squares());
|
2011-07-15 03:19:49 -06:00
|
|
|
pawnPushes = move_pawns<UP>(pawns & ~TRank7BB) & emptySquares;
|
2010-11-16 01:42:12 -07:00
|
|
|
}
|
2009-11-03 04:29:07 -07:00
|
|
|
|
2011-01-05 11:47:01 -07:00
|
|
|
if (Type == MV_EVASION)
|
2010-11-16 01:42:12 -07:00
|
|
|
{
|
|
|
|
emptySquares &= target; // Only blocking squares
|
|
|
|
enemyPieces &= target; // Capture only the checker piece
|
2009-09-23 04:18:04 -06:00
|
|
|
}
|
2008-08-31 23:59:13 -06:00
|
|
|
|
2010-11-16 01:42:12 -07:00
|
|
|
// Promotions and underpromotions
|
|
|
|
if (pawnsOn7)
|
2008-10-18 10:39:13 -06:00
|
|
|
{
|
2011-01-05 11:47:01 -07:00
|
|
|
if (Type == MV_CAPTURE)
|
2010-11-16 01:42:12 -07:00
|
|
|
emptySquares = pos.empty_squares();
|
2008-08-31 23:59:13 -06:00
|
|
|
|
2010-11-16 01:42:12 -07:00
|
|
|
pawns &= ~TRank7BB;
|
2011-07-15 03:19:49 -06:00
|
|
|
mlist = generate_promotions<Type, RIGHT_UP>(pos, mlist, pawnsOn7, enemyPieces);
|
|
|
|
mlist = generate_promotions<Type, LEFT_UP>(pos, mlist, pawnsOn7, enemyPieces);
|
|
|
|
mlist = generate_promotions<Type, UP>(pos, mlist, pawnsOn7, emptySquares);
|
2010-11-16 01:42:12 -07:00
|
|
|
}
|
2009-09-23 03:47:11 -06:00
|
|
|
|
2010-11-16 01:42:12 -07:00
|
|
|
// Standard captures
|
2012-01-03 07:16:01 -07:00
|
|
|
if (Type == MV_CAPTURE || Type == MV_EVASION || Type == MV_NON_EVASION)
|
2010-11-16 01:42:12 -07:00
|
|
|
{
|
2012-01-03 07:16:01 -07:00
|
|
|
mlist = generate_pawn_captures<RIGHT_UP>(mlist, pawns, enemyPieces);
|
|
|
|
mlist = generate_pawn_captures<LEFT_UP>(mlist, pawns, enemyPieces);
|
2009-09-23 04:18:04 -06:00
|
|
|
}
|
|
|
|
|
2010-11-16 01:42:12 -07:00
|
|
|
// Single and double pawn pushes
|
2011-01-05 11:47:01 -07:00
|
|
|
if (Type != MV_CAPTURE)
|
2009-09-23 04:18:04 -06:00
|
|
|
{
|
2011-05-23 04:04:59 -06:00
|
|
|
b1 = (Type != MV_EVASION ? pawnPushes : pawnPushes & emptySquares);
|
2011-07-15 03:19:49 -06:00
|
|
|
b2 = move_pawns<UP>(pawnPushes & TRank3BB) & emptySquares;
|
2009-11-03 04:29:07 -07:00
|
|
|
|
2011-01-05 11:47:01 -07:00
|
|
|
if (Type == MV_CHECK)
|
2009-11-03 04:29:07 -07:00
|
|
|
{
|
2011-01-02 02:41:05 -07:00
|
|
|
// Consider only pawn moves which give direct checks
|
2009-11-03 04:29:07 -07:00
|
|
|
b1 &= pos.attacks_from<PAWN>(ksq, Them);
|
|
|
|
b2 &= pos.attacks_from<PAWN>(ksq, Them);
|
|
|
|
|
2010-11-16 01:42:12 -07:00
|
|
|
// Add pawn moves which gives discovered check. This is possible only
|
|
|
|
// if the pawn is not on the same file as the enemy king, because we
|
|
|
|
// don't generate captures.
|
2009-11-03 04:29:07 -07:00
|
|
|
if (pawns & target) // For CHECK type target is dc bitboard
|
|
|
|
{
|
2011-07-15 03:19:49 -06:00
|
|
|
dc1 = move_pawns<UP>(pawns & target & ~file_bb(ksq)) & emptySquares;
|
|
|
|
dc2 = move_pawns<UP>(dc1 & TRank3BB) & emptySquares;
|
2008-08-31 23:59:13 -06:00
|
|
|
|
2009-11-03 04:29:07 -07:00
|
|
|
b1 |= dc1;
|
|
|
|
b2 |= dc2;
|
|
|
|
}
|
|
|
|
}
|
2011-07-15 03:19:49 -06:00
|
|
|
SERIALIZE_MOVES_D(b1, -UP);
|
|
|
|
SERIALIZE_MOVES_D(b2, -UP -UP);
|
2008-10-18 07:36:58 -06:00
|
|
|
}
|
2009-11-03 04:29:07 -07:00
|
|
|
|
|
|
|
// En passant captures
|
2012-01-03 07:16:01 -07:00
|
|
|
if ( (Type == MV_CAPTURE || Type == MV_EVASION || Type == MV_NON_EVASION)
|
|
|
|
&& pos.ep_square() != SQ_NONE)
|
2009-09-23 07:06:55 -06:00
|
|
|
{
|
2011-10-02 01:33:40 -06:00
|
|
|
assert(Us != WHITE || rank_of(pos.ep_square()) == RANK_6);
|
|
|
|
assert(Us != BLACK || rank_of(pos.ep_square()) == RANK_3);
|
2009-09-23 07:06:55 -06:00
|
|
|
|
2009-11-03 04:29:07 -07:00
|
|
|
// An en passant capture can be an evasion only if the checking piece
|
|
|
|
// is the double pushed pawn and so is in the target. Otherwise this
|
|
|
|
// is a discovery check and we are forced to do otherwise.
|
2011-07-15 03:19:49 -06:00
|
|
|
if (Type == MV_EVASION && !bit_is_set(target, pos.ep_square() - UP))
|
2009-11-03 04:29:07 -07:00
|
|
|
return mlist;
|
|
|
|
|
2009-09-23 07:06:55 -06:00
|
|
|
b1 = pawns & pos.attacks_from<PAWN>(pos.ep_square(), Them);
|
2009-11-03 04:29:07 -07:00
|
|
|
|
2011-12-03 03:56:11 -07:00
|
|
|
assert(b1);
|
2008-10-18 07:36:58 -06:00
|
|
|
|
2009-09-23 07:06:55 -06:00
|
|
|
while (b1)
|
|
|
|
{
|
|
|
|
to = pop_1st_bit(&b1);
|
2012-01-01 08:00:00 -07:00
|
|
|
(*mlist++).move = make_enpassant(to, pos.ep_square());
|
2009-09-23 07:06:55 -06:00
|
|
|
}
|
|
|
|
}
|
2008-10-22 16:18:11 -06:00
|
|
|
return mlist;
|
2008-10-18 07:36:58 -06:00
|
|
|
}
|
2008-10-19 01:49:26 -06:00
|
|
|
|
2009-02-05 08:08:00 -07:00
|
|
|
template<CastlingSide Side>
|
2011-01-07 07:17:28 -07:00
|
|
|
MoveStack* generate_castle_moves(const Position& pos, MoveStack* mlist, Color us) {
|
2008-10-19 09:54:18 -06:00
|
|
|
|
2011-06-27 09:06:15 -06:00
|
|
|
CastleRight f = CastleRight((Side == KING_SIDE ? WHITE_OO : WHITE_OOO) << us);
|
2011-10-02 01:33:40 -06:00
|
|
|
Color them = flip(us);
|
2008-10-19 09:54:18 -06:00
|
|
|
|
2011-06-30 02:29:58 -06:00
|
|
|
// After castling, the rook and king's final positions are exactly the same
|
|
|
|
// in Chess960 as they would be in standard chess.
|
|
|
|
Square kfrom = pos.king_square(us);
|
|
|
|
Square rfrom = pos.castle_rook_square(f);
|
|
|
|
Square kto = relative_square(us, Side == KING_SIDE ? SQ_G1 : SQ_C1);
|
|
|
|
Square rto = relative_square(us, Side == KING_SIDE ? SQ_F1 : SQ_D1);
|
|
|
|
|
|
|
|
assert(!pos.in_check());
|
|
|
|
assert(pos.piece_on(kfrom) == make_piece(us, KING));
|
|
|
|
assert(pos.piece_on(rfrom) == make_piece(us, ROOK));
|
|
|
|
|
|
|
|
// Unimpeded rule: All the squares between the king's initial and final squares
|
|
|
|
// (including the final square), and all the squares between the rook's initial
|
|
|
|
// and final squares (including the final square), must be vacant except for
|
|
|
|
// the king and castling rook.
|
2011-10-30 22:38:44 -06:00
|
|
|
for (Square s = std::min(kfrom, kto); s <= std::max(kfrom, kto); s++)
|
2011-06-30 02:29:58 -06:00
|
|
|
if ( (s != kfrom && s != rfrom && !pos.square_is_empty(s))
|
2011-06-28 07:36:39 -06:00
|
|
|
||(pos.attackers_to(s) & pos.pieces(them)))
|
2011-06-30 02:29:58 -06:00
|
|
|
return mlist;
|
2008-10-19 09:54:18 -06:00
|
|
|
|
2011-10-30 22:38:44 -06:00
|
|
|
for (Square s = std::min(rfrom, rto); s <= std::max(rfrom, rto); s++)
|
2011-06-30 02:29:58 -06:00
|
|
|
if (s != kfrom && s != rfrom && !pos.square_is_empty(s))
|
|
|
|
return mlist;
|
2008-10-19 09:54:18 -06:00
|
|
|
|
2011-06-30 02:29:58 -06:00
|
|
|
// Because we generate only legal castling moves we need to verify that
|
|
|
|
// 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.
|
|
|
|
if (pos.is_chess960())
|
|
|
|
{
|
|
|
|
Bitboard occ = pos.occupied_squares();
|
|
|
|
clear_bit(&occ, rfrom);
|
|
|
|
if (pos.attackers_to(kto, occ) & pos.pieces(them))
|
|
|
|
return mlist;
|
|
|
|
}
|
2008-10-19 09:54:18 -06:00
|
|
|
|
2012-01-01 08:00:00 -07:00
|
|
|
(*mlist++).move = make_castle(kfrom, rfrom);
|
2009-02-05 08:08:00 -07:00
|
|
|
|
2008-10-22 16:18:11 -06:00
|
|
|
return mlist;
|
2008-10-19 09:54:18 -06:00
|
|
|
}
|
2011-01-07 07:17:28 -07:00
|
|
|
|
|
|
|
} // namespace
|