refactor: 增加 doMove() 和 undoMove()

This commit is contained in:
Calcitem 2019-10-02 10:24:56 +08:00
parent d4407e80ea
commit 306373371c
4 changed files with 26 additions and 10 deletions

View File

@ -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<Node*> bestMoves;

View File

@ -114,6 +114,12 @@ public:
// 返回最佳走法的命令行
const char *bestMove();
// 执行着法
void doMove(move_t move);
// 撤销着法
void undoMove();
#ifdef TRANSPOSITION_TABLE_ENABLE
// 清空哈希表
void clearTT();

View File

@ -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<square_t>(-m));

View File

@ -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);