来一发2048!

· · 个人记录

无聊弄的小游戏

蒟蒻也只能做做这样的简单小游戏了==

WASD控制

#include<bits/stdc++.h>
#include<conio.h>
using namespace std;
#define random(x) (rand()%x)

int m[6][6]={0};
int x,y,sum=0,hard;
char c;
bool moved,fir=false;

void show(){
    if(fir) system("cls");
    cout<<"\n       score: "<<sum<<endl;
    cout<<"---------------------\n";//21个 
    for(int i=1;i<=4;i++){
        cout<<'|';
        for(int j=1;j<=4;j++){
            if(m[i][j]==0) cout<<"    |";//每格有4位; 
            else{
                if(m[i][j]<10) cout<<"  "<<m[i][j]<<" |";
                else if(m[i][j]<100) cout<<" "<<m[i][j]<<" |";
                else if(m[i][j]<1000) cout<<m[i][j]<<" |";
                else cout<<m[i][j]<<"|";
            }
        }
        cout<<"\n---------------------\n";
    }
}

void mix(int x1,int y1,int x,int y){//将x1,y1插入x,y; 
    if(m[x1][y1]==m[x][y]){
        m[x][y]*=2;
        sum+=m[x][y];
        m[x1][y1]=0;
        moved=true;
    }
}

void move(int p){
    moved=false;
    if(p==1){
        for(int j=1;j<=4;j++){
            for(int i=1;i<=4;i++){
                if(m[i][j]!=0){
                    int i1=i;
                    while(i1>1){
                        if(m[i1-1][j]!=0){
                            mix(i1,j,i1-1,j);
                            break;
                        }
                        m[i1-1][j]=m[i1][j];
                        m[i1][j]=0;
                        i1--;
                        moved=true;
                    }
                }
            }
        }
    }
    if(p==2){
        for(int j=1;j<=4;j++){
            for(int i=4;i>=1;i--){
                if(m[i][j]!=0){
                    int i1=i;
                    while(i1<4){
                        if(m[i1+1][j]!=0){
                            mix(i1,j,i1+1,j);
                            break;
                        }
                        m[i1+1][j]=m[i1][j];
                        m[i1][j]=0;
                        i1++;
                        moved=true;
                    }
                }
            }
        }
    }
    if(p==3){
        for(int i=1;i<=4;i++){
            for(int j=1;j<=4;j++){
                if(m[i][j]!=0){
                    int j1=j;
                    while(j1>1){
                        if(m[i][j1-1]!=0){
                            mix(i,j1,i,j1-1);
                            break;
                        }
                        m[i][j1-1]=m[i][j1];
                        m[i][j1]=0;
                        j1--;
                        moved=true;
                    }
                }
            }
        }
    }
    if(p==4){
        for(int i=1;i<=4;i++){
            for(int j=4;j>=1;j--){
                if(m[i][j]!=0){
                    int j1=j;
                    while(j1<4){
                        if(m[i][j1+1]!=0){
                            mix(i,j1,i,j1+1);
                            break;
                        }
                        m[i][j1+1]=m[i][j1];
                        m[i][j1]=0;
                        j1++;
                        moved=true;
                    }
                }
            }
        }
    }
    if(moved){
        srand((int)time(0));
        x=random(4)+1;
        y=random(4)+1;
        while(m[x][y]!=0){
            x=random(4)+1;
            y=random(4)+1;
        }
        m[x][y]=(random(2)+1)*2;
        show();
    }
}

bool win(){
    for(int i=1;i<=4;i++){
        for(int j=1;j<=4;j++){
            if(m[i][j]>=hard){
                return true;
            }
        }
    }
    return false;
}

bool die(){
    for(int i=1;i<=4;i++){
        for(int j=1;j<=4;j++){
            if(m[i][j]==0) return false;
            if(m[i][j]==m[i][j-1]||m[i][j]==m[i][j+1]||m[i-1][j]==m[i][j]||m[i][j]==m[i+1][j]) return false;
        } 
    }
    return true;
}

int main(){
    printf("__________2 0 4 8__________\n________made by tyin________\n____Use WASD To Control____\n");
    srand((int)time(0));//初始2个值; 
    x=random(4)+1;
    y=random(4)+1;
    m[x][y]=2;
    x=random(4)+1;
    y=random(4)+1;
    m[x][y]=4;
    show();
    fir=true;
    hard=2048;
    while(1){
        c=getch();
        if(c=='w')
            move(1);
        if(c=='s')
            move(2);
        if(c=='a')
            move(3);
        if(c=='d')
            move(4);
//      if(c<='9'&&c>='0')//外挂系统; 
//          m[1][1]=m[1][2]=pow(4,(c-'0'));

        if(win()){
            cout<<"\nYou Win!!!\nWith the score of "<<sum;
            cout<<"\nDo you want to try "<<hard*2<<" ?\n";
            cout<<"pass c to continue (or pass any key to stop)\n";
            c=getch();
            if(c=='c'){
                system("cls");
                hard*=2;
                show();
            }else return 0;
        }
        if(die()){
            cout<<"\ngame over!\nyour score is "<<sum;
            break;
        }
    }
    cout<<"\n\npass p to stop the game";
    while(c!='p') c=getch(); 
}