扫雷!!!

· · 休闲·娱乐

扫雷!!!

#include<bits/stdc++.h>
#include<windows.h>
using namespace std;
int dx[]={1,-1,0,0,1,-1,-1,1};
int dy[]={0,0,1,-1,1,-1,1,-1};
int n,m,num,x,y,op;
int mp[5003][5003];
char game[5003][5003];
bool check(){
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            if(game[i][j]=='0'&&!mp[i][j]){
                return 0;
            }
        }
    }
    return 1;
}
void Game(){
    cout<<"input size:\n";
    cin>>n>>m;
    cout<<"input num:\n";
    cin>>num;
    int t=0;
    while(num<=0){
        t++;
        if(t>=5){
            cout<<"WHAT WRONG WITH YOU!";
            return;
        }
        cout<<"it must bigger than 0\n";
        cin>>num;
    }
    memset(game,'0',sizeof(game));
    while(num>n*m){
        cout<<"num must lower than size of map";
        cin>>num;
    }
    srand(time(0));
    for(int i=1;i<=num;i++){
        int x=rand()%n+1,y=rand()%m+1;
        while(mp[x][y]==-1){
            x=rand()%n+1,y=rand()%m+1;
        }
        mp[x][y]=-1;
    }
    int flag=0;
    while(!check()&&flag<num){
        if(flag<=num-n*m){
            cout<<"WHAT ARE YOU DOING?";
            return;
        }
        for(int i=1;i<=m;i++){
            cout<<i<<' ';
        }
        cout<<'\n';
        for(int a=1;a<=n;a++){
            for(int b=1;b<=m;b++){
                cout<<game[a][b]<<' ';
            }
            cout<<a<<'\n';
        }
        cout<<"input your id\nnum: "<<num<<'\n'<<"1 for flag,2 for id:\n";
        cin>>op>>x>>y;
        while(!(x>=1&&x<=n&&y>=1&&y<=m)){
            cout<<"id must lower than size";
            cin>>x>>y;
        }
        if(op==1){
            if(game[x][y]=='F'){
                game[x][y]='0';
                if(mp[x][y]==-1){
                    flag--;
                }else{
                    flag++;
                }
            }else{
                game[x][y]='F';
                if(mp[x][y]==-1){
                    flag++;
                }else{
                    flag--;
                }
            }
        }else{
            if(mp[x][y]==-1){
                system("cls");
                cout<<"GAME OVER";
                cout<<"\nagain? YES or NO\n";
                string s;
                cin>>s;
                if(s=="YES"){
                    Game();
                }else{
                    return;
                }
            }else{
                queue<pair<int,int> > q;
                q.push({x,y});
                while(q.size()){
                    int x,y;
                    x=q.front().first;
                    y=q.front().second;
                    q.pop();
                    int Cnt=0;
                    for(int i=0;i<8;i++){
                        int X=x+dx[i],Y=y+dy[i];
                        if(mp[X][Y]==-1){
                            Cnt++;
                        }
                    }
                    if(Cnt){
                        game[x][y]=Cnt+'0';
                    }else{
                        game[x][y]=' ';
                        for(int i=0;i<8;i++){
                            int X=x+dx[i],Y=y+dy[i];
                            if(X>=1&&X<=n&&Y>=1&&Y<=m&&game[X][Y]=='0'){
                                q.push({X,Y});
                            }
                        }
                    }
                }
            }
        }
        system("cls");
    }
    cout<<"YOU WIN!!!";
    cout<<"\nagain? YES or NO\n";
    string s;
    cin>>s;
    if(s=="YES"){
        Game();
    }else{
        return;
    }
}
int main(){
    cout<<"MINE CLEARANCE!\n"; 
    Game();
}