游戏2048c语言代码

上传人:回**** 文档编号:121146118 上传时间:2022-07-18 格式:DOCX 页数:10 大小:11.80KB
返回 下载 相关 举报
游戏2048c语言代码_第1页
第1页 / 共10页
游戏2048c语言代码_第2页
第2页 / 共10页
游戏2048c语言代码_第3页
第3页 / 共10页
点击查看更多>>
资源描述
#include#include#include#includeint code44= 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0; /游戏中的16个格子int temp5;/中间变量int move=0;/*移动次数*/int score=0;/*分数*/void print()/*显示游戏界面*/ int i,j; system(cls);/*清屏*/ system(color b8); printf(2048n); printf(u-UP l-LEFT d-DOWN r-RIGHT 0-EXITn); printf(Score:%d Move:%dn,score,move); printf(|-|n);/*显示横向分隔线*/ for(i=0; i=3; i+) for(j=0; j=3; j+) if(codeij=0) printf(| );/*0显示空格*/ else printf(|%4d,codeij);/*显示数字和分隔线*/ printf(|n|-|n);/*显示横向分隔线*/ int add()/*对中间变量数组进行解决*/ int i; int t=0; int change=0;/*判断数组与否有变化,0不变,1变化*/ do for(i=0; i=3; i+) if(tempi=0) if(tempi!=tempi+1) change=1;/*当一种0背面不是0时数组变化*/ tempi=tempi+1; tempi+1=0; /*去掉中间的0*/ t+; while(t=3); /*反复多次*/ for(i=1; i=3; i+) if(tempi=tempi-1) if(tempi!=0) change=1;/*当两个非零相似的数相加时数组变化*/ score=score+tempi; tempi-1=tempi-1*2; tempi=0; /*把两个相邻的相似的数加起来*/ do for(i=0; i=3; i+) if(tempi=0) tempi=tempi+1; tempi+1=0; /*去掉中间的0*/ t+; while(t=1)/*仅当数发生变化时添加新数*/ do i=(unsigned)rand()%4; j=(unsigned)rand()%4; while(codeij!=0); if(unsigned)rand()%4=0) codeij=4; else codeij=2;/*随机选一种空格填上2或4*/ move+;/*增长次数*/ print();/*显示*/ input=getch();/*输入方向*/ change=0; switch(input) case 0:/*退出*/ printf(Are you sure to exit?(y/n); input=getchar(); if(input=y|input=Y) exit(0); break; case U: case u:/*上*/ for(j=0; j=3; j+) for(i=0; i=3; i+) tempi=codeij;/*把一列数移到中间变量*/ temp4=0; change=change+add(); for(i=0; i=3; i+) codeij=tempi;/*把解决好的中间变量移回来*/ break; case L: case l:/*左*/ for(i=0; i=3; i+) for(j=0; j=3; j+) tempj=codeij;/*把一行数移到中间变量*/ temp4=0; change=change+add(); for(j=0; j=3; j+) codeij=tempj;/*把解决好的中间变量移回来*/ break; case D: case d:/*下*/ for(j=0; j=3; j+) for(i=0; i=3; i+) tempi=code3-ij;/*把一列数移到中间变量*/ temp4=0; change=change+add(); for(i=0; i=3; i+) code3-ij=tempi;/*把解决好的中间变量移回来*/ break; case R: case r:/*右*/ for(i=0; i=3; i+) for(j=0; j=3; j+) tempj=codei3-j;/把一行数移到中间变量 temp4=0; change=change+add(); for(j=0; j=3; j+) codei3-j=tempj;/*把解决好的中间变量移回来*/ break; gameover=1; for(i=0; i=3; i+) for(j=0; j=3; j+) if(codeij=0) gameover=0;/*所有格子都填满则游戏结束*/ printf(Game over!n); getch(); return 0;
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 办公文档 > 解决方案


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!