第一次写,写的有点复杂,大家可以帮忙找找问题,简化一些:
代码:
1 :#include2 #include 3 #include 4 #include 5 #define MAXN 15 6 int chess[MAXN][MAXN]; 7 char map[MAXN*2+1][MAXN*4+3]; 8 int player,Cx,Cy; 9 int Mx,My; 10 char *showtext; 11 int count; 12 int select,b=0; 13 int order1,order2; 14 void copy(char *x,char *y){ 15 while(*y!='\0')*x++=*y++; 16 } 17 void initial(){ 18 showtext=""; 19 count=0; 20 memset(chess,0,sizeof(chess)); 21 Cx=Cy=MAXN/2; 22 player=1; 23 } 24 char *getmap(int x,int y){ 25 if(chess[x][y]==1)return "◆"; 26 else if(chess[x][y]==2)return "○"; 27 else if(x==0&&y==0)return "┏"; 28 else if(x==MAXN-1&&y==0)return "┓"; 29 else if(x==0&&y==MAXN-1)return "┗"; 30 else if(x==MAXN-1&&y==MAXN-1)return "┛"; 31 else if(x==0)return "┣"; 32 else if(x==MAXN-1)return "┫"; 33 else if(y==0)return "┯"; 34 else if(y==MAXN-1)return "┷"; 35 else return "┼"; 36 } 37 char *getlist(int x,int y){ 38 if(x==Cx){ 39 if(y==Cy)return "┏"; 40 else if(y==Cy+1)return "┗"; 41 } 42 else if(x==Cx+1){ 43 if(y==Cy)return "┓"; 44 else if(y==Cy+1)return "┛"; 45 } 46 return " "; 47 } 48 void write(char *x){ 49 copy(map[My]+Mx,x); 50 Mx+=strlen(x); 51 } 52 void ln(){Mx=0;My++;} 53 void show(){ 54 int l=strlen(showtext),i; 55 int x=MAXN*2-l/2; 56 if(x%2==1)x++; 57 copy(map[MAXN]+x,showtext); 58 if(l%2==1)map[MAXN][x+1]=' '; 59 system("cls"); 60 for(i=0;i =0&&chess[Cx-i][Cy]==player)flot++;else break; 96 if(flot>=5)return player;flot=1; 97 for( i=1;i<5;++i)if(Cy+i =0&&chess[Cx][Cy-i]==player)flot++;else break; 99 /*for(int i=1;i<5;++i)printf("chess[%d][%d]=%d ",Cx,Cy+i,chess[Cx][Cy+i]);100 for(int i=1;i<5;++i)printf("chess[%d][%d]=%d ",Cx,Cy-i,chess[Cx][Cy-i]);101 printf("**%d %d ",flot,player);getch();*/102 if(flot>=5)return player;flot=1;103 for( i=1;i<5;++i)if(Cy+i =0&&Cx-i>=0&&chess[Cx-i][Cy-i]==player)flot++;else break;105 if(flot>=5)return player;flot=1;106 for( i=1;i<5;++i)if(Cy+i =0&&chess[Cx-i][Cy+i]==player)flot++;else break;107 for( i=1;i<5;++i)if(Cy-i>=0&&Cx+i =5)return player;109 return 0;110 }111 int defense(int x,int y){ int i,j,flot=0,t=0,temp1,temp2;//t是空格数 112 if(chess[x][y]==1)flot++;113 if(flot){114 for(i=1;i<5;++i)if(x+i =0&&chess[x-j][y]==1)flot++;else if(!chess[x-j][y]&&t+1<2)t++,temp2=j;else break;if(!t)temp2=j;t=0;116 if(flot>=4){i=temp1;j=temp2;//printf("1%d ",flot);getch(); 117 if(x+i =0)if(!chess[x-j][y]){x-=j;chess[x][y]=player;Cx=x;Cy=y;return 1;}119 }120 }flot=0;121 if(chess[x][y]==1)flot++;122 if(flot){123 for(i=1;i<5;++i)if(y+i =0&&chess[x][y-j]==1)flot++;else if(!chess[x][y-j]&&t+1<2)t++,temp2=j;else break;if(!t)temp2=j;t=0;//else if(chess[x][y-j]==1)break;125 if(flot>=4){i=temp1;j=temp2;//printf("2%d ",flot);getch(); 126 if(y+i =0)if(!chess[x][y-j]){y-=j;chess[x][y]=player;Cx=x;Cy=y;return 1;}128 }129 }flot=0;130 if(chess[x][y]==1)flot++;131 if(flot){132 for(i=1;i<=5;++i)if(y+i =0&&x-j>=0&&chess[x-j][y-j]==1)flot++;else if(!chess[x-j][y-j]&&t+1<2)t++,temp2=j;else break;if(!t)temp2=j;t=0;134 if(flot>=4){i=temp1;j=temp2;//printf("3%d ",flot);getch(); 135 if(x+i =0&&y-j>=0)if(!chess[x-j][y-j]){y-=j;x-=j;chess[x][y]=player;Cx=x;Cy=y;return 1;}137 }138 }flot=0;139 if(chess[x][y]==1)flot++;140 if(flot){141 for(i=1;i<5;++i)if(y+i =0&&chess[x-i][y+i]==1)flot++;else if(!chess[x-i][y+i]&&t+1<2)t++,temp1=i;else break;if(!t)temp1=i;t=0;142 for(j=1;j<5;++j)if(y-j>=0&&x+j =4){i=temp1;j=temp2;//printf("4%d ",flot);getch(); 144 if(x+j =0)if(!chess[x+j][y-j]){y-=j;x+=j;chess[x][y]=player;Cx=x;Cy=y;return 1;}145 if(x-i>=0&&y+i =0&&chess[x-j][y]==1)flot++;else if(!chess[x-j][y]&&t+1<2)t++,temp2=j;else break;t=0;155 if(flot>=3){i=temp1;j=temp2;//printf("5%d ",flot);getch(); 156 if(x+i =0)if(!chess[x-j][y]){x-=j;chess[x][y]=player;Cx=x;Cy=y;return 1;}158 }159 }flot=0;160 if(chess[x][y]==1)flot++;161 if(flot){162 for(i=1;i<5;++i)if(y+i =0&&chess[x][y-j]==1)flot++;else if(!chess[x][y-j]&&t+1<2)t++,temp2=j;else break;t=0;164 if(flot>=3){i=temp1;j=temp2;//printf("6%d ",flot);getch(); 165 if(y+i =0)if(!chess[x][y-j]){y-=j;chess[x][y]=player;Cx=x;Cy=y;return 1;}167 } 168 }flot=0;169 if(chess[x][y]==1)flot++;170 if(flot){171 for(i=1;i<=5;++i)if(y+i =0&&x-j>=0&&chess[x-j][y-j]==1)flot++;else if(!chess[x-j][y-j]&&t+1<2)t++,temp2=j;else break;t=0;173 if(flot>=3){i=temp1;j=temp2;//printf("7%d ",flot);getch(); 174 if(x+i =0&&y-j>=0)if(!chess[x-j][y-j]){y-=j;x-=j;chess[x][y]=player;Cx=x;Cy=y;return 1;}176 }177 }flot=0;178 if(chess[x][y]==1)flot++;179 if(flot){180 for(i=1;i<5;++i)if(y+i =0&&chess[x-i][y+i]==1)flot++;else if(!chess[x-i][y+i]&&t+1<2)t++,temp1=i;else break;if(!t)temp1=i;t=0;181 for(j=1;j<5;++j)if(y-j>=0&&x+j =3){i=temp1;j=temp2;//printf("8%d ",flot);getch(); 183 if(x+j =0)if(!chess[x+j][y-j]){y-=j;x+=j;chess[x][y]=player;Cx=x;Cy=y;return 1;}184 if(x-i>=0&&y+i =0&&chess[x-j][y]==player)flot++;else if(!chess[x-j][y]&&t+1<2)t++,temp2=j;else break;if(!t)temp2=j;t=0;197 if(flot>=4){i=temp1;j=temp2;198 if(x+i =0)if(!chess[x-j][y]){x-=j;chess[x][y]=player;Cx=x;Cy=y;return 1;}200 }201 }flot=0;202 if(chess[x][y]==player)flot++;203 if(flot){204 for(i=1;i<5;++i)if(y+i =0&&chess[x][y-j]==player)flot++;else if(!chess[x][y-j]&&t+1<2)t++,temp2=j;else break;if(!t)temp2=j;t=0;//else if(chess[x][y-j]==1)break;206 if(flot>=4){i=temp1;j=temp2;207 if(y+i =0)if(!chess[x][y-j]){y-=j;chess[x][y]=player;Cx=x;Cy=y;return 1;}209 }210 }flot=0;211 if(chess[x][y]==player)flot++;212 if(flot){213 for(i=1;i<=5;++i)if(y+i =0&&x-j>=0&&chess[x-j][y-j]==player)flot++;else if(!chess[x-j][y-j]&&t+1<2)t++,temp2=j;else break;if(!t)temp2=j;t=0;215 if(flot>=4){i=temp1;j=temp2;216 if(x+i =0&&y-j>=0)if(!chess[x-j][y-j]){y-=j;x-=j;chess[x][y]=player;Cx=x;Cy=y;return 1;}218 }219 }flot=0;220 if(chess[x][y]==player)flot++;221 if(flot){222 for(i=1;i<5;++i)if(y+i =0&&chess[x-i][y+i]==player)flot++;else if(!chess[x-i][y+i]&&t+1<2)t++,temp1=i;else break;if(!t)temp1=i;t=0;223 for(j=1;j<5;++j)if(y-j>=0&&x+j =4){i=temp1;j=temp2;225 if(x+j =0)if(!chess[x+j][y-j]){y-=j;x+=j;chess[x][y]=player;Cx=x;Cy=y;return 1;}226 if(x-i>=0&&y+i =0&&chess[x-j][y]==player)flot++;else if(!chess[x-j][y]&&t+1<2)t++,temp2=j;else break;if(!t)temp2=j;t=0;236 if(flot>=3){i=temp1;j=temp2;237 if(x+i =0)if(!chess[x-j][y]){x-=j;chess[x][y]=player;Cx=x;Cy=y;return 1;}239 }240 }flot=0;241 if(chess[x][y]==player)flot++;242 if(flot){243 for(i=1;i<5;++i)if(y+i =0&&chess[x][y-j]==player)flot++;else if(!chess[x][y-j]&&t+1<2)t++,temp2=j;else break;if(!t)temp2=j;t=0;245 if(flot>=3){i=temp1;j=temp2;246 if(y+i =0)if(!chess[x][y-j]){y-=j;chess[x][y]=player;Cx=x;Cy=y;return 1;}248 } 249 }flot=0;250 if(chess[x][y]==player)flot++;251 if(flot){252 for(i=1;i<=5;++i)if(y+i =0&&x-j>=0&&chess[x-j][y-j]==player)flot++;else if(!chess[x-j][y-j]&&t+1<2)t++,temp2=j;else break;if(!t)temp2=j;t=0;254 if(flot>=3){i=temp1;j=temp2;255 if(x+i =0&&y-j>=0)if(!chess[x-j][y-j]){y-=j;x-=j;chess[x][y]=player;Cx=x;Cy=y;return 1;}257 }258 }flot=0;259 if(chess[x][y]==player)flot++;260 if(flot){261 for(i=1;i<5;++i)if(y+i =0&&chess[x-i][y+i]==player)flot++;else if(!chess[x-i][y+i]&&t+1<2)t++,temp1=i;else break;if(!t)temp1=i;t=0;262 for(j=1;j<5;++j)if(y-j>=0&&x+j =3){i=temp1;j=temp2;264 if(x+j =0)if(!chess[x+j][y-j]){y-=j;x+=j;chess[x][y]=player;Cx=x;Cy=y;return 1;}265 if(x-i>=0&&y+i =0&&chess[x-j][y]==player)flot++;else if(!chess[x-j][y]&&t+1<2)t++,temp2=j;else break;if(!t)temp2=j;t=0;274 if(flot>=2){i=temp1;j=temp2;275 if(x+i =0)if(!chess[x-j][y]){x-=j;chess[x][y]=player;Cx=x;Cy=y;return 1;}277 }278 }flot=0;279 if(chess[x][y]==player)flot++;280 if(flot){281 for(i=1;i<5;++i)if(y+i =0&&chess[x][y-j]==player)flot++;else if(!chess[x][y-j]&&t+1<2)t++,temp2=j;else break;if(!t)temp2=j;t=0;283 if(flot>=2){i=temp1;j=temp2;284 if(y+i =0)if(!chess[x][y-j]){y-=j;chess[x][y]=player;Cx=x;Cy=y;return 1;}286 } 287 }flot=0;288 if(chess[x][y]==player)flot++;289 if(flot){290 for(i=1;i<=5;++i)if(y+i =0&&x-j>=0&&chess[x-j][y-j]==player)flot++;else if(!chess[x-j][y-j]&&t+1<2)t++,temp2=j;else break;if(!t)temp2=j;t=0;292 if(flot>=2){i=temp1;j=temp2;293 if(x+i =0&&y-j>=0)if(!chess[x-j][y-j]){y-=j;x-=j;chess[x][y]=player;Cx=x;Cy=y;return 1;}295 }296 }flot=0;297 if(chess[x][y]==player)flot++;298 if(flot){299 for(i=1;i<5;++i)if(y+i =0&&chess[x-i][y+i]==player)flot++;else if(!chess[x-i][y+i]&&t+1<2)t++,temp1=i;else break;if(!t)temp1=i;t=0;300 for(j=1;j<5;++j)if(y-j>=0&&x+j =2){i=temp1;j=temp2;302 if(x+j =0)if(!chess[x+j][y-j]){y-=j;x+=j;chess[x][y]=player;Cx=x;Cy=y;return 1;}303 if(x-i>=0&&y+i =MAXN)continue;332 for( x=1;x =MAXN)continue;333 if(!chess[Cx+x][Cy+y]){Cx+=x;Cy+=y;334 chess[Cx][Cy]=player;335 return;336 }337 }338 }339 for( y=1;y =MAXN)Cx=0;401 if(Cy>=MAXN)Cy=0;402 }403 if(select==59)if(player==2){computer();print();if(win())return;player=3-player;}404 }405 }406 int main(){407 system("title 五子棋 ——崔哥制作");408 system("mode con cols=63 lines=32");409 system("color 5f");410 while(1){system("cls");411 system("echo 请选择你要玩的模式:");412 printf("人机对战请按F1 人人对战请按F2\n");413 select=getch();414 if(select==59||select==60){Run();b++;}415 }416 return 0;417 }