diff --git a/dialog.cpp b/dialog.cpp index 90c82f3..c4e07fb 100644 --- a/dialog.cpp +++ b/dialog.cpp @@ -643,9 +643,9 @@ void Dialog::computerMove() { ui->ComputerScore->setText(bookOK); // 先尝试走一步 POSITION_INFO.makeMove(mapTo256(step), COMPUTER_SIDE); - // 如果走云端走法会产生重复局面,只有自己长将军时才不采纳云端走法 - // 其他情况:对方长将军是有利于自己的,重复局面判和棋,以和为贵 - if (POSITION_INFO.scoreRepeatFlag(POSITION_INFO.getRepeatFlag()) != BAN_SCORE_LOSS) { + // 如果走云端走法会产生重复局面,只有对方长将军才采纳云端走法 + RepeatFlag repeatFlag { POSITION_INFO.getRepeatFlag() }; + if (repeatFlag == 0 or POSITION_INFO.scoreRepeatFlag(repeatFlag) == BAN_SCORE_MATE) { emit threadOK(step); return; } @@ -693,7 +693,7 @@ void Dialog::computerMove() { ui->ComputerScore->setText(QString::fromLocal8Bit("长将局面")); } else { ui->ComputerScore->setText( - QString::fromLocal8Bit("[") + QString::number(CURRENT_DEPTH - 1) + + QString::fromLocal8Bit("[") + QString::number(CURRENT_DEPTH) + QString::fromLocal8Bit("层]") + QString::number(score)); } emit threadOK(mapToStep(POSITION_INFO.mBestMove)); diff --git a/pikachess.hpp b/pikachess.hpp index 86ef546..b6f6178 100644 --- a/pikachess.hpp +++ b/pikachess.hpp @@ -2182,8 +2182,7 @@ Score searchMain() { CURRENT_DEPTH = 1; Score bestScore{0}; // 多线程搜索,每个线程一个PositionInfo,只有置换表是共享的 - // 为什么是MAX_CONCURRENT - 1? - // 因为本身开启其他线程去搜索的线程也要参与搜索,占用一个核心 + // 为什么是MAX_CONCURRENT - 1? 因为本身开启其他线程去搜索的线程也要参与搜索,占用一个核心 QVector threadPositionInfos(MAX_CONCURRENT - 1); // 初始化线程局面 for (auto &threadPositionInfo : threadPositionInfos) { @@ -2193,31 +2192,35 @@ Score searchMain() { auto startTimeStamp = clock(); forever { // 多线程搜索 - QVector> tasks; + QVector> tasks; tasks.reserve(MAX_CONCURRENT - 1); for (auto &threadPositionInfo : threadPositionInfos) { - tasks.emplaceBack(QtConcurrent::run( - [&] { threadPositionInfo.searchRoot(CURRENT_DEPTH); })); + tasks.emplaceBack(QtConcurrent::run([&] { + return threadPositionInfo.searchRoot(CURRENT_DEPTH); + })); } // 自己也搜索 bestScore = POSITION_INFO.searchRoot(CURRENT_DEPTH); // 等待线程结束 - for (auto &task : tasks) { - task.waitForFinished(); + for (auto &task : tasks) task.waitForFinished(); + // 如果赢了或者输了或者产生了长将局面或者超过时间就停止搜索就不用再往下搜索了 + if (bestScore < LOST_SCORE or bestScore > WIN_SCORE or + clock() - startTimeStamp > SEARCH_TIME) { + // 从所有的线程中选出分数最高的那一个走法来走 + for (Count index { 0 }; index < size(tasks); ++index) { + Score threadScore { tasks[index].result() }; + if (bestScore < threadScore) { + bestScore = threadScore; + POSITION_INFO.mBestMove = threadPositionInfos[index].mBestMove; + } + } + // 走棋 + POSITION_INFO.makeMove(POSITION_INFO.mBestMove, COMPUTER_SIDE); + return bestScore; } + // 增加层数 ++CURRENT_DEPTH; - // 如果赢了或者输了或者产生了长将局面就不用再往下搜索了 - if (bestScore < LOST_SCORE or bestScore > WIN_SCORE) { - break; - } - // 超过时间就停止搜索 - if (clock() - startTimeStamp > SEARCH_TIME) { - break; - } } - // 走棋 - POSITION_INFO.makeMove(POSITION_INFO.mBestMove, COMPUTER_SIDE); - return bestScore; } // 用于初始化工作