Negamax没有超越深度1

时间:2014-03-26 20:01:05

标签: chess minimax negamax

我正在使用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;
}

1 个答案:

答案 0 :(得分:1)

尝试:

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