AI进一步优化

This commit is contained in:
liuweilhy 2018-12-09 19:11:22 +08:00
parent 3d36cefa8d
commit d762f55d70
3 changed files with 41 additions and 12 deletions

View File

@ -48,7 +48,7 @@ void AiThread::run()
mutex.unlock();
}
ai_ab.alphaBetaPruning(6);
ai_ab.alphaBetaPruning(8);
const char * str = ai_ab.bestMove();
qDebug() << str;
if (strcmp(str, "error!"))

View File

@ -456,6 +456,8 @@ bool GameController::actionPiece(QPointF pos)
manualListModel.insertRow(++currentRow);
manualListModel.setData(manualListModel.index(currentRow), (*i).c_str());
}
// 播放胜利或失败音效
if (chess.whoWin() != NineChess::NOBODY &&
(manualListModel.data(manualListModel.index(currentRow - 1))).toString().contains("Time over."))
playSound(":/sound/resources/sound/win.wav");
@ -515,24 +517,26 @@ bool GameController::command(const QString &cmd, bool update /*= true*/)
break;
}
// 如果未开局则开局
if (chess.getPhase() == NineChess::GAME_NOTSTARTED)
gameStart();
if (!chess.command(cmd.toStdString().c_str()))
return false;
if (sound == ":/sound/resources/sound/drog.wav" && chess.getAction() == NineChess::ACTION_CAPTURE)
{
if (sound == ":/sound/resources/sound/drog.wav" && chess.getAction() == NineChess::ACTION_CAPTURE) {
sound = ":/sound/resources/sound/capture.wav";
}
if (chess.getPhase() == NineChess::GAME_NOTSTARTED) {
gameReset();
gameStart();
}
if (update) {
playSound(sound);
updateScence(chess);
}
// 发信号更新状态栏
message = QString::fromStdString(chess.getTip());
emit statusBarChanged(message);
// 将新增的棋谱行插入到ListModel
currentRow = manualListModel.rowCount() - 1;
int k = 0;
@ -545,6 +549,11 @@ bool GameController::command(const QString &cmd, bool update /*= true*/)
manualListModel.setData(manualListModel.index(currentRow), (*i).c_str());
}
// 播放胜利或失败音效
if (chess.whoWin() != NineChess::NOBODY &&
(manualListModel.data(manualListModel.index(currentRow - 1))).toString().contains("Time over."))
playSound(":/sound/resources/sound/win.wav");
// AI设置
if (&chess == &(this->chess)) {
// 如果还未决出胜负

View File

@ -195,9 +195,9 @@ int NineChessAi_ab::evaluate(Node *node)
case NineChess::ACTION_CHOOSE:
case NineChess::ACTION_PLACE:
break;
// 如果形成去子状态每有一个可去的子算100
// 如果形成去子状态每有一个可去的子算102
case NineChess::ACTION_CAPTURE:
value += (chessData->turn == NineChess::PLAYER1) ? (chessData->num_NeedRemove) * 100 : -(chessData->num_NeedRemove) * 100;
value += (chessData->turn == NineChess::PLAYER1) ? (chessData->num_NeedRemove) * 102 : -(chessData->num_NeedRemove) * 102;
break;
default:
break;
@ -207,9 +207,9 @@ int NineChessAi_ab::evaluate(Node *node)
// 终局评价最简单
case NineChess::GAME_OVER:
if (chessData->player1_Remain < chessTemp.rule.numAtLest)
value = -infinity;
value = -100000;
else if (chessData->player2_Remain < chessTemp.rule.numAtLest)
value = infinity;
value = 100000;
break;
default:
@ -235,6 +235,18 @@ int NineChessAi_ab::alphaBetaPruning(int depth, int alpha, int beta, Node *node)
if (!depth || chessTemp.data.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;
}
return node->value;
}
@ -286,6 +298,14 @@ int NineChessAi_ab::alphaBetaPruning(int depth, int alpha, int beta, Node *node)
// 取最小值
node->value = minMax;
}
// 删除“孙子”节点,防止层数较深的时候节点树太大
for (auto child : node->children) {
for (auto grandChild : child->children)
deleteTree(grandChild);
child->children.clear();
}
// 返回
return node->value;
}