Nicolas Winkler 7 anni fa
parent
commit
55d4ad4a4f
6 ha cambiato i file con 137 aggiunte e 67 eliminazioni
  1. 9 0
      src/BitBoard.h
  2. 77 0
      src/Minimax.cpp
  3. 46 0
      src/Minimax.h
  4. 3 49
      src/MoveGeneration.cpp
  5. 1 17
      src/MoveGeneration.h
  6. 1 1
      src/UciParser.cpp

+ 9 - 0
src/BitBoard.h

@@ -13,6 +13,15 @@ namespace chessy
     union Index;
 
     struct Bitboard;
+
+    using Side = int;
+
+    const Side WHITE_SIDE = 0;
+    const Side BLACK_SIDE = 1;
+
+    inline constexpr Side otherSide(Side side) {
+        return !side;
+    }
 }
 
 /*!

+ 77 - 0
src/Minimax.cpp

@@ -0,0 +1,77 @@
+#include "Minimax.h"
+#include "ChessGame.h"
+
+using namespace chessy;
+
+
+template<Side side>
+float chessy::MiniMax::evaluate(void) const
+{
+    return 0.0f;
+}
+
+
+template<Side side>
+float MiniMax::minimax(int depth)
+{
+    if (depth == 0) {
+        return { {0, 0}, evaluate() };
+    }
+    BestMove bestMove = { {0, 0}, 0.0f };
+
+    Bitboard friends;
+    Bitboard enemies;
+    Board& board = game.getBoard();
+    if (side == WHITE_SIDE) {
+        friends = board.getWhites();
+        enemies = board.getBlacks();
+    }
+    else {
+        friends = board.getBlacks();
+        enemies = board.getWhites();
+    }
+
+    PawnPushGenerator<side> mg{ game };
+    for (Move push : mg) {
+        Bitboard& pawns = board.getPawns<side>();
+        Bitboard temp = pawns;
+        pawns &= ~(Bitboard::fromIndex(push.origin));
+        pawns |= Bitboard::fromIndex(push.destination);
+        bestMove.overwriteIfBetter(minmax<!side>(depth - 1));
+    }
+        
+    PositionSet knights = game.getBoard().getBlackKnights();
+    for (auto knight : knights) {
+        for (auto pos : KnightMoveGenerator{ knight, blacks }) {
+            ret.push_back(Move{ knight, pos });
+        }
+    }
+
+    PositionSet bishops = cg.getBoard().getBlackBishops();
+    for (auto bishop : bishops) {
+        for (auto pos : PrimitiveBishopMoveGenerator{ bishop, whites, blacks }) {
+            ret.push_back(Move{ bishop, pos });
+        }
+    }
+
+    PositionSet rooks = cg.getBoard().getBlackRooks();
+    for (auto rook : rooks) {
+        for (auto pos : PrimitiveRookMoveGenerator{ rook, whites, blacks }) {
+            //cout << "rook: " << Move(rook, pos).asString() << endl;
+        }
+    }
+
+    PositionSet queens = cg.getBoard().getBlackQueens();
+    for (auto queen : queens) {
+        for (auto pos : PrimitiveQueenMoveGenerator{ queen, whites, blacks }) {
+            //cout << "queen: " << Move(queen, pos).asString() << endl;
+        }
+    }
+
+    Bitboard king = cg.getBoard().getBlackKings();
+    Index kingIndex = king.getLeastSignificantBit();
+    for (auto pos : KingMoveGenerator{ king, blacks }) {
+        //cout << "king: " << Move(kingIndex, pos).asString() << endl;
+    }
+    return {0, 0};
+}

+ 46 - 0
src/Minimax.h

@@ -0,0 +1,46 @@
+#ifndef CHESSY_MINMAX_H
+#define CHESSY_MINIMAX_H
+#include "MoveGeneration.h"
+
+
+namespace chessy
+{
+    class MiniMax;
+}
+
+
+class chessy::MiniMax
+{
+    ChessGame& game;
+
+    struct BestMove {
+        Move move;
+        float value;
+
+        inline void overwriteIfBetter(BestMove other)
+        {
+            if (other.value > value)
+                *this = other;
+        }
+    };
+
+public:
+    inline MiniMax(ChessGame& game) :
+        game{ game } {}
+
+    Move calculateBest(int depth)
+    {
+        minimax<WHITE_SIDE>(depth);
+        return {0, 0};
+    }
+
+    template<Side side>
+    float evaluate(void) const;
+
+private:
+    template<Side side>
+    float minimax(int depth);
+};
+
+
+#endif // CHESSY_MINIMAX_H

+ 3 - 49
src/MoveGeneration.cpp

@@ -9,57 +9,11 @@ template class PawnPushGenerator<BLACK_SIDE>;
 
 std::string Move::asString(void) const
 {
-    return origin.getName() + "-" + destination.getName();
+    return origin.getName() + destination.getName();
 }
 
 
-std::vector<Move> MoveGenerator::generatePossibleMoves(void) const
-{
-    const ChessGame& cg = game;
-    Bitboard whites = cg.getBoard().getWhites();
-    Bitboard blacks = cg.getBoard().getBlacks();
-    std::vector<Move> ret;
-    PawnPushGenerator<BLACK_SIDE> mg{ cg };
-    for (auto push : mg)
-        ret.push_back(push);
-    PositionSet knights = cg.getBoard().getBlackKnights();
-    for (auto knight : knights) {
-        for (auto pos : KnightMoveGenerator{ knight, blacks }) {
-            ret.push_back(Move{ knight, pos });
-        }
-    }
-
-    PositionSet bishops = cg.getBoard().getBlackBishops();
-    for (auto bishop : bishops) {
-        for (auto pos : PrimitiveBishopMoveGenerator{ bishop, whites, blacks }) {
-            ret.push_back(Move{ bishop, pos });
-        }
-    }
-
-    PositionSet rooks = cg.getBoard().getBlackRooks();
-    for (auto rook : rooks) {
-        for (auto pos : PrimitiveRookMoveGenerator{ rook, whites, blacks }) {
-            //cout << "rook: " << Move(rook, pos).asString() << endl;
-        }
-    }
-
-    PositionSet queens = cg.getBoard().getBlackQueens();
-    for (auto queen : queens) {
-        for (auto pos : PrimitiveQueenMoveGenerator{ queen, whites, blacks }) {
-            //cout << "queen: " << Move(queen, pos).asString() << endl;
-        }
-    }
-
-    Bitboard king = cg.getBoard().getBlackKings();
-    Index kingIndex = king.getLeastSignificantBit();
-    for (auto pos : KingMoveGenerator{ king, blacks }) {
-        //cout << "king: " << Move(kingIndex, pos).asString() << endl;
-    }
-    return ret;
-}
-
-
-template<int side>
+template<Side side>
 typename PawnPushGenerator<side>::MoveIterator PawnPushGenerator<side>::begin(void) const
 {
     const Board& board = chessGame.getBoard();
@@ -77,7 +31,7 @@ typename PawnPushGenerator<side>::MoveIterator PawnPushGenerator<side>::begin(vo
 }
 
 
-template<int side>
+template<Side side>
 typename PawnPushGenerator<side>::MoveIterator PawnPushGenerator<side>::end(void) const
 {
     return MoveIterator{ 0 };

+ 1 - 17
src/MoveGeneration.h

@@ -13,8 +13,6 @@ namespace chessy
 
     struct Move;
 
-    class MoveGenerator;
-
     class PositionSet;
     template<int side>
     class PawnPushGenerator;
@@ -23,9 +21,6 @@ namespace chessy
     class PrimitiveRookMoveGenerator;
     class PrimitiveBishopMoveGenerator;
     class KingMoveGenerator;
-
-    const int WHITE_SIDE = 0;
-    const int BLACK_SIDE = 1;
 }
 
 
@@ -48,17 +43,6 @@ struct chessy::Move
 };
 
 
-class chessy::MoveGenerator
-{
-    const ChessGame& game;
-public:
-    inline MoveGenerator(const ChessGame& game) :
-        game{ game } {}
-
-    std::vector<Move> generatePossibleMoves(void) const;
-};
-
-
 class chessy::PositionSet
 {
     Bitboard bitboard;
@@ -98,7 +82,7 @@ public:
 };
 
 
-template<int side>
+template<chessy::Side side>
 class chessy::PawnPushGenerator
 {
     const ChessGame& chessGame;

+ 1 - 1
src/UciParser.cpp

@@ -88,7 +88,7 @@ void UciParser::setoption(const std::vector<std::string>& args)
 {
     Option o;
     
-    optionsManager.setOption();
+    //optionsManager.setOption();
 }