#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define SIZE 15
#define CHARSIZE 2

#define M1 35
#define M2 800
#define M3 15000
#define M4 800000
//OiʾԪiֵ
#define O1 20
#define O2 500
#define O3 4000
#define O4 300000

#define VO 7          //ԪΪ
#define PU 0          //ԪмԼжֵ
#define NO 0          //Ԫ飨߽

void initRecordBorard(void);
void innerLayoutToDisplayArray(void);
void displayBoard(void);
void fiveai1(void);
void fiveai2(void);
//ƽƽĸıturnСһǵ֮Ļ
void changeturn(void);
long nextscore[SIZE][SIZE];
long score[SIZE][SIZE];
//ȡuv
void getuv(void);
//÷
long countscore(int x1,int y1);
//ҵе÷еֵֵ
int greatscore(void);
//ҵе÷еСֵСֵ
int leastscore(void);
//ˮƽķ
long countx(int x1,int y1);
//ֱķ
long county(int x1,int y1);
//ϵ
long countxy(int x1,int y1);
//ϵ
long countyx(int x1,int y1);
//mark
long markx(int x1,int y1);
long marky(int x1,int y1);
long markxy(int x1,int y1);
long markyx(int x1,int y1);
//myԼoppʱķ
long addscore(int my,int opp);
//¼
int searchdepth=0;
//turn¼AIĸ
int turn;
//ѡͬģʽ
int module;
//¼ǰĻغ
int cc=1;
//¼AIĺ
int u,v;
//¼ӵĲ
void reminder(void);//ѵǰ֣player=1A=0B
int player=1;//ڼ¼ǰ˭壻
int x;//
int y;//
void getposition(void);//ڻȡ꣬x,y
void playchess(void);//ڰ½ȥ
int win=0;//ûӮʱ0Ӯ֮aӮ1BӮ2
int parttern; //ģʽ
void judgeend(void);//жǷӮˣ˭Ӯ
void autoplay1(void);//AI
void autoplay2(void);//AI
void validposition(void);//жǷЧ
//ʹõGBK룬ÿһַռ2ֽڡ

//ֲ
int myban(int xban,int yban);
int oppban(int xban,int yban);
int countbanx(int xban,int yban);
int countbany(int xban,int yban);
int countbanxy(int xban,int yban);
int countbanyx(int xban,int yban);

//ģ 
char arrayForEmptyBoard[SIZE][SIZE*CHARSIZE+1] = 
{
		"өөөөөөөөөөөөө",
		"ĩ",
		"ĩ",
		"ĩ",
		"ĩ",
		"ĩ",
		"ĩ",
		"ĩ",
		"ĩ",
		"ĩ",
		"ĩ",
		"ĩ",
		"ĩ",
		"ĩ",
		"۩۩۩۩۩۩۩۩۩۩۩۩۩"
};
//洢ʾ 
char arrayForDisplayBoard[SIZE][SIZE*CHARSIZE+1];
char play1Pic[]="";//array;
char play1CurrentPic[]=""; 

char play2Pic[]="";//;
char play2CurrentPic[]="";

//ڼ¼ǰ̵ĸ 
int arrayForInnerBoardLayout[SIZE][SIZE];

int main()

{
    
    initRecordBorard();    //ʼһ
    innerLayoutToDisplayArray(); 
    displayBoard(); 
    printf("룺дĸ   磺D,5\n");
    printf("ѡģʽ1 ˶ս  2 ˻ս֣ 3 ˻ս˺֣\n");
    printf("ѡģʽΪ");
    scanf("%d\n",&parttern);
    if(parttern==1){
        innerLayoutToDisplayArray(); 
        displayBoard();     
        reminder();
        getposition(); 
        while(win==0){
            playchess();
            judgeend();
        }
    }
    else if(parttern==2){
        innerLayoutToDisplayArray(); 
        displayBoard();     
        reminder();
        getposition(); 
        while(win==0){
            autoplay2();
            judgeend();
        }    
    }      
    else if(parttern==3){
        innerLayoutToDisplayArray(); 
        displayBoard();     
        while(win==0){
        autoplay1();
        judgeend();
        }    
    }
    else
    printf("ѡģʽ1 ˶ս  2 ˻ս֣ 3 ˻ս֣\n"); 
    return 0;
}

//ʼһ̸ 
void initRecordBorard(void){
	//ͨ˫ѭarrayForInnerBoardLayout0
      int i,j;
      for (i=0;i<SIZE;i++)
          for(j=0;j<SIZE;j++)
             arrayForInnerBoardLayout[i][j]=0;
}

//arrayForInnerBoardLayoutм¼λãתarrayForDisplayBoard
void innerLayoutToDisplayArray(void){
	//һarrayForEmptyBoardм¼Ŀ̣ƵarrayForDisplayBoard
    int i,j;
    for(i=0;i<SIZE;i++){
        for(j=0;j<=SIZE*CHARSIZE+1;j++)
           arrayForDisplayBoard[i][j]=arrayForEmptyBoard[i][j];
    }


	//ڶɨarrayForInnerBoardLayout0ԪأߡƵarrayForDisplayBoardӦλ
	//ע⣺arrayForDisplayBoard¼ַַÿַռ2ֽڡ͡ҲַÿҲռ2ֽڡ
    for(i=0;i<SIZE;i++){
        for(j=0;j<SIZE;j++){
           if(arrayForInnerBoardLayout[i][j]==1){
               arrayForDisplayBoard[i][CHARSIZE*j]=play1Pic[0];
               arrayForDisplayBoard[i][CHARSIZE*j+1]=play1Pic[1];
               if(CHARSIZE==3)
                 arrayForDisplayBoard[i][CHARSIZE*j+2]=play1Pic[2];
           }else if(arrayForInnerBoardLayout[i][j]==2){
               arrayForDisplayBoard[i][CHARSIZE*j]=play2Pic[0];
               arrayForDisplayBoard[i][CHARSIZE*j+1]=play2Pic[1];
              if(CHARSIZE==3)
                 arrayForDisplayBoard[i][CHARSIZE*j+2]=play2Pic[2];
           }else if(arrayForInnerBoardLayout[i][j]==-1){
               arrayForDisplayBoard[i][CHARSIZE*j]=play1CurrentPic[0];
               arrayForDisplayBoard[i][CHARSIZE*j+1]=play1CurrentPic[1];
               if(CHARSIZE==3)
                 arrayForDisplayBoard[i][CHARSIZE*j+2]=play1CurrentPic[2];
           }else if(arrayForInnerBoardLayout[i][j]==-2){
               arrayForDisplayBoard[i][CHARSIZE*j]=play2CurrentPic[0];
               arrayForDisplayBoard[i][CHARSIZE*j+1]=play2CurrentPic[1];
              if(CHARSIZE==3)
                 arrayForDisplayBoard[i][CHARSIZE*j+2]=play2CurrentPic[2];
           }
           }
    }
}
//ʾ̸ 
void displayBoard(void){
	int i;
	//һ
	system("clear");   //  
	//ڶarrayForDisplayBoardĻ
    for(i=0;i<SIZE;i++){
        printf("%3d %s\n",SIZE-i,arrayForDisplayBoard[i]);
    }
	

	//һĸA B .... 
    printf("    ");
    for(i=0;i<SIZE;i++)
       printf("%2c",'A'+i);
    printf("\n");

} 

