Nicolas Winkler 7 tahun lalu
induk
melakukan
257dc86854
4 mengubah file dengan 47 tambahan dan 37 penghapusan
  1. 17 11
      src/Minimax.cpp
  2. 1 0
      src/Minimax.h
  3. 22 19
      src/MoveGeneration.cpp
  4. 7 7
      src/MoveGeneration.h

+ 17 - 11
src/Minimax.cpp

@@ -71,11 +71,11 @@ std::pair<Move, MoveValue> chessy::miniMax(ChessGame& cg, int depth)
     std::vector<Move> moves;
     moves.reserve(200);
     if (cg.getTurn() == WHITE_SIDE) {
-        generateAllMoves<WHITE_SIDE>(cg, moves);
+        generateAllMoves<WHITE_SIDE, false>(cg, moves);
         orderMoves<WHITE_SIDE>(cg, moves);
     }
     else {
-        generateAllMoves<BLACK_SIDE>(cg, moves);
+        generateAllMoves<BLACK_SIDE, false>(cg, moves);
         orderMoves<BLACK_SIDE>(cg, moves);
     }
 
@@ -106,7 +106,7 @@ std::pair<Move, MoveValue> chessy::miniMax(ChessGame& cg, int depth)
         }
     }
     if (bestMove.origin == 0 && bestMove.destination == 0) {
-
+        // checkmate
     }
     return { bestMove, alpha }; //negamaxImplementation(chessGame, 5);
 }
