From 42f6b5d0d249d931d0f881e8e896fe2bac7206c0 Mon Sep 17 00:00:00 2001 From: CalciteM Date: Sun, 7 Jul 2019 10:48:19 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20recordHash()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- NineChess/src/ninechessai_ab.cpp | 29 +++++++++++++++++++++-------- NineChess/src/ninechessai_ab.h | 5 ++++- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/NineChess/src/ninechessai_ab.cpp b/NineChess/src/ninechessai_ab.cpp index ca084fb8..da9c7c3b 100644 --- a/NineChess/src/ninechessai_ab.cpp +++ b/NineChess/src/ninechessai_ab.cpp @@ -680,23 +680,19 @@ int NineChessAi_ab::alphaBetaPruning(int depth, int alpha, int beta, Node *node) #endif #ifdef HASH_MAP_ENABLE - // 添加到hashmap - hashMapMutex.lock(); if (iter == hashmap.end()) { - HashValue hashValue; - hashValue.value = node->value; - hashValue.depth = depth; - if (hashmap.size() <= maxHashCount) - hashmap.insert({hash, hashValue}); + // 添加到hashmap + recordHash(hash, depth, node->value, hashfEMPTY); } // 更新更深层数据 else { + hashMapMutex.lock(); if (iter->second.depth < depth) { iter->second.value = node->value; iter->second.depth = depth; } + hashMapMutex.unlock(); } - hashMapMutex.unlock(); #endif // 排序子节点树 @@ -706,6 +702,23 @@ int NineChessAi_ab::alphaBetaPruning(int depth, int alpha, int beta, Node *node) return node->value; } +int NineChessAi_ab::recordHash(uint64_t hash, int16_t depth, int value, enum HashType type) +{ +#ifdef HASH_MAP_ENABLE + hashMapMutex.lock(); + + HashValue hashValue; + hashValue.value = value; + hashValue.depth = depth; + if (hashmap.size() <= maxHashCount) + hashmap.insert({ hash, hashValue }); + + hashMapMutex.unlock(); +#endif // HASH_MAP_ENABLE + + return 0; +} + const char* NineChessAi_ab::bestMove() { vector bestMoves; diff --git a/NineChess/src/ninechessai_ab.h b/NineChess/src/ninechessai_ab.h index 67c5dcca..335216bb 100644 --- a/NineChess/src/ninechessai_ab.h +++ b/NineChess/src/ninechessai_ab.h @@ -27,7 +27,7 @@ class NineChessAi_ab { public: // 定义哈希值的类型 - enum hashType : int16_t + enum HashType : int16_t { hashfEMPTY = 0, hashfALPHA = 1, @@ -112,6 +112,9 @@ protected: // 增加新节点 struct Node *addNode(Node *parent, int value, NineChess::move_t move, enum NineChess::Player player); + // 插入哈希表 + int recordHash(uint64_t hash, int16_t depth, int value, enum HashType type); + // 评价函数 int evaluate(Node *node);