gom


#define BLACK_AI
#define WHITE_AI
#ifdef BLACK_AI
#ifdef WHITE_AI
#define WAIT
#endif
#endif
#include<stdio.h>
#include<windows.h>
#include<time.h>
#include<conio.h>

#define gotoxy(x,y) SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),(COORD){2*(x),(y)})
#define drawo(x,y) gotoxy((x),(y));printf("〇")
#define drawx(x,y) gotoxy((x),(y));printf("×")

#define A(x,y) ((x)+(y)*19)

#define BlacK (1)
#define NonE (0)
#define WhitE (2)

#define OP(who) ((who)==1?2:1)

int s=0;

void getxy(int &x,int &y,int brd[]){
	gotoxy(30,22);printf("                 ");
    do{gotoxy(30,22);scanf("%d",&x);scanf("%d",&y);}
	while(y>18||x>18||brd[A(x,y)]);
}

bool win(int &x,int &y,int who,int brd[]){
	int a=x,b=y,hehe=0;
    while(a&&brd[b*19+(--a)]==who) ++hehe;a=x;
    while(a!=18&&brd[b*19+(++a)]==who) ++hehe;a=x;
    if(hehe>3) return true;hehe=0;
    while(b&&brd[(--b)*19+a]==who) ++hehe;b=y;
    while(b!=18&&brd[(++b)*19+a]==who) ++hehe;b=y;
    if(hehe>3) return true;hehe=0;
    while(a&&b&&brd[(--b)*19+(--a)]==who) ++hehe;a=x;b=y;
    while(a!=18&&b!=18&&brd[(++b)*19+(++a)]==who) ++hehe;a=x;b=y;
	if(hehe>3) return true;hehe=0;	
	while(a!=18&&b&&brd[(--b)*19+(++a)]==who) ++hehe;a=x;b=y;
    while(a&&b!=18&&brd[(++b)*19+(--a)]==who) ++hehe;a=x;b=y;
	if(hehe>3) return true;hehe=0;
	return false;
}

unsigned long is_4_(int &x,int &y,int who,int brd[]){
	int a=x,b=y,hehe=0;unsigned long r=0;bool free;
	
    while(a&&brd[b*19+(--a)]==who) ++hehe;
	free=(a&&!brd[b*19+a]);a=x;
    while(a!=18&&brd[b*19+(++a)]==who) ++hehe;
	free&=(a!=18&&!brd[b*19+a]);a=x;
    if(hehe>2&&free) ++r;hehe=0;
    
    while(b&&brd[(--b)*19+a]==who) ++hehe;
	free=(b&&!brd[b*19+a]);b=y;
    while(b!=18&&brd[(++b)*19+a]==who) ++hehe;
	free&=(b!=18&&!brd[b*19+a]);b=y;
    if(hehe>2&&free) ++r;hehe=0;
    
    while(a&&b&&brd[(--b)*19+(--a)]==who) ++hehe;
	free=(a&&b&&!brd[b*19+a]);a=x;b=y;
    while(a!=18&&b!=18&&brd[(++b)*19+(++a)]==who) ++hehe;
	free&=(a!=18&&b!=18&&!brd[b*19+a]);a=x;b=y;
	if(hehe>2&&free) ++r;hehe=0;
	
	while(a!=18&&b&&brd[(--b)*19+(++a)]==who) ++hehe;
	free=(a!=18&&b&&!brd[b*19+a]);a=x;b=y;
    while(a&&b!=18&&brd[(++b)*19+(--a)]==who) ++hehe;
	free&=(a&&b!=18&&!brd[b*19+a]);a=x;b=y;
	if(hehe>2&&free) ++r;
	
	return r;
}

unsigned long is_4(int &x,int &y,int who,int brd[]){
	int a=x,b=y,c,d,hehe=0;unsigned long r=0;
	brd[a+b*19]=who;
	
	for(d=6;--d;){
		for(c=6;--c;){
		    if(a>=0&&brd[A(a,b)]==who) ++hehe;
	        if(brd[A(a,b)]==OP(who)) hehe=-15;
	        --a;
	    }
	    if(hehe==4) ++r;a+=5;hehe=0;
		if((++a)==18) break;
	}
	a=x;b=y;
	for(d=6;--d;){
		for(c=6;--c;){
		    if(b>=0&&brd[A(a,b)]==who) ++hehe;
	        if(brd[A(a,b)]==OP(who)) hehe=-15;
	        --b;
	    }
	    if(hehe==4) ++r;b+=5;hehe=0;
		if(++b==18) break;
	}
	a=x;b=y;
	for(d=6;--d;){
		for(c=6;--c;){
		    if(a>=0&&b>=0&&brd[A(a,b)]==who) ++hehe;
	        if(brd[A(a,b)]==OP(who)) hehe=-15;
	        --a;--b;
	    }
	    if(hehe==4) ++r;a+=5;b+=5;hehe=0;
		if(++a==18) if(++b==18) break;
	}
	a=x;b=y;
	for(d=6;--d;){
		for(c=6;--c;){
		    if(brd[A(a,b)]==who&&a<=18&&b>=0) ++hehe;
	        if(brd[A(a,b)]==OP(who)) hehe=-15;
	        ++a;--b;
	    }
	    if(hehe==4) ++r;a-=5;b+=5;hehe=0;
		if(--a==0) if(++b==18) break;
	}
	brd[x+y*19]=0;
	if(r){
		gotoxy(30,0);printf("%d   ",s);
	}
	return r;
}

