1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <windows.h> 4 5 #define N 19 6 int pieces[N][N]= {0}; //五子棋盘19*19 7 int flag=1; //状态,0-无子;1-A子;2-B子 8 9 void DrawPanel()//棋盘模块 10 { 11 int count=1; 12 int i,j; 13 system("CLS"); 14 for(i=0; i<=N; i++) //列坐标 15 { 16 if(i==0) 17 { 18 printf(" "); 19 } 20 else 21 { 22 printf("%4d",count); 23 count++; 24 } 25 } 26 printf("\n"); 27 count=1; 28 for(i=0; i<N; i++) 29 { 30 for(j=-1; j<N; j++) 31 { 32 if(j==-1)//若是第一列,则输出字符 33 { 34 printf("%2d",count); 35 count++; 36 } 37 else 38 { 39 if(pieces[i][j]==0) 40 { 41 printf(" ."); 42 } 43 else if(pieces[i][j]==1) 44 { 45 printf(" ●"); 46 } 47 else if(pieces[i][j]==2) 48 { 49 printf(" ○"); 50 } 51 } 52 } 53 printf("\n"); 54 } 55 56 } 57 58 int ZouQiHang() //下棋模块1 59 { 60 int x; 61 if(flag==1) 62 { 63 printf("\t玩家A●"); 64 } 65 else 66 { 67 printf("\t玩家B○"); 68 } 69 printf("\t请输入要走棋子的行数!\n"); 70 printf("\tx="); 71 scanf("%d",&x); 72 while(x>N-1||x<1) 73 { 74 printf("\terror!\n"); 75 if(flag==1) 76 { 77 printf("\t玩家A●"); 78 } 79 else 80 { 81 printf("\t玩家B○"); 82 } 83 printf("\t请输入要走棋子的行数!\n"); 84 printf("\tx="); 85 scanf("%d",&x); 86 } 87 return x; 88 } 89 90 int ZouQiLie()//下棋模块2 91 { 92 int y; 93 if(flag==1) 94 { 95 printf("\t玩家A●"); 96 } 97 else 98 { 99 printf("\t玩家B○"); 100 } 101 printf("\t请输入要走棋子的列数!\n"); 102 printf("\ty="); 103 scanf("%d",&y); 104 while(y>N-1||y<1) 105 { 106 printf("\terror!\n"); 107 if(flag==1) 108 { 109 printf("\t玩家A●"); 110 } 111 else 112 { 113 printf("\t玩家B○"); 114 } 115 printf("\t请输入要走棋子的列数!\n"); 116 printf("\ty="); 117 scanf("%d",&y); 118 } 119 return y; 120 } 121 122 int Check(int x,int y)//判断输赢模块 123 { 124 //行检查 125 int count=1; 126 int i=x,j=y; 127 j++; 128 while(j<N&&count<5)//自此向右 129 { 130 if(pieces[i][j]==pieces[x][y]) 131 { 132 count++; 133 } 134 else 135 { 136 break; 137 } 138 j++; 139 } 140 j=y; 141 j--; 142 while(j>=0&&count<5)//自此向左 143 { 144 if(pieces[i][j]==pieces[x][y]) 145 { 146 count++; 147 } 148 else 149 { 150 break; 151 } 152 j--; 153 } 154 if(count==5)//存在5子连成连续一条线 155 { 156 return pieces[x][y]; 157 } 158 159 //列检查 160 i=x; 161 j=y; 162 count=1; 163 i=i+1; 164 while(i<N&&count<5)//自此向下 165 { 166 if(pieces[i][j]==pieces[x][y]) 167 { 168 count++; 169 } 170 else 171 { 172 break; 173 } 174 i++; 175 } 176 i=x-1; 177 while(i>=0&&count<5)//自此向上 178 { 179 if(pieces[i][j]==pieces[x][y]) 180 { 181 count++; 182 } 183 else 184 { 185 break; 186 } 187 i--; 188 } 189 if(count==5) 190 { 191 return pieces[x][y]; 192 } 193 194 //左斜行 195 i=x; 196 j=y; 197 count=1; 198 199 i=i-1; 200 j=j-1; 201 while(i>=0&&j>=0&&count<5)//自此左斜向上 202 { 203 if(pieces[i][j]==pieces[x][y]) 204 { 205 count++; 206 } 207 else 208 { 209 break; 210 } 211 i--; 212 j--; 213 } 214 215 i=x+1; 216 j=y+1; 217 while(i<N&&j<N&&count<5)//自此左斜向下 218 { 219 if(pieces[i][j]==pieces[x][y]) 220 { 221 count++; 222 } 223 else 224 { 225 break; 226 } 227 i++; 228 j++; 229 } 230 if(count==5) 231 { 232 return pieces[x][y]; 233 } 234 235 //右斜行 236 i=x; 237 j=y; 238 count=1; 239 i=i-1; 240 j=j+1; 241 while(i>=0&&j<N&&count<5)//自此右斜向上 242 { 243 if(pieces[i][j]==pieces[x][y]) 244 { 245 count++; 246 } 247 else 248 { 249 break; 250 } 251 i--; 252 j++; 253 } 254 255 i=x+1; 256 j=y-1; 257 while(i<N&&j>=0&&count<5)////自此右斜向下 258 { 259 if(pieces[i][j]==pieces[x][y]) 260 { 261 count++; 262 } 263 else 264 { 265 break; 266 } 267 i++; 268 j--; 269 } 270 if(count==5) 271 { 272 return pieces[x][y]; 273 } 274 return 0; 275 } 276 277 void HeQi()//信息模块1 278 { 279 Sleep(5000); 280 system("CLS"); 281 printf("\t**********************************************************\n"); 282 printf("\t* *\n"); 283 printf("\t* *\n"); 284 printf("\t* *\n"); 285 printf("\t* 和棋! *\n"); 286 printf("\t* *\n"); 287 printf("\t* *\n"); 288 printf("\t* *\n"); 289 printf("\t* *\n"); 290 printf("\t**********************************************************\n"); 291 } 292 293 void Over() //信息模块2 294 { 295 Sleep(5000); 296 //system("CLS"); 297 printf("\t**********************************************************\n"); 298 printf("\t* *\n"); 299 printf("\t* *\n"); 300 printf("\t* *\n"); 301 printf("\t* 游戏结束! *\n"); 302 printf("\t* *\n"); 303 printf("\t* *\n"); 304 printf("\t* *\n"); 305 printf("\t* *\n"); 306 printf("\t**********************************************************\n"); 307 } 308 309 void Welcome() 310 { 311 system("CLS"); 312 printf("\t**********************************************************\n"); 313 printf("\t* *\n"); 314 printf("\t* *\n"); 315 printf("\t* *\n"); 316 printf("\t* 欢迎! *\n"); 317 printf("\t* *\n"); 318 printf("\t* *\n"); 319 printf("\t* *\n"); 320 printf("\t* *\n"); 321 printf("\t**********************************************************\n"); 322 printf("\tpress any key to start!\n"); 323 getchar(); 324 } 325 326 int main() 327 { 328 int x,y; //行列坐标 329 char ch='y'; 330 int winner; //赢家 331 int countpieces=0;//落子总数 332 Welcome(); 333 DrawPanel(); 334 335 while(countpieces<N*N) 336 { 337 x=ZouQiHang(); 338 y=ZouQiLie(); 339 x=x-1; 340 y=y-1; 341 if(pieces[x][y]==0)//是否可以落子 342 { 343 pieces[x][y]=flag; 344 countpieces++; 345 //改变状态 346 if(flag==1) 347 { 348 flag=2; 349 } 350 else 351 { 352 flag=1; 353 } 354 //判断输赢,继续? 355 winner=Check(x,y); 356 if(winner==1) 357 { 358 DrawPanel(); 359 printf("\t恭喜A●赢了!\n"); 360 break; 361 } 362 else if(winner==2) 363 { 364 DrawPanel(); 365 printf("\t恭喜B○赢了!\n"); 366 break; 367 } 368 } 369 else 370 { 371 printf("ERROR!\n"); 372 printf("落子行列错误,请随意按键重新输入!\n"); 373 getchar(); 374 getchar(); 375 } 376 DrawPanel(); 377 }//while 378 379 if(countpieces==N*N) 380 { 381 HeQi();//和棋 382 } 383 else 384 { 385 Over();//结束 386 } 387 return 0; 388 389 }