五子棋
第一次写,写的有点复杂,大家可以帮忙找找问题,简化一些:
代码:
1 :#include<stdio.h> 2 #include<stdlib.h> 3 #include<conio.h> 4 #include<string.h> 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<MAXN*2+1;i++){ 61 printf("%s",map[i]); 62 if(i<MAXN*2)puts(""); 63 } 64 } 65 void print(){int x,y; 66 Mx=My=0; 67 for(y=0;y<=MAXN;++y){ 68 for(x=0;x<=MAXN;x++){ 69 write(getlist(x,y)); 70 if(y==0||y==MAXN){ 71 if(x!=MAXN)write(" "); } 72 else {/*printf("%d ",x);getch();*/ 73 if(x==MAXN-1||x==0)write("┃"); 74 else if(x!=MAXN)write("│"); 75 } 76 } 77 if(y==MAXN)break; 78 ln();write(" "); 79 for(x=0;x<MAXN;x++){ 80 write(getmap(x,y)); 81 if(x!=MAXN-1){ 82 if(y==0||y==MAXN-1)write("━");else write("─");} 83 }ln(); 84 } 85 86 show(); 87 } 88 int judge(){ 89 if(!chess[Cx][Cy]){chess[Cx][Cy]=player;return 1;} 90 else return 0; 91 } 92 int check(){ 93 int flot=1,i; 94 for(i=1;i<5;++i)if(Cx+i<MAXN&&chess[Cx+i][Cy]==player)flot++;else break; 95 for( i=1;i<5;++i)if(Cx-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<MAXN&&chess[Cx][Cy+i]==player)flot++;else break; 98 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<MAXN&&Cx+i<MAXN&&chess[Cx+i][Cy+i]==player)flot++;else break; 104 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<MAXN&&Cx-i>=0&&chess[Cx-i][Cy+i]==player)flot++;else break; 107 for( i=1;i<5;++i)if(Cy-i>=0&&Cx+i<MAXN&&chess[Cx+i][Cy-i]==player)flot++;else break; 108 if(flot>=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<MAXN&&chess[x+i][y]==1)flot++;else if(!chess[x+i][y]&&t+1<2)t++,temp1=i;else break;if(!t)temp1=i;t=0; 115 for(j=1;i<5;++j)if(x-j>=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<MAXN)if(!chess[x+i][y]){x+=i;chess[x][y]=player;Cx=x;Cy=y;return 1;} 118 if(x-j>=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<MAXN&&chess[x][y+i]==1)flot++;else if(!chess[x][y+i]&&t+1<2)t++,temp1=i;else break;if(!t)temp1=i;t=0;//else if(chess[x][y+i]==1)break; 124 for(j=1;j<5;j++)if(y-j>=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<MAXN)if(!chess[x][y+i]){y+=i;chess[x][y]=player;Cx=x;Cy=y;return 1;} 127 if(y-j>=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<MAXN&&x+i<MAXN&&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; 133 for(j=1;j<5;++j)if(y-j>=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<MAXN&&y+i<MAXN)if(!chess[x+i][y+i]){y+=i;x+=i;chess[x][y]=player;Cx=x;Cy=y;return 1;} 136 if(x-j>=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<MAXN&&x-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<MAXN&&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; 143 if(flot>=4){i=temp1;j=temp2;//printf("4%d ",flot);getch(); 144 if(x+j<MAXN&&y-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<MAXN)if(!chess[x-i][y+i]){y+=i;x-=i;chess[x][y]=player;Cx=x;Cy=y;return 1;} 146 } 147 } 148 //****************************** 149 if(order1){ 150 flot=0; 151 if(chess[x][y]==1)flot++; 152 if(flot){ 153 for(i=1;i<5;++i)if(x+i<MAXN&&chess[x+i][y]==1)flot++;else if(!chess[x+i][y]&&t+1<2)t++,temp1=i;else break;if(!t)temp1=i;t=0; 154 for(j=1;i<5;++j)if(x-j>=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<MAXN)if(!chess[x+i][y]){x+=i;chess[x][y]=player;Cx=x;Cy=y;return 1;} 157 if(x-j>=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<MAXN&&chess[x][y+i]==1)flot++;else if(!chess[x][y+i]&&t+1<2)t++,temp1=i;else break;if(!t)temp1=i;t=0; 163 for(j=1;j<5;j++)if(y-j>=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<MAXN)if(!chess[x][y+i]){y+=i;chess[x][y]=player;Cx=x;Cy=y;return 1;} 166 if(y-j>=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<MAXN&&x+i<MAXN&&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; 172 for(j=1;j<5;++j)if(y-j>=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<MAXN&&y+i<MAXN)if(!chess[x+i][y+i]){y+=i;x+=i;chess[x][y]=player;Cx=x;Cy=y;return 1;} 175 if(x-j>=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<MAXN&&x-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<MAXN&&chess[x+j][y-j]==1)flot++;else if(!chess[x+j][y-j]&&t+1<2)t++,temp2=j;else break;t=0; 182 if(flot>=3){i=temp1;j=temp2;//printf("8%d ",flot);getch(); 183 if(x+j<MAXN&&y-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<MAXN)if(!chess[x-i][y+i]){y+=i;x-=i;chess[x][y]=player;Cx=x;Cy=y;return 1;} 185 } 186 } 187 } 188 return 0; 189 } 190 int attarck(int x,int y) 191 { 192 int i,j,flot=0,temp1,temp2,t; 193 if(chess[x][y]==player)flot++; 194 if(flot){ 195 for(i=1;i<5;++i)if(x+i<MAXN&&chess[x+i][y]==player)flot++;else if(!chess[x+i][y]&&t+1<2)t++,temp1=i;else break;if(!t)temp1=i;t=0; 196 for(j=1;i<5;++j)if(x-j>=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<MAXN)if(!chess[x+i][y]){x+=i;chess[x][y]=player;Cx=x;Cy=y;return 1;} 199 if(x-j>=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<MAXN&&chess[x][y+i]==player)flot++;else if(!chess[x][y+i]&&t+1<2)t++,temp1=i;else break;if(!t)temp1=i;t=0;//else if(chess[x][y+i]==1)break; 205 for(j=1;j<5;j++)if(y-j>=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<MAXN)if(!chess[x][y+i]){y+=i;chess[x][y]=player;Cx=x;Cy=y;return 1;} 208 if(y-j>=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<MAXN&&x+i<MAXN&&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; 214 for(j=1;j<5;++j)if(y-j>=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<MAXN&&y+i<MAXN)if(!chess[x+i][y+i]){y+=i;x+=i;chess[x][y]=player;Cx=x;Cy=y;return 1;} 217 if(x-j>=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<MAXN&&x-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<MAXN&&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; 224 if(flot>=4){i=temp1;j=temp2; 225 if(x+j<MAXN&&y-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<MAXN)if(!chess[x-i][y+i]){y+=i;x-=i;chess[x][y]=player;Cx=x;Cy=y;return 1;} 227 } 228 } 229 //**************************************** 230 if(order2){ 231 flot=0; 232 if(chess[x][y]==player)flot++; 233 if(flot){ 234 for(i=1;i<5;++i)if(x+i<MAXN&&chess[x+i][y]==player)flot++;else if(!chess[x+i][y]&&t+1<2)t++,temp1=i;else break;if(!t)temp1=i;t=0; 235 for(j=1;i<5;++j)if(x-j>=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<MAXN)if(!chess[x+i][y]){x+=i;chess[x][y]=player;Cx=x;Cy=y;return 1;} 238 if(x-j>=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<MAXN&&chess[x][y+i]==player)flot++;else if(!chess[x][y+i]&&t+1<2)t++,temp1=i;else break;if(!t)temp1=i;t=0; 244 for(j=1;j<5;j++)if(y-j>=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<MAXN)if(!chess[x][y+i]){y+=i;chess[x][y]=player;Cx=x;Cy=y;return 1;} 247 if(y-j>=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<MAXN&&x+i<MAXN&&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; 253 for(j=1;j<5;++j)if(y-j>=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<MAXN&&y+i<MAXN)if(!chess[x+i][y+i]){y+=i;x+=i;chess[x][y]=player;Cx=x;Cy=y;return 1;} 256 if(x-j>=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<MAXN&&x-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<MAXN&&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; 263 if(flot>=3){i=temp1;j=temp2; 264 if(x+j<MAXN&&y-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<MAXN)if(!chess[x-i][y+i]){y+=i;x-=i;chess[x][y]=player;Cx=x;Cy=y;return 1;} 266 } 267 } 268 //***************** 269 flot=0; 270 if(chess[x][y]==player)flot++; 271 if(flot){ 272 for(i=1;i<5;++i)if(x+i<MAXN&&chess[x+i][y]==player)flot++;else if(!chess[x+i][y]&&t+1<2)t++,temp1=i;else break;if(!t)temp1=i;t=0; 273 for(j=1;i<5;++j)if(x-j>=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<MAXN)if(!chess[x+i][y]){x+=i;chess[x][y]=player;Cx=x;Cy=y;return 1;} 276 if(x-j>=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<MAXN&&chess[x][y+i]==player)flot++;else if(!chess[x][y+i]&&t+1<2)t++,temp1=i;else break;if(!t)temp1=i;t=0; 282 for(j=1;j<5;j++)if(y-j>=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<MAXN)if(!chess[x][y+i]){y+=i;chess[x][y]=player;Cx=x;Cy=y;return 1;} 285 if(y-j>=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<MAXN&&x+i<MAXN&&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; 291 for(j=1;j<5;++j)if(y-j>=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<MAXN&&y+i<MAXN)if(!chess[x+i][y+i]){y+=i;x+=i;chess[x][y]=player;Cx=x;Cy=y;return 1;} 294 if(x-j>=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<MAXN&&x-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<MAXN&&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; 301 if(flot>=2){i=temp1;j=temp2; 302 if(x+j<MAXN&&y-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)if(!chess[x-i][y+i]){y+=i;x-=i;chess[x][y]=player;Cx=x;Cy=y;return 1;} 304 } 305 } 306 } 307 return 0; 308 } 309 void computer(){int x,y;order1=0; 310 for( y=0;y<MAXN;y++){ 311 for( x=0;x<MAXN;x++){ 312 if(defense(x,y)){return;} 313 } 314 }//printf("保卫1失败 ");getch(); 315 order2=0; 316 for( y=0;y<MAXN;y++){ 317 for( x=0;x<MAXN;x++){ 318 if(attarck(x,y)){return;} 319 } 320 }//printf("攻击失败 ");getch(); 321 for( y=0;y<MAXN;y++){order1=1; 322 for( x=0;x<MAXN;x++){ 323 if(defense(x,y)){return;} 324 } 325 }//printf("保卫2失败 ");getch(); 326 for( y=0;y<MAXN;y++){order2=1; 327 for( x=0;x<MAXN;x++){ 328 if(attarck(x,y)){return;} 329 } 330 }//printf("攻击2失败");getch(); 331 for( y=1;y<MAXN;y++){if(Cy+y>=MAXN)continue; 332 for( x=1;x<MAXN;x++){if(Cx+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;y++){ 340 if(Cy-y<0)continue; 341 for( x=1;x<MAXN;x++){ 342 if(Cx-x<0)continue; 343 if(!chess[Cx-x][Cy-y]){Cx-=x;Cy-=y; 344 chess[Cx][Cy]=player; 345 return; 346 } 347 } 348 } 349 //printf("随机失败 "); getch(); 350 } 351 int win(){ 352 int t=check(); 353 if(t==1){ 354 showtext="黑方获胜!"; 355 print(); 356 system("color 5b"); 357 system("echo 按任意键进行下一局比赛!"); 358 getch();return 1; 359 } 360 else if(t==2){ 361 showtext="白方获胜!"; 362 print(); 363 system("color 4f"); 364 system("echo 按任意键进行下一局比赛!"); 365 getch(); return 1; 366 } 367 else if(count==MAXN*MAXN){ 368 showtext="平局!"; 369 print();system("color 4e"); 370 system("echo 按任意键进行下一句比赛!"); 371 getch(); 372 return 1; 373 } 374 return 0; 375 } 376 void Run(){ 377 int input; 378 initial(); 379 if(b%3==0)system("color e2"); 380 while(1){ 381 print(); 382 input=getch(); 383 if(input==27)exit(0); 384 else if(input==32){ 385 if(judge()){count++; 386 if(win())return; 387 player=3-player; 388 } 389 } 390 else if(input==224){ 391 input=getch(); 392 switch(input){ 393 case 0x4b:Cx--;break; 394 case 0x48:Cy--;break; 395 case 0x4d:Cx++;break; 396 case 0x50:Cy++;break; 397 } 398 if(Cx<0)Cx=MAXN-1; 399 if(Cy<0)Cy=MAXN-1; 400 if(Cx>=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 }