]> git.sesse.net Git - stockfish/blobdiff - src/endgame.cpp
Use a Thread instead of an array index
[stockfish] / src / endgame.cpp
index e44212b887b042109f241dd89515c72ce2fa16b4..5e021f1132917aa36d397a1f090f71938c72cb99 100644 (file)
@@ -22,7 +22,7 @@
 
 #include "bitcount.h"
 #include "endgame.h"
-#include "pawns.h"
+#include "movegen.h"
 
 using std::string;
 
@@ -77,7 +77,7 @@ namespace {
     string fen =  sides[0] + char('0' + int(8 - code.length()))
                 + sides[1] + "/8/8/8/8/8/8/8 w - - 0 10";
 
-    return Position(fen, false, 0).material_key();
+    return Position(fen, false, NULL).material_key();
   }
 
   template<typename M>
@@ -116,10 +116,8 @@ Endgames::~Endgames() {
 template<EndgameType E>
 void Endgames::add(const string& code) {
 
-  typedef typename eg_family<E>::type T;
-
-  map((T*)0)[key(code, WHITE)] = new Endgame<E>(WHITE);
-  map((T*)0)[key(code, BLACK)] = new Endgame<E>(BLACK);
+  map((Endgame<E>*)0)[key(code, WHITE)] = new Endgame<E>(WHITE);
+  map((Endgame<E>*)0)[key(code, BLACK)] = new Endgame<E>(BLACK);
 }
 
 
@@ -133,6 +131,13 @@ Value Endgame<KXK>::operator()(const Position& pos) const {
   assert(pos.non_pawn_material(weakerSide) == VALUE_ZERO);
   assert(pos.piece_count(weakerSide, PAWN) == VALUE_ZERO);
 
+  // Stalemate detection with lone king
+  if (    pos.side_to_move() == weakerSide
+      && !pos.in_check()
+      && !MoveList<MV_LEGAL>(pos).size()) {
+    return VALUE_DRAW;
+  }
+
   Square winnerKSq = pos.king_square(strongerSide);
   Square loserKSq = pos.king_square(weakerSide);
 
@@ -143,9 +148,9 @@ Value Endgame<KXK>::operator()(const Position& pos) const {
 
   if (   pos.piece_count(strongerSide, QUEEN)
       || pos.piece_count(strongerSide, ROOK)
-      || pos.piece_count(strongerSide, BISHOP) > 1)
-      // TODO: check for two equal-colored bishops!
-      result += VALUE_KNOWN_WIN;
+      || pos.bishop_pair(strongerSide)) {
+    result += VALUE_KNOWN_WIN;
+  }
 
   return strongerSide == pos.side_to_move() ? result : -result;
 }
@@ -205,10 +210,10 @@ Value Endgame<KPK>::operator()(const Position& pos) const {
   }
   else
   {
-      wksq = flip(pos.king_square(BLACK));
-      bksq = flip(pos.king_square(WHITE));
-      wpsq = flip(pos.piece_list(BLACK, PAWN)[0]);
-      stm = flip(pos.side_to_move());
+      wksq = ~pos.king_square(BLACK);
+      bksq = ~pos.king_square(WHITE);
+      wpsq = ~pos.piece_list(BLACK, PAWN)[0];
+      stm  = ~pos.side_to_move();
   }
 
   if (file_of(wpsq) >= FILE_E)
@@ -251,10 +256,10 @@ Value Endgame<KRKP>::operator()(const Position& pos) const {
 
   if (strongerSide == BLACK)
   {
-      wksq = flip(wksq);
-      wrsq = flip(wrsq);
-      bksq = flip(bksq);
-      bpsq = flip(bpsq);
+      wksq = ~wksq;
+      wrsq = ~wrsq;
+      bksq = ~bksq;
+      bpsq = ~bpsq;
   }
 
   Square queeningSq = make_square(file_of(bpsq), RANK_1);
@@ -369,7 +374,7 @@ Value Endgame<KBBKN>::operator()(const Position& pos) const {
   result += Value(square_distance(bksq, nsq) * 32);
 
   // Bonus for restricting the knight's mobility
-  result += Value((8 - count_1s<CNT32_MAX15>(pos.attacks_from<KNIGHT>(nsq))) * 8);
+  result += Value((8 - popcount<Max15>(pos.attacks_from<KNIGHT>(nsq))) * 8);
 
   return strongerSide == pos.side_to_move() ? result : -result;
 }
@@ -416,7 +421,7 @@ ScaleFactor Endgame<KBPsK>::operator()(const Position& pos) const {
           && abs(file_of(kingSq) - pawnFile) <= 1)
       {
           // The bishop has the wrong color, and the defending king is on the
-          // file of the pawn(s) or the neighboring file. Find the rank of the
+          // file of the pawn(s) or the adjacent file. Find the rank of the
           // frontmost pawn.
           Rank rank;
           if (strongerSide == WHITE)
@@ -491,11 +496,11 @@ ScaleFactor Endgame<KRPKR>::operator()(const Position& pos) const {
   // pawn is on the left half of the board.
   if (strongerSide == BLACK)
   {
-      wksq = flip(wksq);
-      wrsq = flip(wrsq);
-      wpsq = flip(wpsq);
-      bksq = flip(bksq);
-      brsq = flip(brsq);
+      wksq = ~wksq;
+      wrsq = ~wrsq;
+      wpsq = ~wpsq;
+      bksq = ~bksq;
+      brsq = ~brsq;
   }
   if (file_of(wpsq) > FILE_D)
   {
@@ -768,7 +773,7 @@ ScaleFactor Endgame<KBPPKB>::operator()(const Position& pos) const {
         return SCALE_FACTOR_NONE;
 
   case 1:
-    // Pawns on neighboring files. Draw if defender firmly controls the square
+    // Pawns on adjacent files. Draw if defender firmly controls the square
     // in front of the frontmost pawn's path, and the square diagonally behind
     // this square on the file of the other pawn.
     if (   ksq == blockSq1
@@ -867,10 +872,10 @@ ScaleFactor Endgame<KPKP>::operator()(const Position& pos) const {
 
   if (strongerSide == BLACK)
   {
-      wksq = flip(wksq);
-      bksq = flip(bksq);
-      wpsq = flip(wpsq);
-      stm = flip(stm);
+      wksq = ~wksq;
+      bksq = ~bksq;
+      wpsq = ~wpsq;
+      stm  = ~stm;
   }
 
   if (file_of(wpsq) >= FILE_E)