欢迎来到天天文库
浏览记录
ID:49758784
大小:41.50 KB
页数:6页
时间:2020-03-04
《2048 c语言源代码.doc》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、#include#include#include#include#include#defineN4intgrid[N][N]={0};intD=0;intM=2048;//显示voidshowdata(){inti,j;system("CLS");for(i=0;i2、if(grid[i][j]!=0)printf("%5d",grid[i][j]);elseprintf("%5c",'');printf("%5c",'+');printf("");}for(i=0;i3、){intr,c,x;x=rand()%2*2+2;do{r=rand()%N;c=rand()%N;}while(grid[r][c]!=0);grid[r][c]=x;}//获取最大值intgetMax(){inti,j,max=0;for(i=0;i4、)for(j=0;j=1&&grid[cr][j]!=0){if(grid[cr-1][j]==0)//上方有空位,上移{grid[cr-1][j]=grid[cr][j];grid[cr][j]=0;F=1;}else//上方无空位{if(grid[cr-1][j]==grid[cr][j]&&w==0)//相等,相加{grid[cr-1][j]=grid[cr-1][j]*2;grid[cr][j]=0;w=1;F=1;}else//不等{break;}}cr-5、-;}}}if(D==2)//down{for(i=N-2;i>=0;i--)for(j=0;j6、=1;F=1;}else//不等{break;}}cr++;}}}if(D==3)//left{for(i=0;i=1&&grid[i][cr]!=0){if(grid[i][cr-1]==0)//左方有空位,左移{grid[i][cr-1]=grid[i][cr];grid[i][cr]=0;F=1;}else//左方无空位{if(grid[i][cr-1]==grid[i][cr]&&w==0)//相等,相加{grid[i][cr-1]=grid[i][7、cr-1]*2;grid[i][cr]=0;w=1;F=1;}else//不等{break;}}cr--;}}}if(D==4)//right{for(i=0;i=0;j--){cr=j;w=0;while(cr<=N-2&&grid[i][cr]!=0){if(grid[i][cr+1]==0)//右方有空位,右移{grid[i][cr+1]=grid[i][cr];grid[i][cr]=0;F=1;}else//右方无空位{if(grid[i][cr+1]==grid[i][cr]&&w==0)/8、/相等,相加{grid[i][cr+1]=grid[i][cr+1]*2;grid[i][cr]=0;w=1;F=1;}else//不等{break;}}cr++;}}}retu
2、if(grid[i][j]!=0)printf("%5d",grid[i][j]);elseprintf("%5c",'');printf("%5c",'+');printf("");}for(i=0;i3、){intr,c,x;x=rand()%2*2+2;do{r=rand()%N;c=rand()%N;}while(grid[r][c]!=0);grid[r][c]=x;}//获取最大值intgetMax(){inti,j,max=0;for(i=0;i4、)for(j=0;j=1&&grid[cr][j]!=0){if(grid[cr-1][j]==0)//上方有空位,上移{grid[cr-1][j]=grid[cr][j];grid[cr][j]=0;F=1;}else//上方无空位{if(grid[cr-1][j]==grid[cr][j]&&w==0)//相等,相加{grid[cr-1][j]=grid[cr-1][j]*2;grid[cr][j]=0;w=1;F=1;}else//不等{break;}}cr-5、-;}}}if(D==2)//down{for(i=N-2;i>=0;i--)for(j=0;j6、=1;F=1;}else//不等{break;}}cr++;}}}if(D==3)//left{for(i=0;i=1&&grid[i][cr]!=0){if(grid[i][cr-1]==0)//左方有空位,左移{grid[i][cr-1]=grid[i][cr];grid[i][cr]=0;F=1;}else//左方无空位{if(grid[i][cr-1]==grid[i][cr]&&w==0)//相等,相加{grid[i][cr-1]=grid[i][7、cr-1]*2;grid[i][cr]=0;w=1;F=1;}else//不等{break;}}cr--;}}}if(D==4)//right{for(i=0;i=0;j--){cr=j;w=0;while(cr<=N-2&&grid[i][cr]!=0){if(grid[i][cr+1]==0)//右方有空位,右移{grid[i][cr+1]=grid[i][cr];grid[i][cr]=0;F=1;}else//右方无空位{if(grid[i][cr+1]==grid[i][cr]&&w==0)/8、/相等,相加{grid[i][cr+1]=grid[i][cr+1]*2;grid[i][cr]=0;w=1;F=1;}else//不等{break;}}cr++;}}}retu
3、){intr,c,x;x=rand()%2*2+2;do{r=rand()%N;c=rand()%N;}while(grid[r][c]!=0);grid[r][c]=x;}//获取最大值intgetMax(){inti,j,max=0;for(i=0;i4、)for(j=0;j=1&&grid[cr][j]!=0){if(grid[cr-1][j]==0)//上方有空位,上移{grid[cr-1][j]=grid[cr][j];grid[cr][j]=0;F=1;}else//上方无空位{if(grid[cr-1][j]==grid[cr][j]&&w==0)//相等,相加{grid[cr-1][j]=grid[cr-1][j]*2;grid[cr][j]=0;w=1;F=1;}else//不等{break;}}cr-5、-;}}}if(D==2)//down{for(i=N-2;i>=0;i--)for(j=0;j6、=1;F=1;}else//不等{break;}}cr++;}}}if(D==3)//left{for(i=0;i=1&&grid[i][cr]!=0){if(grid[i][cr-1]==0)//左方有空位,左移{grid[i][cr-1]=grid[i][cr];grid[i][cr]=0;F=1;}else//左方无空位{if(grid[i][cr-1]==grid[i][cr]&&w==0)//相等,相加{grid[i][cr-1]=grid[i][7、cr-1]*2;grid[i][cr]=0;w=1;F=1;}else//不等{break;}}cr--;}}}if(D==4)//right{for(i=0;i=0;j--){cr=j;w=0;while(cr<=N-2&&grid[i][cr]!=0){if(grid[i][cr+1]==0)//右方有空位,右移{grid[i][cr+1]=grid[i][cr];grid[i][cr]=0;F=1;}else//右方无空位{if(grid[i][cr+1]==grid[i][cr]&&w==0)/8、/相等,相加{grid[i][cr+1]=grid[i][cr+1]*2;grid[i][cr]=0;w=1;F=1;}else//不等{break;}}cr++;}}}retu
4、)for(j=0;j=1&&grid[cr][j]!=0){if(grid[cr-1][j]==0)//上方有空位,上移{grid[cr-1][j]=grid[cr][j];grid[cr][j]=0;F=1;}else//上方无空位{if(grid[cr-1][j]==grid[cr][j]&&w==0)//相等,相加{grid[cr-1][j]=grid[cr-1][j]*2;grid[cr][j]=0;w=1;F=1;}else//不等{break;}}cr-
5、-;}}}if(D==2)//down{for(i=N-2;i>=0;i--)for(j=0;j6、=1;F=1;}else//不等{break;}}cr++;}}}if(D==3)//left{for(i=0;i=1&&grid[i][cr]!=0){if(grid[i][cr-1]==0)//左方有空位,左移{grid[i][cr-1]=grid[i][cr];grid[i][cr]=0;F=1;}else//左方无空位{if(grid[i][cr-1]==grid[i][cr]&&w==0)//相等,相加{grid[i][cr-1]=grid[i][7、cr-1]*2;grid[i][cr]=0;w=1;F=1;}else//不等{break;}}cr--;}}}if(D==4)//right{for(i=0;i=0;j--){cr=j;w=0;while(cr<=N-2&&grid[i][cr]!=0){if(grid[i][cr+1]==0)//右方有空位,右移{grid[i][cr+1]=grid[i][cr];grid[i][cr]=0;F=1;}else//右方无空位{if(grid[i][cr+1]==grid[i][cr]&&w==0)/8、/相等,相加{grid[i][cr+1]=grid[i][cr+1]*2;grid[i][cr]=0;w=1;F=1;}else//不等{break;}}cr++;}}}retu
6、=1;F=1;}else//不等{break;}}cr++;}}}if(D==3)//left{for(i=0;i=1&&grid[i][cr]!=0){if(grid[i][cr-1]==0)//左方有空位,左移{grid[i][cr-1]=grid[i][cr];grid[i][cr]=0;F=1;}else//左方无空位{if(grid[i][cr-1]==grid[i][cr]&&w==0)//相等,相加{grid[i][cr-1]=grid[i][
7、cr-1]*2;grid[i][cr]=0;w=1;F=1;}else//不等{break;}}cr--;}}}if(D==4)//right{for(i=0;i=0;j--){cr=j;w=0;while(cr<=N-2&&grid[i][cr]!=0){if(grid[i][cr+1]==0)//右方有空位,右移{grid[i][cr+1]=grid[i][cr];grid[i][cr]=0;F=1;}else//右方无空位{if(grid[i][cr+1]==grid[i][cr]&&w==0)/
8、/相等,相加{grid[i][cr+1]=grid[i][cr+1]*2;grid[i][cr]=0;w=1;F=1;}else//不等{break;}}cr++;}}}retu
此文档下载收益归作者所有