Nicolas Winkler 7 rokov pred
rodič
commit
745b56f99c
9 zmenil súbory, kde vykonal 63 pridanie a 17 odobranie
  1. 1 1
      src/Bitboard.h
  2. 9 2
      src/ChessGame.cpp
  3. 7 3
      src/ChessGame.h
  4. 2 0
      src/Evaluate.cpp
  5. 7 2
      src/Evaluate.h
  6. 18 2
      src/Minimax.cpp
  7. 1 1
      src/Minimax.h
  8. 17 5
      src/TranspositionTable.h
  9. 1 1
      src/Util.h

+ 1 - 1
src/Bitboard.h

@@ -116,7 +116,7 @@ struct chessy::Move
 
     inline Move(Index origin, Index destination, PieceType promotion) :
         origin{ origin }, destination{ destination }, promotion{ promotion },
-        isCastling{ false } {}
+        isCastling{ false }, isEnPassant{ false } {}
 
     inline Move(Index origin, Index destination, bool isCastling) :
         origin{ origin }, destination{ destination },

+ 9 - 2
src/ChessGame.cpp

@@ -8,8 +8,7 @@
 using namespace chessy;
 
 MoveInfo::MoveInfo(Move move, const ChessGame& cg) :
-    move{ move }, movedPiece{ cg.getBoard().getAtPosition(move.origin) },
-    enPassantTarget{ 0 }
+    move{ move }, movedPiece{ cg.getBoard().getAtPosition(move.origin) }
 {
 }
 
@@ -44,6 +43,9 @@ void ChessGame::loadFromFen(const std::string& fenString)
 {
     using namespace std;
 
+    // push zobrist hash of current position
+    history.push_back(hash);
+
     stringstream tokenizer (fenString);
 
     string board;
@@ -151,6 +153,10 @@ std::string ChessGame::generateFen(void) const
 UndoInfo ChessGame::doMove(const MoveInfo& mi)
 {
     assert(hash == zobrist::getHash(*this)); // valid zobrist hash
+   
+    std::string fen = this->generateFen();
+    // push zobrist hash of current position
+    history.push_back(hash);
 
     UndoInfo ui;
     ui.hash = hash;
@@ -246,6 +252,7 @@ void ChessGame::undoMove(const UndoInfo& ui)
     castlingRights = ui.castlingRights;
     enPassant = ui.enPassantBefore;
     hash = ui.hash;
+    history.pop_back();
 
     assert(hash == zobrist::getHash(*this)); // valid zobrist hash
 }

+ 7 - 3
src/ChessGame.h

@@ -31,10 +31,9 @@ struct chessy::MoveInfo
 
     //! if the move is an en-passant move, this field holds the square of
     //! the pawn, that was captured. Otherwise this index should be zero.
-    Index enPassantTarget;
+    //Index enPassantTarget;
 
-    inline MoveInfo(void) :
-        enPassantTarget{ 0 } {}
+    MoveInfo(void) = default;
 
     MoveInfo(Move move, const ChessGame& cg);
 };
@@ -78,6 +77,8 @@ class chessy::ChessGame
     //! holds the zobrist hash value for the current position
     HashValue hash;
 
+    std::vector<HashValue> history;
+
 public:
 
     ChessGame(void);
@@ -112,6 +113,9 @@ public:
 
     inline int getCastlingRights(void) const { return castlingRights; }
 
+    inline HashValue getHash(void) const { return hash; }
+    inline const std::vector<HashValue>& getHistory(void) const { return history; }
+
     void move(Move move);
     inline Side getTurn(void) const { return turn; }
 

+ 2 - 0
src/Evaluate.cpp

@@ -1,5 +1,7 @@
 #include "Evaluate.h"
 
+#include "ChessGame.h"
+
 using namespace chessy;
 
 

+ 7 - 2
src/Evaluate.h

@@ -1,12 +1,17 @@
 #ifndef CHESSY_EVALUATE_H
 #define CHESSY_EVALUATE_H
 
-#include "ChessGame.h"
+
+#include "BitBoard.h"
 
 namespace chessy
 {
     using MoveValue = float;
 
+
+    // forward declaration
+    class ChessGame;
+
     namespace values
     {
         extern const MoveValue PAWN;
@@ -24,5 +29,5 @@ namespace chessy
 
 
 
-#endif // CHESSY_MIrIMAX_H
+#endif // CHESSY_EVALUATE_H
 

+ 18 - 2
src/Minimax.cpp

@@ -218,8 +218,6 @@ MoveValue chessy::quiescence(ChessGame& cg, int maxDepth,
         MoveValue alpha, MoveValue beta)
 {
     MoveValue standingPat = evaluate(cg);
-    if (standingPat == 0.0f)
-        int s = 23;
     if (maxDepth <= 0)
         return standingPat;
 
@@ -228,6 +226,24 @@ MoveValue chessy::quiescence(ChessGame& cg, int maxDepth,
     if(standingPat > alpha)
         alpha = standingPat;
 
+    HashValue hv = cg.getHash();
+    const auto& history = cg.getHistory();
+    int equalCount = 0;
+    /*for (auto i = cg.getHistory().cend() - 1; i >= (history.size() > 100? history.cend()-99 : history.cbegin()); i--) {
+        if (*i == hv) {
+            equalCount++;
+            if (equalCount >= 3)
+                return 0;
+        }
+    }*/
+    for (auto hash : history) {
+        if (hash == hv) {
+            equalCount++;
+            if (equalCount >= 3)
+                return 0;
+        }
+    }
+
     const Board& b = cg.getBoard();
     auto isCheck = [&cg, &b] (Side turn) {
         return turn == BLACK_SIDE ? b.isCheck<WHITE_SIDE>() :

+ 1 - 1
src/Minimax.h

@@ -38,5 +38,5 @@ namespace chessy
 
 
 
-#endif // CHESSY_MIrIMAX_H
+#endif // CHESSY_MINIMAX_H
 

+ 17 - 5
src/TranspositionTable.h

@@ -1,22 +1,26 @@
 #ifndef CHESSY_TRANSPOSITIONTABLE_H
 #define CHESSY_TRANSPOSITIONTABLE_H
+
 #include <array>
 #include <cinttypes>
+#include <unordered_map>
 
+#include "Evaluate.h"
 #include "Bitboard.h"
 
 namespace chessy
 {
     using HashValue = uint64_t;
+
+    struct PositionInfo;
     class TranspositionTable;
 
     // forward declaration
     class ChessGame;
-}
 
-
-namespace chessy
-{
+    /*!
+     * \brief namespace containing the constant values used for zobrist hashing.
+     */
     namespace zobrist
     {
         extern std::array<HashValue, 64 * 12> pieces;
@@ -36,9 +40,17 @@ namespace chessy
 }
 
 
-class chessy::TranspositionTable
+struct chessy::PositionInfo
 {
+    int evaluatedDepth;
+    MoveValue value;
+    Move bestMove;
+};
+
 
+class chessy::TranspositionTable
+{
+    std::unordered_map<HashValue, PositionInfo> table;
 };
 
 #endif //CHESSY_TRANSPOSITIONTABLE_H

+ 1 - 1
src/Util.h

@@ -2,7 +2,7 @@
 #define CHESSY_UTIL_H
 
 
-#define NDEBUG
+//#define NDEBUG
 
 #include <cassert>