Nicolas Winkler 7 anos atrás
pai
commit
b435c1c701
2 arquivos alterados com 77 adições e 0 exclusões
  1. 38 0
      src/TranspositionTable.cpp
  2. 39 0
      src/TranspositionTable.h

+ 38 - 0
src/TranspositionTable.cpp

@@ -0,0 +1,38 @@
+#include "TranspositionTable.h"
+#include "MoveGeneration.h"
+#include <random>
+
+using namespace chessy;
+
+
+HashValue ZobristValues::getHash(const ChessGame& cg) const
+{
+    HashValue value;
+    const Board& b = cg.getBoard();
+    // iterate over all piece types
+    for (int i = 0; i < 12; i++) {
+        Bitboard t = b.getBitboards(i < 6 ? WHITE_SIDE : BLACK_SIDE)[i];
+        for (auto pos : PositionSet{ t }) {
+            value ^= getNumber(PieceType(i), pos);
+        }
+    }
+    value ^= castlingRights[cg.getCastlingRights()];
+    if (cg.getEnPassantIndex() != -1)
+        value ^= enPassant[cg.getEnPassantIndex()];
+}
+
+
+void ZobristValues::create(uint64_t seed)
+{
+    std::mt19937_64 engine(seed);
+    std::uniform_int_distribution<HashValue> distribution;
+    auto rnd = [&]() { return distribution(engine); };
+
+    for (auto& v : pieces)
+        v = rnd();
+    for (auto& v : castlingRights)
+        v = rnd();
+    for (auto& v : enPassant)
+        v = rnd();
+    turn = rnd();
+}

+ 39 - 0
src/TranspositionTable.h

@@ -0,0 +1,39 @@
+#ifndef CHESSY_TRANSPOSITIONTABLE_H
+#define CHESSY_TRANSPOSITIONTABLE_H
+#include <array>
+#include <cinttypes>
+
+#include "ChessGame.h"
+
+namespace chessy
+{
+    using HashValue = uint64_t;
+    struct ZobristValues;
+    class TranspositionTable;
+}
+
+
+struct chessy::ZobristValues
+{
+    std::array<HashValue, 64 * 12> pieces;
+    std::array<HashValue, 16> castlingRights;
+    std::array<HashValue, 8> enPassant;
+    HashValue turn;
+
+    HashValue getHash(const ChessGame&) const;
+
+    inline HashValue getNumber(PieceType p, Index i) const
+    {
+        return pieces[static_cast<int>(p) * 64 + i];
+    }
+
+    void create(uint64_t seed);
+};
+
+
+class chessy::TranspositionTable
+{
+
+};
+
+#endif //CHESSY_TRANSPOSITIONTABLE_H