【问题标题】:Negamax not working beyond depth 1Negamax 无法在深度 1 之外工作
【发布时间】:2014-03-26 20:01:04
【问题描述】:

我正在用 C++ 制作一个国际象棋引擎,使用这个算法,我可以在最大深度设置为 1 的情况下获得预期的游戏。但是,除此之外,它会忽略处于危险中的棋子,甚至似乎愿意将自己置于危险之中。

这是我的代码:

int negamax(int depth, int alpha, int beta)
{
    int max = -INFINITY;

    MoveList legalMoves;
    MoveGeneration::generateCaptureMoves(&legalMoves);
    MoveGeneration::generateQuietMoves(&legalMoves);

    // No legal moves
    if(legalMoves.count == 0)
    {
        if(Position::isCheck())
        {
            // Checkmate
            if(Position::activeColor == WHITE)
                return VAL_VICTORY;
            else
                return -VAL_VICTORY;
        }
        else
        {
            // Stalemate
            return 0;
        }
    }

    // Go through legal moves
    for(int i = 0; i < legalMoves.count; i++)
    {
        // Get move score
        Position::doMove(legalMoves[i]);

        int score;
        if(depth == 0)
            score = quiescence(MAX_QUIESCENCE_DEPTH, alpha, beta);
        else
            score = -negamax(depth - 1, alpha, beta);

        Position::undoMove();

        // Pruning
        if(Position::activeColor == WHITE && score > beta) break;
        if(Position::activeColor == BLACK && score < alpha) break;

        if(Position::activeColor == WHITE && score > alpha) alpha = score;
        if(Position::activeColor == BLACK && score < beta) beta = score;

        // Best so far?
        if(score > max)
        {
            max = score;

            if(depth == MAX_DEPTH)
                bestMove = legalMoves[i];
        }
    }
    return max;
}

【问题讨论】:

    标签: chess minimax negamax


    【解决方案1】:

    试试:

    score = -negamax(depth - 1, -beta, -alpha);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-24
      • 1970-01-01
      • 1970-01-01
      • 2020-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多