一个五子棋程序,请教点问题
下面是我在一本实例书上看到的五子棋程序,看不是很懂,请教下高手:(1)m,n,t,s,flag,flak,z的意思
(2)判断方法,以判断前后为例讲一个就行了,
程序代码:
case 1: /*判断前后数值是否与自己相同*/ while ((m1<=5)&&(flag1==0)) { if(a[j][i+m1]/1==1) { m1=m1+1; t1=t1+1; printf("t1=%d \n",t1); } else flag1=1; } while ((n1<=5)&&(flak1==0)) { if(a[j][i-n1]/1==1) { n1=n1+1; s1=s1+1; printf(" s1=%d \n",s1); } else flak1=1; } z1=s1+t1+1; if(z1>=5) { p=W; e=10; } break;(3)觉得这程序有误或有改进的也希望提出来哈~~
(4)附件是VC6.0编译出来的可执行文件:
五子棋.rar
(32.75 KB)
程序代码:
/* 模拟五子棋游戏。以0表示空棋盘,其他数字表示游戏者以下的位置,A、B两个游戏者,输入棋子坐标指明落子位置。 */ #include <stdio.h> #include <math.h> #define NUM1 5 /*改变棋盘的宽 要大于4*/ #define NUM2 5 /*改变棋盘的长 要大于4*/ #define W 27 /*比NUM1*NUM2+1大的数*/ #define V 28 /*比NUM1*NUM2+2大的数 W不能等于V */ main() { static int a[NUM1][NUM2],b[NUM1][NUM2],c[NUM1+1][NUM2+1]; /*c数组比a,b大一行和列作为坐标方便输入*/ int x,y,i,j,m,n,f=1,p=0; int zuo_biaox=0,zuo_biaoy=0; for(zuo_biaox=1; zuo_biaox<NUM1+1; zuo_biaox=zuo_biaox+1) c[0][zuo_biaox]=zuo_biaox-1; for(zuo_biaoy=1; zuo_biaoy<NUM2+1; zuo_biaoy=zuo_biaoy+1) c[zuo_biaoy][0]=zuo_biaoy-1; for(p=0; p<=NUM1*NUM2; p=p+1) { int e=1; int m1=1,n1=1,t1=0,s1=0,flag1=0,flak1=0,z1=0; int m2=1,n2=1,t2=0,s2=0,flag2=0,flak2=0,z2=0; int m3=1,n3=1,t3=0,s3=0,flag3=0,flak3=0,z3=0; int m4=1,n4=1,t4=0,s4=0,flag4=0,flak4=0,z4=0; /*循环为棋盘变量赋值*/ f=-f; if(f<0) printf("A: inputx&y:\n"); /*提示选手A下子*/ if(f>0) printf("B: inputx&y:\n"); /*提示选手B下子*/ scanf("%d %d", &x,&y ); if((x>=NUM2)||(y>=NUM1)) /*坐标不能超过棋盘格数*/ { f=-f; printf("the number is too big !\n"); /*如果输入数字太大超过棋盘格数*/ continue; /*跳出本次循环*/ } if(c[y+1][x+1]!=0) /*已下棋的坐标不能再输入*/ { f=-f; printf("you can not put here!\n"); /*提示信息*/ continue; /*跳出本次循环*/ } i=x; j=y; if(f<0) { c[j+1][i+1]=1; c[0][0]=1; } if(f>0) { c[j+1][i+1]=2; c[0][0]=2; } if(f<0) /*判断a 数组*/ { a[j][i]=1; for(e=1; e<=4; e=e+1) { switch(e) { case 1: /*判断前后数值是否与自己相同*/ while ((m1<=5)&&(flag1==0)) { if(a[j][i+m1]/1==1) { m1=m1+1; t1=t1+1; printf("t1=%d \n",t1); } else flag1=1; } while ((n1<=5)&&(flak1==0)) { if(a[j][i-n1]/1==1) { n1=n1+1; s1=s1+1; printf(" s1=%d \n",s1); } else flak1=1; } z1=s1+t1+1; if(z1>=5) { p=W; e=10; } break; case 2: /*判断上下数值是否与自己相同*/ while ((m1<=5)&&(flag2==0)) { if(a[j+m2][i]/1==1) { m2=m2+1; t2=t2+1; printf("t2=%d \n",t2); } else flag2=1; } while ((n2<=5)&&(flak2==0)) { if(a[j-n2][i]/1==1) { n2=n2+1; s2=s2+1; printf(" s2=%d \n",s2); } else flak2=1; } z2=s2+t2+1; if(z2>=5) { p=W; e=10; } break; case 3: /*判断右倾斜数值是否与自己相同*/ while ((m3<=5)&&(flag3==0)) { if(a[j-m3][i+m3]/1==1) { m3=m3+1; t3=t3+1; printf("t3=%d \n",t3); } else flag3=1; } while ((n3<=5)&&(flak3==0)) { if(a[j+n3][i-n3]/1==1) { n3=n3+1; s3=s3+1; printf(" s3=%d \n",s3); } else flak3=1; } z3=s3+t3+1; if(z3>=5) { p=W; e=10; } break; case 4: /*判断左倾斜数值是否与自己相同*/ while ((m4<=5)&&(flag4==0)) { if(a[j-m4][i-m4]/1==1) { m4=m4+1; t4=t4+1; printf("t4=%d \n",t4); } else flag4=1; } while ((n4<=5)&&(flak4==0)) { if(a[j+n4][i+n4]/1==1) { n4=n4+1; s4=s4+1; printf(" s4=%d \n",s4); } else flak4=1; } z4=s4+t4+1; if(z4>=5) { p=W; e=10; } break; } } } if(f>0) /*判断b 数组*/ { b[j][i]=2; for(e=1; e<=4; e=e+1) { switch(e) { case 1: /*判断前后数值是否与自己相同*/ while ((m1<=5)&&(flag1==0)) { if(b[j][i+m1]/2==1) { m1=m1+1; t1=t1+1; printf("t1=%d \n",t1); } else flag1=1; } while ((n1<=5)&&(flak1==0)) { if(b[j][i-n1]/2==1) { n1=n1+1; s1=s1+1; printf(" s1=%d \n",s1); } else flak1=1; } z1=s1+t1+1; if(z1>=5) { p=V; e=10; } break; case 2: /*判断上下数值是否与自己相同*/ while ((m1<=5)&&(flag2==0)) { if(b[j+m2][i]/2==1) { m2=m2+1; t2=t2+1; printf("t2=%d \n",t2); } else flag2=1; } while ((n2<=5)&&(flak2==0)) { if(b[j-n2][i]/2==1) { n2=n2+1; s2=s2+1; printf(" s2=%d \n",s2); } else flak2=1; } z2=s2+t2+1; if(z2>=5) { p=V; e=10; } break; case 3: /*判断右倾斜数值是否与自己相同*/ while ((m3<=5)&&(flag3==0)) { if(b[j-m3][i+m3]/2==1) { m3=m3+1; t3=t3+1; printf("t3=%d \n",t3); } else flag3=1; } while ((n3<=5)&&(flak3==0)) { if(b[j+n3][i-n3]/2==1) { n3=n3+1; s3=s3+1; printf(" s3=%d \n",s3); } else flak3=1; } z3=s3+t3+1; if(z3>=5) { p=V; e=10; } break; case 4: /*判断左倾斜数值是否与自己相同*/ while ((m4<=5)&&(flag4==0)) { if(b[j-m4][i-m4]/2==1) { m4=m4+1; t4=t4+1; printf("t4=%d \n",t4); } else flag4=1; } while ((n4<=5)&&(flak4==0)) { if(b[j+n4][i+n4]/2==1) { n4=n4+1; s4=s4+1; printf(" s4=%d \n",s4); } else flak4=1; } z4=s4+t4+1; if(z4>=5) { p=V; e=10; } break; } /*switch*/ } /*for s*/ } for(m=0; m<NUM1+1; m=m+1) { printf("\n"); for(n=0; n<NUM2+1; n=n+1) printf("%d ",c[m][n]); } printf("\n"); if(p>=NUM1*NUM2+1) break; } for(m=0;m<NUM1+1;m=m+1) { printf("\n"); for(n=0; n<NUM2+1; n=n+1) printf("%d ",c[m][n]); printf("\n"); } printf("p=%d\n",p); if(p==W) printf("The winner is A!\n"); if(p==V) printf("The winner is B!\n"); if(p==NUM1*NUM2+1) printf("Noone win!"); /*输出游戏结果*/ getchar(); getchar(); }