unsigned long is_3_(int &x,int &y,int who,int brd[]){
	int a=x,b=y,c,d,hehe=0;unsigned long r=0;
	brd[a+b*19]=who;
	
	for(d=5;--d;){
		for(c=5;--c;){
		    if(a>0&&brd[A(a,b)]==who) ++hehe;
	        if(brd[A(a,b)]==OP(who)) hehe=-15;
	        --a;
	    }
	    if(hehe==3&&!brd[A(a,b)]&&!brd[A(a+5,b)]) ++r;a+=4;hehe=0;
		if(++a==18) break;
	}
	a=x;b=y;
	for(d=5;--d;){
		for(c=5;--c;){
		    if(b>=0&&brd[A(a,b)]==who) ++hehe;
	        if(brd[A(a,b)]==OP(who)) hehe=-15;
	        --b;
	    }
	    if(hehe==3&&!brd[A(a,b)]&&!brd[A(a,b+5)]) ++r;b+=5;hehe=0;
		if(++b==18) break;
	}
	a=x;b=y;
	for(d=5;--d;){
		for(c=5;--c;){
		    if(a>=0&&b>=0&&brd[A(a,b)]==who) ++hehe;
	        if(brd[A(a,b)]==OP(who)) hehe=-15;
	        --a;--b;
	    }
	    if(hehe==3&&!brd[A(a,b)]&&!brd[A(a+5,b+5)]) ++r;a+=4;b+=4;hehe=0;
		if(++a==18) if(++b==18) break;
	}
	a=x;b=y;
	for(d=5;--d;){
		for(c=5;--c;){
		    if(brd[A(a,b)]==who&&a<=18&&b>=0) ++hehe;
	        if(brd[A(a,b)]==OP(who)) hehe=-15;
	        ++a;--b;
	    }
	    if(hehe==3&&!brd[A(a,b)]&&!brd[A(a-5,b+5)]) ++r;a-=4;b+=4;hehe=0;
		if(--a==0) if(++b==18) break;
	}
	brd[x+y*19]=0;
	return r;
}

unsigned runai(int &x,int &y,int who,int brd[]){
	int bestx,besty;
	unsigned long ar[361],*val=ar,*iter=ar,*best=ar,t[4];
	for(besty=-1;++besty!=19;){
	for(bestx=-1;++bestx!=19;++val){
		*val=0;
	    if(brd[A(bestx,besty)]) continue;//covered
		if(win(bestx,besty,who,brd))       *val+=0xc0000000;
		if(win(bestx,besty,OP(who),brd))   *val+=0x40000000;
		*val +=((is_4_(bestx,besty,who,brd)) *   0x08000000);
		*val +=((is_4_(bestx,besty,OP(who),brd))*0x00f00000);
		t[0]=is_4(bestx,besty,who,brd);
		t[1]=is_4(bestx,besty,OP(who),brd);
		t[2]=is_3_(bestx,besty,who,brd);
		t[3]=is_3_(bestx,besty,OP(who),brd);
		*val+=(t[0]*0x00040000);
		*val+=(t[1]*0x00004000);
		*val+=(t[2]*0x00004000);
		*val+=(t[3]*0x00000400);
		if(t[0]&&(t[0]+t[2]>1)) *val+=0x01000000;
		if(t[2]>1)     *val+=0x00200000;
		if(t[1]&&(t[1]+t[3]>1)) *val+=0x00300000;
		if(t[3]>1)     *val+=0x00100000;
		if(x-3<bestx&&bestx<x+3&&y-3<besty&&besty<y+3) ++*val;
		++(*val);
	}
	}
	while(++iter!=val){
		if(*best<*iter) best=iter;
		else if(*best==*iter){
			if(!(rand()%12)) best=iter;
		}
	}
	x=(best-ar)%19;y=(best-ar)/19;
	#ifdef WAIT
	Sleep(468);
	#endif
	return 0;
}

int main(){
	srand(time(NULL));
	bg:
	int x,y,brd[361]={},*iter;
	gotoxy(0,0);
    for(x=0;x!=19;++x) {
        printf("                                      ");
        printf("%d\n",x%10);
    }
    printf(" 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8");
    
    #ifdef BLACK_AI
    x=y=9;
    drawx(9,9);brd[A(9,9)]=BlacK;++s;
    #ifdef WHITE_AI
	//Sleep(200);
	runai(x,y,WhitE,brd);
	#else
	getxy(x,y,brd);
	#endif
	drawo(x,y);brd[A(x,y)]=WhitE;
	#endif
    while(1){
    	#ifdef BLACK_AI
    	runai(x,y,BlacK,brd);
    	#else
        getxy(x,y,brd);
        #endif
        drawx(x,y);brd[A(x,y)]=BlacK;
        if(win(x,y,BlacK,brd)){
        	MessageBox(NULL,"Black win","Win!",0);
        	break;
		}
		if(++s==181){
			MessageBox(NULL,"full","full!",0);
        	break;
		}
		#ifdef WHITE_AI
		runai(x,y,WhitE,brd);
		#else
		getxy(x,y,brd);
		#endif
		drawo(x,y);brd[A(x,y)]=WhitE;
		if(win(x,y,WhitE,brd)){
        	MessageBox(NULL,"White win","Lose!",0);
        	break;
		}
    }
    goto bg;
}

  

 

 

posted @ 2016-03-27 16:44  141421356  阅读(273)  评论(0)    收藏  举报