//¼ӵĲ
void reminder(void){
    if(player){
        printf("Player A : ");
       // player=0;
    }else{
        printf("Player B : ");
     //   player=1;
    }
}
//ȡ
void getposition(void){
    char p;
    int q;
    printf("\n");
    scanf("%c,%d",&p,&q);
    //printf("\n");
    switch(p){
        case 'A': x=0; break;
        case 'B' : x=1; break;
        case 'C' : x=2; break;
        case 'D': x=3; break;
        case 'E': x=4; break;
        case 'F': x=5; break;
        case 'G': x=6; break;
        case 'H': x=7; break;
        case 'I': x=8; break;
        case 'J': x=9; break;
        case 'K': x=10; break;
        case 'L': x=11; break;
        case 'M': x=12; break;
        case 'N': x=13; break;
        case 'O': x=14; break;
        default:
        printf("Ƿ룬꣺"); getposition(); return;
    }
    if(q>0&&q<=15)
    y=15-q;
    else{
        printf("Ƿ룬꣺"); getposition(); return;
    }

}
//
void playchess(void){
    int m,n;
    for(m=0;m<SIZE;m++){
        for(n=0;n<SIZE;n++){
           if(arrayForInnerBoardLayout[m][n]==-1){
               arrayForInnerBoardLayout[m][n]=1;
           }else if(arrayForInnerBoardLayout[m][n]==-2){
              arrayForInnerBoardLayout[m][n]=2;
           }
        }
    }
    if(player==1){
         validposition();    
         arrayForInnerBoardLayout[y][x]=-1;//arrayForInnerBoardLayout[y][x]=-1;
         innerLayoutToDisplayArray();
         displayBoard();
         player=0;
         reminder();
         getposition();

    }else if(player==0){
         validposition();
         arrayForInnerBoardLayout[y][x]=-2;//arrayForInnerBoardLayout[y][x]=-2;
         innerLayoutToDisplayArray();
         displayBoard();
         player=1;
         reminder();
         getposition();
    }
}

//judgeжǷӮˣж˭Ӯ
void judgeend(void){
    int i,j;
    for (i=0;i<(SIZE-5);i++){   //жϺǷ
        for(j=0;j<SIZE;j++){
            if(arrayForInnerBoardLayout[i][j]==1 || arrayForInnerBoardLayout[i][j]==-1){
                if(arrayForInnerBoardLayout[i+1][j]==1 || arrayForInnerBoardLayout[i+1][j]==-1){
                    if(arrayForInnerBoardLayout[i+2][j]==1||arrayForInnerBoardLayout[i+2][j]==-1){
                        if(arrayForInnerBoardLayout[i+3][j]==1||arrayForInnerBoardLayout[i+3][j]==-1){
                            if(arrayForInnerBoardLayout[i+4][j]==1||arrayForInnerBoardLayout[i+4][j]==-1){
                                win=1;
                            }
                        }
                    }
                }
            }else if(arrayForInnerBoardLayout[i][j]==2 || arrayForInnerBoardLayout[i][j]==-2){
                if(arrayForInnerBoardLayout[i+1][j]==2 || arrayForInnerBoardLayout[i+1][j]==-2){
                    if(arrayForInnerBoardLayout[i+2][j]==2||arrayForInnerBoardLayout[i+2][j]==-2){
                        if(arrayForInnerBoardLayout[i+3][j]==2||arrayForInnerBoardLayout[i+3][j]==-2){
                            if(arrayForInnerBoardLayout[i+4][j]==2||arrayForInnerBoardLayout[i+4][j]==-2){
                                win=2;
                            }
                        }
                    }
                }
            }
        }
    }
    for (i=0;i<SIZE;i++){   //жǷ
        for(j=0;j<(SIZE-5);j++){
             if(arrayForInnerBoardLayout[i][j]==1 || arrayForInnerBoardLayout[i][j]==-1){
                if(arrayForInnerBoardLayout[i][j+1]==1 || arrayForInnerBoardLayout[i][j+1]==-1){
                    if(arrayForInnerBoardLayout[i][j+2]==1||arrayForInnerBoardLayout[i][j+2]==-1){
                        if(arrayForInnerBoardLayout[i][j+3]==1||arrayForInnerBoardLayout[i][j+3]==-1){
                            if(arrayForInnerBoardLayout[i][j+4]==1||arrayForInnerBoardLayout[i][j+4]==-1){
                                win=1;
                            }
                        }
                    }
                }
            }else if(arrayForInnerBoardLayout[i][j]==2 || arrayForInnerBoardLayout[i][j]==-2){
               if(arrayForInnerBoardLayout[i][j+1]==2 || arrayForInnerBoardLayout[i][j+1]==-2){
                    if(arrayForInnerBoardLayout[i][j+2]==2||arrayForInnerBoardLayout[i][j+2]==-2){
                        if(arrayForInnerBoardLayout[i][j+3]==2||arrayForInnerBoardLayout[i][j+3]==-2){
                            if(arrayForInnerBoardLayout[i][j+4]==2||arrayForInnerBoardLayout[i][j+4]==-2){
                                win=2;
                            }
                        }
                    }
                }
            }
        }
    }
    for (i=0;i<SIZE;i++){   //жϴб
        for(j=0;j<SIZE;j++){
          if((i+4<SIZE)&& (j+4<SIZE)){
            if(arrayForInnerBoardLayout[i][j]==1||arrayForInnerBoardLayout[i][j]==-1){
                if(arrayForInnerBoardLayout[i+1][j+1]==1||arrayForInnerBoardLayout[i+1][j+1]==-1){
                    if(arrayForInnerBoardLayout[i+2][j+2]==1||arrayForInnerBoardLayout[i+2][j+2]==-1){
                        if(arrayForInnerBoardLayout[i+3][j+3]==1||arrayForInnerBoardLayout[i+3][j+3]==-1){
                            if(arrayForInnerBoardLayout[i+4][j+4]==1||arrayForInnerBoardLayout[i+4][j+4]==-1){
                                win=1;
                            }
                        }
                    }
                }
            }else if(arrayForInnerBoardLayout[i][j]==2|| arrayForInnerBoardLayout[i][j]==-2){
                if(arrayForInnerBoardLayout[i+1][j+1]==2||arrayForInnerBoardLayout[i+1][j+1]==-2){
                    if(arrayForInnerBoardLayout[i+2][j+2]==2||arrayForInnerBoardLayout[i+2][j+2]==-2){
                        if(arrayForInnerBoardLayout[i+3][j+3]==2||arrayForInnerBoardLayout[i+3][j+3]==-2){
                            if(arrayForInnerBoardLayout[i+4][j+4]==2||arrayForInnerBoardLayout[i+4][j+4]==-2){
                                win=2;
                            }
                        }
                    }
                }
            }
          }
        }
    }
   for (i=0;i<SIZE;i++){   //жϴб
        for(j=0;j<SIZE;j++){
          if((i-4>=0) && (j+4<SIZE)){
            if(arrayForInnerBoardLayout[i][j]==1||arrayForInnerBoardLayout[i][j]==-1){
                if(arrayForInnerBoardLayout[i-1][j+1]==1||arrayForInnerBoardLayout[i-1][j+1]==-1){
                    if(arrayForInnerBoardLayout[i-2][j+2]==1||arrayForInnerBoardLayout[i-2][j+2]==-1){
                        if(arrayForInnerBoardLayout[i-3][j+3]==1||arrayForInnerBoardLayout[i-3][j+3]==-1){
                            if(arrayForInnerBoardLayout[i-4][j+4]==1||arrayForInnerBoardLayout[i-4][j+4]==-1){
                                win=1;
                            }
                        }
                    }
                }
            }else if(arrayForInnerBoardLayout[i][j]==2||arrayForInnerBoardLayout[i][j]==-2){
                  if(arrayForInnerBoardLayout[i-1][j+1]==2||arrayForInnerBoardLayout[i-1][j+1]==-2){
                    if(arrayForInnerBoardLayout[i-2][j+2]==2||arrayForInnerBoardLayout[i-2][j+2]==-2){
                        if(arrayForInnerBoardLayout[i-3][j+3]==2||arrayForInnerBoardLayout[i-3][j+3]==-2){
                            if(arrayForInnerBoardLayout[i-4][j+4]==2||arrayForInnerBoardLayout[i-4][j+4]==-2){
                                win=2;
                            }
                        }
                    }
                }
            }
          }
        }
    }
    if(win==1){
        printf("    Player A wins!!!\n");
    }else if(win==2){
        printf("    Player B wins!!!\n");
    }
}


void autoplay2(void){
    int m,n;
    //int u,v;
    for(m=0;m<SIZE;m++){
        for(n=0;n<SIZE;n++){
           if(arrayForInnerBoardLayout[m][n]==-1){
               arrayForInnerBoardLayout[m][n]=1;
           }else if(arrayForInnerBoardLayout[m][n]==-2){
              arrayForInnerBoardLayout[m][n]=2;
           }
        }
    }
    if(player==1){
         validposition();
         
         arrayForInnerBoardLayout[y][x]=-1;//arrayForInnerBoardLayout[y][x]=-1;
         
         innerLayoutToDisplayArray();
         displayBoard();
         arrayForInnerBoardLayout[y][x]=1;
         if(oppban(y,x)!=0){
            printf("ǽλã\n");
            int coon;
            scanf("%d",&coon);
         }
         player=0;    
    }else if(player==0){
            fiveai2();
          //Ҫ䶯ĵط
         arrayForInnerBoardLayout[u][v]=-2;//arrayForInnerBoardLayout[y][x]=-2;
         innerLayoutToDisplayArray();
         displayBoard();
         printf("FIVE AI%c,%d\n",v+'A',15-u);
         player=1;
         printf(":");
         getposition();
         //arrayForInnerBoardLayout[y][x]=1;
         //if(oppban(y,x)!=0)
         //   printf("ǽλã\n");
	//arrayForInnerBoardLayout[y][x]=0;
	
    }
}
void autoplay1(void){
    int m,n;
    //int u,v;
    for(m=0;m<SIZE;m++){
        for(n=0;n<SIZE;n++){
           if(arrayForInnerBoardLayout[m][n]==-1){
               arrayForInnerBoardLayout[m][n]=1;
           }else if(arrayForInnerBoardLayout[m][n]==-2){
              arrayForInnerBoardLayout[m][n]=2;
           }
        }
    }
    if(player==0){
        validposition();
        arrayForInnerBoardLayout[y][x]=-2;//arrayForInnerBoardLayout[y][x]=-1;
        innerLayoutToDisplayArray();
        displayBoard();
        player=1;
    }else if(player==1){
        fiveai1();
        arrayForInnerBoardLayout[u][v]=-1;//arrayForInnerBoardLayout[y][x]=-2;
        innerLayoutToDisplayArray();
        displayBoard();
        printf("FIVE AI%c,%d\n",v+'A',15-u);
        player=0;
        printf(":");
        getposition();
    }
}
void validposition(void){
    while (arrayForInnerBoardLayout[y][x]!=0){
        printf("棺λӣ·ã\n·õλã");
        getposition();
    }
}

void fiveai1(void){
    turn=1;
    if(cc==1){
        u=7;
        v=7;
        cc++;
        return ;
    }
    int i,j;
    for(i=0;i<SIZE;i++){
        for(j=0;j<SIZE;j++){
            
            if(arrayForInnerBoardLayout[i][j]!=0)
                score[i][j]=-1;
            
            else if(myban(i,j)!=0)
                score[i][j]=-2;
            else
                score[i][j]=countscore(i,j);
        }
    }
	for(i=0;i<SIZE;i++){
        for(j=0;j<SIZE;j++){
            printf(" %8ld ",score[i][j]);
        }
	printf("\n");
    }
    //greatscore();
    getuv();
}

