在Objective-C中将Alpha-Beta修剪添加到minMax

时间:2013-01-26 20:09:03

标签: objective-c algorithm artificial-intelligence

您好我在连接4游戏的minMax算法中添加alpha beta pruning时遇到问题你能帮助我吗?这是我的minMax程序的代码,它在我看来,只有一点必须要完成的东西躲过我:S scoreBoard()是我的启发式函数,我返回一个有2个值的数组,第一个是位置在桌子上,另一个是这个位置的得分。

-(NSArray*) miniMaxWihtAlphaBetaPrunning:(BOOL)maxOrMin withAlpha:(NSInteger)alpha
 withBeta:(NSInteger)beta withPlayer:(enum playerColor)player andTreeDepth:
(NSInteger)depth
{
if (depth == 0)
{
    return [NSArray arrayWithObjects:[NSNumber numberWithInt:-1],
[NSNumber  numberWithInt:scoreBoard()],nil];
}
else
{
    NSInteger bestScore = maxOrMin ? redWins: blueWins;
    NSInteger bestMove = -1;

    for (NSInteger column = 0; column < 10; column++)
    {
        if (discPlacedMatrix[0][column] != 0)
        {
            continue;
        }

        NSInteger rowFilled = dropDiscAtPoint(column, player);

        if (rowFilled == -1)
        {
            continue;
        }

        NSInteger s = scoreBoard();

        if (s == (maxOrMin? blueWins : redWins))
        {
            bestMove = column;
            bestScore = s;
            discPlacedMatrix[rowFilled][column] = 0;

            break;
        }

        NSArray* result =  [NSArray arrayWithArray:[self miniMaxWihtAlphaBetaPrunning:!maxOrMin withAlpha:alpha withBeta:beta withPlayer:(player == 1 ? RED : BLUE) andTreeDepth:depth - 1]];

        NSInteger scoreInner = [[result objectAtIndex:1] intValue];

        discPlacedMatrix[rowFilled][column] = 0;

        if (scoreInner == blueWins || scoreInner == redWins)
        {
            scoreInner -= depth * player;
        }
        if (maxOrMin)
        {
            if (scoreInner >= bestScore)
            {
                bestScore = scoreInner;
                bestMove = column;
            }
        }
        else
        {
            if (scoreInner <= bestScore)
            {
                bestScore = scoreInner;
                bestMove = column;
            }
        }
    }

    return [NSArray arrayWithObjects:[NSNumber numberWithInt:bestMove],[NSNumber numberWithInt:bestScore],nil];
}

}

我尝试了一些场景,但艾未未开始

1 个答案:

答案 0 :(得分:0)

听起来你正在寻找类似 jamboree算法的东西。这个算法的基本思想是递归地遍历你的树,并在每个级别,在该级别的部分节点上运行alpha beta算法,然后在其余节点上运行mini-max算法。我现在在工作,但是当我回到家时,我会详细说明。

实施:
http://chessprogramming.wikispaces.com/Jamboree