MCPcopy Create free account
hub / github.com/XMuli/ChineseChess / getBestMove

Method getBestMove

MachineGame.cpp:243–283  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

241}
242
243ChessStep* MachineGame::getBestMove()
244{
245 int depth = getSearchDepth();
246 QVector<ChessStep*> steps;
247 bool aiIsRedTurn = m_bIsRed;
248 getAllMoves(steps, aiIsRedTurn);
249
250 if (steps.isEmpty()) {
251 whoWin();
252 return nullptr;
253 }
254
255 // 走法排序:吃子走法优先
256 std::sort(steps.begin(), steps.end(), [](ChessStep* a, ChessStep* b) {
257 return (a->m_nKillID != -1) > (b->m_nKillID != -1);
258 });
259
260 int bestScore = INT_MIN;
261 ChessStep* bestStep = nullptr;
262
263 for (auto* step : steps) {
264 fakeMove(step);
265 int score = alphaBeta(depth - 1, INT_MIN, INT_MAX, false);
266 unFakeMove(step);
267
268 if (score > bestScore) {
269 bestScore = score;
270 bestStep = step;
271 }
272 }
273
274 // 清理未使用的 step
275 for (auto* s : steps) {
276 if (s != bestStep) delete s;
277 }
278
279 if (!bestStep)
280 whoWin();
281
282 return bestStep;
283}
284
285void MachineGame::machineChooseAndMovePieces()
286{

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected