纯C语言写的拼图游戏源码
发布时间:2020-12-16 07:48:04 所属栏目:百科 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 #includestdio.h #includestdlib.h #includetime.h #includeconio.h int step=0; void map()//游戏菜单函数。 { printf("▇▇▇▇▇▇▇▇▇▇▇▇▇
以下代码由PHP站长网 52php.cn收集自互联网 现在PHP站长网小编把它分享给大家,仅供参考 #include<stdio.h> #include<stdlib.h> #include<time.h> #include<conio.h> int step=0; void map()//游戏菜单函数。 { printf("▇▇▇▇▇▇▇▇▇▇▇▇▇▇n"); printf("█ 拼 图 游 戏 █n"); printf("█ 1. 3×3 █n"); printf("█ 2. 4×4 █n"); printf("█ 3. 5×5 █n"); printf("█ 4. 退出 █n"); printf("▇▇▇▇▇▇▇▇▇▇▇▇▇▇n"); } void fun(int *a,int t,int m,int r) { int i; int g; for(i=0;i<m;i++) { if(i%r==(r-1)&&(i+t)%r==0)//让数组不能跨行转换。 continue; if(i%r==0&&(i+t)%r==(r-1)) continue; if(a[i+t]==0 && (i+t)>=0 && (i+t)<m) { g=a[i]; a[i]=a[i+t]; a[i+t]=g; step++; return ; } } } void move(int *a,int r) {//上下左右移动函数。 char ch; int i,j; while(ch=getch()) { system("cls"); if(ch=='w') { i=-r; fun(a,i,m,r); } else if(ch=='s') { i=r; fun(a,r); } else if(ch=='a') { i=-1; fun(a,r); } else if(ch=='d') { i=1; fun(a,r); } else if(ch=='4') { printf("您已退出游戏,请下次再玩!n"); exit(0); } for(i=0;i<m;i++) { if(a[i]==0) printf(" "); else printf("%2d ",a[i]); if((i+1)%r==0) putchar(10); } printf("共走:%d步!n",step); } } void map_x(int m,int r) { int i=1,j,a[25],n; srand(time(NULL)); printf("拼图游戏开始了:n"); while(i<m) { n=1+rand()%(m-1); for(j=1;j<i;j++) { if(n==a[j]) break; } if(j==i) { a[i]=n; i++; } } a[0]=0; for(i=0;i<m;i++) { if(a[i]==0) printf(" "); else printf("%2d ",a[i]); if((i+1)%r==0) putchar(10); } printf("共走:%d步!n",step); move(a,r); } int main() { int i,n; char ch; map();//显示菜单。 printf("请选择所玩游戏的级别:n"); ch=getch(); while(ch!='1' && ch!='2' && ch!='3' &&ch!='4') { printf("输入有误,请重新输入:n"); ch=getch(); } switch(ch) { case '1': map_x(9,3); break; case '2': map_x(16,4); break; case '3': map_x(25,5); break; case '4': printf("您已退出游戏,请下次再玩!n"); exit(0); break; } return 0; } //对于确定的循环一般用for;而不确定的循环则用while. 以上内容由PHP站长网【52php.cn】收集整理供大家参考研究 如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |