Iphone Objective-C中tic-tac-toe博弈的Minmax算法
我正在写一个minmax算法作为tic-tac-toe游戏的人工智能,我遵循了类似的指令,但该算法似乎不够智能,即使我试图在树中搜索更深的位置,有人能帮助分析哪里出了问题吗?提前非常感谢Iphone Objective-C中tic-tac-toe博弈的Minmax算法,iphone,objective-c,artificial-intelligence,minmax,Iphone,Objective C,Artificial Intelligence,Minmax,我正在写一个minmax算法作为tic-tac-toe游戏的人工智能,我遵循了类似的指令,但该算法似乎不够智能,即使我试图在树中搜索更深的位置,有人能帮助分析哪里出了问题吗?提前非常感谢 - (int) miniMax:(int)depth : (UIImage*) player { NSMutableArray *steps = [self generateMoves]; if (depth == 0 || [steps count] == 0) {
- (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;
}
-(int)miniMax:(int)depth:(UIImage*)播放器{
NSMutableArray*步骤=[自生成移动];
如果(深度==0 | |[步数]==0){
返回[自我评估];
}
int bestScore=player==myImg?-1000000:1000000;
int currentScore=0;
用于(UIImageView*分步执行){
step.image=player;
if(player==myImg){
UIImage*opp=player==xImg?oImg:xImg;
currentScore=[自最小最大值:深度-1:opp];
如果(当前分数>最佳分数){
最佳分数=当前分数;
下一步=步骤;
}
}否则{
UIImage*opp=player==xImg?oImg:xImg;
currentScore=[自最小最大值:深度-1:opp];
如果(当前分数<最佳分数){
最佳分数=当前分数;
下一步=步骤;
}
}
step.image=NULL;
}
返回最佳分数;
}
-(int)评估{
智力得分=0;
分数+=[自我评估行:img0:img1:img2];
分数+=[自我评估行:img3:img4:img5];
分数+=[自我评估行:img6:img7:img8];
分数+=[自我评估行:img0:img3:img6];
分数+=[自我评估行:img1:img4:img7];
分数+=[自我评估行:img2:img5:img8];
分数+=[自我评估行:img2:img4:img6];
分数+=[自我评估行:img0:img4:img8];
返回分数;
}
-(int)evaluateLine:(UIImageView*)img1:(UIImageView*)img2:(UIImageView*)img3{
智力得分=0;
//第一单元
如果([img1图像]==myImg){
得分=1分;
}else if([img1图像]==oppImg){
得分=-1;
}
//第二单元
如果([img2图像]==myImg){
如果(分数=1){
得分=10分;
}否则如果(分数==-1){
返回0;
}否则{
得分=-1;
}
}else if([img2图像]==oppImg){
如果(分数==-1){
分数=-10;
}否则如果(分数=1){
返回0;
}否则{
得分=-1;
}
}
//第三单元
如果([img3图像]==myImg){
如果(分数>0){
分数*=10;
}否则如果(分数<0){
返回0;
}否则{
得分=-1;
}
}否则如果([img3图像]==oppImg){
如果(分数<0){
分数*=10;
}否则如果(分数>1){
返回0;
}否则{
得分=-1;
}
}
返回分数;
}
我在这里使用的是:如果存在与人类玩家持有的相同图像,则分数加1。如果有
是两个或三个球员的形象在一条直线或一排或对角线,总分分别为10分和100分。如果在同一行、列或对角线中同时存在“X”和“O”,则分数为0。计算机会为上述内容保留负分。极小极大假设评估函数是从第一个玩家的角度进行的。你想要的是一个函数,如果第一个玩家更好的话,它总是有一个更高的值
您的启发式函数似乎根据当前播放器(我不是objective-C程序员)调整其值。更改该函数,使其不知道myImg或oppImg是什么——直接使用xImg和oImg。也许会管用 你说的“怎么了”是什么意思?问题是什么?@user2311023问题是它仍然没有那么智能,例如,如果我把两个“X”排成一行,计算机不会试图阻止它。因此,我认为我的算法实现有问题。我认为让人们在没有指出具体问题的情况下检查代码是不合适的。