用于抽象的Minmax算法

我正在写一个minmax算法作为井字游戏的人工智能,我在这里遵循了类似的指令,但算法似乎不够智能,即使我试图在树中搜索更深处,任何人都可以帮助分析哪里出错? 非常感谢你提前!

- (int) miniMax:(int)depth : (UIImage*) player {
    NSMutableArray *steps = [self generateMoves];

    if (depth == 0 || [steps count] == 0) {
        return [self evaluate];
    }

    int bestScore = player == myImg ? -1000000 : 1000000;
    int currentScore = 0;
    for (UIImageView *step in steps) {
        step.image = player;
        if (player == myImg) {
            UIImage *opp = player == xImg ? oImg : xImg;
            currentScore = [self miniMax:depth - 1 :opp];
            if (currentScore > bestScore) {
                bestScore = currentScore;
                nextStep = step;
            }
        } else {
            UIImage *opp = player == xImg ? oImg : xImg;
            currentScore = [self miniMax:depth - 1 :opp];
            if (currentScore < bestScore) {
                bestScore = currentScore;
                nextStep = step;
            }
        }
        step.image = NULL;
    }

    return bestScore;
}

- (int) evaluate {
    int score = 0;
    score += [self evaluateLine:img0 :img1 :img2];
    score += [self evaluateLine:img3 :img4 :img5];
    score += [self evaluateLine:img6 :img7 :img8];

    score += [self evaluateLine:img0 :img3 :img6];
    score += [self evaluateLine:img1 :img4 :img7];
    score += [self evaluateLine:img2 :img5 :img8];

    score += [self evaluateLine:img2 :img4 :img6];
    score += [self evaluateLine:img0 :img4 :img8];
    return score;
}

- (int) evaluateLine:(UIImageView*)img1 :(UIImageView*)img2 :(UIImageView*)img3 {
    int score = 0;
    // first cell
    if ([img1 image] == myImg) {
        score = 1;
    } else if ([img1 image] == oppImg){
        score = -1;
    }

    // second cell
    if ([img2 image] == myImg) {
        if (score == 1) {
            score = 10;
        } else if (score == -1) {
            return 0;
        } else {
            score = -1;
        }
    } else if ([img2 image] == oppImg){
        if (score == -1) {
            score = -10;
        } else if (score == 1) {
            return 0;
        } else {
            score = -1;
        }
    }

    // third cell
    if ([img3 image] == myImg) {
        if (score > 0) {
            score *= 10;
        } else if (score < 0) {
            return 0;
        } else {
            score = -1;
        }
    } else if ([img3 image] == oppImg){
        if (score < 0) {
            score *= 10;
        } else if (score > 1) {
            return 0;
        } else {
            score = -1;
        }
    }

    return score;
}

我在这里使用的是:如果存在与人类玩家持有的相同图像,则分数加1.如果在一行或一行或对角线上存在两个或三个玩家的图像,则总分数分别为10和100。 如果在同一行,列或对角线上同时存在“X”和“O”,则分数为0.计算机持有上述这些负分数。


Minimax假设评估函数是从第一个玩家的角度出发的。 你想要的是一个功能,如果第一个玩家比较好,那么它总是会有更高的价值。

看来你的启发函数根据当前的玩家来调整它的值(我不是一个Objective-C程序员)。 更改该功能,以便它不知道myImg或oppImg是什么 - 直接使用xImg和oImg。 可能只是工作。

链接地址: http://www.djcxy.com/p/56369.html

上一篇: Minmax Algorithm for tic

下一篇: Evaluation function of an abstract strategy game