void fiveai2(void){
    turn=2;
    int i,j;
    for(i=0;i<SIZE;i++){
        for(j=0;j<SIZE;j++){
            if(arrayForInnerBoardLayout[i][j]!=0)
                score[i][j]=-1;
            else
                score[i][j]=countscore(i,j);
        }
    }
    for(i=0;i<SIZE;i++){
        for(j=0;j<SIZE;j++){
            printf(" %8ld ",score[i][j]);
        }
	printf("\n");
    }
    //greatscore();
    getuv();
}

long countscore(int x1,int y1){
    long direc[4];
    direc[0]=countx(x1,y1);
    direc[1]=county(x1,y1);
    direc[2]=countxy(x1,y1);
    direc[3]=countyx(x1,y1);
    return direc[0]+direc[1]+direc[2]+direc[3];
}

long countx(int x1,int y1){
    long goal[5];
    int i1,j1=0;
    for(i1=y1-4;i1<=y1;i1++)
        goal[j1++]=markx(x1,i1);
    return goal[0]+goal[1]+goal[2]+goal[3]+goal[4];
}
long markx(int x1,int y1){
    if(y1<0||(y1+4)>=SIZE)
        return NO;
    else{
        int i2,my=0,opp=0;//rֻ
        for(i2=y1;i2<=(y1+4);i2++){
            if(arrayForInnerBoardLayout[x1][i2]==turn)
                my++;
            if(arrayForInnerBoardLayout[x1][i2]!=turn&&arrayForInnerBoardLayout[x1][i2]!=0)
                opp++;
        }
        return addscore(my,opp);
    }
}
long county(int x1,int y1){
    long goal[5];
    int i1,j1=0;
    for(i1=x1-4;i1<=x1;i1++)
        goal[j1++]=marky(i1,y1);
    return goal[0]+goal[1]+goal[2]+goal[3]+goal[4];
}
long marky(int x1,int y1){
    if(x1<0||(x1+4)>=SIZE)
        return NO;
    else{
        int i2,my=0,opp=0;//rֻ
        for(i2=x1;i2<=(x1+4);i2++){
            if(arrayForInnerBoardLayout[i2][y1]==turn)
                my++;
            if(arrayForInnerBoardLayout[i2][y1]!=turn&&arrayForInnerBoardLayout[i2][y1]!=0)
                opp++;
        }
        return addscore(my,opp);
    }
}
long countxy(int x1,int y1){
    long goal[5];
    int i1,j1,number=0;
    for(i1=x1-4,j1=y1-4;i1<=x1,j1<=y1;i1++,j1++)
        goal[number++]=markxy(i1,j1);
    return goal[0]+goal[1]+goal[2]+goal[3]+goal[4];
}
long countyx(int x1,int y1){
    long goal[5];
    int i1,j1,number=0;
    for(i1=x1+4,j1=y1-4;i1>=x1,j1<=y1;i1--,j1++)
        goal[number++]=markyx(i1,j1);
    return goal[0]+goal[1]+goal[2]+goal[3]+goal[4];
}
long markxy(int x1,int y1){
    if(x1<0||y1<0||(x1+4)>=SIZE||(y1+4)>=SIZE)
        return NO;
    else{
        int i2,j2,my=0,opp=0;
        for(i2=x1,j2=y1;i2<=(x1+4),j2<=(y1+4);i2++,j2++){
            if(arrayForInnerBoardLayout[i2][j2]==turn)
                my++;
            if(arrayForInnerBoardLayout[i2][j2]!=turn&&arrayForInnerBoardLayout[i2][j2]!=0)
                opp++;
        }
        return addscore(my,opp);
    }
}
long markyx(int x1,int y1){
    if((x1-4)<0||y1<0||x1>=SIZE||(y1+4)>=SIZE)
        return NO;
    else{
        int q,w,my=0,opp=0;
        for(q=x1,w=y1;q>=(x1-4),w<=(y1+4);q--,w++){
            if(arrayForInnerBoardLayout[q][w]==turn)
                my++;
            if(arrayForInnerBoardLayout[q][w]!=turn&&arrayForInnerBoardLayout[q][w]!=0)
                opp++;
        }
        return addscore(my,opp);
    }
}

long addscore(int my,int opp){
    if(my!=0&&opp!=0)
        return PU;
    else if(opp==0){
        switch(my){
            case 1:return M1; break;
            case 2:return M2; break;
            case 3:return M3; break;
            case 4:return M4; break;
            default: return VO;
        }
    }
    else{
        switch(opp){
            case 1:return O1; break;
            case 2:return O2; break;
            case 3:return O3; break;
            case 4:return O4; break;
            default:return VO;
        }
    }
}

int greatscore(void){
    int q=0,w=0;
    //u=q,v=w;
    int max=-1;
    for(q=0;q<SIZE;q++){
        for(w=0;w<SIZE;w++){
            if(score[q][w]>max){
                max=score[q][w];
                //u=q;
                //v=w;
            }
        }
    }
    return max;
}
int leastscore(void){
    int q=0,w=0;
    //u=q,v=w;
    int min=-1;
    for(q=0;q<SIZE;q++){
        for(w=0;w<SIZE;w++){
            if(nextscore[q][w]>min){
                min=nextscore[q][w];
                //u=q;
                //v=w;
            }
        }
    }
    return min;
}
void getuv(void){
    int i3,j3,i4,j4;
    //long nextscore[SIZE][SIZE];
    int xmax[225];
    int ymax[225];
    int oppminscore[225];
    int minposition=0;
    int depth;
    for(i3=0;i3<SIZE;i3++){
        for(j3=0;j3<SIZE;j3++){
            if(score[i3][j3]==greatscore()){

                arrayForInnerBoardLayout[i3][j3]=turn;//̽ӣһǵûԭ
                //һǵûԭ

                changeturn();
                //ǵûԭ

                //ʼnextscore
                for(i4=0;i4<SIZE;i4++){
                    for(j4=0;j4<SIZE;j4++){
                        if(arrayForInnerBoardLayout[i4][j4]!=0)
                            nextscore[i4][j4]=-1;
                        else
                            nextscore[i4][i4]=countscore(i4,j4);
                    }
                }
                depth=1;
                for(i4=0;i4<SIZE;i4++){
                    for(j4=0;j4<SIZE && depth==1;j4++){
                        if(depth==1 && nextscore[i4][j4]==leastscore()){
                            xmax[minposition]=i3;
                            ymax[minposition]=j3;
                            oppminscore[minposition]=leastscore();
                            minposition++;
                            depth++;
                        }
		    }
                }

                changeturn();
                arrayForInnerBoardLayout[i3][j3]=0;//ԭ
            }
        }
    }
    int justcount;
    long maxmin=20000000000;
    for(justcount=0;justcount<minposition;justcount++){
        if(oppminscore[justcount]<maxmin){
            u=xmax[justcount];
            v=ymax[justcount];
            maxmin=oppminscore[justcount];
        }
    }
}

