Alpha-beta修剪为TicTacToe做了太少的迭代

时间:2014-07-14 15:22:40

标签: ios objective-c artificial-intelligence minimax

我目前正在调整我的Minimax算法以考虑alpha-beta修剪。我的目标是18297次迭代,但目前我只得到14801.我不确定我在哪里丢失迭代,任何帮助都会受到赞赏。

这是我的代码:

static int iterations = 0;
- (int) minimaxWithRoot:(Board *) board withDepth: (NSInteger) depth andMaximisingPlayer:(BOOL)maxPlayer andAlpha:(int)alpha beta:(int)beta
{
    iterations = iterations + 1;

    if (board.gameOver || [[board possibleMoves] count] == 0) {
        int score = [self scoreForBoard:board];
        return score;
    }

    if (maxPlayer)
    {
        for (NSValue *moveWrapper in [board possibleMoves]) {
            CGPoint move = moveWrapper.CGPointValue;

            Board *newBoard = [board copy];
            [newBoard playCrossMove:move];

            int value = [self minimaxWithRoot:newBoard withDepth:depth - 1 andMaximisingPlayer:NO andAlpha:alpha beta:beta];
            if (value > alpha) {
                alpha = value;
                self.chosenMove = move;
            }

            if (alpha >= beta) {
                break;
            }
        }
        return alpha;
    } else {
        for (NSValue *moveWrapper in [board possibleMoves]) {
            CGPoint move = moveWrapper.CGPointValue;

            Board *newBoard = [board copy];
            [newBoard playCircleMove:move];

            int value = [self minimaxWithRoot:newBoard withDepth:depth - 1 andMaximisingPlayer:YES andAlpha:alpha beta:beta];

            if (value < beta) {
                beta = value;
                self.chosenMove = move;
            }

            if (beta <= alpha) {
                break;
            }
        }
        return beta;
    }
}

0 个答案:

没有答案