2048游戏 (C++ Windows)

采用了 (getch())(Sleep()) 优化游戏体验

#include<bits/stdc++.h>
#include<conio.h>
#include<windows.h>
#define rep(a,b,c) for(int c=(a);c<=(b);++c)
#define drep(a,b,c) for(int c=(a);c>=(b);--c)
#define REP rep(1,4,i)rep(1,4,j)
using namespace std;
int a[6][6],q[6][6],mxx,sc;
char p[6];
inline void ps(){rep(0,4,i)putchar(p[i]);}
inline void pn(int k)
{
    int a=log10(k)+1;
    if(!k)
    {
        p[0]=p[1]=p[2]=p[3]=p[4]=' ';return;
    }
    switch(a)
    {
        case 1:
        {
            p[0]=p[1]=p[3]=p[4]=' ';
            p[2]=k+'0';return;
        }
        case 2:
        {
            p[0]=p[1]=p[4]=' ';p[2]=int(k/10)+'0';
            p[3]=k%10+'0';return;
        }
        case 3:
        {
            p[0]=p[4]=' ';
            int qwq=3;
            while(k>0)p[qwq--]=k%10+'0',k/=10;return;
        }
        case 4:
        {
            p[0]=' ';
            int qwq=4;
            while(k>0)p[qwq--]=k%10+'0',k/=10;return;
        }
        case 5:
        {
            drep(4,0,i)p[i]=k%10,k/=10;return;
        }
    }
}

//
inline void print()
{
//  res=0;
    system("cls");
    puts("_________________________");
    puts("|     |     |     |     |");
    putchar('|');rep(1,4,i)pn(a[1][i]),ps(),putchar('|');puts("");
    puts("|_____|_____|_____|_____|");
    puts("|     |     |     |     |");
    putchar('|');rep(1,4,i)pn(a[2][i]),ps(),putchar('|');puts("");
    puts("|_____|_____|_____|_____|");
    puts("|     |     |     |     |");
    putchar('|');rep(1,4,i)pn(a[3][i]),ps(),putchar('|');puts("");
    puts("|_____|_____|_____|_____|");
    puts("|     |     |     |     |");
    putchar('|');rep(1,4,i)pn(a[4][i]),ps(),putchar('|');puts("");
    puts("|_____|_____|_____|_____|");
    printf("
 Your Score is : ___  %d 
 Your Max Square is ___ %d 
",sc,mxx);
}

//

inline void debug(){puts("
");rep(1,4,i){rep(1,4,j)cout<<a[i][j];puts("");}puts("");}
inline void d(){puts("");rep(1,4,i){rep(1,4,j)cout<<q[i][j];puts("");}puts("");}

//

inline bool eql(){rep(1,4,i)rep(1,4,j)if(a[i][j]!=q[i][j])return 0;return 1;}
inline bool empty(){rep(1,4,i)rep(1,4,j)if(!a[i][j])return 1;return 0;}

//

inline bool check()
{
    rep(1,4,i)rep(1,4,j)
    {
        if(!a[i][j])return 1;
        if(a[i][j]==a[i][j+1]||a[i][j]==a[i+1][j])return 1;
    }return 0;
}

//

inline void Rnd()
{
    if(empty())
    {
        int x=rand()%4+1,y=rand()%4+1;
        while(a[x][y])x=rand()%4+1,y=rand()%4+1;
        a[x][y]=(int(rand()%6)==0)?4:2;
    }
}

//

inline void tidy()
{
//  d();
    int j,k,b[6];
    rep(1,4,i)
    {
        rep(1,4,qwq)b[qwq]=0;
        j=1;k=4;while(j<=k)
        {
            if(!q[i][j])
            {   
                rep(j,k,l)q[i][l]=q[i][l+1];k--;

            }
            else if(q[i][j-1]==q[i][j]&&!b[j-1])
            {   
                sc+=q[i][j-1]<<1;
                rep(j,k,l)q[i][l]=q[i][l+1];
                q[i][j-1]*=2;k--;
            }
            else j++;
        }
    }
}

//

inline void rotate()
{
    int tmp[6][6];
    REP tmp[i][j]=q[i][j];
    REP q[i][j]=tmp[j][5-i];
}
inline void move_right()
{
    REP q[i][j]=a[i][j];
    rotate();rotate();tidy();
    rotate();rotate();
    bool jud=!empty()||!eql();
    REP a[i][j]=q[i][j];
    print();Sleep(80);
    if(jud){Rnd();}print();
}
inline void move_left()
{

    REP q[i][j]=a[i][j];tidy();
    bool jud=!empty()||!eql();
    REP a[i][j]=q[i][j];
    print();Sleep(80);
    if(jud){Rnd();}print();
}
inline void move_up()
{
    REP q[i][j]=a[i][j];
    rotate();tidy();
    rep(1,3,qwq)rotate();
    bool jud=!empty()||!eql();
    REP a[i][j]=q[i][j];
    print();Sleep(80);
    if(jud){Rnd();}print();
}
inline void move_down()
{
    puts("");
    REP q[i][j]=a[i][j];
    rep(1,3,qwq)rotate();
    tidy();
    rotate();
    bool jud=!empty()||!eql();
    REP a[i][j]=q[i][j];
    print();Sleep(80);
    if(jud){Rnd();}print();
}

//
int main()
{
    std::ios::sync_with_stdio(false);
    srand(time(0));
    memset(a,0,sizeof(a));
    char ch;
    Rnd();Rnd();
    print();
    while(check())
    {
        ch=getch();
        if(ch=='w')move_up();
        if(ch=='s')move_down();
        if(ch=='a')move_left();
        if(ch=='d')move_right();
        rep(1,4,i)rep(1,4,j)mxx=max(mxx,a[i][j]);
//      if(mx==131072)main();
    }
    puts("

You DIE !!!");
    Sleep(1000);
    puts("Press any key to replay : ______________");
    Sleep(1000);
    getch();
    main();
}
//Good luck
原文地址:https://www.cnblogs.com/tale365/p/14314525.html