2048小游戏

· · 休闲·娱乐

c++ 2048 awa

↑↓←→按键控制

更新日志:

20241128彩色版添加restart功能

20241129跟同机房大牢学了个压行(雾)

#include<bits/stdc++.h>
#include<windows.h>#include<wincon.h>
#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000)?1:0)
#define FOR(i,a,b) for(ll (i)=(a);(i)<=(b);(i)++)
using namespace std;typedef long long ll;struct color{void pw(const char *s){printf("\033[0m\033[1;40m%s\033[0m",s);}void pr(const char *s){printf("\033[0m\033[1;31m%s\033[0m",s);}void pg(const char *s){printf("\033[0m\033[1;32m%s\033[0m",s);}void py(const char *s){printf("\033[0m\033[1;33m%s\033[0m",s);}void pb(const char *s){printf("\033[0m\033[1;34m%s\033[0m",s);}void pp(const char *s){printf("\033[0m\033[1;35m%s\033[0m",s);}void pc(const char *s){printf("\033[0m\033[1;36m%s\033[0m",s);}}CLR;ll side_len,maxn,mapp[12][12],Modetype,hard,hell,hcon,havehole,memi,memj;ll maxnow=2,maxlen=1,cnt0,tmpnow=0,tmplen;unsigned lencon;string S="-",Q="?",E,H=" ";ll sdd[8]={0,2,4,6,8,10,12,14},colornum=16384,usecolor=0,cnt=0,t=0;ll diri[]={-1,1,0,0},dirj[]={0,0,-1,1};bool flag,hole[12][12],go;void colorset(ll x){if(colornum < maxnow){colornum=maxnow;ll temp=maxnow,len=0;while(temp)len++,temp/=2;FOR(i,1,7)sdd[i]=(ll)((1.0*len/7.0)*i);}if(x<=(1ll<<sdd[1]))printf("\033[0m\033[1;40m%*lld\033[0m ",lencon,x);else if(x<=(1ll<<sdd[2]))printf("\033[0m\033[1;33m%*lld\033[0m ",lencon,x);else if(x<=(1ll<<sdd[3]))printf("\033[0m\033[1;36m%*lld\033[0m ",lencon,x);else if(x<=(1ll<<sdd[4]))printf("\033[0m\033[1;32m%*lld\033[0m ",lencon,x);else if(x<=(1ll<<sdd[5]))printf("\033[0m\033[1;34m%*lld\033[0m ",lencon,x);else if(x<=(1ll<<sdd[6]))printf("\033[0m\033[1;31m%*lld\033[0m ",lencon,x);else printf("\033[0m\033[1;35m%*lld\033[0m ",lencon,x);}void formcontrol(ll x,ll i,ll j){if(hole[i][j])printf("\033[0m\033[7;40m%s\033[0m",H.c_str());else if((hard&&memi==i&&memj==j&&!hell||hell&&hcon)&&Modetype&&!go){if(hell&&!x)printf("\033[0m\033[1;40m%s\033[0m ",S.c_str());else if(usecolor){if(x<=(1ll<<sdd[1]))printf("\033[0m\033[1;40m%s\033[0m ",Q.c_str());else if(x<=(1ll<<sdd[2]))printf("\033[0m\033[1;33m%s\033[0m ",Q.c_str());else if(x<=(1ll<<sdd[3]))printf("\033[0m\033[1;36m%s\033[0m ",Q.c_str());else if(x<=(1ll<<sdd[4]))printf("\033[0m\033[1;32m%s\033[0m ",Q.c_str());else if(x<=(1ll<<sdd[5]))printf("\033[0m\033[1;34m%s\033[0m ",Q.c_str());else if(x<=(1ll<<sdd[6]))printf("\033[0m\033[1;31m%s\033[0m ",Q.c_str());else printf("\033[0m\033[1;35m%s\033[0m ",Q.c_str());}else printf("\033[0m\033[1;40m%s\033[0m ",Q.c_str());}else if(x){if(usecolor)colorset(x);else printf("%*lld ",lencon,x);}else printf("\033[0m\033[1;40m%s\033[0m ",S.c_str());CLR.pw("|");}void printboard(){if (havehole){maxnow=2,S="-",Q="?",H=" ";FOR(i,1,side_len)FOR(j,1,side_len)maxnow=max(maxnow,mapp[i][j]);}ll tmplen=0,tmpnum=maxnow;if(maxnow!=tmpnow||havehole){while(tmpnum)tmplen++,tmpnum/=10;lencon=maxlen=max(tmplen,maxlen);tmpnow=maxnow;if(havehole)lencon=maxlen=tmplen;while(S.length()<maxlen)S=' '+S;while(Q.length()<maxlen)Q=' '+Q;while(H.length()<maxlen+1)H+=' ';}system("cls");CLR.pw("GAMEMODE: ");if(!Modetype)if(usecolor)CLR.py("Computer ");else printf("Computer ");if(!hard&&!hell&&!havehole&&Modetype)if(usecolor)CLR.py("Normal ");else printf("Normal ");if(hell)if(usecolor)CLR.pp("Hell ");else printf("Hell ");if(hard)if(usecolor)CLR.pr("Gambler ");else printf("Gambler ");if(havehole)if(usecolor)CLR.pb("Hole ");else printf("Hole ");printf ("\n");FOR(i,1,side_len){CLR.pw("|");FOR(j,1,side_len)formcontrol(mapp[i][j],i,j);printf("\n");}printf("%lld * %lld, %lld is probable. MAX = ",side_len,side_len,(1ll<<maxn));if(usecolor)colorset(maxnow);else printf("%lld",maxnow);printf ("\n");if(usecolor){printf ("                   %sCOLOR : ",E.c_str());CLR.pw("%");CLR.py("%");CLR.pc("%");CLR.pg("%");CLR.pb("%");CLR.pr("%");CLR.pp("%");}}void merge(ll d){ll td=d/2*2+((d%2)^1);FOR(i,1,side_len)FOR(j,1,side_len){bool del=0;ll x,y,ox,oy;if(d==0)ox=x=i,oy=y=j;else if(d==1)ox=x=side_len-i+1,oy=y=j;else if(d==2)ox=x=j,oy=y=i;else if(d==3)ox=x=j,oy=y=side_len-i+1;if(!mapp[x][y])continue;while(0<x+diri[td]&&x+diri[td]<=side_len&&0<y+dirj[td]&&y+dirj[td]<=side_len){x+=diri[td],y+=dirj[td];if(hole[x][y])del=1;else if(!mapp[x][y])continue;else if(mapp[x][y]==mapp[ox][oy]){if(!del){mapp[ox][oy]<<=1,mapp[x][y]=0,flag=true,cnt0++,maxnow=max(maxnow,mapp[ox][oy]);break;}else{mapp[x][y]=0,cnt0++,flag=true;break;}}else break;}x=ox,y=oy;while(0<x+diri[d]&&x+diri[d]<=side_len&&0<y+dirj[d]&&y+dirj[d]<=side_len&&mapp[x+diri[d]][y+dirj[d]]==0){x+=diri[d],y+=dirj[d],flag=true;if(hole[x][y]){mapp[ox][oy]=0,cnt0++,flag=true;break;}}swap(mapp[ox][oy],mapp[x][y]);}}void randnum(bool typ){if(hell)hcon=rand()%4;if(havehole&&cnt0==side_len%2+1)return;ll tmp=1ll<<(rand()%maxn+1);if(!typ)maxnow=max(maxnow,tmp);while(1){ll i=rand()%side_len+1,j=rand()%side_len+1;if(hard&&!typ)memi=i,memj=j;if(!mapp[i][j]&&!hole[i][j]){mapp[i][j]=(typ?2:tmp);return;}}}bool movable(){FOR(i,1,side_len)FOR(j,1,side_len)FOR(d,0,3){ll ni=i+diri[d],nj=j+dirj[d];if (0<ni&&ni<=side_len&&0<nj&&nj<=side_len&&(mapp[i][j]==mapp[ni][nj]||!mapp[ni][nj]))return true;}return false;}void gameover(){go=true,printboard();if(usecolor)CLR.pr("\rGAME_OVER...");else printf("\rGAME_OVER...");printf("\nPress 'R' to restart.");while(1){Sleep(50);if(KEY_DOWN('R')) return;if(KEY_DOWN('C')){usecolor^=1;printboard();if(usecolor)CLR.pr("\rGAME_OVER...");else printf("\rGAME_OVER...");printf("\nPress 'R' to restart.");}}}void person(){while(1){flag=false;printboard();if(usecolor)CLR.pg("\rRunning...");else printf("\rRunning...");if(!cnt0)if(!movable()){gameover();return;}while(1){Sleep(50);bool press=false;if(KEY_DOWN('R')){gameover();return;}if(KEY_DOWN('C')){usecolor^=1,printboard();if(usecolor)CLR.pg("\rRunning...");else printf("\rRunning...");}else if(KEY_DOWN(VK_UP))press=true,merge(0);else if(KEY_DOWN(VK_DOWN))press=true,merge(1);else if(KEY_DOWN(VK_LEFT))press=true,merge(2);else if(KEY_DOWN(VK_RIGHT))press=true,merge(3);if(press&&flag)break;}printboard();if(usecolor)CLR.pg("\rRunning...");else printf("\rRunning...");Sleep(100),randnum(0),cnt0--;}}void computer(ll dir){while(1){flag=false;printboard();if(usecolor)CLR.pg("\rRunning...");else printf("\rRunning...");if(!cnt0)if(!movable()){gameover();return;}if(KEY_DOWN('R')){gameover();return;}dir+1>=4?dir=0:++dir;if(dir==0)merge(1);if(dir==1)merge(2);if(dir==2)merge(0);if(dir==3)merge(3);Sleep(10);printboard();if(usecolor)CLR.pg("\rRunning...");else printf("\rRunning...");if(flag)randnum(0),cnt0--;if(KEY_DOWN(VK_SPACE)){printboard();if(usecolor)CLR.py("\rSuspend...");else printf("\rSuspend...");Sleep(100);while(1){Sleep(50);if(KEY_DOWN('C')){usecolor^=1,printboard();if(usecolor)CLR.py("\rSuspend...");else printf("\rSuspend...");}if(KEY_DOWN(VK_SPACE))break;if(KEY_DOWN('R')){gameover();return;}} Sleep(100);}if(KEY_DOWN('C')){usecolor^=1,printboard();if(usecolor)CLR.pg("\rRunning...");else printf("\rRunning...");}}}void running(){printf("Loading[                ]...\rLoading[");for(ll i=1;i<=16;i++){Sleep(50);printf("/");}system("cls");randnum(1),randnum(1);Modetype?person():computer(0);}void base_set(){if(!t)Sleep(1000);printf("You can press 'R' to restart while 'Running'.\n");if(!t)Sleep(2000);CLR.pr("But\n");if(!t)Sleep(2000);printf("Don't try to input anything but "),CLR.pg("integer"),printf(" while setting following options.\n\n");if(!t++)Sleep(3000);while(1){printf("Player  ('1' for yourself, '0' for computer)\rPlayer");scanf("%lld",&Modetype);if(Modetype==1||Modetype==0)break;else printf("Unaccepted number ?u?\n");}Modetype?printf("\n"):printf("You can press 'Space' to suspend while running.\n\n");CLR.pw("%");CLR.py("%");CLR.pc("%");CLR.pg("%");CLR.pb("%");CLR.pr("%");CLR.pp("%\n");while(1){printf("Use colored board?   ('1' for true, '0' for false)\rUse colored board? ");scanf("%lld", &usecolor);if(usecolor==1||usecolor==0)break;else printf("Unaccepted number ?u?\n");}printf("You can press 'C' for switching board type while running.\n\n");while(1){printf("Length of the side : ");scanf("%lld", &side_len);printf("\n");if(2<=side_len&&side_len<=10)break;else printf({"The length may not suitble for human. X_X\n"});}cnt0=side_len*side_len-2;while(1){printf("Maximum random number = 2 ^ ");scanf("%lld", &maxn);printf("\n");if(1<=maxn)break;else printf({"Be realistic. @w@\n"});}ll tmp=(1ll<<maxn);while(tmp)tmp/=10,E+=' ';if(Modetype){printf("Do you wanna a 'black' hole at the centre of your board?\n");while(1){printf("   ('1' for true, '0' for false)\r ");scanf("%lld",&havehole);printf ("\n");if(havehole==1||havehole==0)break;else printf("Unaccepted number ?u?\n");}if(havehole){hole[(side_len/2)+1][(side_len/2)+1]=true;hole[(side_len+1)/2][(side_len+1)/2]=true;}printf("If you are a gambler, new random number would be replaced by a '?' temporarily.\n");while(1){cnt++;if(cnt>3){printf("\nTest my patience is the behave of a gambler. (╯▔ 皿▔ )╯\n"),hell=1;Sleep(2000),CLR.pr("So you are not only a gambler!(Whole board replaced by '?' with 75%)\n"),Sleep(5000);break;}if(cnt==1)CLR.pw("So are you a gambler?   ('1' for true, '0' for false)\rSo are you a gambler? ");else if(cnt==2)CLR.pr("So are you a gambler?   ('1' for true, '0' for false)\rSo are you a gambler? ");else CLR.pp("So are you a gambler?   ('1' for true, '0' for false)\rSo are you a gambler? ");scanf("%lld",&hard);if(hard==1||hard==0)break;else CLR.py("Unaccepted number ?u?\n");}}}void reset(){ CLR.py("\nCreated By ");CLR.pc("XYHLYCHRIS \n\n");side_len=maxn=Modetype=hard=hell=hcon=havehole=memi=memj=cnt0=tmpnow=tmplen=lencon=usecolor=cnt=flag=go=0;maxnow=2,maxlen=1,colornum=16384;S="-",Q="?",E="",H=" ";FOR(i,0,7)sdd[i]=(i<<1);memset(mapp,0,sizeof mapp),memset(hole,0,sizeof hole);}int main(){srand((ll)time(0));while(1){system("cls"),reset(),base_set(),running(),system("cls"),system("pause");}return 0;}
//和同机房大佬学的压行法(= # w # =)*

