Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Napoleon - transpositiontable.cpp

transpositiontable.cpp

Caricato da: Crybot
Scarica il programma completo

  1. #include "transpositiontable.h"
  2. #include "constants.h"
  3. #include <ctime>
  4. namespace Napoleon
  5. {
  6.     TranspositionTable::TranspositionTable(unsigned long size)
  7.     {
  8.         BucketSize = 4;
  9.         Size = size/(sizeof(HashEntry*)*BucketSize);
  10.  
  11.         Table = new HashEntry*[Size];
  12.         for (unsigned i=0; i<Size; i++)
  13.         {
  14.             Table[i] = new HashEntry[BucketSize];
  15.         }
  16.     }
  17.  
  18.     void TranspositionTable::Save(ZobristKey key, Byte depth, int score, Move move, Byte bound)
  19.     {
  20.         HashEntry* hash;
  21.         int min = Constants::MaxPly;
  22.  
  23.         for (int i=0; i<BucketSize; i++)
  24.         {
  25.             hash = &Table[key % Size][i];
  26.             if (hash->Depth < min)
  27.                 min = hash->Depth;
  28.         }
  29.  
  30.         hash = &Table[key % Size][min];
  31.  
  32.         hash->Hash = key;
  33.         hash->Score = score;
  34.         hash->Depth = depth;
  35.         hash->Bound = bound;
  36.         hash->BestMove = move;
  37.     }
  38.  
  39.     int TranspositionTable::Probe(ZobristKey key, Byte depth, int alpha, Move* move, int beta)
  40.     {
  41.         HashEntry* hash = &Table[key % Size][0];
  42.  
  43.         for (int i=0; i<BucketSize; i++, hash++)
  44.         {
  45.             if (hash->Hash == key)
  46.             {
  47.                 if (hash->Depth >= depth)
  48.                 {
  49.                     if (hash->Bound == Exact)
  50.                         return hash->Score;
  51.                     if (hash->Bound == Alpha && hash->Score <= alpha)
  52.                         return alpha;
  53.                     if (hash->Bound == Beta && hash->Score >= beta)
  54.                         return beta;
  55.                 }
  56.                 *move = hash->BestMove; // get best move on this position
  57.             }
  58.         }
  59.         return TranspositionTable::Unknown;
  60.     }
  61.  
  62.     void TranspositionTable::Clear()
  63.     {
  64.         for (unsigned i=0 ; i<Size; i++)
  65.         {
  66.             for (int l=0; l<BucketSize; l++)
  67.                 Table[i][l].Hash = 0;
  68.         }
  69.     }
  70.  
  71.     Move TranspositionTable::GetPv(ZobristKey key)
  72.     {
  73.         HashEntry* hash = &Table[key % Size][0];
  74.  
  75.         for (int i=0; i<BucketSize; i++, hash++)
  76.         {
  77.             if (hash->Hash == key)
  78.             {
  79.                 if (!hash->BestMove.IsNull())
  80.                     return hash->BestMove;
  81.             }
  82.         }
  83.  
  84.         return Constants::NullMove;
  85.     }
  86. }