1、C语言五子棋游戏源代码#include#define N 10void welcome();void initqipan();void showqi(int i);void save(int p);void panduan(int p);void heqi();void over();int zouqihang();int zouqilie();/*结构体*/struct zuobiao int xN*N; int yN*N;weizhiN*N;/*主函数*/void main() int p=0; welcome(); initqipan(); for(p=1;p=N*N;p+) weizh
2、ip.xp=zouqihang(); weizhip.yp=zouqilie(); save(p); showqi(p); panduan(p); if(p=N*N) heqi(); over();/*建立棋盘*/void initqipan() int i,j; for(i=0;iN;i+) printf(%d,i); printf( ); printf(n); for(i=1;iN;i+) for(j=0;jN;j+) if(j=0) printf(%d,i); else printf(); printf(n); /*显示棋子*/void showqi(int p) int i,j,k,m
3、; int aN*N,bN*N; FILE *fp; fp=fopen(wuzi_list,rb); for(i=1;i=N*N;i+) fread(&weizhii,sizeof(struct zuobiao),1,fp); ai=weizhii.xi; bi=weizhii.yi; for(m=1;mp;m+) while(weizhip.xp=am&weizhip.yp=bm) printf(error!n); weizhip.xp=zouqihang(); weizhip.yp=zouqilie(); m=1; for(i=0;iN;i+) printf(%d,i); printf(
4、); printf(n); for(i=1;iN;i+) for(j=1;jN;j+) if(j=1) printf(%d,i); for(k=1;kp)printf(); else continue; printf(n); /*走棋行*/int zouqihang() int x; printf(请输入要走棋子所在行数!n); printf(x=); scanf(%d,&x); while(xN-1|xN-1|y1) printf(error!n); printf(请输入要走棋子所在列数!n); printf(y=); scanf(%d,&y); return y;/*文件保存*/void
5、save(int i) FILE *fp; fp=fopen(wuzi_list,wb); fwrite(&weizhii,sizeof(struct zuobiao),1,fp);/*判断输赢*/void panduan(int p) int i,j,k8=1,1,1,1,1,1,1,1,; int aN*N,bN*N; FILE *fp; fp=fopen(wuzi_list,rb); for(i=1;i=p;i+) fread(&weizhii,sizeof(struct zuobiao),1,fp); ai=weizhii.xi; bi=weizhii.yi; /*判断行*/ for(
6、i=1;i=p;i+) if(i%2=1) for(j=1;j=p;j=j+2) if(ai=aj)&(bi=bj-1) k0+; continue; else if(ai=aj)&(bi=bj-2) k0+; continue; else if(ai=aj)&(bi=bj-3) k0+; continue; else if(ai=aj)&(bi=bj-4) k0+; continue; else if(k0=5) printf(Player 1 wins!n); else continue; if(k0=5) break; k0=1; else if(k0=5) break; else if
7、(i%2=0) for(j=2;j=p;j=j+2) if(ai=aj)&(bi=bj-1) k1+; continue; else if(ai=aj)&(bi=bj-2) k1+; continue; else if(ai=aj)&(bi=bj-3) k1+; continue; else if(ai=aj)&(bi=bj-4) k1+; continue; else if(k1=5) printf(Player 2 wins!n); else continue; if(k1=5) break; k1=1; /*判断列*/ for(i=1;i=p;i+) if(k0=5|k1=5) brea
8、k; else if(i%2=1) for(j=1;j=p;j=j+2) if(ai=aj-1)&(bi=bj) k2+; continue; else if(ai=aj-2)&(bi=bj) k2+; continue; else if(ai=aj-3)&(bi=bj) k2+; continue; else if(ai=aj-4)&(bi=bj) k2+; continue; else if(k2=5) printf(Player 1 wins!n); else continue; if(k2=5) break; k2=1; else if(k2=5) break; else if(i%2
9、=0) for(j=2;j=p;j=j+2) if(ai=aj-1)&(bi=bj) k3+; continue; else if(ai=aj-2)&(bi=bj) k3+; continue; else if(ai=aj-3)&(bi=bj) k3+; continue; else if(ai=aj-4)&(bi=bj) k3+; continue; else if(k3=5) printf(Player 2 wins!n); else continue; if(k3=5) break; k3=1; /*判断对角(左上-右下)*/ for(i=1;i=p;i+) if(k0=5|k1=5|k
10、2=5|k3=5) break; else if(i%2=1) for(j=1;j=p;j=j+2) if(ai=aj-1)&(bi=bj-1) k4+; continue; else if(ai=aj-2)&(bi=bj-2) k4+; continue; else if(ai=aj-3)&(bi=bj-3) k4+; continue; else if(ai=aj-4)&(bi=bj-4) k4+; continue; else if(k4=5) printf(Player 1 wins!n); else continue; if(k4=5) break; k4=1; else if(k2
11、=5) break; else if(i%2=0) for(j=2;j=p;j=j+2) if(ai=aj-1)&(bi=bj-1) k5+; continue; else if(ai=aj-2)&(bi=bj-2) k5+; continue; else if(ai=aj-3)&(bi=bj-3) k5+; continue; else if(ai=aj-4)&(bi=bj-4) k5+; continue; else if(k5=5) printf(Player 2 wins!n); else continue; if(k5=5) break; k5=1; /*判断对角(左下-右上)*/
12、for(i=1;i=p;i+) if(k0=5|k1=5|k2=5|k3=5|k4=5|k5=5) break; else if(i%2=1) for(j=1;j=p;j=j+2) if(ai=aj+1)&(bi=bj-1) k6+; continue; else if(ai=aj+2)&(bi=bj-2) k6+; continue; else if(ai=aj+3)&(bi=bj-3) k6+; continue; else if(ai=aj+4)&(bi=bj-4) k6+; continue; else if(k6=5) printf(Player 1 wins!n); else co
13、ntinue; if(k6=5) break; k6=1; else if(k6=5) break; else if(i%2=0) for(j=2;j=p;j=j+2) if(ai=aj+1)&(bi=bj-1) k7+; continue; else if(ai=aj+2)&(bi=bj-2) k7+; continue; else if(ai=aj+3)&(bi=bj-3) k7+; continue; else if(ai=aj+4)&(bi=bj-4) k7+; continue; else if(k7=5) printf(Player 2 wins!n); else continue; if(k7=5) break; k7=1; /*和棋*/void heqi() printf(*n); printf( Tie!n); printf(*n);/*游戏结束*/void over() printf(*n); printf( game over!n); printf(*n);/*游戏开始*/void welcome() printf(*n); printf( Welcome!n); printf(*n);
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1