2048

· · 个人记录

#include <iostream>
#include <conio.h>
#include <cstdio>
#include <random>
#include <ctime>

#define TRACK_LOST 2221
#define TRACK_COMPLETE 7200
#define FULL_RECALL 12000
#define PURE_MEMORY 17200

using namespace std;

mt19937 rng(time(0));

int map[8][8]={};


//操作:bool 并(int direction=1,2,3,4),随机生成2/4(80%/20%)
//

void init();//Done
void randGenerate();//Done
void settle();//Track Lost/Complete,Full Recall,Pure Memory//Done
void display();//Done
void rotate(int (&a)[8][8], int r);//顺时针旋转 (r*90d) 
void rotateOnce(int (&a)[8][8]);

bool merge(int dir, int (&a)[8][8], bool ask=false);

int pts;

int main()
{
    cout<<"WELCOME TO 2048!\nUse WASD(Do not use the CapsLock) to control.\n";
    system("pause");
    system("cls");
    init();
    char op='\0';
    while(1)
    {
        bool flag=false;
        do
        {
            op=_getch();
        }
        while(op!='w' && op!='a' && op!='s' && op!='d');
        switch (op)
        {
            case 'w':
                flag=merge(1,map);
                break;
            case 'a':
                flag=merge(2,map);
                break;
            case 's':
                flag=merge(3,map);
                break;
            case 'd':
                flag=merge(4,map);
                break;
            default:
                cout<<"ERR: Illegal Input!";
                return 0;
        }
        system("cls");
        if(flag)
        {
            randGenerate();
        }
        display();
        cout<<endl;
        int tmp[8][8]={};
        bool c=0;
        for(int i=1; i<=4; i++)
        {
            for(int j=1; j<=4; j++)
            {
                tmp[i][j]=map[i][j];
                if(map[i][j]==0)
                {
                    c=true;
                }
            }
        }
        bool f=(merge(1,tmp,1) || merge (2,tmp,1) || merge(3,tmp,1) || merge(4,tmp,1));
        if(c)
        {
            f=true;
        }
        if(!f)
        {
            cout<<"Game Over!\nFinal Score: ";
            settle(); 
            return 0;
        }
        else
        {
            cout<<endl<<"Score: "<<pts<<endl;
        }
    }
    return 0;
}


void init()
{
    randGenerate();
    randGenerate();
    display();
    cout<<"\nScore: 0\n";
    return;
}

void display()
{
    for(int i=1; i<=4; i++)
    {
        for(int j=1; j<=4; j++)
        {
            printf("%6d", map[i][j]);
        }
        cout<<endl;
    }
    return;
}

void randGenerate()
{
    int cnt=0;
    for(int i=1; i<=4; i++)
    {
        for(int j=1; j<=4; j++)
        {
            if(map[i][j]==0)
            {
                cnt++;  
            }
        } 
    }
    if(cnt!=0)
    {
        int rnd=rng()%cnt+1;
        int d=rng()%10;
        d=1<<(d/9+1);
        int tmp=0;
        for(int i=1; i<=4; i++)
        {
            for(int j=1; j<=4; j++)
            {
                if(map[i][j]==0)
                {
                    tmp++;
                }
                if(tmp==rnd)
                {
                    map[i][j]=d;
                    return;
                }
            }
        }
    }
    else
    {

    }
    return;
}

void settle()
{
    printf("%03d'%03d\n", pts/1000,pts%1000);
    if(pts<TRACK_LOST)
    {
        cout<<"ptt-=13.28"<<endl; 
    }
    else if(pts>=TRACK_LOST && pts<TRACK_COMPLETE)
    {
        cout<<"< TRACK LOST >" <<endl;
    }
    else if(pts>=TRACK_COMPLETE && pts<FULL_RECALL)
    {
        cout<<"< TRACK COMPLETE >"<<endl;
    }
    else if(pts>=FULL_RECALL && pts<PURE_MEMORY)
    {
        cout<<"< FULL RECALL >"<<endl;
    }
    else
    {
        cout<<"< PURE MEMORY >"<<endl;
    }
    system("pause");
    return;
}

