]> git.sesse.net Git - stockfish/blobdiff - src/endgame.cpp
Use frontmost_sq() and backmost_sq helpers
[stockfish] / src / endgame.cpp
index cc98de90d4f40f71f900c04c1bbc71bd2322ec11..9c1c56f11d445504e4bcb735ef60548b12ea4d87 100644 (file)
@@ -89,10 +89,7 @@ namespace {
 
 Endgames::Endgames() {
 
-  add<KK>("KK");
   add<KPK>("KPK");
-  add<KBK>("KBK");
-  add<KNK>("KNK");
   add<KNNK>("KNNK");
   add<KBNK>("KBNK");
   add<KRKP>("KRKP");
@@ -411,9 +408,6 @@ Value Endgame<KBBKN>::operator()(const Position& pos) const {
 
 
 /// Some cases of trivial draws
-template<> Value Endgame<KK>::operator()(const Position&) const { return VALUE_DRAW; }
-template<> Value Endgame<KBK>::operator()(const Position&) const { return VALUE_DRAW; }
-template<> Value Endgame<KNK>::operator()(const Position&) const { return VALUE_DRAW; }
 template<> Value Endgame<KNNK>::operator()(const Position&) const { return VALUE_DRAW; }
 template<> Value Endgame<KmmKm>::operator()(const Position&) const { return VALUE_DRAW; }
 
@@ -449,22 +443,12 @@ 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);
-          }
+          Square pawnSq = frontmost_sq(strongerSide, 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
-              || relative_rank(strongerSide, kingSq) >= rank)
+              || relative_rank(weakerSide, kingSq) <= relative_rank(weakerSide, pawnSq))
               return SCALE_FACTOR_DRAW;
       }
   }
@@ -475,9 +459,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 = backmost_sq(weakerSide, pos.pieces(weakerSide, PAWN));
 
       Square strongerKingSq = pos.king_square(strongerSide);
       Square weakerKingSq = pos.king_square(weakerSide);