线程退出逻辑优化

This commit is contained in:
liuweilhy 2018-12-15 17:44:20 +08:00
parent a71536f825
commit df2b4f8f7a
3 changed files with 52 additions and 59 deletions

View File

@ -29,6 +29,8 @@ void AiThread::run()
// 设一个标识1号线程只管玩家12号线程只管玩家2
int i = 0;
qDebug() << "Thread" << id << "start";
while (!isInterruptionRequested()) {
mutex.lock();
if (chess->whosTurn() == NineChess::PLAYER1)
@ -48,12 +50,12 @@ void AiThread::run()
mutex.unlock();
}
ai_ab.alphaBetaPruning(8);
ai_ab.alphaBetaPruning(6);
const char * str = ai_ab.bestMove();
qDebug() << str;
if (strcmp(str, "error!"))
emit command(str);
qDebug() << "Thread" << id << " run " << ++iTemp << "times";
qDebug() << "Thread" << id << "run" << ++iTemp << "times";
// 执行完毕后继续判断
if (!isInterruptionRequested()) {
@ -62,7 +64,7 @@ void AiThread::run()
mutex.unlock();
}
}
qDebug() << "Thread" << id << " quit.";
qDebug() << "Thread" << id << "quit";
}
void AiThread::pause()
@ -89,6 +91,7 @@ void AiThread::stop()
requestInterruption();
mutex.lock();
waiting_ = false;
ai_ab.quit();
pauseCondition.wakeAll();
mutex.unlock();
}

View File

@ -221,7 +221,6 @@ void GameController::setEngine1(bool arg)
{
isEngine1 = arg;
if (arg) {
qDebug() << "Player1 is computer.";
ai1.setAi(chess);
if (ai1.isRunning())
ai1.resume();
@ -229,7 +228,6 @@ void GameController::setEngine1(bool arg)
ai1.start();
}
else {
qDebug() << "Player1 is not computer.";
ai1.stop();
}
}
@ -238,7 +236,6 @@ void GameController::setEngine2(bool arg)
{
isEngine2 = arg;
if (arg) {
qDebug() << "Player2 is computer.";
ai2.setAi(chess);
if (ai2.isRunning())
ai2.resume();
@ -246,7 +243,6 @@ void GameController::setEngine2(bool arg)
ai2.start();
}
else {
qDebug() << "Player2 is not computer.";
ai2.stop();
}
}
@ -273,7 +269,6 @@ void GameController::playSound(const QString &soundPath)
}
}
// 上下翻转
void GameController::flip()
{
@ -413,13 +408,6 @@ void GameController::turnLeft()
}
}
//bool GameController::eventFilter(QObject * watched, QEvent * event)
//{
// return QObject::eventFilter(watched, event);
//}
void GameController::timerEvent(QTimerEvent *event)
{
Q_UNUSED(event)
@ -476,7 +464,7 @@ void GameController::timerEvent(QTimerEvent *event)
*/
}
// 槽函数根据QGraphicsScene的信号和状态来执行选子、落子或去子
// 关键槽函数根据QGraphicsScene的信号和状态来执行选子、落子或去子
bool GameController::actionPiece(QPointF pos)
{
// 点击非落子点,不执行
@ -633,7 +621,33 @@ bool GameController::actionPiece(QPointF pos)
return result;
}
// 棋谱的命令行执行
bool GameController::giveUp()
{
bool result = false;
if (chess.whosTurn() == NineChess::PLAYER1)
result = chess.giveup(NineChess::PLAYER1);
else if (chess.whosTurn() == NineChess::PLAYER2)
result = chess.giveup(NineChess::PLAYER2);
if (result)
{
// 将新增的棋谱行插入到ListModel
currentRow = manualListModel.rowCount() - 1;
int k = 0;
// 输出命令行
for (auto i = (chess.getCmdList())->begin(); i != (chess.getCmdList())->end(); ++i) {
// 跳过已添加的因标准list容器没有下标
if (k++ <= currentRow)
continue;
manualListModel.insertRow(++currentRow);
manualListModel.setData(manualListModel.index(currentRow), (*i).c_str());
}
if (chess.whoWin() != NineChess::NOBODY)
playSound(":/sound/resources/sound/loss.wav");
}
return result;
}
// 关键槽函数棋谱的命令行执行与actionPiece独立
bool GameController::command(const QString &cmd, bool update /*= true*/)
{
Q_UNUSED(hasSound)
@ -758,32 +772,6 @@ bool GameController::phaseChange(int row, bool forceUpdate)
return true;
}
bool GameController::giveUp()
{
bool result = false;
if (chess.whosTurn() == NineChess::PLAYER1)
result = chess.giveup(NineChess::PLAYER1);
else if (chess.whosTurn() == NineChess::PLAYER2)
result = chess.giveup(NineChess::PLAYER2);
if (result)
{
// 将新增的棋谱行插入到ListModel
currentRow = manualListModel.rowCount() - 1;
int k = 0;
// 输出命令行
for (auto i = (chess.getCmdList())->begin(); i != (chess.getCmdList())->end(); ++i) {
// 跳过已添加的因标准list容器没有下标
if (k++ <= currentRow)
continue;
manualListModel.insertRow(++currentRow);
manualListModel.setData(manualListModel.index(currentRow), (*i).c_str());
}
if (chess.whoWin() != NineChess::NOBODY)
playSound(":/sound/resources/sound/loss.wav");
}
return result;
}
bool GameController::updateScence()
{
return updateScence(chess);

View File

@ -149,6 +149,7 @@ void NineChessAi_ab::setChess(const NineChess &chess)
this->chess = chess;
chessTemp = chess;
chessData = &(chessTemp.data);
requiredQuit = false;
deleteTree(rootNode);
rootNode = new Node;
rootNode->value = 0;
@ -234,20 +235,23 @@ int NineChessAi_ab::alphaBetaPruning(int depth, int alpha, int beta, Node *node)
// 当前节点的MinMax值最终赋值给节点value与alpha和Beta不同
int minMax;
if (!depth || chessTemp.data.phase == NineChess::GAME_OVER) {
// 搜索到叶子节点(决胜局面)
if (chessData->phase == NineChess::GAME_OVER) {
node->value = evaluate(node);
// 搜索到决胜局面
if (chessData->phase == NineChess::GAME_OVER)
if (node->value > 0)
node->value += depth;
else
node->value -= depth;
else {
if (chessData->turn == NineChess::PLAYER1)
node->value += depth;
else
node->value -= depth;
}
if (node->value > 0)
node->value += depth;
else
node->value -= depth;
return node->value;
}
// 搜索到第0层或需要退出
if (!depth || requiredQuit) {
node->value = evaluate(node);
if (chessData->turn == NineChess::PLAYER1)
node->value += depth;
else
node->value -= depth;
return node->value;
}
@ -256,8 +260,7 @@ int NineChessAi_ab::alphaBetaPruning(int depth, int alpha, int beta, Node *node)
// 排序子节点树
sortChildren(node);
// 根据演算模型执行MiniMax检索
// 对先手搜索Max
// 根据演算模型执行MiniMax检索对先手搜索Max
if (chessTemp.whosTurn() == NineChess::PLAYER1) {
minMax = -infinity;
for (auto child : node->children) {
@ -342,7 +345,6 @@ const char *NineChessAi_ab::move2string(int16_t move)
return cmdline;
}
void NineChessAi_ab::reverse(const NineChess *node1, NineChess *node2, int i)
{