- enum CastlingSide {
- KING_SIDE,
- QUEEN_SIDE
- };
-
- // Function
- bool castling_is_check(const Position&, CastlingSide);
-
- // Templates
- template<CastlingSide Side>
- MoveStack* generate_castle_moves(const Position&, MoveStack*);
- template<PieceType>
- MoveStack* generate_piece_checks(const Position&, MoveStack*, Color us, Bitboard, Square);
- template<Color Us, Rank, Bitboard, SquareDelta>
- MoveStack* do_generate_pawn_blocking_evasions(const Position& pos, Bitboard not_pinned,
- Bitboard blockSquares, MoveStack* mlist);
-
- // Template generate_pawn_captures() with specializations
- template<Color, Color, Bitboard, SquareDelta, SquareDelta, SquareDelta>
- MoveStack* do_generate_pawn_captures(const Position& pos, MoveStack* mlist);
-
- template<Color>
- inline MoveStack* generate_pawn_captures(const Position& p, MoveStack* m) {
- return do_generate_pawn_captures<WHITE, BLACK, Rank8BB, DELTA_NE, DELTA_NW, DELTA_N>(p, m);
- }
- template<>
- inline MoveStack* generate_pawn_captures<BLACK>(const Position& p, MoveStack* m) {
- return do_generate_pawn_captures<BLACK, WHITE, Rank1BB, DELTA_SE, DELTA_SW, DELTA_S>(p, m);
- }
-
- // Template generate_pawn_noncaptures() with specializations
- template<Color, Color, Bitboard, Bitboard, SquareDelta, SquareDelta, SquareDelta>
- MoveStack* do_generate_pawn_noncaptures(const Position& pos, MoveStack* mlist);
-
- template<Color>
- inline MoveStack* generate_pawn_noncaptures(const Position& p, MoveStack* m) {
- return do_generate_pawn_noncaptures<WHITE, BLACK, Rank8BB, Rank3BB, DELTA_NE, DELTA_NW, DELTA_N>(p, m);
- }
- template<>
- inline MoveStack* generate_pawn_noncaptures<BLACK>(const Position& p, MoveStack* m) {
- return do_generate_pawn_noncaptures<BLACK, WHITE, Rank1BB, Rank6BB, DELTA_SE, DELTA_SW, DELTA_S>(p, m);
- }
-
- // Template generate_pawn_checks() with specializations
- template<Color, Color, Bitboard, Bitboard, SquareDelta>
- MoveStack* do_generate_pawn_checks(const Position&, Bitboard, Square, MoveStack*);
-
- template<Color>
- inline MoveStack* generate_pawn_checks(const Position& p, MoveStack* m, Bitboard dc, Square ksq) {
- return do_generate_pawn_checks<WHITE, BLACK, Rank8BB, Rank3BB, DELTA_N>(p, dc, ksq, m);
- }
- template<>
- inline MoveStack* generate_pawn_checks<BLACK>(const Position& p, MoveStack* m, Bitboard dc, Square ksq) {
- return do_generate_pawn_checks<BLACK, WHITE, Rank1BB, Rank6BB, DELTA_S>(p, dc, ksq, m);
- }
-
- // Template generate_piece_moves() with specializations
- template<PieceType>
- MoveStack* generate_piece_moves(const Position&, MoveStack*, Color us, Bitboard);
- template<>
- MoveStack* generate_piece_moves<KING>(const Position& pos, MoveStack* mlist, Color us, Bitboard target);
-
- // Template generate_piece_blocking_evasions() with specializations
- template<PieceType>
- MoveStack* generate_piece_blocking_evasions(const Position&, MoveStack*, Color us, Bitboard, Bitboard);
- template<>
- MoveStack* generate_piece_blocking_evasions<PAWN>(const Position& p, MoveStack* m, Color us, Bitboard np, Bitboard bs);
-}
-
-
-////
-//// Functions
-////
-
-
-/// generate_captures generates() all pseudo-legal captures and queen
-/// promotions. The return value is the number of moves generated.
-
-int generate_captures(const Position& pos, MoveStack* mlist) {
-
- assert(pos.is_ok());
- assert(!pos.is_check());
-
- Color us = pos.side_to_move();
- Bitboard target = pos.pieces_of_color(opposite_color(us));
- MoveStack* mlist_start = mlist;
-
- mlist = generate_piece_moves<QUEEN>(pos, mlist, us, target);
- mlist = generate_piece_moves<ROOK>(pos, mlist, us, target);
- mlist = generate_piece_moves<BISHOP>(pos, mlist, us, target);
- mlist = generate_piece_moves<KNIGHT>(pos, mlist, us, target);
-
- if (us == WHITE)
- mlist = generate_pawn_captures<WHITE>(pos, mlist);
- else
- mlist = generate_pawn_captures<BLACK>(pos, mlist);
-
- mlist = generate_piece_moves<KING>(pos, mlist, us, target);
- return int(mlist - mlist_start);
-}
-
-
-/// generate_noncaptures() generates all pseudo-legal non-captures and
-/// underpromotions. The return value is the number of moves generated.
-
-int generate_noncaptures(const Position& pos, MoveStack* mlist) {
-
- assert(pos.is_ok());
- assert(!pos.is_check());
-
- Color us = pos.side_to_move();
- Bitboard target = pos.empty_squares();
- MoveStack* mlist_start = mlist;
-
- if (us == WHITE)
- mlist = generate_pawn_noncaptures<WHITE>(pos, mlist);
- else
- mlist = generate_pawn_noncaptures<BLACK>(pos, mlist);
-
- 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);
- mlist = generate_castle_moves<KING_SIDE>(pos, mlist);
- mlist = generate_castle_moves<QUEEN_SIDE>(pos, mlist);
- return int(mlist - mlist_start);
-}
-
-
-/// generate_checks() generates all pseudo-legal non-capturing, non-promoting
-/// checks. It returns the number of generated moves.
-
-int generate_checks(const Position& pos, MoveStack* mlist, Bitboard dc) {
-
- assert(pos.is_ok());
- assert(!pos.is_check());
-
- Color us = pos.side_to_move();
- Square ksq = pos.king_square(opposite_color(us));
- MoveStack* mlist_start = mlist;
-
- assert(pos.piece_on(ksq) == king_of_color(opposite_color(us)));
-
- // Pawn moves
- if (us == WHITE)
- mlist = generate_pawn_checks<WHITE>(pos, mlist, dc, ksq);
- else
- mlist = generate_pawn_checks<BLACK>(pos, mlist, dc, ksq);
-
- // Pieces moves
- mlist = generate_piece_checks<KNIGHT>(pos, mlist, us, dc, ksq);
- mlist = generate_piece_checks<BISHOP>(pos, mlist, us, dc, ksq);
- mlist = generate_piece_checks<ROOK>(pos, mlist, us, dc, ksq);
- mlist = generate_piece_checks<QUEEN>(pos, mlist, us, dc, ksq);
- mlist = generate_piece_checks<KING>(pos, mlist, us, dc, ksq);
-
- // 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<QUEEN_SIDE>(pos, mlist);
-
- 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<KING_SIDE>(pos, mlist);
-
- return int(mlist - mlist_start);
-}