哈希必然记录并缩小 THREEFOLD_REPETITION 宏控制范围

This commit is contained in:
CalciteM 2019-09-09 00:45:06 +08:00
parent 3a043b469c
commit 6360e1ff20
4 changed files with 6 additions and 65 deletions

View File

@ -43,9 +43,8 @@ HashMap<hash_t, MillGameAi_ab::HashValue> bookHashMap(bookHashsize);
vector<hash_t> openingBook;
#endif // BOOK_LEARNING
#ifdef THREEFOLD_REPETITION
// 用于检测重复局面
vector<hash_t> positions;
#endif
MillGameAi_ab::MillGameAi_ab()
{
@ -133,10 +132,8 @@ struct MillGameAi_ab::Node *MillGameAi_ab::addNode(
#endif
#ifdef DEBUG_AB_TREE
#if ((defined TRANSPOSITION_TABLE_ENABLE) || (defined BOOK_LEARNING) || (defined THREEFOLD_REPETITION))
newNode->hash = 0;
#endif
#endif
#ifdef DEBUG_AB_TREE
#ifdef TRANSPOSITION_TABLE_ENABLE
@ -281,9 +278,7 @@ void MillGameAi_ab::setGame(const MillGame &game)
//openingBook.clear();
#endif // BOOK_LEARNING
#ifdef THREEFOLD_REPETITION
positions.clear();
#endif
}
this->game_ = game;

View File

@ -85,9 +85,7 @@ public:
#ifdef TRANSPOSITION_TABLE_ENABLE
bool isHash; // 是否从 Hash 读取
#endif /* TRANSPOSITION_TABLE_ENABLE */
#if ((defined TRANSPOSITION_TABLE_ENABLE) || (defined BOOK_LEARNING) || (defined THREEFOLD_REPETITION))
hash_t hash; // 哈希值
#endif
#endif /* DEBUG_AB_TREE */
};

View File

