From c665c6480347e45213d70f7f89c969c030bd2c23 Mon Sep 17 00:00:00 2001 From: CalciteM Team Date: Fri, 13 Sep 2019 13:00:44 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=B8=BA=20value=5Ft=20=E9=87=8D?= =?UTF-8?q?=E8=BD=BD=E8=BF=90=E7=AE=97=E7=AC=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ai/evaluate.cpp | 18 +++++++++--------- src/ai/search.cpp | 8 ++++---- src/game/types.h | 22 +++++++++++++++++++++- 3 files changed, 34 insertions(+), 14 deletions(-) diff --git a/src/ai/evaluate.cpp b/src/ai/evaluate.cpp index 93a21202..ce64d0a2 100644 --- a/src/ai/evaluate.cpp +++ b/src/ai/evaluate.cpp @@ -43,14 +43,14 @@ value_t Evaluation::getValue(Position &dummyPosition, PositionContext *positionC case PHASE_PLACING: // 按手中的棋子计分,不要break; nPiecesInHandDiff = positionContext->nPiecesInHand_1 - positionContext->nPiecesInHand_2; - value = (value_t)(value + nPiecesInHandDiff * VALUE_EACH_PIECE_INHAND); + value += nPiecesInHandDiff * VALUE_EACH_PIECE_INHAND; #ifdef DEBUG_AB_TREE node->nPiecesInHandDiff = nPiecesInHandDiff; #endif // 按场上棋子计分 nPiecesOnBoardDiff = positionContext->nPiecesOnBoard_1 - positionContext->nPiecesOnBoard_2; - value = (value_t)(value + nPiecesOnBoardDiff * VALUE_EACH_PIECE_ONBOARD); + value += nPiecesOnBoardDiff * VALUE_EACH_PIECE_ONBOARD; #ifdef DEBUG_AB_TREE node->nPiecesOnBoardDiff = nPiecesOnBoardDiff; #endif @@ -65,7 +65,7 @@ value_t Evaluation::getValue(Position &dummyPosition, PositionContext *positionC case ACTION_CAPTURE: nPiecesNeedRemove = (positionContext->turn == PLAYER1) ? positionContext->nPiecesNeedRemove : -(positionContext->nPiecesNeedRemove); - value = (value_t)(value + nPiecesNeedRemove * VALUE_EACH_PIECE_NEEDREMOVE); + value += nPiecesNeedRemove * VALUE_EACH_PIECE_NEEDREMOVE; #ifdef DEBUG_AB_TREE node->nPiecesNeedRemove = nPiecesNeedRemove; #endif @@ -96,7 +96,7 @@ value_t Evaluation::getValue(Position &dummyPosition, PositionContext *positionC case ACTION_CAPTURE: nPiecesNeedRemove = (positionContext->turn == PLAYER1) ? positionContext->nPiecesNeedRemove : -(positionContext->nPiecesNeedRemove); - value = (value_t)(value + nPiecesNeedRemove * VALUE_EACH_PIECE_NEEDREMOVE_2); + value += nPiecesNeedRemove * VALUE_EACH_PIECE_NEEDREMOVE_2; #ifdef DEBUG_AB_TREE node->nPiecesNeedRemove = nPiecesNeedRemove; #endif @@ -114,7 +114,7 @@ value_t Evaluation::getValue(Position &dummyPosition, PositionContext *positionC Board::N_SEATS * Board::N_RINGS) { if (dummyPosition.getRule()->isStartingPlayerLoseWhenBoardFull) { // winner = PLAYER2; - value = value_t(value - VALUE_WIN); + value -= VALUE_WIN; #ifdef DEBUG_AB_TREE node->result = -3; #endif @@ -129,12 +129,12 @@ value_t Evaluation::getValue(Position &dummyPosition, PositionContext *positionC dummyPosition.getRule()->isLoseWhenNoWay) { // 规则要求被“闷”判负,则对手获胜 if (positionContext->turn == PLAYER1) { - value = value_t(value - VALUE_WIN); + value -= VALUE_WIN; #ifdef DEBUG_AB_TREE node->result = -2; #endif } else { - value = value_t(value + VALUE_WIN); + value += VALUE_WIN; #ifdef DEBUG_AB_TREE node->result = 2; #endif @@ -143,12 +143,12 @@ value_t Evaluation::getValue(Position &dummyPosition, PositionContext *positionC // 剩余棋子个数判断 if (positionContext->nPiecesOnBoard_1 < dummyPosition.getRule()->nPiecesAtLeast) { - value = value_t(value - VALUE_WIN); + value -= VALUE_WIN; #ifdef DEBUG_AB_TREE node->result = -1; #endif } else if (positionContext->nPiecesOnBoard_2 < dummyPosition.getRule()->nPiecesAtLeast) { - value = value_t(value + VALUE_WIN); + value += VALUE_WIN; #ifdef DEBUG_AB_TREE node->result = 1; #endif diff --git a/src/ai/search.cpp b/src/ai/search.cpp index 59c1cad9..d0e496d9 100644 --- a/src/ai/search.cpp +++ b/src/ai/search.cpp @@ -466,9 +466,9 @@ value_t MillGameAi_ab::alphaBetaPruning(depth_t depth, value_t alpha, value_t be // 为争取速胜,value 值 +- 深度 if (node->value > 0) { - node->value = value_t(node->value + depth); + node->value += depth; } else { - node->value = value_t(node->value - depth); + node->value -= depth; } #ifdef DEBUG_AB_TREE @@ -491,9 +491,9 @@ value_t MillGameAi_ab::alphaBetaPruning(depth_t depth, value_t alpha, value_t be // 为争取速胜,value 值 +- 深度 (有必要?) if (positionContext->turn == PLAYER1) { - node->value = (value_t)(node->value + depth); + node->value += depth; } else { - node->value = (value_t)(node->value - depth); + node->value -= depth; } #ifdef DEBUG_AB_TREE diff --git a/src/game/types.h b/src/game/types.h index 9308d08a..56a3ee33 100644 --- a/src/game/types.h +++ b/src/game/types.h @@ -112,7 +112,27 @@ enum value_t : int16_t VALUE_EACH_PIECE_NEEDREMOVE = 100, VALUE_EACH_PIECE_NEEDREMOVE_2 = 128, }; - + +// Additional operators to add integers to a Value +constexpr value_t operator+(value_t v, int i) +{ + return value_t(int(v) + i); +} + +constexpr value_t operator-(value_t v, int i) +{ + return value_t(int(v) - i); +} + +inline value_t &operator+=(value_t &v, int i) +{ + return v = v + i; +} + +inline value_t &operator-=(value_t &v, int i) +{ + return v = v - i; +} // 动作状态标识 enum action_t : uint16_t