void changeturn(void){
    if(turn==1)
        turn=2;
    else if(turn==2)
        turn=1;
    //һҪǵ֮ٱ
}

//ְ

/*int myban(int xban,int yban){
    if(arrayForInnerBoardLayout[xban][yban]==0){//ӱ
        //̽ӣǵûָ
        arrayForInnerBoardLayout[xban][yban]=1;
        int ban=0;
        int xnum,ynum,xynum,yxnum;
        xnum=countbanx(xban,yban);
        ynum=countbany(xban,yban);
        xynum=countbanxy(xban,yban);
        yxnum=countbanyx(xban,yban);
        printf("%d %d %d %d\n",xnum,ynum,xynum,yxnum);
        //
        if(xnum>5||ynum>5||xynum>5||yxnum>5){
            ban=1;
            arrayForInnerBoardLayout[xban][yban]=0;
            return ban;
        }
       
        if((xnum==3&&ynum==3)||(xnum==3&&xynum==3)||(xnum==3&&yxnum==3)||(ynum==3&&xynum==3)||(ynum==3&&yxnum==3)||(xynum==3&&yxnum==3)){
            ban=1;
            arrayForInnerBoardLayout[xban][yban]=0;
            return ban;
        }
        if((xnum==4&&ynum==4)||(xnum==4&&xynum==4)||(xnum==4&&yxnum==4)||(ynum==4&&xynum==4)||(ynum==4&&yxnum==4)||(xynum==4&&yxnum==4)){
            ban=1;
            arrayForInnerBoardLayout[xban][yban]=0;
            return ban;
        }
        arrayForInnerBoardLayout[xban][yban]=0;
        return ban;
    }else{
        //printf("ӱ\n");
        return 0;

    }
        
}  */
//
int myban(int xban,int yban){
    if(arrayForInnerBoardLayout[xban][yban]==0){//ӱ
        //̽ӣǵûָ
        arrayForInnerBoardLayout[xban][yban]=1;
        int ban=0;
        int xnum,ynum,xynum,yxnum;
        xnum=countbanx(xban,yban);
        ynum=countbany(xban,yban);
        xynum=countbanxy(xban,yban);
        yxnum=countbanyx(xban,yban);

        //
        if(xnum>5||ynum>5||xynum>5||yxnum>5){
            ban=1;
            arrayForInnerBoardLayout[xban][yban]=0;
            return ban;
        }
        if((xnum==3&&ynum==3)||(xnum==3&&xynum==3)||(xnum==3&&yxnum==3)||(ynum==3&&xynum==3)||(ynum==3&&yxnum==3)||(xynum==3&&yxnum==3)){
            ban=1;
            arrayForInnerBoardLayout[xban][yban]=0;
            return ban;
        }
        if((xnum==4&&ynum==4)||(xnum==4&&xynum==4)||(xnum==4&&yxnum==4)||(ynum==4&&xynum==4)||(ynum==4&&yxnum==4)||(xynum==4&&yxnum==4)){
            ban=1;
            arrayForInnerBoardLayout[xban][yban]=0;
            return ban;
        }
        arrayForInnerBoardLayout[xban][yban]=0;
        return ban;
    }else{
        //printf("ӱ\n");
        return 0;

    }
        
}

int oppban(int xban,int yban){
    
        int ban=0;
        int xnum,ynum,xynum,yxnum;
        xnum=countbanx(xban,yban);
        ynum=countbany(xban,yban);
        xynum=countbanxy(xban,yban);
        yxnum=countbanyx(xban,yban);
        printf("%d %d %d %d\n",xnum,ynum,xynum,yxnum);
        //
        if(xnum>5||ynum>5||xynum>5||yxnum>5){
            ban=1;
            
            return ban;
        }
        if((xnum==3&&ynum==3)||(xnum==3&&xynum==3)||(xnum==3&&yxnum==3)||(ynum==3&&xynum==3)||(ynum==3&&yxnum==3)||(xynum==3&&yxnum==3)){
            ban=1;
            
            return ban;
        }
        if((xnum==4&&ynum==4)||(xnum==4&&xynum==4)||(xnum==4&&yxnum==4)||(ynum==4&&xynum==4)||(ynum==4&&yxnum==4)||(xynum==4&&yxnum==4)){
            ban=1;
           
            return ban;
        }
        
        return ban;
}