bool merge(int dir, int (&a)[8][8], bool ask)
{
    //bool ischanged=false;
    int tmp2[8][8]={};
    for(int i=1; i<=4; i++)
    {
        for(int j=1; j<=4; j++)
        {
            tmp2[i][j]=a[i][j];
        }
    }
    rotate(tmp2,dir);
    for(int i=1; i<=4; i++)
    {
        int not0=0;
        int tmp[8]={};
        for(int j=1; j<=4; j++)
        {
            if(tmp2[i][j])
            {
                tmp[++not0]=tmp2[i][j];
            }
        }
        if(not0==0 || not0==1)
        {
            tmp2[i][4]=tmp[not0];
            tmp2[i][1]=tmp2[i][2]=tmp2[i][3]=0;
        }
        else if(not0==2)
        {
            if(tmp[1]==tmp[2])
            {
//              ischanged=true;
                if(!ask)
                {
                    pts+=tmp[1]<<1;
                }
                tmp2[i][4]=tmp[1]<<1;
                tmp2[i][1]=tmp2[i][2]=tmp2[i][3]=0;
            }
            else
            {
                tmp2[i][4]=tmp[2];
                tmp2[i][3]=tmp[1];
                tmp2[i][1]=tmp2[i][2]=0;
            }
        }
        else if(not0==3)
        {
            if(tmp[2]==tmp[3])
            {
//              ischanged=true;
                if(!ask)
                {
                    pts+=tmp[2]<<1;
                }
                tmp2[i][4]=tmp[2]<<1;
                tmp2[i][3]=tmp[1];
                tmp2[i][1]=tmp2[i][2]=0;
            }
            else if(tmp[1]==tmp[2])
            {
//              ischanged=true;
                if(!ask)
                {
                    pts+=tmp[1]<<1;
                }
                tmp2[i][4]=tmp[3];
                tmp2[i][3]=tmp[1]<<1;
                tmp2[i][1]=tmp2[i][2]=0;
            }
            else
            {
                tmp2[i][4]=tmp[3];
                tmp2[i][3]=tmp[2];
                tmp2[i][2]=tmp[1];
                tmp2[i][1]=0;
            }
        }
        else//not0==4
        {
            if(tmp[3]==tmp[4])
            {
//              ischanged=true;
                if(!ask)
                {
                    pts+=tmp[3]<<1;
                }
                tmp2[i][4]=tmp[3]<<1;
                tmp2[i][3]=0;
                if(tmp[1]==tmp[2])
                {
//                  ischanged=true;
                    if(!ask)
                    {
                        pts+=tmp[1]<<1;
                    }
                    tmp2[i][3]=tmp[1]<<1;
                    tmp2[i][1]=tmp2[i][2]=0;
                }
                else
                {
                    tmp2[i][3]=tmp[2];
                    tmp2[i][2]=tmp[1];
                    tmp2[i][1]=0;
                }
            }
            else if(tmp[2]==tmp[3])
            {
//              ischanged=true;
                if(!ask)
                {
                    pts+=tmp[2]<<1;
                }
                tmp2[i][4]=tmp[4];
                tmp2[i][3]=tmp[2]<<1;
                tmp2[i][2]=tmp[1];
                tmp2[i][1]=0;
            }
            else if(tmp[1]==tmp[2])
            {
//              ischanged=true;
                if(!ask)
                {
                    pts+=tmp[1]<<1;
                }
                tmp2[i][4]=tmp[4];
                tmp2[i][3]=tmp[3];
                tmp2[i][2]=tmp[1]<<1;
                tmp2[i][1]=0;
            }
            else
            {
                tmp2[i][4]=tmp[4];
                tmp2[i][3]=tmp[3];
                tmp2[i][2]=tmp[2];
                tmp2[i][1]=tmp[1];
            }
        }
    }
    rotate(tmp2,4-dir);
    bool ischanged=false;
    for(int i=1; i<=4; i++)
    {
        for(int j=1; j<=4; j++)
        {
            if(tmp2[i][j]!=a[i][j])
            {
                ischanged=true;
            }
            a[i][j]=tmp2[i][j];
        }
    }
//  for(int i=1; i<=4; i++)
//  {
//      for(int j=1; j<=4; j++)
//      {
//          a[i][j]=tmp2[i][j];
//      }
//  }
    return ischanged;
}


void rotate(int (&a)[8][8], int r)//顺时针旋转 (r*90d)
{
    for(int i=1; i<=r; i++)
    {
        rotateOnce(a);
    }
    return;
}

void rotateOnce(int (&a)[8][8])
{
    int tmp[8][8]={};
    tmp[1][1]=a[4][1];
    tmp[1][2]=a[3][1];
    tmp[1][3]=a[2][1];
    tmp[1][4]=a[1][1];
    tmp[2][1]=a[4][2];
    tmp[2][2]=a[3][2];
    tmp[2][3]=a[2][2];
    tmp[2][4]=a[1][2];
    tmp[3][1]=a[4][3];
    tmp[3][2]=a[3][3];
    tmp[3][3]=a[2][3];
    tmp[3][4]=a[1][3];
    tmp[4][1]=a[4][4];
    tmp[4][2]=a[3][4];
    tmp[4][3]=a[2][4];
    tmp[4][4]=a[1][4];
    for(int i=1; i<=4; i++)
    {
        for(int j=1; j<=4; j++)
        {
            a[i][j]=tmp[i][j];
        }
    }
    return;
}

//4 4 4 4
//0 0 8 8