完成 updateHash 函数

This commit is contained in:
CalciteM Team 2019-07-14 13:56:36 +08:00
parent 113aca04b5
commit 30fabb7a4c
3 changed files with 98 additions and 72 deletions

View File

@ -179,25 +179,6 @@ NineChess::~NineChess()
{
}
#ifdef HASH_MAP_ENABLE
void NineChess::constructHash()
{
context.hash = 0ull;
context.gameMovingHash = rand64();
context.actionCaptureHash = rand64();
context.player2sTurnHash = rand64();
uint64_t zobrist[N_POINTS][POINT_TYPE_COUNT];
for (int p = 0; p < N_POINTS; p++) {
for (int t = NineChess::POINT_TYPE_EMPTY; t <= NineChess::POINT_TYPE_FORBIDDEN; t++) {
zobrist[p][t] = rand64();
}
}
}
#endif /* HASH_MAP_ENABLE */
NineChess::Player NineChess::getOpponent(NineChess::Player player)
{
switch (player)
@ -1314,57 +1295,6 @@ bool NineChess::choose(int pos)
return false;
}
#ifdef HASH_MAP_ENABLE
uint64_t NineChess::getHash()
{
return context.hash;
}
// hash函数对应可重复去子的规则
uint64_t NineChess::updateHash(int pos)
{
#if 0
/*
* hashCheckCode
* 56-630
* 5501
* 5401
* 6-534822448
* 0b000b010b100b11
* 4-5232
* 0-3player1的手棋数player2的
*/
#endif
uint64_t hash = 0ull;
for (int i = POS_BEGIN; i < POS_END; i++) {
// hash ^= context.zobrist[i][pointType]; // TODO: 待完善
}
uint64_t temp = board_[pos] & 0x30 >> 4;
//context.hashCheckCode |= (temp) << ((pos - 8) * 2 + 6);
// TODO: context.hash =
if (context.turn == PLAYER2) {
//context.hashCheckCode |= 1ull << 55;
context.hash ^= context.player2sTurnHash;
}
if (context.action == ACTION_CAPTURE) {
//context.hashCheckCode |= 1ull << 54;
context.hash ^= context.actionCaptureHash;
}
//context.hashCheckCode |= (uint64_t)context.nPiecesNeedRemove << 4;
//context.hashCheckCode |= context.nPiecesInHand_1;
// TODO: hash 应该 不需要
return context.hash; // TODO: 返回什么
}
#endif /* HASH_MAP_ENABLE */
bool NineChess::giveup(Player loser)
{
if (context.stage == GAME_MOVING || context.stage == GAME_PLACING) {
@ -2345,3 +2275,94 @@ void NineChess::rotate(int degrees, bool cmdChange /*= true*/)
}
}
}
#ifdef HASH_MAP_ENABLE
#if 0
/*
* hash hash []
* 56-630
* 5501
* 5401
* 6-534822448
* 0b000b010b100b11
* 4-5232
* 0-3player1的手棋数player2的
*/
#endif
/*
* hash
* 8-63 (56): zobrist
* TODO: 8
* 4-7 (4)player1的手棋数player2的, 115
* 2-3232
* 1: 01
* 001
*/
void NineChess::constructHash()
{
context.hash = 0ull;
//context.gameMovingHash = rand64();
//context.actionCaptureHash = rand64();
//context.player2sTurnHash = rand64();
uint64_t zobrist[N_POINTS][POINT_TYPE_COUNT];
// 预留末8位后续填充局面特征标志
for (int p = 0; p < N_POINTS; p++) {
for (int t = NineChess::POINT_TYPE_EMPTY; t <= NineChess::POINT_TYPE_FORBIDDEN; t++) {
zobrist[p][t] = rand56();
}
}
}
uint64_t NineChess::getHash()
{
return context.hash;
}
// hash函数对应可重复去子的规则
uint64_t NineChess::updateHash(int pos)
{
#if 0
// 这里不做
for (int i = POS_BEGIN; i < POS_END; i++) {
hash ^= context.zobrist[i][pointType];
}
#endif
// PieceType is board_[pos]
// 0b00表示空白0b01=1 表示先手棋子0b10=2 表示后手棋子0b11=3 表示禁点
int pointType = board_[pos] & 0x30 >> 4;
//context.hashCheckCode |= (temp) << ((pos - 8) * 2 + 6);
// TODO: context.hash =
// 清除或者放置棋子
context.hash ^= context.zobrist[pos][pointType];
// 清除标记位
context.hash &= ~0xFF;
// 置位
if (context.turn == PLAYER2) {
context.hash |= 1ULL;
}
if (context.action == ACTION_CAPTURE) {
context.hash |= 1ULL << 1;
}
// TODO: 是否真的需要这几位?
context.hash |= (uint64_t)context.nPiecesNeedRemove << 4;
context.hash |= (uint64_t)context.nPiecesInHand_1;
return context.hash;
}
#endif /* HASH_MAP_ENABLE */

View File

@ -134,6 +134,11 @@ public:
((uint64_t)rand() << 60);
}
uint64_t rand56(void)
{
return rand64() << 8;
}
// 玩家标识, 轮流状态, 胜负标识
enum Player : uint16_t
{
@ -491,7 +496,7 @@ protected:
#ifdef HASH_MAP_ENABLE
// hash相关
uint64_t getHash();
uint64_t updateHash(int pos);
uint64_t NineChess::updateHash(int pos);
#endif /* HASH_MAP_ENABLE */
private:

View File

@ -996,7 +996,7 @@ int NineChessAi_ab::recordHash(const HashValue &hashValue)
void NineChessAi_ab::clearHashMap()
{
//hashMapMutex.lock();
//hashMap.clear();
hashmap.clear();
//hashMapMutex.unlock();
}
#endif /* HASH_MAP_ENABLE */