#define BLACK_AI #define WHITE_AI #include<stdio.h> #include<windows.h> #include<time.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) void getxy(int &x,int& y,int brd[]){ gotoxy(30,20);printf(" "); gotoxy(30,20); 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,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;hehe=0; return r; } unsigned long is_3_(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>1&&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>1&&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>1&&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>1&&free) ++r; 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)) * 0x04000000); *val +=((is_4_(bestx,besty,OP(who),brd))*0x00400000); 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+=0x00800000; 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()%6)) best=iter; } } x=(best-ar)%19;y=(best-ar)/19; 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; #ifdef WHITE_AI Sleep(200); runai(x,y,WhitE,brd); #else getxy(x,y,brd); #endif #endif drawo(x,y);brd[A(x,y)]=WhitE; while(1){ #ifdef BLACK_AI Sleep(400); 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; } #ifdef WHITE_AI Sleep(400); 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; } } Sleep(1000);goto bg; }
原文:http://www.cnblogs.com/TheName/p/5326071.html