AI进一步优化
This commit is contained in:
parent
3d36cefa8d
commit
d762f55d70
|
@ -48,7 +48,7 @@ void AiThread::run()
|
||||||
mutex.unlock();
|
mutex.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
ai_ab.alphaBetaPruning(6);
|
ai_ab.alphaBetaPruning(8);
|
||||||
const char * str = ai_ab.bestMove();
|
const char * str = ai_ab.bestMove();
|
||||||
qDebug() << str;
|
qDebug() << str;
|
||||||
if (strcmp(str, "error!"))
|
if (strcmp(str, "error!"))
|
||||||
|
|
|
@ -456,6 +456,8 @@ bool GameController::actionPiece(QPointF pos)
|
||||||
manualListModel.insertRow(++currentRow);
|
manualListModel.insertRow(++currentRow);
|
||||||
manualListModel.setData(manualListModel.index(currentRow), (*i).c_str());
|
manualListModel.setData(manualListModel.index(currentRow), (*i).c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 播放胜利或失败音效
|
||||||
if (chess.whoWin() != NineChess::NOBODY &&
|
if (chess.whoWin() != NineChess::NOBODY &&
|
||||||
(manualListModel.data(manualListModel.index(currentRow - 1))).toString().contains("Time over."))
|
(manualListModel.data(manualListModel.index(currentRow - 1))).toString().contains("Time over."))
|
||||||
playSound(":/sound/resources/sound/win.wav");
|
playSound(":/sound/resources/sound/win.wav");
|
||||||
|
@ -515,24 +517,26 @@ bool GameController::command(const QString &cmd, bool update /*= true*/)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 如果未开局则开局
|
||||||
|
if (chess.getPhase() == NineChess::GAME_NOTSTARTED)
|
||||||
|
gameStart();
|
||||||
|
|
||||||
if (!chess.command(cmd.toStdString().c_str()))
|
if (!chess.command(cmd.toStdString().c_str()))
|
||||||
return false;
|
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";
|
sound = ":/sound/resources/sound/capture.wav";
|
||||||
}
|
}
|
||||||
|
|
||||||
if (chess.getPhase() == NineChess::GAME_NOTSTARTED) {
|
|
||||||
gameReset();
|
|
||||||
gameStart();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (update) {
|
if (update) {
|
||||||
playSound(sound);
|
playSound(sound);
|
||||||
updateScence(chess);
|
updateScence(chess);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 发信号更新状态栏
|
||||||
|
message = QString::fromStdString(chess.getTip());
|
||||||
|
emit statusBarChanged(message);
|
||||||
|
|
||||||
// 将新增的棋谱行插入到ListModel
|
// 将新增的棋谱行插入到ListModel
|
||||||
currentRow = manualListModel.rowCount() - 1;
|
currentRow = manualListModel.rowCount() - 1;
|
||||||
int k = 0;
|
int k = 0;
|
||||||
|
@ -545,6 +549,11 @@ bool GameController::command(const QString &cmd, bool update /*= true*/)
|
||||||
manualListModel.setData(manualListModel.index(currentRow), (*i).c_str());
|
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设置
|
// AI设置
|
||||||
if (&chess == &(this->chess)) {
|
if (&chess == &(this->chess)) {
|
||||||
// 如果还未决出胜负
|
// 如果还未决出胜负
|
||||||
|
|
|
@ -195,9 +195,9 @@ int NineChessAi_ab::evaluate(Node *node)
|
||||||
case NineChess::ACTION_CHOOSE:
|
case NineChess::ACTION_CHOOSE:
|
||||||
case NineChess::ACTION_PLACE:
|
case NineChess::ACTION_PLACE:
|
||||||
break;
|
break;
|
||||||
// 如果形成去子状态,每有一个可去的子,算100分
|
// 如果形成去子状态,每有一个可去的子,算102分
|
||||||
case NineChess::ACTION_CAPTURE:
|
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;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
|
@ -207,9 +207,9 @@ int NineChessAi_ab::evaluate(Node *node)
|
||||||
// 终局评价最简单
|
// 终局评价最简单
|
||||||
case NineChess::GAME_OVER:
|
case NineChess::GAME_OVER:
|
||||||
if (chessData->player1_Remain < chessTemp.rule.numAtLest)
|
if (chessData->player1_Remain < chessTemp.rule.numAtLest)
|
||||||
value = -infinity;
|
value = -100000;
|
||||||
else if (chessData->player2_Remain < chessTemp.rule.numAtLest)
|
else if (chessData->player2_Remain < chessTemp.rule.numAtLest)
|
||||||
value = infinity;
|
value = 100000;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
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) {
|
if (!depth || chessTemp.data.phase == NineChess::GAME_OVER) {
|
||||||
node->value = evaluate(node);
|
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;
|
return node->value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -286,6 +298,14 @@ int NineChessAi_ab::alphaBetaPruning(int depth, int alpha, int beta, Node *node)
|
||||||
// 取最小值
|
// 取最小值
|
||||||
node->value = minMax;
|
node->value = minMax;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 删除“孙子”节点,防止层数较深的时候节点树太大
|
||||||
|
for (auto child : node->children) {
|
||||||
|
for (auto grandChild : child->children)
|
||||||
|
deleteTree(grandChild);
|
||||||
|
child->children.clear();
|
||||||
|
}
|
||||||
|
|
||||||
// 返回
|
// 返回
|
||||||
return node->value;
|
return node->value;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue