diff --git a/include/config.h b/include/config.h index 51ea4621..391efd28 100644 --- a/include/config.h +++ b/include/config.h @@ -163,4 +163,4 @@ #define likely(expr) (__builtin_expect(!!(expr), 1)) #define unlikely(expr) (__builtin_expect(!!(expr), 0)) -#endif // CONFIG_H \ No newline at end of file +#endif // CONFIG_H diff --git a/src/ai/search.cpp b/src/ai/search.cpp index 097a57a6..af5639fe 100644 --- a/src/ai/search.cpp +++ b/src/ai/search.cpp @@ -38,7 +38,7 @@ player_t gSideToMove; using namespace CTSL; // 用于检测重复局面 (Position) -vector moveHistory; +vector moveHistory; AIAlgorithm::AIAlgorithm() { @@ -198,7 +198,7 @@ int AIAlgorithm::search(depth_t depth) static int nRepetition = 0; if (state->position->getPhase() == PHASE_MOVING) { - key_t key = state->position->getPosKey(); + hash_t key = state->position->getPosKey(); if (std::find(moveHistory.begin(), moveHistory.end(), key) != moveHistory.end()) { nRepetition++; @@ -343,7 +343,7 @@ value_t AIAlgorithm::search(depth_t depth, value_t alpha, value_t beta) #if defined (TRANSPOSITION_TABLE_ENABLE) || defined(ENDGAME_LEARNING) // 获取哈希值 - key_t posKey = st->position->getPosKey(); + hash_t posKey = st->position->getPosKey(); #endif #ifdef ENDGAME_LEARNING diff --git a/src/ai/search.h b/src/ai/search.h index f377ab2f..e43783ca 100644 --- a/src/ai/search.h +++ b/src/ai/search.h @@ -208,7 +208,7 @@ public: #include "tt.h" #ifdef THREEFOLD_REPETITION -extern vector moveHistory; +extern vector moveHistory; #endif #endif /* SEARCH_H */ diff --git a/src/ai/tt.cpp b/src/ai/tt.cpp index 4694e174..7ea2865e 100644 --- a/src/ai/tt.cpp +++ b/src/ai/tt.cpp @@ -21,13 +21,13 @@ #ifdef TRANSPOSITION_TABLE_ENABLE static constexpr int TRANSPOSITION_TABLE_SIZE = 0x2000000; // 8-128M:102s, 4-64M:93s 2-32M:91s 1-16M: 冲突 -HashMap TT(TRANSPOSITION_TABLE_SIZE); +HashMap TT(TRANSPOSITION_TABLE_SIZE); #ifdef TRANSPOSITION_TABLE_FAKE_CLEAN uint8_t transpositionTableAge; #endif // TRANSPOSITION_TABLE_FAKE_CLEAN -value_t TranspositionTable::probe(const key_t &key, +value_t TranspositionTable::probe(const hash_t &key, const depth_t &depth, const value_t &alpha, const value_t &beta, @@ -88,7 +88,7 @@ out: return VALUE_UNKNOWN; } -bool TranspositionTable::search(const key_t &key, TTEntry &tte) +bool TranspositionTable::search(const hash_t &key, TTEntry &tte) { return TT.find(key, tte); @@ -117,7 +117,7 @@ bool TranspositionTable::search(const key_t &key, TTEntry &tte) #endif } -void TranspositionTable::prefetch(const key_t &key) +void TranspositionTable::prefetch(const hash_t &key) { TT.prefetchValue(key); } @@ -125,7 +125,7 @@ void TranspositionTable::prefetch(const key_t &key) int TranspositionTable::save(const value_t &value, const depth_t &depth, const bound_t &type, - const key_t &key + const hash_t &key #ifdef TT_MOVE_ENABLE , const move_t &ttMove #endif // TT_MOVE_ENABLE diff --git a/src/ai/tt.h b/src/ai/tt.h index 2c6f6f48..6d142cf5 100644 --- a/src/ai/tt.h +++ b/src/ai/tt.h @@ -30,7 +30,7 @@ using namespace CTSL; #ifdef TRANSPOSITION_TABLE_ENABLE -extern const key_t zobrist[SQ_EXPANDED_COUNT][PIECETYPE_COUNT]; +extern const hash_t zobrist[SQ_EXPANDED_COUNT][PIECETYPE_COUNT]; enum bound_t : uint8_t { @@ -57,8 +57,8 @@ class TranspositionTable { public: // 查找哈希表 - static bool search(const key_t &key, TTEntry &tte); - static value_t probe(const key_t &key, + static bool search(const hash_t &key, TTEntry &tte); + static value_t probe(const hash_t &key, const depth_t &depth, const value_t &alpha, const value_t &beta, @@ -72,7 +72,7 @@ public: static int save(const value_t &value, const depth_t &depth, const bound_t &type, - const key_t &key + const hash_t &key #ifdef TT_MOVE_ENABLE , const move_t &ttMove #endif // TT_MOVE_ENABLE @@ -82,13 +82,13 @@ public: static void clear(); // 预读取 - static void prefetch(const key_t &key); + static void prefetch(const hash_t &key); private: friend struct TTEntry; }; -extern HashMap TT; +extern HashMap TT; #ifdef TRANSPOSITION_TABLE_FAKE_CLEAN extern uint8_t transpositionTableAge; diff --git a/src/base/hashmap.h b/src/base/hashmap.h index 219295ff..102ccc0c 100644 --- a/src/base/hashmap.h +++ b/src/base/hashmap.h @@ -26,7 +26,7 @@ namespace CTSL //Concurrent Thread Safe Library //during the creation of the bucket. All the key buckets are created during the construction of the map. //Locks are taken per bucket, hence multiple threads can write simultaneously in different buckets in the key map #ifdef HASH_KEY_DISABLE - #define hashFn key_t + #define hashFn hash_t template #else template > @@ -34,7 +34,7 @@ namespace CTSL //Concurrent Thread Safe Library class HashMap { public: - HashMap(key_t hashSize_ = HASH_SIZE_DEFAULT) : hashSize(hashSize_) + HashMap(hash_t hashSize_ = HASH_SIZE_DEFAULT) : hashSize(hashSize_) { #ifdef DISABLE_HASHBUCKET hashTable = new HashNode[hashSize]; //create the key table as an array of key nodes @@ -230,7 +230,7 @@ namespace CTSL //Concurrent Thread Safe Library #else F hashFn; #endif - const key_t hashSize; + const hash_t hashSize; #ifdef DISABLE_HASHBUCKET #ifndef HASHMAP_NOLOCK mutable std::shared_timed_mutex mutex_; diff --git a/src/base/zobrist.cpp b/src/base/zobrist.cpp index 0992896f..262d1a1a 100644 --- a/src/base/zobrist.cpp +++ b/src/base/zobrist.cpp @@ -19,7 +19,7 @@ #include "zobrist.h" -const key_t zobrist[SQ_EXPANDED_COUNT][PIECETYPE_COUNT] = { +const hash_t zobrist[SQ_EXPANDED_COUNT][PIECETYPE_COUNT] = { #ifdef TRANSPOSITION_TABLE_CUTDOWN {0x4E421A, 0x3962FF, 0x6DB6EE, 0x219AE1}, {0x1F3DE2, 0xD9AACB, 0xD51733, 0xD3F9EA}, diff --git a/src/base/zobrist.h b/src/base/zobrist.h index cb6131ae..492cb95d 100644 --- a/src/base/zobrist.h +++ b/src/base/zobrist.h @@ -22,4 +22,4 @@ #include "config.h" #include "position.h" -extern const key_t zobrist[SQ_EXPANDED_COUNT][PIECETYPE_COUNT]; +extern const hash_t zobrist[SQ_EXPANDED_COUNT][PIECETYPE_COUNT]; diff --git a/src/game/position.cpp b/src/game/position.cpp index a0986247..ef18822a 100644 --- a/src/game/position.cpp +++ b/src/game/position.cpp @@ -352,7 +352,7 @@ bool Position::start() // 如果游戏处于未开始状态 case PHASE_READY: // 启动计时器 - startTime = time(NULL); + startTime = time(nullptr); // 进入开局状态 phase = PHASE_PLACING; return true; @@ -363,6 +363,20 @@ bool Position::start() bool Position::placePiece(square_t square, bool updateCmdlist) { + ring_t r; + seat_t s; + int i; + // 时间的临时变量 + int seconds = -1; + + // 对于开局落子 + int piece = '\x00'; + int n = 0; + + int playerId = Player::toId(sideToMove); + + bitboard_t fromTo; + // 如果局面为“结局”,返回false if (phase == PHASE_GAMEOVER) return false; @@ -380,19 +394,8 @@ bool Position::placePiece(square_t square, bool updateCmdlist) return false; // 格式转换 - ring_t r; - seat_t s; Board::squareToPolar(square, r, s); - // 时间的临时变量 - int seconds = -1; - - // 对于开局落子 - int piece = '\x00'; - int n = 0; - - int playerId = Player::toId(sideToMove); - if (phase == PHASE_PLACING) { piece = (0x01 | sideToMove) + rule.nTotalPiecesEachSide - nPiecesInHand[playerId]; nPiecesInHand[playerId]--; @@ -479,7 +482,6 @@ bool Position::placePiece(square_t square, bool updateCmdlist) // 如果落子不合法 if (nPiecesOnBoard[sideId] > rule.nPiecesAtLeast || !rule.allowFlyWhenRemainThreePieces) { - int i; for (i = 0; i < 4; i++) { if (square == MoveList::moveTable[currentSquare][i]) break; @@ -503,7 +505,7 @@ bool Position::placePiece(square_t square, bool updateCmdlist) moveStep++; } - bitboard_t fromTo = square_bb(currentSquare) | square_bb(square); + fromTo = square_bb(currentSquare) | square_bb(square); board.byTypeBB[ALL_PIECES] ^= fromTo; board.byTypeBB[playerId] ^= fromTo; @@ -852,6 +854,7 @@ bool Position::doMove(move_t m) if (selectPiece(from_sq(m))) { return placePiece(to_sq(m)); } + break; case MOVETYPE_PLACE: return placePiece(to_sq(m)); default: @@ -1174,13 +1177,13 @@ void Position::constructKey() key = 0; } -key_t Position::getPosKey() +hash_t Position::getPosKey() { // TODO: 每次获取哈希值时更新 key 值剩余8位,放在此处调用不优雅 return updateKeyMisc(); } -key_t Position::updateKey(square_t square) +hash_t Position::updateKey(square_t square) { // PieceType is board.locations[square] @@ -1196,18 +1199,18 @@ key_t Position::updateKey(square_t square) return key; } -key_t Position::revertKey(square_t square) +hash_t Position::revertKey(square_t square) { return updateKey(square); } -key_t Position::updateKeyMisc() +hash_t Position::updateKeyMisc() { const int KEY_MISC_BIT = 8; // 清除标记位 key = key << KEY_MISC_BIT >> KEY_MISC_BIT; - key_t hi = 0; + hash_t hi = 0; // 置位 @@ -1219,17 +1222,17 @@ key_t Position::updateKeyMisc() hi |= 1U << 1; } - hi |= static_cast(nPiecesNeedRemove) << 2; - hi |= static_cast(nPiecesInHand[BLACK]) << 4; // TODO: 或许换 phase 也可以? + hi |= static_cast(nPiecesNeedRemove) << 2; + hi |= static_cast(nPiecesInHand[BLACK]) << 4; // TODO: 或许换 phase 也可以? - key = key | (hi << (CHAR_BIT * sizeof(key_t) - KEY_MISC_BIT)); + key = key | (hi << (CHAR_BIT * sizeof(hash_t) - KEY_MISC_BIT)); return key; } -key_t Position::getNextPrimaryKey(move_t m) +hash_t Position::getNextPrimaryKey(move_t m) { - key_t npKey = key /* << 8 >> 8 */; + hash_t npKey = key /* << 8 >> 8 */; square_t sq = static_cast(to_sq(m));; movetype_t mt = type_of(m); diff --git a/src/game/position.h b/src/game/position.h index 1fe05ed0..09333899 100644 --- a/src/game/position.h +++ b/src/game/position.h @@ -56,7 +56,7 @@ public: Board board; // 局面的哈希值 - key_t key {0}; + hash_t key {0}; // 局面阶段标识 enum phase_t phase {PHASE_NONE}; @@ -238,11 +238,11 @@ public: bool removePiece(square_t square, bool updateCmdlist = false); // key 相关 - key_t getPosKey(); - key_t revertKey(square_t square); - key_t updateKey(square_t square); - key_t updateKeyMisc(); - key_t getNextPrimaryKey(move_t m); + hash_t getPosKey(); + hash_t revertKey(square_t square); + hash_t updateKey(square_t square); + hash_t updateKeyMisc(); + hash_t getNextPrimaryKey(move_t m); // 赢盘数 int score[COLOR_COUNT] = { 0 }; diff --git a/src/game/types.h b/src/game/types.h index 4ed55a1a..47ad926f 100644 --- a/src/game/types.h +++ b/src/game/types.h @@ -30,9 +30,9 @@ using score_t = uint32_t; typedef uint32_t bitboard_t; #ifdef TRANSPOSITION_TABLE_CUTDOWN -using key_t = uint32_t; +using hash_t = uint32_t; #else -using key_t = uint64_t; +using hash_t = uint64_t; #endif /* TRANSPOSITION_TABLE_CUTDOWN */ enum move_t : int32_t