From 4d72e101d2c6b1d9e6b95f9838717490664e5926 Mon Sep 17 00:00:00 2001 From: CalciteM Date: Sun, 7 Jul 2019 10:24:26 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86=E6=90=9C=E7=B4=A2=E5=93=88=E5=B8=8C?= =?UTF-8?q?=E6=94=BE=E5=9C=A8=E5=8F=B6=E5=AD=90=E7=BB=93=E7=82=B9=E5=A4=84?= =?UTF-8?q?=E7=90=86=E6=B5=81=E7=A8=8B=E4=B9=8B=E5=89=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- NineChess/src/config.h | 2 +- NineChess/src/ninechessai_ab.cpp | 81 +++++++++++++++++--------------- NineChess/src/ninechessai_ab.h | 17 ++++++- 3 files changed, 60 insertions(+), 40 deletions(-) diff --git a/NineChess/src/config.h b/NineChess/src/config.h index d23f4416..eb881b63 100644 --- a/NineChess/src/config.h +++ b/NineChess/src/config.h @@ -42,7 +42,7 @@ #define DRAW_SEAT_NUMBER #endif -#define IDS_SUPPORT +//#define IDS_SUPPORT #define SAVE_CHESSBOOK_WHEN_ACTION_NEW_TRIGGERED diff --git a/NineChess/src/ninechessai_ab.cpp b/NineChess/src/ninechessai_ab.cpp index b6312b83..ca084fb8 100644 --- a/NineChess/src/ninechessai_ab.cpp +++ b/NineChess/src/ninechessai_ab.cpp @@ -517,45 +517,9 @@ int NineChessAi_ab::alphaBetaPruning(int depth, int alpha, int beta, Node *node) node->isLeaf = false; node->isTimeout = false; node->isHash = false; + node->hash = 0; #endif - // 搜索到叶子节点(决胜局面) - if (chessContext->stage == NineChess::GAME_OVER) { - // 局面评估 - node->value = evaluate(node); - - // 为争取速胜,value 值 +- 深度 - if (node->value > 0) - node->value += depth; - else - node->value -= depth; - -#ifdef DEBUG_AB_TREE - node->isLeaf = true; -#endif - - return node->value; - } - - // 搜索到第0层或需要退出 - if (!depth || requiredQuit) { - // 局面评估 - node->value = evaluate(node); - - // 为争取速胜,value 值 +- 深度 - if (chessContext->turn == NineChess::PLAYER1) - node->value += depth; - else - node->value -= depth; - -#ifdef DEBUG_AB_TREE - if (requiredQuit) { - node->isTimeout = true; - } -#endif - return node->value; - } - #ifdef HASH_MAP_ENABLE // 检索 hashmap uint64_t hash = chessTemp.getHash(); @@ -589,6 +553,48 @@ int NineChessAi_ab::alphaBetaPruning(int depth, int alpha, int beta, Node *node) hashMapMutex.unlock(); #endif /* HASH_MAP_ENABLE */ + // 搜索到叶子节点(决胜局面) + if (chessContext->stage == NineChess::GAME_OVER) { + // 局面评估 + node->value = evaluate(node); + + // 为争取速胜,value 值 +- 深度 + if (node->value > 0) + node->value += depth; + else + node->value -= depth; + +#ifdef DEBUG_AB_TREE + node->isLeaf = true; +#endif + + // TODO: RecordHash + + return node->value; + } + + // 搜索到第0层或需要退出 + if (!depth || requiredQuit) { + // 局面评估 + node->value = evaluate(node); + + // 为争取速胜,value 值 +- 深度 + if (chessContext->turn == NineChess::PLAYER1) + node->value += depth; + else + node->value -= depth; + +#ifdef DEBUG_AB_TREE + if (requiredQuit) { + node->isTimeout = true; + } +#endif + + // TODO: RecordHash + + return node->value; + } + // 生成子节点树,即生成每个合理的着法 generateLegalMoves(node); @@ -818,5 +824,6 @@ unordered_map::iterator NineChessAi_ab::fin } } } + return iter; } diff --git a/NineChess/src/ninechessai_ab.h b/NineChess/src/ninechessai_ab.h index 2b7a108b..67c5dcca 100644 --- a/NineChess/src/ninechessai_ab.h +++ b/NineChess/src/ninechessai_ab.h @@ -26,11 +26,21 @@ using namespace std; class NineChessAi_ab { public: + // 定义哈希值的类型 + enum hashType : int16_t + { + hashfEMPTY = 0, + hashfALPHA = 1, + hashfBETA = 2, + hashfEXACT = 3 + }; + // 定义哈希表的值 struct HashValue { int16_t value; int16_t depth; + enum hashType type; }; // 定义一个节点结构体 @@ -156,8 +166,11 @@ private: // 哈希表的默认大小 static const size_t maxHashCount = 1024 * 1024; - // 定义极大值,等于16位有符号整形数字的最大值 - static const int INF_VALUE = INT32_MAX; + // 定义极大值 + static const int INF_VALUE = 0x1 << 30; + + // 定义未知值 + static const int UNKNOWN_VALUE = INT32_MAX; private: // 命令行