主要是使用递归消除方块,代码如下:

#include<bits/stdc++.h>
#include<conio.h>
#include<windows.h>
using namespace std;
int ma[100][100],cli,s,sco;
void mapx();
void dow();
void color(int a){
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), a);
}
void ne(){
    int a,b,ss;
    struct timeb timeSeed;
    ftime(&timeSeed);
    srand(timeSeed.time * 1000 + timeSeed.millitm);  // milli time
// 生成[b,a]范围内的数值,包括b和a
    for(int i=0;i<10;i++){
        if(ma[0][i]==0){
            a=1;b=4;
            unsigned int range = a - b + 1;
            unsigned int crand = rand() % range + b;
            ss=crand%4+1;
            ma[0][i]=ss;
            dow();
        }
    }
}
void dow(){
    for(int i=0;i<20;i++){
        for(int ii=0;ii<10;ii++){
            if(ma[i][ii]==0){
                for(int k=i-1;k>=0;k--){
                    ma[k+1][ii]=ma[k][ii];
                    ma[k][ii]=0;
                }
            }
        }
        Sleep(1);
        mapx();
    }
}
void pp(int x,int y){
    for(int i=0;i<4;i++){
        if(i==0&&ma[y][x+1]==s){
            ma[y][x+1]=0;
            sco++;
            pp(x+1,y);
        }
        if(i==1&&ma[y+1][x]==s){
            ma[y+1][x]=0;
            sco++;
            pp(x,y+1);
        }
        if(i==2&&ma[y][x-1]==s&&x-1>-1){
            ma[y][x-1]=0;
            sco++;
            pp(x-1,y);
        }
        if(i==3&&ma[y-1][x]==s&&y-1>-1){
            ma[y-1][x]=0;
            sco++;
            pp(x,y-1);
    }
    }
    
}
void mapx(){
        system("cls");
    for(int i=0;i<20;i++){
        for(int ii=0;ii<10;ii++){
            if(ma[i][ii]==0){
                cout<<" ";
            }
            if(ma[i][ii]==1){
                color(12);
                cout<<"#";
            }
                if(ma[i][ii]==2){
                color(14);
                cout<<"#";
            }
                if(ma[i][ii]==3){
                color(9);
                cout<<"#";
            }
                if(ma[i][ii]==4){
                color(10);
                cout<<"#";
            }
        }
        cout<<endl;
    }
    color(15);
    for(int i=0;i<cli;i++){
        cout<<" ";
    }cout<<"A"<<endl<<"score:"<<sco;
}
void start(){
    cout<<"               消消乐游戏"<<endl;
    cout<<"       ''A''是光标;a,d控制光标" <<endl;
    cout<<"      空格消除,每次消除消耗3点分数"<<endl;
    Sleep(5000);
    cout<<"开始吧";
    Sleep(1000);
    system("cls");
}
int main(){
    int a=0;sco=20;
    start(); 
    for(int i=0;i<20;i++){
    ne();    
    }
    
    while(1){
        if(_kbhit()){
            int b=getch();
            if(b==97&&cli>0){
            cli--;    
            }
            if(b==100&&cli<9){
            cli++;    
            }
            if(b==32){
                sco-=2;
                s=ma[19][cli];
                ma[19][cli]=0;
                pp(cli,19);
                dow();
                ne();
            }
            mapx();
        }
        if(sco<3){
            if(MessageBox(NULL,TEXT("哎呀,分数不够了,要再来一次吗?"),TEXT(" "),MB_YESNO)==IDYES){
                memset(ma,0,sizeof(ma));
                sco=20;
                for(int i=0;i<20;i++){
                    ne();
                }
                continue;
            }else{
                MessageBox(NULL,TEXT("拜拜"),TEXT(":)"),MB_OK);
                return 0;
            }
            
        }
    }
}

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