Nicolas Winkler 7 سال پیش
والد
کامیت
aa36a944c7
1فایلهای تغییر یافته به همراه64 افزوده شده و 44 حذف شده
  1. 64 44
      src/Search.h

+ 64 - 44
src/Search.h

@@ -2,6 +2,7 @@
 #define CHESSY_SEARCH_H
 
 #include "ChessGame.h"
+#include <utility>
 
 namespace chessy
 {
@@ -24,6 +25,9 @@ public:
 
     template<typename... Args>
     inline void iterateAll(Args&&... args);
+
+    template<Side side, typename... Args>
+    inline void iteratePawns(Args&&... args);
 private:
 };
 
@@ -41,16 +45,19 @@ inline void aga() {
     chessy::ChessGame cg;
     chessy::MinimaxN mm;
     chessy::Search<chessy::MinimaxN> search = { mm, cg };
-    search.iterateAll(5, 8ULL);
+    search.iterateAll(5, 7);
 }
 
 
+template<typename T>
 template<typename... Args>
-inline void chessy::Search::iterateAll(Args&&... args)
+inline void chessy::Search<T>::iterateAll(Args&&... args)
 {
     Board& board = game.getBoard();
+
     Bitboard friends;
     Bitboard enemies;
+    Side side = WHITE_SIDE;
     if (side == WHITE_SIDE) {
         friends = board.getWhites();
         enemies = board.getBlacks();
@@ -61,49 +68,12 @@ inline void chessy::Search::iterateAll(Args&&... args)
     }
 
     const Board temp = board;
-    PawnPushGenerator<side> mg{ game };
-    for (Move push : mg) {
-        Bitboard& pawns = board.getPawns<side>();
-        Bitboard pTemp = pawns;
-        pawns.applyMove(push);
-        BestMove m = minimax<otherSide(side)>(depth - 1);
-        m.move = push;
-        bestMove.overwriteIfBetter(m);
-        pawns = pTemp;
-    }
-
-    PawnDoublePushGenerator<side> dp{ game };
-    for (Move push : dp) {
-        Bitboard& pawns = board.getPawns<side>();
-        Bitboard pTemp = pawns;
-        pawns.applyMove(push);
-        BestMove m = minimax<otherSide(side)>(depth - 1);
-        m.move = push;
-        bestMove.overwriteIfBetter(m);
-        pawns = pTemp;
-    }
-
-    PawnCaptureGenerator<side, LEFT> pl{ game };
-    for (Move capture : pl) {
-        Bitboard& pawns = board.getPawns<side>();
-        board.removeAt(capture.destination);
-        pawns.applyMove(capture);
-        BestMove m = minimax<otherSide(side)>(depth - 1);
-        m.move = capture;
-        bestMove.overwriteIfBetter(m);
-        board = temp;
-    }
     
-    PawnCaptureGenerator<side, RIGHT> pr{ game };
-    for (Move capture : pr) {
-        Bitboard& pawns = board.getPawns<side>();
-        board.removeAt(capture.destination);
-        pawns.applyMove(capture);
-        BestMove m = minimax<otherSide(side)>(depth - 1);
-        m.move = capture;
-        bestMove.overwriteIfBetter(m);
-        board = temp;
-    }
+    if (side == WHITE_SIDE)
+        iteratePawns<WHITE_SIDE, Args...>(std::forward<Args...>(args)...);
+    else
+        iteratePawns<BLACK_SIDE, Args...>(std::forward<Args...>(args)...);
+
 
 
     Bitboard& ns = board.getKnights<side>();
@@ -182,4 +152,54 @@ inline void chessy::Search::iterateAll(Args&&... args)
 }
 
 
+template<typename T>
+template<chessy::Side side, typename... Args>
+inline void chessy::Search<T>::iteratePawns(Args&&... args)
+{
+    PawnPushGenerator<side> mg{ game };
+    for (Move push : mg) {
+        Bitboard& pawns = board.getPawns<side>();
+        Bitboard pTemp = pawns;
+        pawns.applyMove(push);
+        BestMove m = minimax<otherSide(side)>(depth - 1);
+        m.move = push;
+        bestMove.overwriteIfBetter(m);
+        pawns = pTemp;
+    }
+
+    PawnDoublePushGenerator<side> dp{ game };
+    for (Move push : dp) {
+        Bitboard& pawns = board.getPawns<side>();
+        Bitboard pTemp = pawns;
+        pawns.applyMove(push);
+        BestMove m = minimax<otherSide(side)>(depth - 1);
+        m.move = push;
+        bestMove.overwriteIfBetter(m);
+        pawns = pTemp;
+    }
+
+    PawnCaptureGenerator<side, LEFT> pl{ game };
+    for (Move capture : pl) {
+        Bitboard& pawns = board.getPawns<side>();
+        board.removeAt(capture.destination);
+        pawns.applyMove(capture);
+        BestMove m = minimax<otherSide(side)>(depth - 1);
+        m.move = capture;
+        bestMove.overwriteIfBetter(m);
+        board = temp;
+    }
+
+    PawnCaptureGenerator<side, RIGHT> pr{ game };
+    for (Move capture : pr) {
+        Bitboard& pawns = board.getPawns<side>();
+        board.removeAt(capture.destination);
+        pawns.applyMove(capture);
+        BestMove m = minimax<otherSide(side)>(depth - 1);
+        m.move = capture;
+        bestMove.overwriteIfBetter(m);
+        board = temp;
+    }
+}
+
+
 #endif // CHESSY_SEARCH_H