]> git.sesse.net Git - stockfish/blobdiff - src/movepick.cpp
Fix a bug in king discoveries checks
[stockfish] / src / movepick.cpp
index 0017353cae8873b8fd0c3c211db935efac618ea6..e1f8e4e77f944bd5831245b81dd16c467c9935f0 100644 (file)
@@ -1,13 +1,14 @@
 /*
-  Glaurung, a UCI chess playing engine.
-  Copyright (C) 2004-2008 Tord Romstad
+  Stockfish, a UCI chess playing engine derived from Glaurung 2.1
+  Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
+  Copyright (C) 2008 Marco Costalba
 
-  Glaurung is free software: you can redistribute it and/or modify
+  Stockfish is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.
   
-  Glaurung is distributed in the hope that it will be useful,
+  Stockfish is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
@@ -73,11 +74,11 @@ MovePicker::MovePicker(const Position& p, bool pvnode, Move ttm, Move mk,
   numOfBadCaptures = 0;
   dc = p.discovered_check_candidates(p.side_to_move());
 
-  if(p.is_check())
+  if (p.is_check())
     phaseIndex = EvasionsPhaseIndex;
-  else if(depth > Depth(0))
+  else if (depth > Depth(0))
     phaseIndex = MainSearchPhaseIndex;
-  else if(depth == Depth(0))
+  else if (depth == Depth(0))
     phaseIndex = QsearchWithChecksPhaseIndex;
   else
     phaseIndex = QsearchWithoutChecksPhaseIndex;
@@ -99,7 +100,7 @@ Move MovePicker::get_next_move() {
   while (true)
   {
     // If we already have a list of generated moves, pick the best move from
-    // the list, and return it:
+    // the list, and return it.
     move = pick_move_from_list();
     if (move != MOVE_NONE)
     {
@@ -107,7 +108,7 @@ Move MovePicker::get_next_move() {
         return move;
     }
 
-    // Next phase:
+    // Next phase
     phaseIndex++;
     switch (PhaseTable[phaseIndex]) {
 
@@ -199,11 +200,10 @@ Move MovePicker::get_next_move(Lock &lock) {
 /// MovePicker::score_captures(), MovePicker::score_noncaptures(),
 /// MovePicker::score_evasions() and MovePicker::score_qcaptures() assign a
 /// numerical move ordering score to each move in a move list.  The moves
-/// with highest scores will be picked first by
-/// MovePicker::pick_move_from_list().
+/// with highest scores will be picked first by pick_move_from_list().
 
 void MovePicker::score_captures() {
-  // Winning and equal captures in the main search are ordered by MVV/LVA.
+  // Winning and equal captures in the main search are ordered by MVV.
   // Suprisingly, this appears to perform slightly better than SEE based
   // move ordering.  The reason is probably that in a position with a winning
   // capture, capturing a more valuable (but sufficiently defended) piece
@@ -217,11 +217,8 @@ void MovePicker::score_captures() {
       Move m = moves[i].move;
       moves[i].score = pos.see(m);
       if (moves[i].score >= 0)
-      {
-          moves[i].score = HistoryMax;
-          moves[i].score += move_promotion(m) ? QueenValueMidgame
-                          : pos.midgame_value_of_piece_on(move_to(m));
-      }
+          moves[i].score = move_promotion(m) ? QueenValueMidgame
+                         : pos.midgame_value_of_piece_on(move_to(m));
   }
 }
 
@@ -266,7 +263,7 @@ void MovePicker::score_evasions() {
 
 void MovePicker::score_qcaptures() {
 
-  // Use MVV/LVA ordering
+  // Use MVV ordering
   for (int i = 0; i < numOfMoves; i++)
   {
       Move m = moves[i].move;
@@ -276,6 +273,23 @@ void MovePicker::score_qcaptures() {
 }
 
 
+/// find_best_index() loops across the moves and returns index of
+/// the highest scored one.
+
+int MovePicker::find_best_index() {
+
+  int bestScore = -10000000, bestIndex = -1;
+
+  for (int i = movesPicked; i < numOfMoves; i++)
+      if (moves[i].score > bestScore)
+      {
+          bestIndex = i;
+          bestScore = moves[i].score;
+      }
+  return bestIndex;
+}
+
+
 /// MovePicker::pick_move_from_list() picks the move with the biggest score
 /// from a list of generated moves (moves[] or badCaptures[], depending on
 /// the current move generation phase).  It takes care not to return the
@@ -286,7 +300,6 @@ void MovePicker::score_qcaptures() {
 
 Move MovePicker::pick_move_from_list() {
 
-  int bestScore = -10000000;
   int bestIndex;
   Move move;
 
@@ -297,7 +310,7 @@ Move MovePicker::pick_move_from_list() {
       assert(movesPicked >= 0);
       while (movesPicked < numOfMoves)
       {
-          bestScore = -10000000;
+          int bestScore = -10000000;
           bestIndex = -1;
           for (int i = movesPicked; i < numOfMoves; i++)
           {
@@ -335,18 +348,7 @@ Move MovePicker::pick_move_from_list() {
           // the entire move list for the best move.  If many moves have already
           // been searched and it is not a PV node, we are probably failing low
           // anyway, so we just pick the first move from the list.
-          if (pvNode || movesPicked < 12)
-          {
-              bestScore = -10000000;
-              bestIndex = -1;
-              for (int i = movesPicked; i < numOfMoves; i++)
-                  if(moves[i].score > bestScore)
-                  {
-                      bestIndex = i;
-                      bestScore = moves[i].score;
-                  }
-          } else
-              bestIndex = movesPicked;
+          bestIndex = (movesPicked < 12 || pvNode ? find_best_index() : movesPicked);
 
           if (bestIndex != -1)
           {
@@ -365,14 +367,7 @@ Move MovePicker::pick_move_from_list() {
       assert(movesPicked >= 0);
       while (movesPicked < numOfMoves)
       {
-          bestScore = -10000000;
-          bestIndex = -1;
-          for (int i = movesPicked; i < numOfMoves; i++)
-              if (moves[i].score > bestScore)
-              {
-                  bestIndex = i;
-                  bestScore = moves[i].score;
-              }
+          bestIndex = find_best_index();
 
           if (bestIndex != -1)
           {
@@ -403,18 +398,8 @@ Move MovePicker::pick_move_from_list() {
       assert(movesPicked >= 0);
       while (movesPicked < numOfMoves)
       {
-          bestScore = -10000000;
-          if (movesPicked < 4) // FIXME makes sens to score qcaps?
-          {
-              bestIndex = -1;
-              for (int i = movesPicked; i < numOfMoves; i++)
-                  if(moves[i].score > bestScore)
-                  {
-                      bestIndex = i;
-                      bestScore = moves[i].score;
-                  }
-          } else
-              bestIndex = movesPicked;
+          // FIXME makes sens to score qcaps?
+          bestIndex = (movesPicked < 4 ? find_best_index() : movesPicked);
 
           if (bestIndex != -1)
           {
@@ -450,6 +435,7 @@ Move MovePicker::pick_move_from_list() {
 /// picked next move. It can be used in search to further differentiate
 /// according to the current move type: capture, non capture, escape, etc.
 MovePicker::MovegenPhase MovePicker::current_move_type() const {
+
   return PhaseTable[phaseIndex];
 }