/*int countbanx(int xban,int yban){
    int Left_y,Right_y;
	int Left_Info,Right_Info;
	int x_count=1;
    int p=1;
                                                                   
	for(Left_y=yban-1;arrayForInnerBoardLayout[xban][Left_y]==p&&Left_y>=0;Left_y--)       //
	  x_count++;
	for(Right_y=y+1;arrayForInnerBoardLayout[xban][Right_y]==p&&Right_y<SIZE;Right_y++)
	  x_count++;
	
	if(Left_y>=0)    //̫ڲ߽ʱµıԵֵ
	  Left_Info=arrayForInnerBoardLayout[xban][Left_y];
	if(Right_y<SIZE)
	  Right_Info=arrayForInnerBoardLayout[xban][Right_y];
	
	//
	if(Left_y>=0&&Right_y<SIZE&&Left_Info==0&&Right_Info==0){
	
		//
		if(x_count==1&&((Left_y>=3&&arrayForInnerBoardLayout[xban][Left_y-1]==p&&arrayForInnerBoardLayout[xban][Left_y-2]==p&&arrayForInnerBoardLayout[xban][Left_y-3]==0)||(Right_y<=12&&arrayForInnerBoardLayout[xban][Right_y+1]==p&&arrayForInnerBoardLayout[xban][Right_y+2]==p&&arrayForInnerBoardLayout[xban][Right_y+3]==0)))
		  x_count+=2;
		
		//
		if(x_count==2&&((Left_y>=2&&arrayForInnerBoardLayout[xban][Left_y-1]==p&&arrayForInnerBoardLayout[xban][Left_y-2]==0)||(Right_y<=13&&arrayForInnerBoardLayout[xban][Right_y+1]==p&&arrayForInnerBoardLayout[xban][Right_y+2]==0)))
		  x_count++;
		
		if(x_count==3)          //3
		  return 3;
		if(x_count==4)          //4
		  return 4;
	}
	
	//
	else if(((Left_y<0||(Left_y>=0&&Left_Info!=0))&&Right_y<SIZE&&Right_Info==0)||(Left_y>=0&&Left_Info==0&&(Right_y==SIZE||(Right_y<SIZE&&Right_Info!=0)))){
		if(x_count==4)
		  return 4;        //
	}
	
	else
	  return 0;
}
int countbany(int xban,int yban){
    int Up_x,Down_x;
	int Up_Info,Down_Info;
	int y_count=1;
	
	for(Up_x=xban-1;arrayForInnerBoardLayout[Up_x][yban]==1&&Up_x>=0;Up_x--)        //
	  y_count++;
	for(Down_x=xban+1;arrayForInnerBoardLayout[Down_x][yban]==1&&Down_x<SIZE;Down_x++)
	  y_count++;
	
	if(Up_x>=0)
	  Up_Info=arrayForInnerBoardLayout[Up_x][yban];
	if(Down_x<SIZE)
	  Down_Info=arrayForInnerBoardLayout[Down_x][yban];
	
	//
	if(Up_x>=0&&Down_x<SIZE&&Up_Info==0&&Down_Info==0){
	
		if(y_count==1&&((Up_x>=3&&arrayForInnerBoardLayout[Up_x-1][yban]==1&&arrayForInnerBoardLayout[Up_x-2][yban]==1&&arrayForInnerBoardLayout[Up_x-3][yban]==0)||(Down_x<=12&&arrayForInnerBoardLayout[Down_x+1][yban]==1&&arrayForInnerBoardLayout[Down_x+2][yban]==1&&arrayForInnerBoardLayout[Down_x+3][yban]==0)))
			  y_count+=2;      //
		
		if(y_count==2&&((Up_x>=2&&arrayForInnerBoardLayout[Up_x-1][yban]==1&&arrayForInnerBoardLayout[Up_x-2][yban]==0)||(Down_x<=13&&arrayForInnerBoardLayout[Down_x+1][yban]==1&&arrayForInnerBoardLayout[Down_x+2][yban]==0)))
			  y_count++;       //
		
		if(y_count==3)
		  return 3;          //3
		if(y_count==4)
		  return 4;         //4
		}
	
	//
	else if(((Up_x<0||(Up_x>=0&&Up_Info!=0))&&Down_x<SIZE&&Down_Info==0)||(Up_x>=0&&Up_Info==0&&(Down_x==SIZE||(Down_x<SIZE&&Down_Info!=0)))){
		if(y_count==4)
		  return 4;     //4
		}
	
	else
	  return 0;

}
int countbanxy(int xban,int yban){
    int Left_x,Left_y;
	int Right_x,Right_y;
	int LtoR_count=1;
	int LeftUp_Info,RightDown_Info;
	
	for(Left_x=xban-1,Left_y=yban-1;Left_x>=0&&Left_y>=0&&arrayForInnerBoardLayout[Left_x][Left_y]==1;Left_x--,Left_y--)    //
	  LtoR_count++;
	for(Right_x=xban+1,Right_y=yban+1;Right_x<SIZE&&Right_y<SIZE&&arrayForInnerBoardLayout[Right_x][Right_y]==1;Right_x++,Right_y++)
	  LtoR_count++;
	
	if(Left_x>=0&&Left_y>=0)
	  LeftUp_Info=arrayForInnerBoardLayout[Left_x][Left_y];
	if(Right_x<SIZE&&Right_y<SIZE)
	  RightDown_Info=arrayForInnerBoardLayout[Right_x][Right_y];
	
	if(Left_x>=0&&Left_y>=0&&Right_x<SIZE&&Right_y<SIZE&&LeftUp_Info==0&&RightDown_Info==0){     //
			
		if(LtoR_count==1&&((Left_x>=3&&Left_y>=3&&arrayForInnerBoardLayout[Left_x-1][Left_y-1]==1&&arrayForInnerBoardLayout[Left_x-2][Left_y-2]==1&&arrayForInnerBoardLayout[Left_x-3][Left_y-3]==0)||(Right_x<=12&&Right_y<=12&&arrayForInnerBoardLayout[Right_x+1][Right_y+1]==1&&arrayForInnerBoardLayout[Right_x+2][Right_y+2]==1&&arrayForInnerBoardLayout[Right_x+3][Right_y+3]==0)))
		  LtoR_count+=2;       //
		
		if(LtoR_count==2&&((Left_x>=2&&Left_y>=2&&arrayForInnerBoardLayout[Left_x-1][Left_y-1]==1&&arrayForInnerBoardLayout[Left_x-2][Left_y-2]==0)||(Right_x<=13&&Right_y<=13&&arrayForInnerBoardLayout[Right_x+1][Right_y+1]==1&&arrayForInnerBoardLayout[Right_x+2][Right_y+2]==0)))
		  LtoR_count++;         //
		
		if(LtoR_count==3)
		  return 3;           //3
		if(LtoR_count==4)
		  return 4;            //4
		}

	else if(((Left_x<0||Left_y<0||(Left_x>=0&&Left_y>=0&&LeftUp_Info!=0))&&(Right_x<SIZE&&Right_y<SIZE&&RightDown_Info==0))||((Left_x>=0&&Left_y>=0&&LeftUp_Info==0)&&(Right_x==SIZE||Right_y==SIZE||(Right_x<SIZE&&Right_y<SIZE&&RightDown_Info!=0)))){
			if(LtoR_count==4)                        //
			  return 4;     //4
		}
	
	else
	  return 0;
}
int countbanyx(int xban,int yban){
    int Left_x,Left_y;
	int Right_x,Right_y;
	int RtoL_count=1;
	int LeftDown_Info,RightUp_Info;
	
	for(Left_x=xban+1,Left_y=y-1;Left_x<SIZE&&Left_y>=0&&arrayForInnerBoardLayout[Left_x][Left_y]==1;Left_x++,Left_y--)     //
	  RtoL_count++;
	for(Right_x=xban-1,Right_y=yban+1;Right_x>=0&&Right_y<SIZE&&arrayForInnerBoardLayout[Right_x][Right_y]==1;Right_x--,Right_y++)
	  RtoL_count++;
		
	if(Left_x<SIZE&&Left_y>=0)
	  LeftDown_Info=arrayForInnerBoardLayout[Left_x][Left_y];
	if(Right_x>=0&&Right_y<SIZE)
	  RightUp_Info=arrayForInnerBoardLayout[Right_x][Right_y];
	
	if(Left_x<SIZE&&Left_y>=0&&Right_x>=0&&Right_y<SIZE&&LeftDown_Info==0&&RightUp_Info==0){        //
			
		if(RtoL_count==1&&((Left_x<=12&&Left_y>=3&&arrayForInnerBoardLayout[Left_x+1][Left_y-1]==1&&arrayForInnerBoardLayout[Left_x+2][Left_y-2]==1&&arrayForInnerBoardLayout[Left_x+3][Left_y-3]==0)||(Right_x>=3&&Right_y<=12&&arrayForInnerBoardLayout[Right_x-1][Right_y+1]==1&&arrayForInnerBoardLayout[Right_x-2][Right_y+2]==1&&arrayForInnerBoardLayout[Right_x-3][Right_y+3]==0)))
		  RtoL_count+=2;                 //
			
		if(RtoL_count==2&&((Left_x<=13&&Left_y>=2&&arrayForInnerBoardLayout[Left_x+1][Left_y-1]==1&&arrayForInnerBoardLayout[Left_x+2][Left_y-2]==0)||(Right_x>=2&&Right_y<=13&&arrayForInnerBoardLayout[Right_x-1][Right_y+1]==1&&arrayForInnerBoardLayout[Right_x-2][Right_y+2]==0)))
		  RtoL_count++;             //
		
		if(RtoL_count==3)
		  return 3;               //3
		if(RtoL_count==4)
		  return 4;         //4
		}

	else if(((Left_x==SIZE||Left_y<0||(Left_x<SIZE&&Left_y>=0&&LeftDown_Info!=0))&&(Right_x>=0&&Right_y<SIZE&&RightUp_Info==0))||((Left_x<SIZE&&Left_y>=0&&LeftDown_Info==0)&&(Right_x<0||Right_y==SIZE||(Right_x>=0&&Right_y<SIZE&&RightUp_Info!=0)))){
			if(RtoL_count==4)                //4
			  return 4;
		}
	
	else
	  return 0;
}*/
int countbanx(int xban,int yban){
    int lefty,righty;
    int leftinfo,rightinfo;
    int x_count=1;
    for(lefty=yban-1;arrayForInnerBoardLayout[xban][lefty]==1&&lefty>=0;lefty--)
        x_count++;//
    for(righty=yban+1;arrayForInnerBoardLayout[xban][righty]==1&&righty<SIZE;righty++)
        x_count++;
    if(lefty>=0)
        leftinfo=arrayForInnerBoardLayout[xban][lefty];
    if(righty<SIZE)
        rightinfo=arrayForInnerBoardLayout[xban][righty];

    //жϻ
    if(lefty>=0&&righty<SIZE&&leftinfo==0&&rightinfo==0){
        //
		if(x_count==1&&((lefty>=3&&arrayForInnerBoardLayout[xban][lefty-1]==1&&arrayForInnerBoardLayout[xban][lefty-2]==1&&arrayForInnerBoardLayout[xban][lefty-3]==0)||(righty<=12&&arrayForInnerBoardLayout[xban][righty+1]==1&&arrayForInnerBoardLayout[xban][righty+2]==1&&arrayForInnerBoardLayout[xban][righty+3]==0)))
		  x_count+=2;
		
		//
		if(x_count==2&&((lefty>=2&&arrayForInnerBoardLayout[xban][lefty-1]==1&&arrayForInnerBoardLayout[xban][lefty-2]==0)||(righty<=13&&arrayForInnerBoardLayout[xban][righty+1]==1&&arrayForInnerBoardLayout[xban][righty+2]==0)))
		  x_count++;
		
		if(x_count==3)          //3
		  return 3;
		if(x_count==4)          //4
		  return 4;
    }
    //
	else if(((lefty<0||(lefty>=0&&leftinfo!=0))&&righty<SIZE&&rightinfo==0)||(lefty>=0&&leftinfo==0&&(righty==SIZE||(righty<SIZE&&rightinfo!=0)))){
		if(x_count==4)
		  return 4;        //
	}
	
	else
	  return 0;

}
int countbany(int xban,int yban){
    int Up_x,Down_x;
	int Up_Info,Down_Info;
	int y_count=1;
	
	for(Up_x=xban-1;arrayForInnerBoardLayout[Up_x][yban]==1&&Up_x>=0;Up_x--)        //
	  y_count++;
	for(Down_x=xban+1;arrayForInnerBoardLayout[Down_x][yban]==1&&Down_x<SIZE;Down_x++)
	  y_count++;
	
	if(Up_x>=0)
	  Up_Info=arrayForInnerBoardLayout[Up_x][yban];
	if(Down_x<SIZE)
	  Down_Info=arrayForInnerBoardLayout[Down_x][yban];
	
	//
	if(Up_x>=0&&Down_x<SIZE&&Up_Info==0&&Down_Info==0){
	
		if(y_count==1&&((Up_x>=3&&arrayForInnerBoardLayout[Up_x-1][yban]==1&&arrayForInnerBoardLayout[Up_x-2][yban]==1&&arrayForInnerBoardLayout[Up_x-3][yban]==0)||(Down_x<=12&&arrayForInnerBoardLayout[Down_x+1][yban]==1&&arrayForInnerBoardLayout[Down_x+2][yban]==1&&arrayForInnerBoardLayout[Down_x+3][yban]==0)))
			  y_count+=2;      //
		
		if(y_count==2&&((Up_x>=2&&arrayForInnerBoardLayout[Up_x-1][yban]==1&&arrayForInnerBoardLayout[Up_x-2][yban]==0)||(Down_x<=13&&arrayForInnerBoardLayout[Down_x+1][yban]==1&&arrayForInnerBoardLayout[Down_x+2][yban]==0)))
			  y_count++;       //
		
		if(y_count==3)
		  return 3;          //3
		if(y_count==4)
		  return 4;         //4
		}
	
	//
	else if(((Up_x<0||(Up_x>=0&&Up_Info!=0))&&Down_x<SIZE&&Down_Info==0)||(Up_x>=0&&Up_Info==0&&(Down_x==SIZE||(Down_x<SIZE&&Down_Info!=0)))){
		if(y_count==4)
		  return 4;     //4
		}
	
	else
	  return 0;

}
int countbanxy(int xban,int yban){
    int Left_x,Left_y;
	int Right_x,Right_y;
	int LtoR_count=1;
	int LeftUp_Info,RightDown_Info;
	
	for(Left_x=xban-1,Left_y=yban-1;Left_x>=0&&Left_y>=0&&arrayForInnerBoardLayout[Left_x][Left_y]==1;Left_x--,Left_y--)    //
	  LtoR_count++;
	for(Right_x=xban+1,Right_y=yban+1;Right_x<SIZE&&Right_y<SIZE&&arrayForInnerBoardLayout[Right_x][Right_y]==1;Right_x++,Right_y++)
	  LtoR_count++;
	
	if(Left_x>=0&&Left_y>=0)
	  LeftUp_Info=arrayForInnerBoardLayout[Left_x][Left_y];
	if(Right_x<SIZE&&Right_y<SIZE)
	  RightDown_Info=arrayForInnerBoardLayout[Right_x][Right_y];
	
	if(Left_x>=0&&Left_y>=0&&Right_x<SIZE&&Right_y<SIZE&&LeftUp_Info==0&&RightDown_Info==0){     //
			
		if(LtoR_count==1&&((Left_x>=3&&Left_y>=3&&arrayForInnerBoardLayout[Left_x-1][Left_y-1]==1&&arrayForInnerBoardLayout[Left_x-2][Left_y-2]==1&&arrayForInnerBoardLayout[Left_x-3][Left_y-3]==0)||(Right_x<=12&&Right_y<=12&&arrayForInnerBoardLayout[Right_x+1][Right_y+1]==1&&arrayForInnerBoardLayout[Right_x+2][Right_y+2]==1&&arrayForInnerBoardLayout[Right_x+3][Right_y+3]==0)))
		  LtoR_count+=2;       //
		
		if(LtoR_count==2&&((Left_x>=2&&Left_y>=2&&arrayForInnerBoardLayout[Left_x-1][Left_y-1]==1&&arrayForInnerBoardLayout[Left_x-2][Left_y-2]==0)||(Right_x<=13&&Right_y<=13&&arrayForInnerBoardLayout[Right_x+1][Right_y+1]==1&&arrayForInnerBoardLayout[Right_x+2][Right_y+2]==0)))
		  LtoR_count++;         //
		
		if(LtoR_count==3)
		  return 3;           //3
		if(LtoR_count==4)
		  return 4;            //4
		}

	else if(((Left_x<0||Left_y<0||(Left_x>=0&&Left_y>=0&&LeftUp_Info!=0))&&(Right_x<SIZE&&Right_y<SIZE&&RightDown_Info==0))||((Left_x>=0&&Left_y>=0&&LeftUp_Info==0)&&(Right_x==SIZE||Right_y==SIZE||(Right_x<SIZE&&Right_y<SIZE&&RightDown_Info!=0)))){
			if(LtoR_count==4)                        //
			  return 4;     //4
		}
	
	else
	  return 0;
}
int countbanyx(int xban,int yban){
    int Left_x,Left_y;
	int Right_x,Right_y;
	int RtoL_count=1;
	int LeftDown_Info,RightUp_Info;
	
	for(Left_x=xban+1,Left_y=y-1;Left_x<SIZE&&Left_y>=0&&arrayForInnerBoardLayout[Left_x][Left_y]==1;Left_x++,Left_y--)     //
	  RtoL_count++;
	for(Right_x=xban-1,Right_y=yban+1;Right_x>=0&&Right_y<SIZE&&arrayForInnerBoardLayout[Right_x][Right_y]==1;Right_x--,Right_y++)
	  RtoL_count++;
		
	if(Left_x<SIZE&&Left_y>=0)
	  LeftDown_Info=arrayForInnerBoardLayout[Left_x][Left_y];
	if(Right_x>=0&&Right_y<SIZE)
	  RightUp_Info=arrayForInnerBoardLayout[Right_x][Right_y];
	
	if(Left_x<SIZE&&Left_y>=0&&Right_x>=0&&Right_y<SIZE&&LeftDown_Info==0&&RightUp_Info==0){        //
			
		if(RtoL_count==1&&((Left_x<=12&&Left_y>=3&&arrayForInnerBoardLayout[Left_x+1][Left_y-1]==1&&arrayForInnerBoardLayout[Left_x+2][Left_y-2]==1&&arrayForInnerBoardLayout[Left_x+3][Left_y-3]==0)||(Right_x>=3&&Right_y<=12&&arrayForInnerBoardLayout[Right_x-1][Right_y+1]==1&&arrayForInnerBoardLayout[Right_x-2][Right_y+2]==1&&arrayForInnerBoardLayout[Right_x-3][Right_y+3]==0)))
		  RtoL_count+=2;                 //
			
		if(RtoL_count==2&&((Left_x<=13&&Left_y>=2&&arrayForInnerBoardLayout[Left_x+1][Left_y-1]==1&&arrayForInnerBoardLayout[Left_x+2][Left_y-2]==0)||(Right_x>=2&&Right_y<=13&&arrayForInnerBoardLayout[Right_x-1][Right_y+1]==1&&arrayForInnerBoardLayout[Right_x-2][Right_y+2]==0)))
		  RtoL_count++;             //
		
		if(RtoL_count==3)
		  return 3;               //3
		if(RtoL_count==4)
		  return 4;         //4
		}

	else if(((Left_x==SIZE||Left_y<0||(Left_x<SIZE&&Left_y>=0&&LeftDown_Info!=0))&&(Right_x>=0&&Right_y<SIZE&&RightUp_Info==0))||((Left_x<SIZE&&Left_y>=0&&LeftDown_Info==0)&&(Right_x<0||Right_y==SIZE||(Right_x>=0&&Right_y<SIZE&&RightUp_Info!=0)))){
			if(RtoL_count==4)                //4
			  return 4;
		}
	
	else
	  return 0;
}