@ -29,10 +29,8 @@ MillGame::MillGame()
// 单独提出 board 等数据,免得每次都写 context.board;
board_ = context.board.board_;
#if ((defined TRANSPOSITION_TABLE_ENABLE) || (defined BOOK_LEARNING) || (defined THREEFOLD_REPETITION))
// 创建哈希数据
constructHash();
#endif
#ifdef BOOK_LEARNING
// TODO: 开局库文件被加载了多次
@ -173,9 +171,7 @@ bool MillGame::setContext(const struct Rule *rule, step_t maxStepsLedToDraw, int
// 当前棋局3×8
if (board == nullptr) {
memset(context.board.board_, 0, sizeof(context.board.board_));
#if ((defined TRANSPOSITION_TABLE_ENABLE) || (defined BOOK_LEARNING) || (defined THREEFOLD_REPETITION))
context.hash = 0;
#endif
} else {
memcpy(context.board.board_, board, sizeof(context.board.board_));
}
@ -328,10 +324,8 @@ bool MillGame::reset()
// 用时置零
elapsedSeconds_1 = elapsedSeconds_2 = 0;
#if ((defined TRANSPOSITION_TABLE_ENABLE) || (defined BOOK_LEARNING) || (defined THREEFOLD_REPETITION))
// 哈希归零
context.hash = 0;
#endif
// 提示
setTips();
@ -426,9 +420,8 @@ bool MillGame::place(int pos, int time_p, int8_t rs)
board_[pos] = piece;
#if ((defined TRANSPOSITION_TABLE_ENABLE) || (defined BOOK_LEARNING) || (defined THREEFOLD_REPETITION))
updateHash(pos);
#endif
move_ = pos;
if (rs) {
@ -524,15 +517,11 @@ bool MillGame::place(int pos, int time_p, int8_t rs)
board_[pos] = board_[currentPos];
#if ((defined TRANSPOSITION_TABLE_ENABLE) || (defined BOOK_LEARNING) || (defined THREEFOLD_REPETITION))
updateHash(pos);
#endif
board_[currentPos] = '\x00';
#if ((defined TRANSPOSITION_TABLE_ENABLE) || (defined BOOK_LEARNING) || (defined THREEFOLD_REPETITION))
revertHash(currentPos);
#endif
currentPos = pos;
n = context.board.addMills(currentRule, currentPos);
@ -620,17 +609,11 @@ bool MillGame::capture(int pos, int time_p, int8_t cp)
// 去子(设置禁点)
if (currentRule.hasForbiddenPoint && context.stage == GAME_PLACING) {
#if ((defined TRANSPOSITION_TABLE_ENABLE) || (defined BOOK_LEARNING) || (defined THREEFOLD_REPETITION))
revertHash(pos);
#endif
board_[pos] = '\x0f';
#if ((defined TRANSPOSITION_TABLE_ENABLE) || (defined BOOK_LEARNING) || (defined THREEFOLD_REPETITION))
updateHash(pos);
#endif
} else { // 去子
#if ((defined TRANSPOSITION_TABLE_ENABLE) || (defined BOOK_LEARNING) || (defined THREEFOLD_REPETITION))
revertHash(pos);
#endif
board_[pos] = '\x00';
}
@ -651,9 +634,7 @@ bool MillGame::capture(int pos, int time_p, int8_t cp)
currentPos = 0;
context.nPiecesNeedRemove--;
#if ((defined TRANSPOSITION_TABLE_ENABLE) || (defined BOOK_LEARNING) || (defined THREEFOLD_REPETITION))
updateHash(pos);
#endif
// 去子完成
@ -876,7 +857,7 @@ bool MillGame::command(const char *cmd)
cmdlist.emplace_back(string(cmdline));
return true;
}
#endif
#endif /* THREEFOLD_REPETITION */
return false;
}
@ -1093,9 +1074,7 @@ void MillGame::cleanForbiddenPoints()
pos = r * Board::N_SEATS + s;
if (board_[pos] == '\x0f') {
#if ((defined TRANSPOSITION_TABLE_ENABLE) || (defined BOOK_LEARNING) || (defined THREEFOLD_REPETITION))
revertHash(pos);
#endif
board_[pos] = '\x00';
}
}
@ -1184,23 +1163,8 @@ void MillGame::getElapsedTime(time_t &p1_ms, time_t &p2_ms)
p2_ms = elapsedSeconds_2;
}
#if ((defined TRANSPOSITION_TABLE_ENABLE) || (defined BOOK_LEARNING) || (defined THREEFOLD_REPETITION))
#if 0
/*
* hash hash []
* 56-630
* 5501
* 5401
* 6-534822448
* 0b000b010b100b11
* 4-5232
* 0-3player1的手棋数player2的
*/
#endif
/*
* hash
* hash
* 8-63 (56): zobrist
* TODO: 8
* 4-7 (4)player1的手棋数player2的, 115
@ -1215,18 +1179,6 @@ void MillGame::constructHash()
#include "zobrist.h"
memcpy(context.zobrist, zobrist0, sizeof(hash_t) * Board::N_POINTS * POINT_TYPE_COUNT);
#if 0
// 预留末8位后续填充局面特征标志
for (int p = 0; p < N_POINTS; p++) {
//loggerDebug("{\n");
for (int t = MillGame::POINT_TYPE_EMPTY; t <= MillGame::POINT_TYPE_FORBIDDEN; t++) {
context.zobrist[p][t] = rand56();
//loggerDebug("%llX, ", context.zobrist[p][t]);
}
//loggerDebug("},\n");
}
#endif
}
hash_t MillGame::getHash()
@ -1275,4 +1227,4 @@ hash_t MillGame::updateHashMisc()
return context.hash;
}
#endif /* TRANSPOSITION_TABLE_ENABLE etc. */

View File

@ -41,13 +41,11 @@ class GameContext
public:
Board board;
#if ((defined TRANSPOSITION_TABLE_ENABLE) || (defined BOOK_LEARNING) || (defined THREEFOLD_REPETITION))
// 局面的哈希值
hash_t hash{};
// Zobrist 数组
hash_t zobrist[Board::N_POINTS][POINT_TYPE_COUNT]{};
#endif /* TRANSPOSITION_TABLE_ENABLE */
// 局面阶段标识
enum GameStage stage;
@ -322,13 +320,11 @@ public:
bool place(int pos, int time_p = -1, int8_t cp = 0);
bool capture(int pos, int time_p = -1, int8_t cp = 0);
#if ((defined TRANSPOSITION_TABLE_ENABLE) || (defined BOOK_LEARNING) || (defined THREEFOLD_REPETITION))
// hash相关
// hash 相关
hash_t getHash();
hash_t revertHash(int pos);
hash_t updateHash(int pos);
hash_t updateHashMisc();
#endif
public: /* TODO: move to private */
// 棋局上下文