]> git.sesse.net Git - stockfish/commitdiff
Introduce and use struct MoveList
authorMarco Costalba <mcostalba@gmail.com>
Sat, 2 Jul 2011 12:33:06 +0000 (13:33 +0100)
committerMarco Costalba <mcostalba@gmail.com>
Sat, 2 Jul 2011 13:04:33 +0000 (14:04 +0100)
No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
src/book.cpp
src/move.cpp
src/movegen.h
src/position.cpp
src/search.cpp
src/tt.cpp

index ed5cba4b0eb208b7b3ae3eff52226fe0013c5c3b..0cf4ed0d6c262d97228cc427ac59d3a384e71dfd 100644 (file)
@@ -455,18 +455,16 @@ Move Book::get_move(const Position& pos, bool findBestMove) {
   // book move is a promotion we have to convert to our representation, in
   // all other cases we can directly compare with a Move after having
   // masked out special Move's flags that are not supported by PolyGlot.
-  int p = (bookMove >> 12) & 7;
+  int promotion = (bookMove >> 12) & 7;
 
-  if (p)
+  if (promotion)
       bookMove = int(make_promotion_move(move_from(Move(bookMove)),
-                 move_to(Move(bookMove)), PieceType(p + 1)));
-
-  // Verify the book move (if any) is legal
-  MoveStack mlist[MAX_MOVES];
-  MoveStack* last = generate<MV_LEGAL>(pos, mlist);
-  for (MoveStack* cur = mlist; cur != last; cur++)
-      if ((int(cur->move) & ~(3 << 14)) == bookMove) // Mask out special flags
-          return cur->move;
+                                         move_to(Move(bookMove)),
+                                         PieceType(promotion + 1)));
+  // Verify the book move is legal
+  for (MoveList<MV_LEGAL> ml(pos); !ml.end(); ++ml)
+      if ((ml.move() & ~(3 << 14)) == bookMove) // Mask out special flags
+          return ml.move();
 
   return MOVE_NONE;
 }
index 3c906b2a7d06b9fda1559e597a31ed9baf5c6bc6..060e206bfd36a3e2bc081eca2242074da2e3a594 100644 (file)
@@ -71,12 +71,9 @@ const string move_to_uci(Move m, bool chess960) {
 
 Move move_from_uci(const Position& pos, const string& str) {
 
-  MoveStack mlist[MAX_MOVES];
-  MoveStack* last = generate<MV_LEGAL>(pos, mlist);
-
-  for (MoveStack* cur = mlist; cur != last; cur++)
-      if (str == move_to_uci(cur->move, pos.is_chess960()))
-          return cur->move;
+  for (MoveList<MV_LEGAL> ml(pos); !ml.end(); ++ml)
+      if (str == move_to_uci(ml.move(), pos.is_chess960()))
+          return ml.move();
 
   return MOVE_NONE;
 }
index 580ca2e74645cfd183b6457ec0c1a6fe594fd5d0..4e58d0ffaea115d9e801deb8d5fb175f5f19a209 100644 (file)
@@ -36,4 +36,18 @@ enum MoveType {
 template<MoveType>
 MoveStack* generate(const Position& pos, MoveStack* mlist);
 
+template<MoveType T>
+struct MoveList {
+
+  explicit MoveList(const Position& pos) : cur(mlist), last(generate<T>(pos, mlist)) {}
+  void operator++() { cur++; }
+  bool end() const { return cur == last; }
+  Move move() const { return cur->move; }
+  int size() const { return last - mlist; }
+
+private:
+  MoveStack mlist[MAX_MOVES];
+  MoveStack *cur, *last;
+};
+
 #endif // !defined(MOVEGEN_H_INCLUDED)
index 1e01dc1f7298f5ece888713fb3edcdf3fd215910..7490953dbe3dd16a2034303bde978fe00ad4c10d 100644 (file)
@@ -1724,8 +1724,7 @@ template bool Position::is_draw<true>() const;
 
 bool Position::is_mate() const {
 
-  MoveStack moves[MAX_MOVES];
-  return in_check() && generate<MV_LEGAL>(*this, moves) == moves;
+  return in_check() && !MoveList<MV_LEGAL>(*this).size();
 }
 
 
index 4402cbd5788f8f7da347c9ee85522a69c8c890ab..3512f20e376b8f354947b93399a0be740e3c11e3 100644 (file)
@@ -363,27 +363,24 @@ void init_search() {
 
 int64_t perft(Position& pos, Depth depth) {
 
-  MoveStack mlist[MAX_MOVES];
   StateInfo st;
-  Move m;
   int64_t sum = 0;
 
   // Generate all legal moves
-  MoveStack* last = generate<MV_LEGAL>(pos, mlist);
+  MoveList<MV_LEGAL> ml(pos);
 
   // If we are at the last ply we don't need to do and undo
   // the moves, just to count them.
   if (depth <= ONE_PLY)
-      return int(last - mlist);
+      return ml.size();
 
   // Loop through all legal moves
   CheckInfo ci(pos);
-  for (MoveStack* cur = mlist; cur != last; cur++)
+  for ( ; !ml.end(); ++ml)
   {
-      m = cur->move;
-      pos.do_move(m, st, ci, pos.move_gives_check(m, ci));
+      pos.do_move(ml.move(), st, ci, pos.move_gives_check(ml.move(), ci));
       sum += perft(pos, depth - ONE_PLY);
-      pos.undo_move(m);
+      pos.undo_move(ml.move());
   }
   return sum;
 }
@@ -1992,25 +1989,22 @@ split_point_start: // At split points actual search starts from here
 
   void RootMoveList::init(Position& pos, Move searchMoves[]) {
 
-    MoveStack mlist[MAX_MOVES];
     Move* sm;
-
-    clear();
     bestMoveChanges = 0;
+    clear();
 
     // Generate all legal moves and add them to RootMoveList
-    MoveStack* last = generate<MV_LEGAL>(pos, mlist);
-    for (MoveStack* cur = mlist; cur != last; cur++)
+    for (MoveList<MV_LEGAL> ml(pos); !ml.end(); ++ml)
     {
-        // If we have a searchMoves[] list then verify cur->move
+        // If we have a searchMoves[] list then verify the move
         // is in the list before to add it.
-        for (sm = searchMoves; *sm && *sm != cur->move; sm++) {}
+        for (sm = searchMoves; *sm && *sm != ml.move(); sm++) {}
 
-        if (searchMoves[0] && *sm != cur->move)
+        if (sm != searchMoves && *sm != ml.move())
             continue;
 
         RootMove rm;
-        rm.pv[0] = cur->move;
+        rm.pv[0] = ml.move();
         rm.pv[1] = MOVE_NONE;
         rm.pv_score = -VALUE_INFINITE;
         push_back(rm);
index 86b16c85686ea8724e6f3fc9fc7748fd273bc755..b2701ef0d2c7688b6dd12a03da5afc8f090aa005 100644 (file)
@@ -17,7 +17,6 @@
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
-#include <cassert>
 #include <cstring>
 #include <iostream>
 
@@ -60,7 +59,7 @@ void TranspositionTable::set_size(size_t mbSize) {
   if (!entries)
   {
       std::cerr << "Failed to allocate " << mbSize
-                << " MB for transposition table." << std::endl;
+                << "MB for transposition table." << std::endl;
       exit(EXIT_FAILURE);
   }
   clear();