啊?你们好奇为什么要压行?当然是便于你们复制啦~~~/

20241130修复hole模式更新判断问题,优化部分运行逻辑,彩色版和极简版同步更新。

代码:

彩色但是跑的man一些

#include<bits/stdc++.h>
#include<windows.h>
#include<wincon.h>
#ifdef __cplusplus
extern "C" {
#endif
BOOL WINAPI GetCurrentConsoleFont(HANDLE hConsoleOutput,BOOL bMaximumWindow,PCONSOLE_FONT_INFO lpConsoleCurrentFont);
#ifdef __cplusplus
}
#endif
using namespace std; typedef long long ll;
#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1 : 0)
#define FOR(i, a, b) for(ll (i)=(a);(i)<=(b);(i)++)
struct color{
    void pw(const char *s) {printf("\033[0m\033[1;40m%s\033[0m", s);}
    void pr(const char *s) {printf("\033[0m\033[1;31m%s\033[0m", s);}
    void pg(const char *s) {printf("\033[0m\033[1;32m%s\033[0m", s);}
    void py(const char *s) {printf("\033[0m\033[1;33m%s\033[0m", s);}
    void pb(const char *s) {printf("\033[0m\033[1;34m%s\033[0m", s);}
    void pp(const char *s) {printf("\033[0m\033[1;35m%s\033[0m", s);}
    void pc(const char *s) {printf("\033[0m\033[1;36m%s\033[0m", s);}
} CLR;
ll side_len, maxn, mapp[12][12], Modetype, hard, hell, hcon, havehole, memi, memj;
ll maxnow = 2, maxlen = 1, cnt0, tmpnow = 0, tmplen; unsigned lencon; string S = "-", Q = "?", E, H = " ";
ll sdd[8] = {0, 2, 4, 6, 8, 10, 12, 14}, colornum = 16384, usecolor = 0, cnt = 0, t = 0;
ll diri[] = {-1, 1, 0, 0}, dirj[] = {0, 0, -1, 1};
bool flag, hole[12][12], go;
void colorset(ll x) {
    if (colornum < maxnow) { colornum = maxnow;
        ll temp = maxnow, len = 0;
        while (temp) len++, temp /= 2;
        FOR(i, 1, 7) sdd[i] = (ll)((1.0 * len / 7.0) * i);
    }
    if      (x <= (1ll << sdd[1])) printf ("\033[0m\033[1;40m%*lld\033[0m ", lencon, x);
    else if (x <= (1ll << sdd[2])) printf ("\033[0m\033[1;33m%*lld\033[0m ", lencon, x);
    else if (x <= (1ll << sdd[3])) printf ("\033[0m\033[1;36m%*lld\033[0m ", lencon, x);
    else if (x <= (1ll << sdd[4])) printf ("\033[0m\033[1;32m%*lld\033[0m ", lencon, x);
    else if (x <= (1ll << sdd[5])) printf ("\033[0m\033[1;34m%*lld\033[0m ", lencon, x);
    else if (x <= (1ll << sdd[6])) printf ("\033[0m\033[1;31m%*lld\033[0m ", lencon, x);
    else                           printf ("\033[0m\033[1;35m%*lld\033[0m ", lencon, x);
}
void formcontrol(ll x, ll i, ll j) {
    if (hole[i][j]) printf ("\033[0m\033[7;40m%s\033[0m", H.c_str());
    else if ((hard && memi == i && memj == j && !hell || hell && hcon) && Modetype && !go) {
        if (hell && !x) printf ("\033[0m\033[1;40m%s\033[0m ", S.c_str());
        else if (usecolor) {
            if      (x <= (1ll << sdd[1])) printf ("\033[0m\033[1;40m%s\033[0m ", Q.c_str());
            else if (x <= (1ll << sdd[2])) printf ("\033[0m\033[1;33m%s\033[0m ", Q.c_str());
            else if (x <= (1ll << sdd[3])) printf ("\033[0m\033[1;36m%s\033[0m ", Q.c_str());
            else if (x <= (1ll << sdd[4])) printf ("\033[0m\033[1;32m%s\033[0m ", Q.c_str());
            else if (x <= (1ll << sdd[5])) printf ("\033[0m\033[1;34m%s\033[0m ", Q.c_str());
            else if (x <= (1ll << sdd[6])) printf ("\033[0m\033[1;31m%s\033[0m ", Q.c_str());
            else                           printf ("\033[0m\033[1;35m%s\033[0m ", Q.c_str());
        } else printf ("\033[0m\033[1;40m%s\033[0m ", Q.c_str());
    }
    else if (x) {if(usecolor)colorset(x); else printf("%*lld ", lencon, x);}
    else printf ("\033[0m\033[1;40m%s\033[0m ", S.c_str()); CLR.pw("|");
}
void printboard() {
    if (havehole) { maxnow = 2, S = "-", Q = "?", H = " ";
        FOR(i, 1, side_len) FOR(j, 1, side_len) maxnow = max(maxnow, mapp[i][j]);}
    ll tmplen = 0, tmpnum = maxnow;
    if (maxnow != tmpnow || havehole) {while (tmpnum) tmplen++, tmpnum /= 10;
        lencon = maxlen = max(tmplen, maxlen); tmpnow = maxnow;
        if (havehole) lencon = maxlen = tmplen;
        while (S.length() < maxlen) S = ' ' + S;
        while (Q.length() < maxlen) Q = ' ' + Q;
        while (H.length() < maxlen + 1) H += ' ';}
    system ("cls"); CLR.pw("GAMEMODE: ");
    if (!Modetype) if(usecolor)CLR.py("Computer ");else printf("Computer ");
    if (!hard&&!hell&&!havehole&&Modetype) if(usecolor)CLR.py("Normal ");else printf("Normal ");
    if (hell) if(usecolor)CLR.pp("Hell ");else printf ("Hell ");
    if (hard) if(usecolor)CLR.pr("Gambler ");else printf ("Gambler ");
    if (havehole) if(usecolor)CLR.pb("Hole ");else printf ("Hole "); printf ("\n");
    FOR(i, 1, side_len) { CLR.pw("|");
        FOR(j, 1, side_len) formcontrol(mapp[i][j], i, j);
    printf ("\n");}
    printf ("%lld * %lld, %lld is probable. MAX = ", side_len, side_len, (1ll << maxn));
    if (usecolor) colorset(maxnow); else printf("%lld", maxnow); printf ("\n");
    if (usecolor) {printf ("                   %sCOLOR : ", E.c_str());
CLR.pw("%");CLR.py("%");CLR.pc("%");CLR.pg("%");CLR.pb("%");CLR.pr("%");CLR.pp("%");}
}
void merge(ll d) {
    ll td = d / 2 * 2 + ((d % 2) ^ 1);
    FOR(i, 1, side_len) FOR(j, 1, side_len) {
        bool del = 0; ll x, y, ox, oy;
        if (d == 0) ox = x = i, oy = y = j;
        else if (d == 1) ox = x = side_len - i + 1, oy = y = j;
        else if (d == 2) ox = x = j, oy = y = i;
        else if (d == 3) ox = x = j, oy = y = side_len - i + 1;
        if (!mapp[x][y]) continue;
        while (0 < x + diri[td] && x + diri[td] <= side_len
            && 0 < y + dirj[td] && y + dirj[td] <= side_len) {
             x += diri[td], y += dirj[td];
             if (hole[x][y]) del = 1;
             else if (!mapp[x][y]) continue;
             else if (mapp[x][y] == mapp[ox][oy]) {
                if (!del) {mapp[ox][oy] <<= 1, mapp[x][y] = 0, flag = true,
                cnt0++, maxnow = max(maxnow, mapp[ox][oy]); break;}
                else {mapp[x][y] = 0, cnt0++, flag = true; break;}}
             else break;
        } x = ox, y = oy;
        while (0 < x + diri[d] && x + diri[d] <= side_len
            && 0 < y + dirj[d] && y + dirj[d] <= side_len
            && mapp[x + diri[d]][y + dirj[d]] == 0) {
             x += diri[d], y += dirj[d], flag = true;
             if (hole[x][y]) {mapp[ox][oy] = 0, cnt0++, flag = true; break;}
        } swap(mapp[ox][oy], mapp[x][y]);
    }
}
void randnum(bool typ) {
    if (hell) hcon = rand() % 4;
    if (havehole && cnt0 == side_len % 2 + 1) return;
    ll tmp = 1ll << (rand() % maxn + 1); if (!typ) maxnow = max(maxnow, tmp);
    while (1) {ll i = rand() % side_len + 1, j = rand() % side_len + 1;
        if (hard && !typ) memi = i, memj = j;
        if (!mapp[i][j] && !hole[i][j]) {mapp[i][j] = (typ? 2: tmp); return;}
    }
}
bool movable() {
    FOR(i, 1, side_len)FOR(j, 1, side_len)FOR(d, 0, 3){
        ll ni = i + diri[d], nj = j + dirj[d];
        if (0 < ni && ni <= side_len && 0 < nj && nj <= side_len
            &&(mapp[i][j] == mapp[ni][nj] || !mapp[ni][nj])) return true;
    } return false;
}
void gameover() { go = true, printboard();
    if(usecolor) CLR.pr ("\rGAME_OVER...");else printf("\rGAME_OVER...");printf("\nPress 'R' to restart.");
    while (1) {Sleep(50); if (KEY_DOWN('R')) return; if (KEY_DOWN('C')) {usecolor ^= 1; printboard();
    if(usecolor)CLR.pr("\rGAME_OVER...");else printf("\rGAME_OVER...");printf("\nPress 'R' to restart.");}}
}
void person() {
    while (1) { flag = false;
        printboard(); if(usecolor)CLR.pg("\rRunning...");else printf("\rRunning...");
        if (!cnt0) if (!movable()) {gameover(); return;}
        while (1) { Sleep(50);
            bool press = false; if(KEY_DOWN('R')){gameover(); return;}
            if (KEY_DOWN('C')) {usecolor ^= 1, printboard();
            if(usecolor)CLR.pg("\rRunning...");else printf("\rRunning...");}
            else if (KEY_DOWN(VK_UP)) press = true, merge(0);
            else if (KEY_DOWN(VK_DOWN)) press = true, merge(1);
            else if (KEY_DOWN(VK_LEFT)) press = true, merge(2);
            else if (KEY_DOWN(VK_RIGHT)) press = true, merge(3);
        if (press && flag) break;}
        printboard(); if(usecolor)CLR.pg("\rRunning...");else printf("\rRunning...");
        Sleep(100), randnum(0), cnt0--;
    }
}
void computer(ll dir) {
    while (1) { flag = false;
        printboard(); if(usecolor)CLR.pg("\rRunning...");else printf("\rRunning...");
        if (!cnt0) if (!movable()) {gameover(); return;}
        if(KEY_DOWN('R')){gameover(); return;}
        dir + 1 >= 4? dir = 0: ++dir;
        if (dir == 0) merge(1); if (dir == 1) merge(2); if (dir == 2) merge(0); if (dir == 3) merge(3);
        Sleep(10); printboard(); if(usecolor)CLR.pg("\rRunning...");else printf("\rRunning...");
        if (flag) randnum(0), cnt0--;
        if (KEY_DOWN(VK_SPACE)) {
            printboard(); if(usecolor)CLR.py("\rSuspend...");else printf("\rSuspend...");;
            Sleep(100); while (1) { Sleep(50);
                if (KEY_DOWN('C')){usecolor ^= 1, printboard();
                if (usecolor)CLR.py("\rSuspend...");else printf("\rSuspend...");}
                if (KEY_DOWN(VK_SPACE)) break; if(KEY_DOWN('R')){gameover(); return;}
            } Sleep(100);
        }
        if(KEY_DOWN('C')){usecolor ^= 1,printboard();if(usecolor)CLR.pg("\rRunning...");else printf("\rRunning...");}
    }
}
void running() {
    printf ("Loading[                ]...\rLoading[");
    for (ll i = 1; i <= 16; i++) {
        Sleep(50);printf ("/");
    } system("cls");
    randnum(1), randnum(1);
    Modetype? person(): computer(0);
}
void base_set() { if(!t) Sleep(1000);
    printf("You can press 'R' to restart while 'Running'.\n");if(!t)Sleep(2000);CLR.pr("But\n");if(!t)Sleep(2000);
    printf("Don't try to input anything but "),CLR.pg("integer"),printf(" while setting following options.\n\n");
    if(!t++)Sleep(3000); while (1) {
        printf ("Player  ('1' for yourself, '0' for computer)\rPlayer"); scanf("%lld", &Modetype);
        if (Modetype == 1 || Modetype == 0) break; else printf ("Unaccepted number ?u?\n");
    } Modetype? printf("\n"): printf("You can press 'Space' to suspend while running.\n\n");
CLR.pw("%");CLR.py("%");CLR.pc("%");CLR.pg("%");CLR.pb("%");CLR.pr("%");CLR.pp("%\n");
    while (1) {
        printf ("Use colored board?   ('1' for true, '0' for false)\rUse colored board? "); scanf("%lld", &usecolor);
        if (usecolor == 1 || usecolor == 0) break; else printf ("Unaccepted number ?u?\n");
    } printf ("You can press 'C' for switching board type while running.\n\n");
    while (1) {
        printf ("Length of the side : "); scanf ("%lld", &side_len); printf("\n");
        if (2 <= side_len && side_len <= 10) break; else printf({"The length may not suitble for human. X_X\n"});
    } cnt0 = side_len * side_len - 2;
    while (1) {
        printf ("Maximum random number = 2 ^ "); scanf("%lld", &maxn); printf("\n");
        if (1 <= maxn) break; else printf({"Be realistic. @w@\n"});
    } ll tmp = (1ll << maxn); while (tmp) tmp /= 10, E += ' ';
    if (Modetype) {
        printf ("Do you wanna a 'black' hole at the centre of your board?\n");
        while (1) {
            printf ("   ('1' for true, '0' for false)\r "); scanf("%lld", &havehole); printf ("\n");
            if (havehole== 1 || havehole == 0) break; else printf ("Unaccepted number ?u?\n");
        }
        if (havehole) {hole[(side_len / 2) + 1][(side_len / 2) + 1] = true;
            hole[(side_len + 1) / 2][(side_len + 1) / 2] = true;}
        printf ("If you are a gambler, new random number would be replaced by a '?' temporarily.\n");
        while (1) { cnt++;
            if (cnt > 3) {
                printf ("\nTest my patience is the behave of a gambler. (╯▔ 皿▔ )╯\n"), hell = 1;
                Sleep(2000), printf("So you are not only a gambler!(Whole board replaced by '?' with 75%)\n"),Sleep(5000);
            break;} if (cnt == 1) CLR.pw("So are you a gambler?   ('1' for true, '0' for false)\rSo are you a gambler? ");
            else if (cnt == 2) CLR.pr("So are you a gambler?   ('1' for true, '0' for false)\rSo are you a gambler? ");
            else CLR.pp("So are you a gambler?   ('1' for true, '0' for false)\rSo are you a gambler? ");
            scanf("%lld", &hard); if (hard == 1 || hard == 0) break; else CLR.py("Unaccepted number ?u?\n");
        }
    }
}
void reset() { CLR.py("\nCreated By ");CLR.pc("XYHLYCHRIS \n\n");
    side_len=maxn=Modetype=hard=hell=hcon=havehole=memi=memj=cnt0=tmpnow=tmplen=lencon=usecolor=cnt=flag=go=0;
    maxnow = 2, maxlen = 1, colornum = 16384; S = "-", Q = "?", E = "", H = " "; FOR(i, 0, 7) sdd[i] = (i << 1);
    memset(mapp,0,sizeof mapp),memset(hole,0,sizeof hole);
}
int main() {
    srand((ll)time(0));while(1){system("cls"), reset(), base_set(), running(), system("cls"), system("pause");}
    return 0;
}

