我做了一个十分垃圾的魔法版本2048

· · 个人记录

废话不多说,上代码

#include<bits/stdc++.h>
#include <windows.h>

using namespace std;

//defines
int a[5][5]={0};
int score=0;
int magicnt=10;
int wl=0;
char ans;
string name="F4Per";
string du;

//functions

void starting(){
    //start page
    system("cls");
    system("color EC");
    system("title magic2048");
    cout<<"------------------------------------------------------------------"<<endl;
    cout<<"|    #########     ##########     ##             #########    |"<<endl;
    cout<<"|           ##     ##      ##     ##   ##        ##      #    |"<<endl;
    cout<<"|           ##     ##      ##     ##   ##        ##      #    |"<<endl;
    cout<<"|           ##     ##      ##     ##   ##        ##      #    |"<<endl;
    cout<<"|    #########     ##      ##     ###########    #########    |"<<endl;
    cout<<"|    ##            ##      ##          ##        ##      #    |"<<endl;
    cout<<"|    ##            ##      ##          ##        ##      #    |"<<endl;
    cout<<"|    ##            ##      ##          ##        ##      #    |"<<endl;
    cout<<"|    #########     ##########          ##        #########    |"<<endl;
    cout<<"------------------------------------------------------------------"<<endl;
    cout<<"MAGIC VERSION!!!!!!!!!!!!!!"<<endl<<endl;
    cout<<"created by chala_tea"<<endl<<endl;
    cout<<"1.start       2.exit"<<endl;
    cin>>ans;
    if(ans=='2') exit(0);

    //story telling
    system("cls");
    cout<<"what's your name?(1.skip)"<<endl;
    cin>>name;
    if(name=="1"){
        name="F4Per";
        return;
    }
    system("cls");
    cout<<"Once upon a time, you were a magician trainee called "<<name
        <<" and you were going to have the final task."<<endl;
    Sleep(5000);
    cout<<"The task is about the game 2048."<<endl;
    Sleep(2000);
    cout<<"You can press 'w' to let blocks go to upside,"<<endl<<"'a' to let blocks go to leftside,"<<endl
        <<"'s' to let blocks go to below,"<<endl<<"or 'd' to let blocks go to rightside."<<endl;
    Sleep(6000);
    cout<<"But you have magics."<<endl<<"press 'f1 x,y' to make block on x,y times 2"<<endl
        <<"or press 'f2 x,y' to make block on x,y divide 2"<<endl;
    Sleep(6000);
    cout<<"But remember, magic can only use for 10 times."<<endl;
    Sleep(3000);
    cout<<"Good luck to you all and have a good time~";
    Sleep(3000);
}

void rdm(){
    int rx=1+rand()%4,ry=1+rand()%4;
    while(a[rx][ry]!=0)rx=1+rand()%4,ry=1+rand()%4;
    a[rx][ry]=2;
}

void print(){
    system("cls");
    for(int i=1;i<=4;i++){
        cout<<"---------------------"<<endl;
        for(int j=1;j<=4;j++){
            int now=a[i][j],d=0;
            while(now)now/=10,d++;
            if(d==0) cout<<"|    ";
            else if(d==1)cout<<"| "<<a[i][j]<<"  ";
            else if(d==2)cout<<"| "<<a[i][j]<<" ";
            else if(d==3)cout<<"|"<<a[i][j]<<" ";
            else if(d==4)cout<<"|"<<a[i][j];
        }
        cout<<"|"<<endl;
    }
    cout<<"---------------------"<<endl;
    cout<<"times of magic is "<<magicnt<<endl;
    cout<<"score:"<<score<<endl;
    cout<<"what do you want to do next?"<<endl;
    cin>>du;
}

void control(){
    if(du=="w"){
        for(int i=1;i<=4;i++){
            for(int j=1;j<=4;j++){
                int k=i-1,orz=a[i][j];
                while(k>0&&(a[k][j]==0||a[k][j]==orz)){
                    if(a[k][j]==orz)a[i][j]*=2,a[k][j]=0,score+=a[i][j];
                    k--;
                }
                swap(a[k+1][j],a[i][j]);
            }
        }
    }
    else if(du=="a"){
        for(int i=1;i<=4;i++){
            for(int j=1;j<=4;j++){
                int k=j-1,orz=a[i][j];
                while(k>0&&(a[i][k]==0||a[i][k]==orz)){
                    if(a[i][k]==orz)a[i][j]*=2,a[i][k]=0,score+=a[i][j];
                    k--;
                }
                swap(a[i][k+1],a[i][j]);
            }
        }
    }
    else if(du=="s"){
        for(int i=4;i>=1;i--){
            for(int j=1;j<=4;j++){
                int k=i+1,orz=a[i][j];
                while(k<5&&(a[k][j]==0||a[k][j]==orz)){
                    if(a[k][j]==orz)a[i][j]*=2,a[k][j]=0,score+=a[i][j];
                    k++;
                }
                swap(a[k-1][j],a[i][j]);
            }
        }
    }
    else if(du=="d"){
        for(int i=1;i<=4;i++){
            for(int j=4;j>=1;j--){
                int k=j+1,orz=a[i][j];
                while(k<5&&(a[i][k]==0||a[i][k]==orz)){
                    if(a[i][k]==orz)a[i][j]*=2,a[i][k]=0,score+=a[i][j];
                    k++;    
                }
                swap(a[i][k-1],a[i][j]);
            }
        }
    }
    else if(du=="f1"){
        int cx,cy;
        cin>>cx>>cy;
        a[cx][cy]*=2,magicnt--;
    }
    else if(du=="f2"){
        int cx,cy;
        cin>>cx>>cy;
        a[cx][cy]/=2,magicnt--;
    }
    else{
        wl=3;
        return;
    }
    rdm();
}

bool yn(){
    bool flg=0;
    for(int i=1;i<=4;i++){
        for(int j=1;j<=4;j++){
            if(a[i][j]==2048){
                wl=1;
                return false;
            }
            if(a[i][j]==1){
                wl=4;
                return false;
            }
            if(a[i][j]==0)flg=1;
        }
    }
    if(!flg){
        wl=2;
        return false;
    }
    return true;
}

void ending(){
    system("cls");
    cout<<"TASK OVER!"<<endl;
    Sleep(1000);
    cout<<name<<", your result is......"<<endl;
    Sleep(3000);
    if(wl==1)score+=1000;
    else if(wl==3)score-=5000;
    else if(wl==4)score+=20000;
    if(score>=10000)cout<<"Outstanding magician!";
    else if(score>=7000)cout<<"Exceeds Expectations magician!";
    else if(score>=5000)cout<<"Acceptable magician!";
    else if(score>=3000)cout<<"Poor magician!";
    else if(score>=1000)cout<<"Dreadful Expectations magician!";
    else if(score>=0)cout<<"Troll magician!";
    else cout<<"Welcome to azkaban!";
    cout<<endl<<"And your final score is:"<<score<<endl;
    cout<<"see you next time!";
}
int main(){

    starting();
    rdm();
    while(yn()){
        print();
        control();
    }
    ending();
    return 0;
}

各位神犇们,有什么需要修改的可以在评论区告诉我哦~