]> git.sesse.net Git - stockfish/blobdiff - src/endgame.cpp
Replace hardcoded 128 by constant
[stockfish] / src / endgame.cpp
index cff5e6994054699fb40aec7c33404a7fb40a7073..65b858613dd8f74ae6ad73674187473e059afe25 100644 (file)
@@ -131,13 +131,11 @@ Value Endgame<KXK>::operator()(const Position& pos) const {
 
   assert(pos.non_pawn_material(weakerSide) == VALUE_ZERO);
   assert(!pos.count<PAWN>(weakerSide));
+  assert(!pos.checkers()); // Eval is never called when in check
 
   // Stalemate detection with lone king
-  if (    pos.side_to_move() == weakerSide
-      && !pos.checkers()
-      && !MoveList<LEGAL>(pos).size()) {
-    return VALUE_DRAW;
-  }
+  if (pos.side_to_move() == weakerSide && !MoveList<LEGAL>(pos).size())
+      return VALUE_DRAW;
 
   Square winnerKSq = pos.king_square(strongerSide);
   Square loserKSq = pos.king_square(weakerSide);
@@ -149,9 +147,8 @@ Value Endgame<KXK>::operator()(const Position& pos) const {
 
   if (   pos.count<QUEEN>(strongerSide)
       || pos.count<ROOK>(strongerSide)
-      || pos.bishop_pair(strongerSide)) {
-    result += VALUE_KNOWN_WIN;
-  }
+      || pos.bishop_pair(strongerSide))
+      result += VALUE_KNOWN_WIN;
 
   return strongerSide == pos.side_to_move() ? result : -result;
 }
@@ -410,17 +407,10 @@ Value Endgame<KBBKN>::operator()(const Position& pos) const {
 }
 
 
-/// K and two minors vs K and one or two minors or K and two knights against
-/// king alone are always draw.
-template<>
-Value Endgame<KmmKm>::operator()(const Position&) const {
-  return VALUE_DRAW;
-}
+/// Some cases of trivial draws
+template<> Value Endgame<KNNK>::operator()(const Position&) const { return VALUE_DRAW; }
+template<> Value Endgame<KmmKm>::operator()(const Position&) const { return VALUE_DRAW; }
 
-template<>
-Value Endgame<KNNK>::operator()(const Position&) const {
-  return VALUE_DRAW;
-}
 
 /// K, bishop and one or more pawns vs K. It checks for draws with rook pawns and
 /// a bishop of the wrong color. If such a draw is detected, SCALE_FACTOR_DRAW
@@ -453,18 +443,7 @@ ScaleFactor Endgame<KBPsK>::operator()(const Position& pos) const {
           // The bishop has the wrong color, and the defending king is on the
           // file of the pawn(s) or the adjacent file. Find the rank of the
           // frontmost pawn.
-          Rank rank;
-          if (strongerSide == WHITE)
-          {
-              for (rank = RANK_7; !(rank_bb(rank) & pawns); rank--) {}
-              assert(rank >= RANK_2 && rank <= RANK_7);
-          }
-          else
-          {
-              for (rank = RANK_2; !(rank_bb(rank) & pawns); rank++) {}
-              rank = Rank(rank ^ 7);  // HACK to get the relative rank
-              assert(rank >= RANK_2 && rank <= RANK_7);
-          }
+          Rank rank = relative_rank(strongerSide, lsb(weakerSide, pawns));
           // If the defending king has distance 1 to the promotion square or
           // is placed somewhere in front of the pawn, it's a draw.
           if (   square_distance(kingSq, queeningSq) <= 1
@@ -479,9 +458,8 @@ ScaleFactor Endgame<KBPsK>::operator()(const Position& pos) const {
       && pos.non_pawn_material(weakerSide) == 0
       && pos.count<PAWN>(weakerSide) >= 1)
   {
-      // Get weaker pawn closest to opponent's queening square
-      Bitboard wkPawns = pos.pieces(weakerSide, PAWN);
-      Square weakerPawnSq = strongerSide == WHITE ? msb(wkPawns) : lsb(wkPawns);
+      // Get weakerSide pawn that is closest to home rank
+      Square weakerPawnSq = lsb(weakerSide, pos.pieces(weakerSide, PAWN));
 
       Square strongerKingSq = pos.king_square(strongerSide);
       Square weakerKingSq = pos.king_square(weakerSide);
@@ -704,7 +682,7 @@ ScaleFactor Endgame<KPsK>::operator()(const Position& pos) const {
       // Does the defending king block the pawns?
       if (   square_distance(ksq, relative_square(strongerSide, SQ_A8)) <= 1
           || (    file_of(ksq) == FILE_A
-              && !(in_front_bb(strongerSide, ksq) & pawns)))
+              && !(in_front_bb(strongerSide, rank_of(ksq)) & pawns)))
           return SCALE_FACTOR_DRAW;
   }
   // Are all pawns on the 'h' file?
@@ -713,7 +691,7 @@ ScaleFactor Endgame<KPsK>::operator()(const Position& pos) const {
     // Does the defending king block the pawns?
     if (   square_distance(ksq, relative_square(strongerSide, SQ_H8)) <= 1
         || (    file_of(ksq) == FILE_H
-            && !(in_front_bb(strongerSide, ksq) & pawns)))
+            && !(in_front_bb(strongerSide, rank_of(ksq)) & pawns)))
         return SCALE_FACTOR_DRAW;
   }
   return SCALE_FACTOR_NONE;