You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Chess AI设为深度3却仅评估1步的问题排查求助

国际象棋AI Minimax函数逻辑错误排查求助

我正在开发一款国际象棋AI,已完成Minimax函数初稿,但该函数似乎完全忽略第一步之后的最大化/最小化逻辑。我使用名为move的结构体来存储走法(algebraicMove字段)和评估值(eval字段),以选择评估最优的走法。以下是该函数代码:

AI_Player::move AI_Player::chooseMoveHelper(int depth, std::string maximizingPlayer, Board currBoard, std::string moveMadeToGetHere /*null move*/) { //the "actual AI" algorithm
    //initializing current node -- O(1)
    move currentNode{moveMadeToGetHere, currBoard.evaluatePosition()};

    //creting a copy of the board so we don't mess up the actual board
    Board boardCpy = currBoard;

    //determine remaining pieces based on color -- O(1)
    std::vector<Board::square> piecesRemaining;
    maximizingPlayer == "white"? piecesRemaining = boardCpy.piecesLeft("white") : piecesRemaining = boardCpy.piecesLeft("black");

    //base case -- O(1)
    if (depth == 0 /*OR checkmate, which hasn't been coded in yet*/)
        return currentNode;

    if (maximizingPlayer == "white") {
        move maxEvalMove{ "/////", -999999999 };
        for (int piece = 0; piece < piecesRemaining.size(); piece++) {
            std::string squareOfPiece = piecesRemaining[piece].file + std::to_string(piecesRemaining[piece].rank);
            std::vector<std::string> moves = playerBoard->legalMoves(squareOfPiece);
            for (int i = 0; i < moves.size(); i++) {
                boardCpy.makeMove(squareOfPiece + ":" + moves[i]);
                move nextEval{squareOfPiece + ":" + moves[i], chooseMoveHelper(depth - 1, "black", boardCpy, squareOfPiece + ":" + moves[i]).eval};

                //call function to adjust evaluation based on piece placement
                nextEval.eval += boardCpy.adjustEvalByPiecePlacement(boardCpy[moves[i]]->piece, moves[i]);

                if (nextEval.eval > maxEvalMove.eval)
                    maxEvalMove = nextEval;
                boardCpy.makeMove(moves[i] + ":" + squareOfPiece);
            }
        }
        return maxEvalMove;
    }
    else {
        move minEvalMove{ "/////", 999999999 };
        for (int piece = 0; piece < piecesRemaining.size(); piece++) {
            std::string squareOfPiece = piecesRemaining[piece].file + std::to_string(piecesRemaining[piece].rank);
            std::vector<std::string> moves = playerBoard->legalMoves(squareOfPiece);
            for (int i = 0; i < moves.size(); i++) {
                //store current piece on square we'll move to:
                char capturedPiece = boardCpy[moves[i]]->piece;

                boardCpy.makeMove(squareOfPiece + ":" + moves[i]);
                move nextEval{ squareOfPiece + ":" + moves[i], chooseMoveHelper(depth - 1, "white", boardCpy, squareOfPiece + ":" + moves[i]).eval };

                //call function to adjust evaluation based on piece placement
                nextEval.eval -= boardCpy.adjustEvalByPiecePlacement(boardCpy[moves[i]]->piece, moves[i]);

                if (nextEval.eval < minEvalMove.eval)
                    minEvalMove = nextEval;
                boardCpy.makeMove(moves[i] + ":" + squareOfPiece);
                boardCpy.editSquare(boardCpy[moves[i]], capturedPiece);
            }
        }
        return minEvalMove;
    }

}

我用如下测试局面进行验证:

r  n  b  .  k  b  n  r
p  p  p  .  p  p  p  p
.  .  .  .  .  .  .  .
.  .  .  q  .  .  .  .
.  .  .  .  .  .  .  .
.  .  N  .  .  .  .  .
P  P  P  P  .  P  P  P
R  .  B  Q  K  B  N  R

在该局面中,第4行的黑方皇后(小写q)正被第6行的白方骑士(N)攻击,理应选择规避走法。但设置搜索深度为3时,AI却选择让黑后吃掉前方的白兵,导致黑后下一步会有3种被吃的可能。我已通过调试反复检查,仍无法定位逻辑错误,特求助排查该问题。


内容的提问来源于stack exchange,提问作者Aryan MP

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 01:32:13