From 091321a6e11dd14dd8b15d72a60484b8e9a94f29 Mon Sep 17 00:00:00 2001 From: CalciteM Team Date: Fri, 13 Sep 2019 13:31:49 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E5=A2=9E=E5=8A=A0=E9=87=8D?= =?UTF-8?q?=E8=BD=BD=E7=B1=BB=E5=9E=8B=E8=BF=90=E7=AE=97=E7=AC=A6=E7=9A=84?= =?UTF-8?q?=E5=AE=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit valut_t OK, 但是 direction_t 未奏效,待查. --- src/ai/evaluate.cpp | 4 ++-- src/ai/search.cpp | 4 ++-- src/game/board.cpp | 6 +++--- src/game/types.h | 48 +++++++++++++++++++++++++++++++++++---------- 4 files changed, 45 insertions(+), 17 deletions(-) diff --git a/src/ai/evaluate.cpp b/src/ai/evaluate.cpp index ce64d0a2..c6694833 100644 --- a/src/ai/evaluate.cpp +++ b/src/ai/evaluate.cpp @@ -78,8 +78,8 @@ value_t Evaluation::getValue(Position &dummyPosition, PositionContext *positionC case PHASE_MOVING: // 按场上棋子计分 - value = (value_t)(positionContext->nPiecesOnBoard_1 * VALUE_EACH_PIECE_ONBOARD - - positionContext->nPiecesOnBoard_2 * VALUE_EACH_PIECE_ONBOARD); + value = positionContext->nPiecesOnBoard_1 * VALUE_EACH_PIECE_ONBOARD - + positionContext->nPiecesOnBoard_2 * VALUE_EACH_PIECE_ONBOARD; #ifdef EVALUATE_MOBILITY // 按棋子活动能力计分 diff --git a/src/ai/search.cpp b/src/ai/search.cpp index d0e496d9..a39c449a 100644 --- a/src/ai/search.cpp +++ b/src/ai/search.cpp @@ -358,7 +358,7 @@ int MillGameAi_ab::alphaBetaPruning(depth_t depth) TranspositionTable::clearTranspositionTable(); // 每次走子前清空哈希表 #endif #endif - alphaBetaPruning(i, (value_t)-VALUE_INFINITE, VALUE_INFINITE, rootNode); + alphaBetaPruning(i, -VALUE_INFINITE, VALUE_INFINITE, rootNode); } timeEnd = chrono::steady_clock::now(); @@ -525,7 +525,7 @@ value_t MillGameAi_ab::alphaBetaPruning(depth_t depth, value_t alpha, value_t be // 根据演算模型执行 MiniMax 检索,对先手,搜索 Max, 对后手,搜索 Min - minMax = dummyPosition.whosTurn() == PLAYER1 ? (value_t)-VALUE_INFINITE : VALUE_INFINITE; + minMax = dummyPosition.whosTurn() == PLAYER1 ? -VALUE_INFINITE : VALUE_INFINITE; for (auto child : node->children) { // 上下文入栈保存,以便后续撤销着法 diff --git a/src/game/board.cpp b/src/game/board.cpp index c9b88d40..e59565df 100644 --- a/src/game/board.cpp +++ b/src/game/board.cpp @@ -338,8 +338,8 @@ int Board::getSurroundedEmptyLocationCount(enum player_t turn, const Rule &curre (nPiecesOnBoard_1 > currentRule.nPiecesAtLeast || !currentRule.allowFlyWhenRemainThreePieces)) || (turn == PLAYER2 && (nPiecesOnBoard_2 > currentRule.nPiecesAtLeast || !currentRule.allowFlyWhenRemainThreePieces))) { - int d, moveLocation; - for (d = 0; d < DIRECTIONS_COUNT; d++) { + int moveLocation; + for (direction_t d = DIRECTION_BEGIN; d < DIRECTIONS_COUNT; d = (direction_t)(d + 1)) { moveLocation = MoveList::moveTable[location][d]; if (moveLocation) { if (locations[moveLocation] == 0x00 || @@ -396,7 +396,7 @@ bool Board::isAllSurrounded(enum player_t turn, const Rule ¤tRule, int nPi continue; } - for (int d = 0; d < DIRECTIONS_COUNT; d++) { + for (direction_t d = DIRECTION_BEGIN; d < DIRECTIONS_COUNT; d = (direction_t)(d + 1)) { moveLocation = MoveList::moveTable[i][d]; if (moveLocation && !locations[moveLocation]) return false; diff --git a/src/game/types.h b/src/game/types.h index 56a3ee33..e78191d6 100644 --- a/src/game/types.h +++ b/src/game/types.h @@ -54,6 +54,7 @@ enum square_t : int enum direction_t { DIRECTION_CLOCKWISE = 0, // 顺时针 + DIRECTION_BEGIN = DIRECTION_CLOCKWISE, DIRECTION_ANTICLOCKWISE = 1, // 逆时针 DIRECTION_INWARD = 2, // 向内 DIRECTION_OUTWARD = 3, // 向外 @@ -113,6 +114,42 @@ enum value_t : int16_t VALUE_EACH_PIECE_NEEDREMOVE_2 = 128, }; +// 动作状态标识 +enum action_t : uint16_t +{ + ACTION_NONE = 0x0000, + ACTION_CHOOSE = 0x0100, // 选子 + ACTION_PLACE = 0x0200, // 落子 + ACTION_CAPTURE = 0x0400 // 提子 +}; + +#define ENABLE_BASE_OPERATORS_ON(T) \ +constexpr T operator+(T d1, T d2) { return T(int(d1) + int(d2)); } \ +constexpr T operator-(T d1, T d2) { return T(int(d1) - int(d2)); } \ +constexpr T operator-(T d) { return T(-int(d)); } \ +inline T& operator+=(T& d1, T d2) { return d1 = d1 + d2; } \ +inline T& operator-=(T& d1, T d2) { return d1 = d1 - d2; } + +#define ENABLE_INCR_OPERATORS_ON(T) \ +inline T& operator++(T& d) { return d = T(int(d) + 1); } \ +inline T& operator--(T& d) { return d = T(int(d) - 1); } + +#define ENABLE_FULL_OPERATORS_ON(T) \ +ENABLE_BASE_OPERATORS_ON(T) \ +constexpr T operator*(int i, T d) { return T(i * int(d)); } \ +constexpr T operator*(T d, int i) { return T(int(d) * i); } \ +constexpr T operator/(T d, int i) { return T(int(d) / i); } \ +constexpr int operator/(T d1, T d2) { return int(d1) / int(d2); } \ +inline T& operator*=(T& d, int i) { return d = T(int(d) * i); } \ +inline T& operator/=(T& d, int i) { return d = T(int(d) / i); } + +ENABLE_FULL_OPERATORS_ON(value_t) +ENABLE_FULL_OPERATORS_ON(direction_t) + +ENABLE_INCR_OPERATORS_ON(direction_t) +ENABLE_INCR_OPERATORS_ON(piece_t) +ENABLE_INCR_OPERATORS_ON(square_t) + // Additional operators to add integers to a Value constexpr value_t operator+(value_t v, int i) { @@ -132,15 +169,6 @@ inline value_t &operator+=(value_t &v, int i) inline value_t &operator-=(value_t &v, int i) { return v = v - i; -} - -// 动作状态标识 -enum action_t : uint16_t -{ - ACTION_NONE = 0x0000, - ACTION_CHOOSE = 0x0100, // 选子 - ACTION_PLACE = 0x0200, // 落子 - ACTION_CAPTURE = 0x0400 // 提子 -}; +} #endif /* TYPES_H */