tic-tac-toe Minimax(极小化极大算法)

来源:互联网 发布:网络吞吐率 编辑:程序博客网 时间:2024/06/05 21:05

                     说起人工智能,说起博弈论,不得不说的一个经典的小游戏tic-tac-toe,围棋,五子棋,六子棋都可用此法测试

public void minmax(int chess[][], int depth,  int Alpha, int Beta)//找最大
{
int best=-1000, tempt=0;
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
{
if(chess[i][j]==0)//有空棋子
{
chess[i][j]=2;//填black,找MAX
tempt=min(chess, depth-1, Alpha, Beta);
if(tempt>best)
{
best = tempt;
this.x=i;
this.y=j;//记录下标
}
chess[i][j] = 0;
}//if(chess[i][j]==0)//有空棋子
}
}
public void minmax2(int chess[][], int depth, int Alpha, int Beta)//找MIN这是下white
{
int best = 1000, tempt=0;
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
{
if(chess[i][j]==0)//有空棋子
{
chess[i][j]=1;//填black,并找MAX
tempt=max(chess, depth-1, Alpha, Beta);
if(tempt < best)
{
best = tempt;
this.x=i;
this.y=j;//记录下标
}
chess[i][j] = 0;
}//if(chess[i][j]==0)//有空棋子
}
}
//MAX_SEARCH
public int max(int chess[][], int depth, int Alpha, int Beta)//找MAX
{
if(depth<=0||getState(chess)!=0)                  //搜到头是评价函数
{
return caculateFun(chess);//越正 对黑棋越有利,越负对白棋越有利
}
int tempt=0;
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
{
if(chess[i][j]==0)//有空棋子
{
chess[i][j] = 2;//填白子并找最大的
tempt = min(chess, depth-1, Alpha, Beta);
chess[i][j] = 0;
if(tempt>Alpha)
                    {
                        Alpha = tempt;  //-----
                        if(Alpha >= Beta)  //------
                            return Alpha;  //进行α剪枝   返回贝塔也行 下面对应改变 
                    }
}
}
return Alpha;
}
//MIN_SEARCH
public int min(int chess[][], int depth, int Alpha, int Beta)//找MIN
{
if(depth<=0||getState(chess)!=0)                  //搜到头是评价函数
{
return caculateFun(chess);//+depth;//越正 对黑棋越有利,越负对白棋越有利
}
int tempt=0;
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
{
if(chess[i][j]==0)//有空棋子
{
chess[i][j] = 1;
tempt = max(chess, depth-1, Alpha, Beta);
chess[i][j]=0;
if(tempt < Beta)//找MIN
{
Beta = tempt;
if(Alpha >= Beta)  //------
                            return Beta;  //进行beta剪枝
}
}
}
return Beta;
}
public int layChessMaxMin(int chess[][], int depth, int Alpha,int Beta)//结果放在静态变量的想x,y中
{
if(depth<=0||getState(chess)!=0)      //搜到头是评价函数
return caculateFun(chess);
int tempt=0;
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
{
if(chess[i][j]==0)//有空棋子
if(depth%2==1)//1 3
{
chess[i][j]=2;//black
tempt = layChessMaxMin(chess, depth-1, Alpha, Beta);//从下一层找最大的
chess[i][j]=0;
if(tempt>Alpha)
{
Alpha = tempt;  //-----
if(depth==DEPTH)//搜索深度
{
this.x=i;
this.y=j;
}
if(Alpha >= Beta)  //------
return Alpha;  //进行α剪枝返回贝塔也行 下面对应改变
}
}//if(depth%2==1)///////////////////////////////////////////////////
else //if(depth=0,2 4
{
chess[i][j] = 1;
tempt = layChessMaxMin(chess, depth-1, Alpha, Beta);
chess[i][j]=0;
if(tempt < Beta)//找最小//---------
{  
Beta = tempt;
if(Alpha >= Beta)  //------
return Beta;  //进行beta剪枝
}
}
}
System.out.println("BLACK think");
return depth%2==1? Alpha : Beta;
}
public int caculateFun(int chess[][])//评价函数black赢返回100 blue赢返回-100
{   
if(getState(chess)==1)//blue色赢了返回-1000
return -100;
if(getState(chess)==2)//black赢了返回   1000
return  100;
if(getState(chess)==-1)//平局返回   0
return 0;
int x = 0, y = 0;
for(int i=0;i<3;i++)
    {
         if(chess[i][0]!=1 && chess[i][1]!=1 && chess[i][2]!=1) 
         x++;
         if(chess[0][i]!=1&& chess[1][i]!=1 && chess[2][i]!=1) 
         x++;
    }
        if((chess[0][0]!=1&&chess[1][1]!=1&&chess[2][2]!=1))
        x++;
        if((chess[2][0]!=1&&chess[1][1]!=1&&chess[0][2]!=1)) 
        x++;
        


for(int i=0;i<3;i++)
    {
         if(chess[i][0]!=2 && chess[i][1]!=2 && chess[i][2]!=2) 
         y++;
         if(chess[0][i]!=2 && chess[1][i]!=2 && chess[2][i]!=2) 
         y++;
    }
        if((chess[0][0]!=2&&chess[1][1]!=2&&chess[2][2]!=2))
        y++;
        if((chess[2][0]!=2&&chess[1][1]!=2&&chess[0][2]!=2)) 
        y++;
return x-y;
}
public int layChessMaxMin2(int chess[][], int depth, int Alpha,int Beta)//结果放在静态变量的想x,y中
{
if(depth<=0||getState(chess)!=0)      //搜到头,评价
return caculateFun(chess);
int tempt=0;
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
{
if(chess[i][j]==0)//有空棋子
if(depth%2==1)//初始化best的值,
{
chess[i][j]=1;//white
tempt = layChessMaxMin2(chess, depth-1, Alpha, Beta);//从下一层找最小的
chess[i][j]=0;
if(tempt < Beta)//找最小
{
Beta = tempt;
if(depth==DEPTH)//搜索深度
{
this.x=i;
this.y=j;
}
if(Alpha >= Beta)  
return Beta;  //进行beta剪枝
}
}//if(depth%2==1)
else //if(depth=0,2)//
{
chess[i][j] = 2;
tempt = layChessMaxMin2(chess, depth-1, Alpha, Beta);
chess[i][j]=0;
if(tempt>Alpha)//找MAX
{
Alpha = tempt;  
if(Alpha >= Beta)  
return Alpha;  //进行α剪枝返回贝塔也行 下面对应改变
}
}
}//
return depth%2==1? Beta : Alpha;
}
boolean isFull(int array[][])
{
for(int i = 0; i < 3; i++)
for(int j = 0; j < 3; j++)
if(array[i][j]==0)
return false;
return true;
}
int num(int array[][])
{
int n=0;
for(int i = 0; i < 3; i++)
for(int j = 0; j < 3; j++)
if(array[i][j]!=0)
n++;
return n;
}
public int getState(int chess[][])//black赢返回1 white赢返回2  -1为返回平局
{
    for(int i=0;i<3;i++)
    {
         if(chess[i][0]==1 && chess[i][1]==1 && chess[i][2]==1) return 1;
         if(chess[i][0]==2 && chess[i][1]==2 && chess[i][2]==2) return 2;
    }
    for(int i=0;i<3;i++)
    {
         if(chess[0][i]==1 && chess[1][i]==1 && chess[2][i]==1) return 1;
         if(chess[0][i]==2 && chess[1][i]==2 && chess[2][i]==2) return 2;
    }
         if((chess[0][0]==1&&chess[1][1]==1&&chess[2][2]==1)||(chess[2][0]==1&&chess[1][1]==1&&chess[0][2]==1)) return 1;
    if((chess[0][0]==2&&chess[1][1]==2&&chess[2][2]==2)||(chess[2][0]==2&&chess[1][1]==-1&&chess[0][2]==2)) return 2;


    if(isFull(chess))//平局
    return -1;
    return 0;        //进行中


}
}