@@ -115,12 +115,8 @@ std::pair<Move, MoveValue> chessy::miniMax(ChessGame& cg, int depth)
 MoveValue chessy::negamaxImplementation(ChessGame& cg, int depth,
         chessy::MoveValue alpha, chessy::MoveValue beta)
 {
-    if (depth <= 0) {
-        if (cg.getTurn() == WHITE_SIDE)
-            return evaluate<WHITE_SIDE>(cg);
-        else
-            return evaluate<BLACK_SIDE>(cg);
-    }
+    if (depth <= 0)
+        return evaluate(cg.getTurn(), cg);
 
     const Board& b = cg.getBoard();
     auto isCheck = [&cg, &b] (Side turn) {
@@ -130,11 +126,11 @@ MoveValue chessy::negamaxImplementation(ChessGame& cg, int depth,
 
     std::vector<Move> moves;
     if (cg.getTurn() == WHITE_SIDE) {
-        generateAllMoves<WHITE_SIDE>(cg, moves);
+        generateAllMoves<WHITE_SIDE, false>(cg, moves);
         orderMoves<WHITE_SIDE>(cg, moves);
     }
     else {
-        generateAllMoves<BLACK_SIDE>(cg, moves);
+        generateAllMoves<BLACK_SIDE, false>(cg, moves);
         orderMoves<BLACK_SIDE>(cg, moves);
     }
 
@@ -198,6 +194,16 @@ MoveValue chessy::evaluate(const ChessGame& game)
     return piecePoints;
 }
 
+
+MoveValue chessy::evaluate(Side side, const ChessGame& game)
+{
+    if (side == WHITE_SIDE)
+        evaluate<WHITE_SIDE>(game);
+    else
+        evaluate<BLACK_SIDE>(game);
+}
+
+
 /*
 template MiniMax::BestMove MiniMax::minimax<WHITE_SIDE>(int);
 template MiniMax::BestMove MiniMax::minimax<BLACK_SIDE>(int);

+ 1 - 0
src/Minimax.h

@@ -29,6 +29,7 @@ namespace chessy
 
     template<Side side>
     MoveValue evaluate(const ChessGame& game);
+    MoveValue evaluate(Side side, const ChessGame& game);
 }
 
 

+ 22 - 19
src/MoveGeneration.cpp

@@ -271,7 +271,7 @@ void chessy::generatePawnCaptures(const ChessGame& cg, std::vector<Move>& moves)
 }
 
 
-template<Side side>
+template<Side side, bool capturesOnly>
 void chessy::generatePawnPromotions(const ChessGame& cg, std::vector<Move>& moves)
 {
     PromotionGenerator<side> pg{ cg };
@@ -280,7 +280,7 @@ void chessy::generatePawnPromotions(const ChessGame& cg, std::vector<Move>& move
 }
 
 
-template<Side side>
+template<Side side, bool capturesOnly>
 void chessy::generateKnightMoves(const ChessGame& cg, std::vector<Move>& moves)
 {
     const Board& b = cg.getBoard();
@@ -294,7 +294,7 @@ void chessy::generateKnightMoves(const ChessGame& cg, std::vector<Move>& moves)
 }
 
 
-template<Side side, typename Generator>
+template<Side side, bool capturesOnly, typename Generator>
 void generateMoves(Bitboard position, Bitboard enemies, Bitboard friendly,
     std::vector<Move>& moves)
 {
@@ -308,37 +308,37 @@ void generateMoves(Bitboard position, Bitboard enemies, Bitboard friendly,
 }
 
 
-template<Side side>
+template<Side side, bool capturesOnly>
 void chessy::generateQueenMoves(const ChessGame& cg, std::vector<Move>& moves)
 {
     const Board& b = cg.getBoard();
-    generateMoves<side, PrimitiveQueenMoveGenerator>(
+    generateMoves<side, capturesOnly, PrimitiveQueenMoveGenerator>(
         b.getQueens<side>(), b.get<otherSide(side)>(), b.get<side>(), moves
     );
 }
 
 
-template<Side side>
+template<Side side, bool capturesOnly>
 void chessy::generateRookMoves(const ChessGame& cg, std::vector<Move>& moves)
 {
     const Board& b = cg.getBoard();
-    generateMoves<side, PrimitiveRookMoveGenerator>(
+    generateMoves<side, capturesOnly, PrimitiveRookMoveGenerator>(
         b.getRooks<side>(), b.get<otherSide(side)>(), b.get<side>(), moves
     );
 }
 
 
-template<Side side>
+template<Side side, bool capturesOnly>
 void chessy::generateBishopMoves(const ChessGame& cg, std::vector<Move>& moves)
 {
     const Board& b = cg.getBoard();
-    generateMoves<side,PrimitiveBishopMoveGenerator>(
+    generateMoves<side, capturesOnly, PrimitiveBishopMoveGenerator>(
         b.getBishops<side>(), b.get<otherSide(side)>(), b.get<side>(), moves
     );
 }
 
 
-template<Side side>
+template<Side side, bool capturesOnly>
 void chessy::generateKingMoves(const ChessGame& cg, std::vector<Move>& moves)
 {
     const Board& b = cg.getBoard();
@@ -388,18 +388,18 @@ void chessy::generateEnPassant(const ChessGame& cg, std::vector<Move>& moves)
     }
 }
 
-template<Side side>
+template<Side side, bool capturesOnly>
 void chessy::generateAllMoves(const ChessGame& cg, std::vector<Move>& moves)
 {
     generatePawnPushes<side>(cg, moves);
     generatePawnDoublePushes<side>(cg, moves);
     generatePawnCaptures<side>(cg, moves);
-    generatePawnPromotions<side>(cg, moves);
-    generateKnightMoves<side>(cg, moves);
-    generateQueenMoves<side>(cg, moves);
-    generateBishopMoves<side>(cg, moves);
-    generateRookMoves<side>(cg, moves);
-    generateKingMoves<side>(cg, moves);
+    generatePawnPromotions<side, false>(cg, moves);
+    generateKnightMoves<side, false>(cg, moves);
+    generateQueenMoves<side, false>(cg, moves);
+    generateBishopMoves<side, false>(cg, moves);
+    generateRookMoves<side, false>(cg, moves);
+    generateKingMoves<side, false>(cg, moves);
     generateCastling<side>(cg, moves);
     generateEnPassant<side>(cg, moves);
 }
@@ -444,8 +444,11 @@ namespace chessy
     template class CastlingGenerator<WHITE_SIDE>;
     template class CastlingGenerator<BLACK_SIDE>;
 
-    template void generateAllMoves<WHITE_SIDE>(const ChessGame&, std::vector<Move>&);
-    template void generateAllMoves<BLACK_SIDE>(const ChessGame&, std::vector<Move>&);
+    template void generateAllMoves<WHITE_SIDE, false>(const ChessGame&, std::vector<Move>&);
+    template void generateAllMoves<BLACK_SIDE, false>(const ChessGame&, std::vector<Move>&);
+
+    template void generateAllMoves<WHITE_SIDE, true>(const ChessGame&, std::vector<Move>&);
+    template void generateAllMoves<BLACK_SIDE, true>(const ChessGame&, std::vector<Move>&);
 
     template void orderMoves<WHITE_SIDE>(const ChessGame&, std::vector<Move>&);
     template void orderMoves<BLACK_SIDE>(const ChessGame&, std::vector<Move>&);

+ 7 - 7
src/MoveGeneration.h

@@ -38,23 +38,23 @@ namespace chessy
     void generatePawnDoublePushes(const ChessGame& cg, std::vector<Move>& moves);
     template<Side side>
     void generatePawnCaptures(const ChessGame& cg, std::vector<Move>& moves);
-    template<Side side>
+    template<Side side, bool capturesOnly>
     void generatePawnPromotions(const ChessGame& cg, std::vector<Move>& moves);
-    template<Side side>
+    template<Side side, bool capturesOnly>
     void generateKnightMoves(const ChessGame& cg, std::vector<Move>& moves);
-    template<Side side>
+    template<Side side, bool capturesOnly>
     void generateQueenMoves(const ChessGame& cg, std::vector<Move>& moves);
-    template<Side side>
+    template<Side side, bool capturesOnly>
     void generateRookMoves(const ChessGame& cg, std::vector<Move>& moves);
-    template<Side side>
+    template<Side side, bool capturesOnly>
     void generateBishopMoves(const ChessGame& cg, std::vector<Move>& moves);
-    template<Side side>
+    template<Side side, bool capturesOnly>
     void generateKingMoves(const ChessGame& cg, std::vector<Move>& moves);
     template<Side side>
     void generateCastling(const ChessGame& cg, std::vector<Move>& moves);
     template<Side side>
     void generateEnPassant(const ChessGame& cg, std::vector<Move>& moves);
-    template<Side side>
+    template<Side side, bool capturesOnly>
     void generateAllMoves(const ChessGame& cg, std::vector<Move>& moves);
     template<Side side>
     void orderMoves(const ChessGame& cg, std::vector<Move>& moves);