2048
随手写个2048全当练模拟了...上下左右,WASD都是可以的...你的操作会保存在 log.txt 中...
顺手贴代码.
#include<cstdio> #include<conio.h> #include<cstring> #include<cstdlib> #include<iostream> using namespace std; const int N = 4; const int M = 16; const int MUL = 8221; int seq; int w[M]; FILE *fout=fopen("log.txt","w"); void initSeed(int seed){ seq = seed; } int GetRand(){ return (seq = (seq * MUL) + (seq >> 16)) & 15; } //1 w 2 s 3 a 4 d int Read(char ch=getch()){ if(isascii(ch)){ switch(ch){ case 'w':case 'W':return 1; case 's':case 'S':return 2; case 'a':case 'A':return 3; case 'd':case 'D':return 4; } }else{ ch=getch(); switch(ch){ case 72:return 1; case 75:return 3; case 77:return 4; case 80:return 2; } }return -1; } void GetW(int x=GetRand()){ for(;w[x];x=GetRand());w[x]=2; } void Up(int a[]){ int u[5],b[5]; for(int i=0,j,c,t;i<4;i++){ memset(u,0,sizeof(u)),memset(b,0,sizeof(b)); for(c=0,j=i;j<16;j+=4) if(a[j]) b[++c]=a[j],a[j]=0; for(j=1;j<=c;j++) if(!u[j]) if(b[j] == b[j+1]) b[j]+=b[j+1],u[j+1]=1; for(j=1,t=i;j<=c;j++) if(!u[j]) a[t]=b[j],t+=4; } } void Down(int a[]){ int u[5],b[5]; for(int i=0,j,c,t;i<4;i++){ memset(u,0,sizeof(u)),memset(b,0,sizeof(b)); for(c=0,j=i+12;j>=0;j-=4) if(a[j]) b[++c]=a[j],a[j]=0; for(j=1;j<=c;j++) if(!u[j] && b[j]) if(b[j] == b[j+1]) b[j]+=b[j+1],u[j+1]=1; for(j=1,t=i+12;j<=c;j++) if(!u[j]) a[t]=b[j],t-=4; } } void Left(int a[]){ int u[5],b[5]; for(int i=0,j,c,t;i<16;i+=4){ memset(u,0,sizeof(u)),memset(b,0,sizeof(b)); for(c=0,j=i;j<i+4;j++) if(a[j]) b[++c]=a[j],a[j]=0; for(j=1;j<=c;j++) if(!u[j] && b[j]) if(b[j] == b[j+1]) b[j]+=b[j+1],u[j+1]=1; for(j=1,t=i;j<=c;j++) if(!u[j]) a[t]=b[j],t++; } } void Right(int a[]){ int u[5],b[5]; for(int i=0,j,c,t;i<16;i+=4){ memset(u,0,sizeof(u)),memset(b,0,sizeof(b)); for(c=0,j=i+3;j>=i;j--) if(a[j]) b[++c]=a[j],a[j]=0; for(j=1;j<=c;j++) if(!u[j] && b[j]) if(b[j] == b[j+1]) b[j]+=b[j+1],u[j+1]=1; for(j=1,t=i+3;j<=c;j++) if(!u[j]) a[t]=b[j],t--; } } void Copy(int a[],int b[]){ for(int i=0;i<16;i++) b[i]=a[i]; } int check(int a[],int b[]){ for(int i=0;i<16;i++) if(a[i]!=b[i]) return 1;return 0; } void Out(){ system("cls"); for(int i=1;i<=8;i++) putchar('\n'); for(int i=0;i<M;i++){ if(!w[i]) printf(" -%c"," \n"[(i+1)%4 == 0]); else printf("%5d%c",w[i]," \n"[(i+1)%4 == 0]); } } int Over(){ int t[M]; Copy(w,t),Up(t);if(check(w,t)) return 0; Copy(w,t),Down(t);if(check(w,t)) return 0; Copy(w,t),Left(t);if(check(w,t)) return 0; Copy(w,t),Right(t);if(check(w,t)) return 0; return 1; } void Print(char c){ fprintf(fout,"%c",c); } void Play(int x){ initSeed(x),GetW(),GetW(),Out(); int cp[M]; for(;;){ int od=Read(),f=0; switch(od){ case 1:Copy(w,cp),Up(cp);if(check(w,cp)) Up(w),f=1,Print('U');break; case 2:Copy(w,cp),Down(cp);if(check(w,cp)) Down(w),f=1,Print('D');break; case 3:Copy(w,cp),Left(cp);if(check(w,cp)) Left(w),f=1,Print('L');break; case 4:Copy(w,cp),Right(cp);if(check(w,cp)) Right(w),f=1,Print('R');break; } if(f) GetW(); if(!Over()) Out(); else{ puts("You lose!");return; } } } int main(){ for(int x;;){ cin>>x; Play(x); }return 0; }