白色但是滚筒洗衣机 #w#*(computer模式转的飞快)

#include<bits/stdc++.h>
#include<windows.h>
#include<wincon.h>
#ifdef __cplusplus
extern "C" {
#endif
BOOL WINAPI GetCurrentConsoleFont(HANDLE hConsoleOutput,BOOL bMaximumWindow,PCONSOLE_FONT_INFO lpConsoleCurrentFont);
#ifdef __cplusplus
}
#endif
#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1 : 0)
using namespace std;
typedef long long ll;
ll side_len, maxn, mapp[12][12], Modetype;
ll maxnow = 0, maxlen = 1, cnt0, tmpnow, tmplen; unsigned lencon; string S = "-";
ll diri[] = {-1, 1, 0, 0}, dirj[] = {0, 0, -1, 1};
bool flag;
void base_set() {
    printf ("\nCreated By XYHLYCHRIS \n\n");
    while (1) {
        printf ("Player  ('1' for yourself, '0' for computer)");
        printf ("\rPlayer"); scanf("%lld", &Modetype);
        if (Modetype == 1 || Modetype == 0) break;
        else printf ("Unaccepted number ?u?\n");
    }
    Modetype? printf("\n"): printf("You can press Space to suspend while running.\n\n");
    while (1) {
        printf ("Length of the side : "); 
        scanf ("%lld", &side_len); printf("\n");
        if (2 <= side_len && side_len <= 10) break;
        else printf({"The length may not suitble for human. X_X\n"});
    } cnt0 = side_len * side_len - 2;
    while (1) {
        printf ("Maximum random number = 2 ^ ");
        scanf("%lld", &maxn); printf("\n");
        if (2 <= maxn && maxn < side_len * side_len) break;
        else printf({"Be realistic. @w@\n"});
    }
}
void formcontrol(ll x) {if (x) printf("%*lld |", lencon, x); else printf ("%s |", S.c_str());}
void printboard() {
    ll tmplen = 0, tmpnum = maxnow;
    if (maxnow > tmpnow) {while (tmpnum) tmplen++, tmpnum /= 10;
        lencon = maxlen = max(tmplen, maxlen); tmpnow = maxnow;
        while (S.length() < maxlen) S = ' ' + S;}
    system ("cls");
    for (ll i = 1; i <= side_len; i++) {
        printf ("|");
        for (ll j = 1; j <= side_len; j++) formcontrol(mapp[i][j]);
        printf ("\n");
    }
    printf ("%lld * %lld, %lld is probable. MAX = %lld",
            side_len, side_len, (1ll << maxn), maxnow);
}
bool movable() {
    for (ll i = 1; i <= side_len; i++) {
        for (ll j = 1; j <= side_len; j++) {
            for (ll d = 0; d < 4; d++) {
                ll ni = i + diri[d], nj = j + dirj[d];
                if (0 < ni && ni <= side_len
                 && 0 < nj && nj <= side_len
                 && (mapp[i][j] == mapp[ni][nj] || !mapp[ni][nj])) return true;
            }
        }
    } return false;
}
void merge(ll d) {
    ll td = d / 2 * 2 + ((d % 2) ^ 1);
    for (ll i = 1; i <= side_len; i++) {
        for (ll j = 1; j <= side_len; j++) {
            ll x, y, ox, oy;
            if (d == 0) ox = x = i, oy = y = j;
            else if (d == 1) ox = x = side_len - i + 1, oy = y = j;
            else if (d == 2) ox = x = j, oy = y = i;
            else if (d == 3) ox = x = j, oy = y = side_len - i + 1;
            if (!mapp[x][y]) continue;
            while (0 < x + diri[td] && x + diri[td] <= side_len
                && 0 < y + dirj[td] && y + dirj[td] <= side_len) {
                 x += diri[td], y += dirj[td];
                 if (!mapp[x][y]) continue;
                 else if (mapp[x][y] == mapp[ox][oy]) {
                    mapp[ox][oy] <<= 1, mapp[x][y] = 0, flag = true,
                    cnt0++, maxnow = max(maxnow, mapp[ox][oy]); break;}
                 else break;
            } x = ox, y = oy;
            while (0 < x + diri[d] && x + diri[d] <= side_len
                && 0 < y + dirj[d] && y + dirj[d] <= side_len
                && mapp[x + diri[d]][y + dirj[d]] == 0) {
                 x += diri[d], y += dirj[d], flag = true;
            } swap(mapp[ox][oy], mapp[x][y]);
        }
    }
}
void randnum(bool typ) {
    ll tmp = 1ll << (rand() % maxn + 1); if (!typ) maxnow = max(maxnow, tmp);
    while (1) {
        ll i = rand() % side_len + 1, j = rand() % side_len + 1;
        if (!mapp[i][j]) {
            mapp[i][j] = (typ? 2: tmp);
            break;
        }
    }
}
void gameover() {printboard(), printf ("\nGAME_OVER..."), Sleep(1000000000000000000);}
void person() {
    while (1) {
        flag = false;
        printboard(), printf ("\nRunning...");
        if (!cnt0) if (!movable()) {gameover(); return;}
        while (1) {
            Sleep(50);
            bool press = false;
            if (KEY_DOWN(VK_UP)) press = true, merge(0);
            else if (KEY_DOWN(VK_DOWN)) press = true, merge(1);
            else if (KEY_DOWN(VK_LEFT)) press = true, merge(2);
            else if (KEY_DOWN(VK_RIGHT)) press = true, merge(3);
            if (press && flag) break;
        }
        printboard(), printf ("\nRunning...");
        Sleep(100), randnum(0), cnt0--;
    }
}
void computer(ll dir) {
    while (1) {
        flag = false;
        printboard(), printf ("\nRunning...");
        if (!cnt0) if (!movable()) {gameover(); return;}
        dir + 1 >= 4? dir = 0: ++dir;
        if (dir == 0) merge(1);
        if (dir == 1) merge(2);
        if (dir == 2) merge(0);
        if (dir == 3) merge(3);
        Sleep(8);
        if (flag) randnum(0), cnt0--;
        if (KEY_DOWN(VK_SPACE)) {
            printboard(), printf ("\nSuspend...");
            Sleep(100); while (1) {
                Sleep(50);
                if (KEY_DOWN(VK_SPACE)) break;
            } Sleep(100);
        }
    }
}
void running() {
    printf ("Loading[                ]...");
    for (ll i = 1; i <= 16; i++) { Sleep(50); printf ("\rLoading[");
        for (ll j = 1; j <= i; j++) printf ("/");
    } system("cls");
    srand((ll)time(0));
    for (ll i = 1; i <= 2; i++) randnum(1);
    Modetype? person(): computer(0);
}
int main() {
    system("cls"), base_set(), running();
    return 0;
}