为了正常的体验网站,请在浏览器设置里面开启Javascript功能!
首页 > 拼图游戏代码

拼图游戏代码

2018-03-16 8页 doc 21KB 10阅读

用户头像

is_882336

暂无简介

举报
拼图游戏代码拼图游戏代码 #include #include #include #include #include #include void HideCursor() { CONSOLE_CURSOR_INFO cursor_info={1,0}; SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info); } void GotoXY(int y,int x) //光标定位 { HANDLE hout; COORD coord; ...
拼图游戏代码
拼图游戏代码 #include #include #include #include #include #include void HideCursor() { CONSOLE_CURSOR_INFO cursor_info={1,0}; SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info); } void GotoXY(int y,int x) //光标定位 { HANDLE hout; COORD coord; coord.X=x; coord.Y=y; hout = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleCursorPosition(hout,coord); } int s[20][20],map[20][20]; int x,y; void swap(int *a,int *b) { int c=*a; *a=*b; *b=c; } int F(int i,int j,int n) //逆序数 { int k=s[i][j]; int ans = 0; for(;j<=n;j++) if(k>s[i][j]) ans++; i++; for(;i<=n;i++) for(j=1;j<=n;j++) if(k>s[i][j]) ans++; return ans; } void ok(int n,int ans) //判断是否可完成 { int i,j; for(i=1;i<=n;i++) for(j=1;j<=n;j++) { ans = (ans+F(i,j,n))%2; } if(ans%2) //不可完成对调一块 if(s[1][1]==0) swap(&s[1][2],&s[1][3]); else if(s[1][2] == 0) swap(&s[1][1],&s[1][3]); else swap(&s[1][1],&s[1][2]); } void mk(int n) //打乱拼图 { srand(time(0)); int i,j,a,b,t; for (i=n;i>0;i--) for (j=n;j>0;j--) { a=rand()%i+1; b=rand()%j+1; swap(&s[a][b],&s[i][j]); } for(i=1;i<=n;i++) for(j=1;j<=n;j++) if(s[i][j]==0) {x=i;y=j;} } void input(int n) //初始化 { int i,j,k,a,b,t; for (i=1;i<=n;i++) for (j=1;j<=n;j++) { s[i][j]=(i-1)*n+j-1; map[i][j]=s[i][j]+1; } map[n][n]=0; } int ins(int n) //判断是否完成拼图 { int i,j; for(i=1;i<=n;i++) for(j=1;j<=n;j++) if(s[i][j]!=map[i][j]) return 0; return 1; } void move(int ex,int ey) //移动操作 { GotoXY(ex-1,ey*3-3); printf(" "); GotoXY(x-1,y*3-3); printf("%-3d",s[ex][ey]); s[x][y]=s[ex][ey]; x=ex;y=ey; } void output(int n) //输出 { int i,j,k; system("cls"); for(i=1;i<=n;i++) { for(j=1;j<=n;j++) printf(s[i][j]?"%-3d":" ",s[i][j]); printf(" "); for(j=1;j<=n;j++) printf(map[i][j]?"%-3d":" ",map[i][j]); printf("\n"); } printf("\n\n\n\n\n操作:w/?:上 a/?:左 s/?:下 d/右:右 Esc:重新开始\n"); } void des() //说明 { printf("\t本游戏为数字拼图1.2,在w,a,s,d基础\n\ 上增加方向键控制数字板移动\n\n\ 界面很简陋,仅有控制台,本人没接触到图形界面\n\n\ 其中数字代表可移动板,空格代表可移动位置\n\n\ 游戏不可完成性BUG基本消除\n\n\ 按任意键返回..."); if((char)getch()<0) getch(); system("cls"); } int main() { SetConsoleTitle("拼图游戏"); system("mode con cols=55 lines=24"); HideCursor(); int i,j,t,k,key; char ch; begin: printf("\t\t##拼图游戏欢迎您的使用##\n\n"); printf("请输入指令 1:游戏介绍 2:开始游戏 Esc:退出游戏\n"); ch=getch(); system("cls"); if(ch==' ') return 0; else if(ch=='1') {des();goto begin;} else if(ch=='2') while(1) { t=1; printf("请输入拼图大小(3~7) Esc:上一步\n"); ch=getch(); if(ch==' ') {system("cls");goto begin;} k=ch-'0'; if(21) move(x-1,y);else t--;break; case 'd':case 77:case 'D':if(y>1) move(x,y-1);else t--;break; case ' ':system("cls"); goto begin; default :t--;break; } s[x][y]=0; if(ins(k)) { GotoXY(k+1,k/2); printf("****G O O D !****\n 共用%d步\n\n 按任意键继续...\n\n",t); if((char)getch()<0) getch(); system("cls"); break; } else {GotoXY(k+2,1);printf("已用%d步\n",t++);} } } else {goto begin;} return 0; }
/
本文档为【拼图游戏代码】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。 本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。 网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。

历史搜索

    清空历史搜索