首页 > 代码库 > TEN源代码

TEN源代码

高级版

井字棋。

技术分享
  1 // It is made by XZZ
  2 #include<cstdio>
  3 #include<algorithm>
  4 #include<cstdlib>
  5 #ifdef WIN32
  6 #include<conio.h>
  7 #else
  8 #include<curses.h>
  9 #endif
 10 using namespace std;
 11 #define rep(a,b,c) for(int a=b;a<=c;a++)
 12 #define drep(a,b,c) for(int a=b;a>=c;a--)
 13 #define il inline
 14 #define rg register
 15 #define vd void
 16 typedef long long ll;
 17 int s[10][10],x,y;
 18 const int n=9;
 19 const int To[10][10]={
 20     {0,0,0,0,0,0,0,0,0,0},
 21     {0,1,2,3,1,2,3,1,2,3},
 22     {0,4,5,6,4,5,6,4,5,6},
 23     {0,7,8,9,7,8,9,7,8,9},
 24     {0,1,2,3,1,2,3,1,2,3},
 25     {0,4,5,6,4,5,6,4,5,6},
 26     {0,7,8,9,7,8,9,7,8,9},
 27     {0,1,2,3,1,2,3,1,2,3},
 28     {0,4,5,6,4,5,6,4,5,6},
 29     {0,7,8,9,7,8,9,7,8,9}};
 30 const int Fr[10][10]={
 31     {0,0,0,0,0,0,0,0,0,0},
 32     {0,1,1,1,2,2,2,3,3,3},
 33     {0,1,1,1,2,2,2,3,3,3},
 34     {0,1,1,1,2,2,2,3,3,3},
 35     {0,4,4,4,5,5,5,6,6,6},
 36     {0,4,4,4,5,5,5,6,6,6},
 37     {0,4,4,4,5,5,5,6,6,6},
 38     {0,7,7,7,8,8,8,9,9,9},
 39     {0,7,7,7,8,8,8,9,9,9},
 40     {0,7,7,7,8,8,8,9,9,9}};
 41 const int Xmax[10]={9,3,3,3,6,6,6,9,9,9},Xmin[10]={0,1,1,1,4,4,4,7,7,7};
 42 const int Ymax[10]={9,3,6,9,3,6,9,3,6,9},Ymin[10]={0,1,4,7,1,4,7,1,4,7};
 43 const int Xbeg[10]={5,2,2,2,5,5,5,8,8,8},Ybeg[10]={5,2,5,8,2,5,8,2,5,8};
 44 int sum[10];
 45 il vd out(int now){
 46     system("cls");
 47     rep(i,1,n){
 48         if(i%3==1)printf("----------------------------------\n");
 49         rep(j,1,n){
 50             if(j%3==1)printf("| ");
 51                 if(i==x&&j==y)putchar([);
 52             else putchar( );
 53             if(s[i][j]==0)printf(" ");
 54                 else if(s[i][j]==1)printf("O");
 55                 else printf("X");
 56                 if(i==x&&j==y)putchar(]);
 57             else putchar( );
 58         }
 59         printf("|\n");
 60     }
 61     printf("----------------------------------\n");
 62     if(now==1)printf("O");
 63     else printf("X");
 64     int sc[3]={0};
 65     for(int i=1;i<=9;i++){
 66         #define d(A,B,C,D,E,F) a=A+Xmin[i]-1,c=C+Xmin[i]-1,e=E+Xmin[i]-1,b=B+Ymin[i]-1,d=D+Ymin[i]-1,f=F+Ymin[i]-1;if(s[a][b]==s[c][d]&&s[c][d]==s[e][f]&&s[e][f])++sc[s[a][b]];
 67         int a,b,c,d,e,f;
 68         d(1,1,1,2,1,3);
 69         d(2,1,2,2,2,3);
 70         d(3,1,3,2,3,3);
 71         d(1,1,2,1,3,1);
 72         d(1,2,2,2,3,2);
 73         d(1,3,2,3,3,3);
 74         d(1,1,2,2,3,3);
 75         d(1,3,2,2,3,1);
 76     }
 77     printf(" time\n\nO :%d  X: %d\n",sc[1],sc[2]);
 78 }
 79 int main(){
 80     int a=1,b=2,last=5;
 81     while(1){
 82         if(sum[last]==9)last=0;
 83         x=Xbeg[last],y=Ybeg[last];
 84         char ch=0;
 85         while(ch!= ||s[x][y]){
 86             out(a);
 87             #ifndef WIN32
 88             initscr();
 89             #endif
 90             ch=getch();
 91             #ifndef WIN32
 92             endwin();
 93             #endif
 94             if(ch==a)y=max(Ymin[last],y-1);
 95             else if(ch==s)x=min(Xmax[last],x+1);
 96             else if(ch==d)y=min(Ymax[last],y+1);
 97             else if(ch==w)x=max(Xmin[last],x-1);
 98         }
 99         s[x][y]=a,last=To[x][y],++sum[Fr[x][y]];
100         swap(a,b);
101     }
102     return 0;
103 }
点击搞事情

 

TEN源代码