将搜索哈希放在叶子结点处理流程之前

This commit is contained in:
CalciteM 2019-07-07 10:24:26 +08:00
parent d38200dce5
commit 4d72e101d2
3 changed files with 60 additions and 40 deletions

View File

@ -42,7 +42,7 @@
#define DRAW_SEAT_NUMBER
#endif
#define IDS_SUPPORT
//#define IDS_SUPPORT
#define SAVE_CHESSBOOK_WHEN_ACTION_NEW_TRIGGERED

View File

@ -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<uint64_t, NineChessAi_ab::HashValue>::iterator NineChessAi_ab::fin
}
}
}
return iter;
}

View File

@ -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:
// 命令行