Subversion Repositories Games.Chess Giants

Rev

Rev 154 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.   Stockfish, a UCI chess playing engine derived from Glaurung 2.1
  3.   Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
  4.   Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
  5.   Copyright (C) 2015-2018 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
  6.  
  7.   Stockfish is free software: you can redistribute it and/or modify
  8.   it under the terms of the GNU General Public License as published by
  9.   the Free Software Foundation, either version 3 of the License, or
  10.   (at your option) any later version.
  11.  
  12.   Stockfish is distributed in the hope that it will be useful,
  13.   but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.   GNU General Public License for more details.
  16.  
  17.   You should have received a copy of the GNU General Public License
  18.   along with this program.  If not, see <http://www.gnu.org/licenses/>.
  19. */
  20.  
  21. #include <cassert>
  22.  
  23. #include "movegen.h"
  24. #include "position.h"
  25.  
  26. namespace {
  27.  
  28.   template<CastlingRight Cr, bool Checks, bool Chess960>
  29.   ExtMove* generate_castling(const Position& pos, ExtMove* moveList, Color us) {
  30.  
  31.     static const bool KingSide = (Cr == WHITE_OO || Cr == BLACK_OO);
  32.  
  33.     if (pos.castling_impeded(Cr) || !pos.can_castle(Cr))
  34.         return moveList;
  35.  
  36.     // After castling, the rook and king final positions are the same in Chess960
  37.     // as they would be in standard chess.
  38.     Square kfrom = pos.square<KING>(us);
  39.     Square rfrom = pos.castling_rook_square(Cr);
  40.     Square kto = relative_square(us, KingSide ? SQ_G1 : SQ_C1);
  41.     Bitboard enemies = pos.pieces(~us);
  42.  
  43.     assert(!pos.checkers());
  44.  
  45.     const Direction K = Chess960 ? kto > kfrom ? WEST : EAST
  46.                                  : KingSide    ? WEST : EAST;
  47.  
  48.     for (Square s = kto; s != kfrom; s += K)
  49.         if (pos.attackers_to(s) & enemies)
  50.             return moveList;
  51.  
  52.     // Because we generate only legal castling moves we need to verify that
  53.     // when moving the castling rook we do not discover some hidden checker.
  54.     // For instance an enemy queen in SQ_A1 when castling rook is in SQ_B1.
  55.     if (Chess960 && (attacks_bb<ROOK>(kto, pos.pieces() ^ rfrom) & pos.pieces(~us, ROOK, QUEEN)))
  56.         return moveList;
  57.  
  58.     Move m = make<CASTLING>(kfrom, rfrom);
  59.  
  60.     if (Checks && !pos.gives_check(m))
  61.         return moveList;
  62.  
  63.     *moveList++ = m;
  64.     return moveList;
  65.   }
  66.  
  67.  
  68.   template<GenType Type, Direction D>
  69.   ExtMove* make_promotions(ExtMove* moveList, Square to, Square ksq) {
  70.  
  71.     if (Type == CAPTURES || Type == EVASIONS || Type == NON_EVASIONS)
  72.         *moveList++ = make<PROMOTION>(to - D, to, QUEEN);
  73.  
  74.     if (Type == QUIETS || Type == EVASIONS || Type == NON_EVASIONS)
  75.     {
  76.         *moveList++ = make<PROMOTION>(to - D, to, ROOK);
  77.         *moveList++ = make<PROMOTION>(to - D, to, BISHOP);
  78.         *moveList++ = make<PROMOTION>(to - D, to, KNIGHT);
  79.     }
  80.  
  81.     // Knight promotion is the only promotion that can give a direct check
  82.     // that's not already included in the queen promotion.
  83.     if (Type == QUIET_CHECKS && (PseudoAttacks[KNIGHT][to] & ksq))
  84.         *moveList++ = make<PROMOTION>(to - D, to, KNIGHT);
  85.     else
  86.         (void)ksq; // Silence a warning under MSVC
  87.  
  88.     return moveList;
  89.   }
  90.  
  91.  
  92.   template<Color Us, GenType Type>
  93.   ExtMove* generate_pawn_moves(const Position& pos, ExtMove* moveList, Bitboard target) {
  94.  
  95.     // Compute our parametrized parameters at compile time, named according to
  96.     // the point of view of white side.
  97.     const Color     Them     = (Us == WHITE ? BLACK      : WHITE);
  98.     const Bitboard  TRank8BB = (Us == WHITE ? Rank8BB    : Rank1BB);
  99.     const Bitboard  TRank7BB = (Us == WHITE ? Rank7BB    : Rank2BB);
  100.     const Bitboard  TRank3BB = (Us == WHITE ? Rank3BB    : Rank6BB);
  101.     const Direction Up       = (Us == WHITE ? NORTH      : SOUTH);
  102.     const Direction Right    = (Us == WHITE ? NORTH_EAST : SOUTH_WEST);
  103.     const Direction Left     = (Us == WHITE ? NORTH_WEST : SOUTH_EAST);
  104.  
  105.     Bitboard emptySquares;
  106.  
  107.     Bitboard pawnsOn7    = pos.pieces(Us, PAWN) &  TRank7BB;
  108.     Bitboard pawnsNotOn7 = pos.pieces(Us, PAWN) & ~TRank7BB;
  109.  
  110.     Bitboard enemies = (Type == EVASIONS ? pos.pieces(Them) & target:
  111.                         Type == CAPTURES ? target : pos.pieces(Them));
  112.  
  113.     // Single and double pawn pushes, no promotions
  114.     if (Type != CAPTURES)
  115.     {
  116.         emptySquares = (Type == QUIETS || Type == QUIET_CHECKS ? target : ~pos.pieces());
  117.  
  118.         Bitboard b1 = shift<Up>(pawnsNotOn7)   & emptySquares;
  119.         Bitboard b2 = shift<Up>(b1 & TRank3BB) & emptySquares;
  120.  
  121.         if (Type == EVASIONS) // Consider only blocking squares
  122.         {
  123.             b1 &= target;
  124.             b2 &= target;
  125.         }
  126.  
  127.         if (Type == QUIET_CHECKS)
  128.         {
  129.             Square ksq = pos.square<KING>(Them);
  130.  
  131.             b1 &= pos.attacks_from<PAWN>(ksq, Them);
  132.             b2 &= pos.attacks_from<PAWN>(ksq, Them);
  133.  
  134.             // Add pawn pushes which give discovered check. This is possible only
  135.             // if the pawn is not on the same file as the enemy king, because we
  136.             // don't generate captures. Note that a possible discovery check
  137.             // promotion has been already generated amongst the captures.
  138.             Bitboard dcCandidates = pos.discovered_check_candidates();
  139.             if (pawnsNotOn7 & dcCandidates)
  140.             {
  141.                 Bitboard dc1 = shift<Up>(pawnsNotOn7 & dcCandidates) & emptySquares & ~file_bb(ksq);
  142.                 Bitboard dc2 = shift<Up>(dc1 & TRank3BB) & emptySquares;
  143.  
  144.                 b1 |= dc1;
  145.                 b2 |= dc2;
  146.             }
  147.         }
  148.  
  149.         while (b1)
  150.         {
  151.             Square to = pop_lsb(&b1);
  152.             *moveList++ = make_move(to - Up, to);
  153.         }
  154.  
  155.         while (b2)
  156.         {
  157.             Square to = pop_lsb(&b2);
  158.             *moveList++ = make_move(to - Up - Up, to);
  159.         }
  160.     }
  161.  
  162.     // Promotions and underpromotions
  163.     if (pawnsOn7 && (Type != EVASIONS || (target & TRank8BB)))
  164.     {
  165.         if (Type == CAPTURES)
  166.             emptySquares = ~pos.pieces();
  167.  
  168.         if (Type == EVASIONS)
  169.             emptySquares &= target;
  170.  
  171.         Bitboard b1 = shift<Right>(pawnsOn7) & enemies;
  172.         Bitboard b2 = shift<Left >(pawnsOn7) & enemies;
  173.         Bitboard b3 = shift<Up   >(pawnsOn7) & emptySquares;
  174.  
  175.         Square ksq = pos.square<KING>(Them);
  176.  
  177.         while (b1)
  178.             moveList = make_promotions<Type, Right>(moveList, pop_lsb(&b1), ksq);
  179.  
  180.         while (b2)
  181.             moveList = make_promotions<Type, Left >(moveList, pop_lsb(&b2), ksq);
  182.  
  183.         while (b3)
  184.             moveList = make_promotions<Type, Up   >(moveList, pop_lsb(&b3), ksq);
  185.     }
  186.  
  187.     // Standard and en-passant captures
  188.     if (Type == CAPTURES || Type == EVASIONS || Type == NON_EVASIONS)
  189.     {
  190.         Bitboard b1 = shift<Right>(pawnsNotOn7) & enemies;
  191.         Bitboard b2 = shift<Left >(pawnsNotOn7) & enemies;
  192.  
  193.         while (b1)
  194.         {
  195.             Square to = pop_lsb(&b1);
  196.             *moveList++ = make_move(to - Right, to);
  197.         }
  198.  
  199.         while (b2)
  200.         {
  201.             Square to = pop_lsb(&b2);
  202.             *moveList++ = make_move(to - Left, to);
  203.         }
  204.  
  205.         if (pos.ep_square() != SQ_NONE)
  206.         {
  207.             assert(rank_of(pos.ep_square()) == relative_rank(Us, RANK_6));
  208.  
  209.             // An en passant capture can be an evasion only if the checking piece
  210.             // is the double pushed pawn and so is in the target. Otherwise this
  211.             // is a discovery check and we are forced to do otherwise.
  212.             if (Type == EVASIONS && !(target & (pos.ep_square() - Up)))
  213.                 return moveList;
  214.  
  215.             b1 = pawnsNotOn7 & pos.attacks_from<PAWN>(pos.ep_square(), Them);
  216.  
  217.             assert(b1);
  218.  
  219.             while (b1)
  220.                 *moveList++ = make<ENPASSANT>(pop_lsb(&b1), pos.ep_square());
  221.         }
  222.     }
  223.  
  224.     return moveList;
  225.   }
  226.  
  227.  
  228.   template<PieceType Pt, bool Checks>
  229.   ExtMove* generate_moves(const Position& pos, ExtMove* moveList, Color us,
  230.                           Bitboard target) {
  231.  
  232.     assert(Pt != KING && Pt != PAWN);
  233.  
  234.     const Square* pl = pos.squares<Pt>(us);
  235.  
  236.     for (Square from = *pl; from != SQ_NONE; from = *++pl)
  237.     {
  238.         if (Checks)
  239.         {
  240.             if (    (Pt == BISHOP || Pt == ROOK || Pt == QUEEN)
  241.                 && !(PseudoAttacks[Pt][from] & target & pos.check_squares(Pt)))
  242.                 continue;
  243.  
  244.             if (pos.discovered_check_candidates() & from)
  245.                 continue;
  246.         }
  247.  
  248.         Bitboard b = pos.attacks_from<Pt>(from) & target;
  249.  
  250.         if (Checks)
  251.             b &= pos.check_squares(Pt);
  252.  
  253.         while (b)
  254.             *moveList++ = make_move(from, pop_lsb(&b));
  255.     }
  256.  
  257.     return moveList;
  258.   }
  259.  
  260.  
  261.   template<Color Us, GenType Type>
  262.   ExtMove* generate_all(const Position& pos, ExtMove* moveList, Bitboard target) {
  263.  
  264.     const bool Checks = Type == QUIET_CHECKS;
  265.  
  266.     moveList = generate_pawn_moves<Us, Type>(pos, moveList, target);
  267.     moveList = generate_moves<KNIGHT, Checks>(pos, moveList, Us, target);
  268.     moveList = generate_moves<BISHOP, Checks>(pos, moveList, Us, target);
  269.     moveList = generate_moves<  ROOK, Checks>(pos, moveList, Us, target);
  270.     moveList = generate_moves< QUEEN, Checks>(pos, moveList, Us, target);
  271.  
  272.     if (Type != QUIET_CHECKS && Type != EVASIONS)
  273.     {
  274.         Square ksq = pos.square<KING>(Us);
  275.         Bitboard b = pos.attacks_from<KING>(ksq) & target;
  276.         while (b)
  277.             *moveList++ = make_move(ksq, pop_lsb(&b));
  278.     }
  279.  
  280.     if (Type != CAPTURES && Type != EVASIONS && pos.can_castle(Us))
  281.     {
  282.         if (pos.is_chess960())
  283.         {
  284.             moveList = generate_castling<MakeCastling<Us,  KING_SIDE>::right, Checks, true>(pos, moveList, Us);
  285.             moveList = generate_castling<MakeCastling<Us, QUEEN_SIDE>::right, Checks, true>(pos, moveList, Us);
  286.         }
  287.         else
  288.         {
  289.             moveList = generate_castling<MakeCastling<Us,  KING_SIDE>::right, Checks, false>(pos, moveList, Us);
  290.             moveList = generate_castling<MakeCastling<Us, QUEEN_SIDE>::right, Checks, false>(pos, moveList, Us);
  291.         }
  292.     }
  293.  
  294.     return moveList;
  295.   }
  296.  
  297. } // namespace
  298.  
  299.  
  300. /// generate<CAPTURES> generates all pseudo-legal captures and queen
  301. /// promotions. Returns a pointer to the end of the move list.
  302. ///
  303. /// generate<QUIETS> generates all pseudo-legal non-captures and
  304. /// underpromotions. Returns a pointer to the end of the move list.
  305. ///
  306. /// generate<NON_EVASIONS> generates all pseudo-legal captures and
  307. /// non-captures. Returns a pointer to the end of the move list.
  308.  
  309. template<GenType Type>
  310. ExtMove* generate(const Position& pos, ExtMove* moveList) {
  311.  
  312.   assert(Type == CAPTURES || Type == QUIETS || Type == NON_EVASIONS);
  313.   assert(!pos.checkers());
  314.  
  315.   Color us = pos.side_to_move();
  316.  
  317.   Bitboard target =  Type == CAPTURES     ?  pos.pieces(~us)
  318.                    : Type == QUIETS       ? ~pos.pieces()
  319.                    : Type == NON_EVASIONS ? ~pos.pieces(us) : 0;
  320.  
  321.   return us == WHITE ? generate_all<WHITE, Type>(pos, moveList, target)
  322.                      : generate_all<BLACK, Type>(pos, moveList, target);
  323. }
  324.  
  325. // Explicit template instantiations
  326. template ExtMove* generate<CAPTURES>(const Position&, ExtMove*);
  327. template ExtMove* generate<QUIETS>(const Position&, ExtMove*);
  328. template ExtMove* generate<NON_EVASIONS>(const Position&, ExtMove*);
  329.  
  330.  
  331. /// generate<QUIET_CHECKS> generates all pseudo-legal non-captures and knight
  332. /// underpromotions that give check. Returns a pointer to the end of the move list.
  333. template<>
  334. ExtMove* generate<QUIET_CHECKS>(const Position& pos, ExtMove* moveList) {
  335.  
  336.   assert(!pos.checkers());
  337.  
  338.   Color us = pos.side_to_move();
  339.   Bitboard dc = pos.discovered_check_candidates();
  340.  
  341.   while (dc)
  342.   {
  343.      Square from = pop_lsb(&dc);
  344.      PieceType pt = type_of(pos.piece_on(from));
  345.  
  346.      if (pt == PAWN)
  347.          continue; // Will be generated together with direct checks
  348.  
  349.      Bitboard b = pos.attacks_from(pt, from) & ~pos.pieces();
  350.  
  351.      if (pt == KING)
  352.          b &= ~PseudoAttacks[QUEEN][pos.square<KING>(~us)];
  353.  
  354.      while (b)
  355.          *moveList++ = make_move(from, pop_lsb(&b));
  356.   }
  357.  
  358.   return us == WHITE ? generate_all<WHITE, QUIET_CHECKS>(pos, moveList, ~pos.pieces())
  359.                      : generate_all<BLACK, QUIET_CHECKS>(pos, moveList, ~pos.pieces());
  360. }
  361.  
  362.  
  363. /// generate<EVASIONS> generates all pseudo-legal check evasions when the side
  364. /// to move is in check. Returns a pointer to the end of the move list.
  365. template<>
  366. ExtMove* generate<EVASIONS>(const Position& pos, ExtMove* moveList) {
  367.  
  368.   assert(pos.checkers());
  369.  
  370.   Color us = pos.side_to_move();
  371.   Square ksq = pos.square<KING>(us);
  372.   Bitboard sliderAttacks = 0;
  373.   Bitboard sliders = pos.checkers() & ~pos.pieces(KNIGHT, PAWN);
  374.  
  375.   // Find all the squares attacked by slider checkers. We will remove them from
  376.   // the king evasions in order to skip known illegal moves, which avoids any
  377.   // useless legality checks later on.
  378.   while (sliders)
  379.   {
  380.       Square checksq = pop_lsb(&sliders);
  381.       sliderAttacks |= LineBB[checksq][ksq] ^ checksq;
  382.   }
  383.  
  384.   // Generate evasions for king, capture and non capture moves
  385.   Bitboard b = pos.attacks_from<KING>(ksq) & ~pos.pieces(us) & ~sliderAttacks;
  386.   while (b)
  387.       *moveList++ = make_move(ksq, pop_lsb(&b));
  388.  
  389.   if (more_than_one(pos.checkers()))
  390.       return moveList; // Double check, only a king move can save the day
  391.  
  392.   // Generate blocking evasions or captures of the checking piece
  393.   Square checksq = lsb(pos.checkers());
  394.   Bitboard target = between_bb(checksq, ksq) | checksq;
  395.  
  396.   return us == WHITE ? generate_all<WHITE, EVASIONS>(pos, moveList, target)
  397.                      : generate_all<BLACK, EVASIONS>(pos, moveList, target);
  398. }
  399.  
  400.  
  401. /// generate<LEGAL> generates all the legal moves in the given position
  402.  
  403. template<>
  404. ExtMove* generate<LEGAL>(const Position& pos, ExtMove* moveList) {
  405.  
  406.   Bitboard pinned = pos.pinned_pieces(pos.side_to_move());
  407.   Square ksq = pos.square<KING>(pos.side_to_move());
  408.   ExtMove* cur = moveList;
  409.  
  410.   moveList = pos.checkers() ? generate<EVASIONS    >(pos, moveList)
  411.                             : generate<NON_EVASIONS>(pos, moveList);
  412.   while (cur != moveList)
  413.       if (   (pinned || from_sq(*cur) == ksq || type_of(*cur) == ENPASSANT)
  414.           && !pos.legal(*cur))
  415.           *cur = (--moveList)->move;
  416.       else
  417.           ++cur;
  418.  
  419.   return moveList;
  420. }
  421.