diff --git a/src/ai/search.cpp b/src/ai/search.cpp index 5cbca556..4e4998e6 100644 --- a/src/ai/search.cpp +++ b/src/ai/search.cpp @@ -573,11 +573,7 @@ value_t AIAlgorithm::search(depth_t depth, value_t alpha, value_t beta, Node *no minMax = tempGame.position.sideToMove == PLAYER_1 ? -VALUE_INFINITE : VALUE_INFINITE; for (auto child : node->children) { - // 棋局入栈保存,以便后续撤销着法 - positionStack.push(tempGame.position); - - // 执行着法 - tempGame.command(child->move); + doMove(child->move); #ifdef DEAL_WITH_HORIZON_EFFECT // 克服“水平线效应”: 若遇到吃子,则搜索深度增加 @@ -597,9 +593,7 @@ value_t AIAlgorithm::search(depth_t depth, value_t alpha, value_t beta, Node *no // 递归 Alpha-Beta 剪枝 value = search(depth - 1 + epsilon, alpha, beta, child); - // 棋局弹出栈,撤销着法 - tempGame.position = positionStack.top(); - positionStack.pop(); + undoMove(); if (tempGame.position.sideToMove == PLAYER_1) { // 为走棋一方的层, 局面对走棋的一方来说是以 α 为评价 @@ -689,6 +683,22 @@ value_t AIAlgorithm::search(depth_t depth, value_t alpha, value_t beta, Node *no return node->value; } +void AIAlgorithm::doMove(move_t move) +{ + // 棋局入栈保存,以便后续撤销着法 + positionStack.push(tempGame.position); + + // 执行着法 + tempGame.command(move); +} + +void AIAlgorithm::undoMove() +{ + // 棋局弹出栈,撤销着法 + tempGame.position = positionStack.top(); + positionStack.pop(); +} + const char* AIAlgorithm::bestMove() { vector bestMoves; diff --git a/src/ai/search.h b/src/ai/search.h index d65b676c..c10a431d 100644 --- a/src/ai/search.h +++ b/src/ai/search.h @@ -114,6 +114,12 @@ public: // 返回最佳走法的命令行 const char *bestMove(); + // 执行着法 + void doMove(move_t move); + + // 撤销着法 + void undoMove(); + #ifdef TRANSPOSITION_TABLE_ENABLE // 清空哈希表 void clearTT(); diff --git a/src/game/position.cpp b/src/game/position.cpp index d8dd4815..3c399097 100644 --- a/src/game/position.cpp +++ b/src/game/position.cpp @@ -743,7 +743,7 @@ bool Game::command(const char *cmd) return false; } -bool Game::command(int m) +bool Game::command(move_t m) { if (m < 0) { return capture(static_cast(-m)); diff --git a/src/game/position.h b/src/game/position.h index 5f96a4ab..a5b7143c 100644 --- a/src/game/position.h +++ b/src/game/position.h @@ -241,7 +241,7 @@ public: void setTips(); // 下面几个函数没有算法无关判断和无关操作,节约算法时间 - bool command(int move); + bool command(move_t move); bool choose(square_t square); bool place(square_t square, int8_t cp = 0); bool capture(square_t square, int8_t cp = 0);