扫雷

· · 休闲·娱乐

2024/10/29更新

#include<bits/stdc++.h>
using namespace std;
#define int long long
int n,m,cls,p,ans,a[105][105],pri[105][105],dx[10]{0,1,1,1,0,0,-1,-1,-1},dy[10]{0,-1,0,1,-1,1,-1,0,1},flip[105][105],fl,rfl,x,y,dx1[6]{0,1,0,-1},dy1[6]{0,0,1,0,-1};
char op;
void che(){
   while(1){
    cin>>op>>x>>y;
    if(op!='F'&&op!='C'){
        cout<<"type in 'F' or 'C'!";
        continue;
       }
       if(op=='F'){
        if(flip[x][y]==1){
            cout<<"already checked,try again\n";
            continue;
        }
    }
    return;
   }
}
struct node{
   int x,y;
};
queue<node>q;
void flood(int x,int y){
   while(!q.empty()){
    q.pop();
   }
   q.push({x,y});
   while(!q.empty()){
    node p=q.front();
    q.pop(); 
    if(flip[p.x][p.y]!=2)flip[p.x][p.y]=1;
    if(pri[p.x][p.y]!=0){
        continue;
    }
    for(int i=1;i<=4;i++){
        if(p.x+dx1[i]<=0||p.x+dx1[i]>n||p.y+dy1[i]<=0||p.y+dy1[i]>m)continue;
        if(flip[p.x+dx1[i]][p.y+dy1[i]]){
            continue;
        }
        q.push({p.x+dx1[i],p.y+dy1[i]}); 
    }
   }
}
signed main(){
   cout<<"game number\n\
(if you don't know,type a random number 0<=x<=100000000):";
   cin>>p;
   srand(p);
   cout<<"map size(two numbers):";
   cin>>n>>m;
   cout<<"Show number of unflaged mines? (Y/N)";
   char pp;
   cin>>pp;
   cout<<"clear screen every round? (Y/N)";
   char tmp;
   cin>>tmp;
   if(tmp=='Y'){
    cls=1;
   }
   else{
    cls=0;
   }
   for(int i=1;i<=n;i++){
    for(int j=1;j<=m;j++){
        a[i][j]=rand()%14;
        if(a[i][j]==8){
            a[i][j]=1;
            ans++;
        }
        else{
            a[i][j]=0;
        }
    }
   }
   for(int i=1;i<=n;i++){
    for(int j=1;j<=m;j++){
        if(a[i][j]){
            pri[i][j]=-1;
            continue;
        }
        for(int k=1;k<=8;k++){
            if(a[i+dx[k]][j+dy[k]]){
                pri[i][j]++;
            }
        }
    }
   }
//  for(int i=1;i<=n;i++){
//      for(int j=1;j<=m;j++){
//          if(pri[i][j]==-1){
//              cout<<'@';
//              continue;
//          }
//          cout<<pri[i][j];
//      }
//      cout<<endl;
//  }
   while(rfl!=ans){
    if(cls)system("cls");
    cout<<"placed "<<ans<<" mines\n";
    cout<<n<<' '<<m<<endl;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            if(flip[i][j]==1){
                cout<<pri[i][j];
            }
            else if(flip[i][j]==2){
                cout<<"F";
            }
            else{
                cout<<"#";
            }
        }
        cout<<endl;
    }
    if(pp=='Y'){
        cout<<ans-rfl<<" mines not flaged yet\n";
    }
    cout<<"to flag(or unflag): F x y \n \
(type in the place you want to flag/unflag in x and y)\n";
    cout<<"to check: C x y (type in the place you want to check in x and y)\n";
    che();
    if(op=='C'){
        if(pri[x][y]==-1){
            cout<<"GAME OVER";
            return 0;
        }
        flood(x,y);
    }
    else{
        if(flip[x][y]==2){
            flip[x][y]=0;
            if(pri[x][y]==-1){
                rfl--;      
            }
        }
        else{
            flip[x][y]=2;
            if(pri[x][y]==-1){
                rfl++;
            }
        }
    }
   }
   cout<<"YOU WIN!\n";
   for(int i=1;i<=n;i++){
    for(int j=1;j<=m;j++){
        if(pri[i][j]==-1){
            cout<<'@';
            continue;
        }
        cout<<pri[i][j];
    }
    cout<<endl;
   }
}

实验板(可能有bug)

#include<bits/stdc++.h>
using namespace std;
#define int long long
int n,m,cls,p,ans,a[105][105],pri[105][105],dx[10] {0,1,1,1,0,0,-1,-1,-1},dy[10] {0,-1,0,1,-1,1,-1,0,1},flip[105][105],fl,rfl,x,y,dx1[6] {0,1,0,-1},dy1[6] {0,0,1,0,-1};
char op;
void che() {
    while(1) {
        cin>>op>>x>>y;
        if(op!='F'&&op!='C'){
            cout<<"type in 'F' or 'C'!";
            continue;
        }
        if(op=='F') {
            if(flip[x][y]==1) {
                cout<<"already checked,try again\n";
                continue;
            }
        }
        return;
    }
}
struct node {
    int x,y;
};
queue<node>q;
void flood(int x,int y) {
    while(!q.empty()) {
        q.pop();
    }
    q.push({x,y});
    while(!q.empty()) {
        node p=q.front();
        q.pop();
        if(flip[p.x][p.y]!=2)flip[p.x][p.y]=1;
        if(pri[p.x][p.y]!=0) {
            continue;
        }
        for(int i=1; i<=4; i++) {
            if(p.x+dx1[i]<=0||p.x+dx1[i]>n||p.y+dy1[i]<=0||p.y+dy1[i]>m)continue;
            if(flip[p.x+dx1[i]][p.y+dy1[i]]) {
                continue;
            }
            q.push({p.x+dx1[i],p.y+dy1[i]});
        }
    }
}
int delay(int ms) {
    std::this_thread::sleep_for(std::chrono::milliseconds(ms));
}
void say(string str) {
    int n=str.size();
    for(int i=0; i<n; i++) {
        char c=str[i];
        cout<<c;
        delay(50);
    }
}
void fast_say(string str) {
    int n=str.size();
    for(int i=0; i<n; i++) {
        char c=str[i];
        cout<<c;
        delay(5);
    }
}
signed main() {
    say("game number\n\
(if you don't know,type a random number 0<=x<=100000000):");
    cin>>p;
    srand(p);
    say("finder mode? (Y/N)\n");
    char sp1;
    cin>>sp1;
    say("HARD MODE? (Y/N)\n");
    char hard;
    cin>>hard;
    say("map size(two numbers):");
    cin>>n>>m;
    say("Show number of unflaged mines? (Y/N)");
    char pp;
    cin>>pp;
    say("clear screen every round? (Y/N)");
    char tmp;
    cin>>tmp;
    if(tmp=='Y') {
        cls=1;
    } else {
        cls=0;
    }
    for(int i=1; i<=n; i++) {
        for(int j=1; j<=m; j++) {
            a[i][j]=rand()%14;
            if(a[i][j]==8) {
                a[i][j]=1;
                ans++;
            } else {
                a[i][j]=0;
            }
        }
    }
    if(sp1=="Y"){
        fast_say("finder mode:find the place with number 8 to win(check it),or win the normal way\n");
        int ax,ay;
        ax=rand()%n+1;
        ay=rand()%m+1;
        a[x-1][y-1]=1;
        a[x-1][y]=1;
        a[x-1][y+1]=1;
        a[x][y-1]=1;
        a[x][y+1]=1;
        a[x][y-1]=1;
        a[x][y]=1;
        a[x][y+1]=1;
        pri[x][y]=8;
    }
    for(int i=1; i<=n; i++) {
        for(int j=1; j<=m; j++) {
            if(a[i][j]) {
                pri[i][j]=-1;
                continue;
            }
            if(pri[i][j])continue;
            for(int k=1; k<=8; k++) {
                if(a[i+dx[k]][j+dy[k]]) {
                    pri[i][j]++;
                }
            }
        }
    }
    if(hard=='Y') {
        for(int i=1; i<=n; i++) {
            for(int j=1; j<=m; j++) {
                if(rand()%6!=5)continue;
                if(pri[i][j]!=-1) {
                    pri[i][j]=rand()%7+1;
                }
            }
        }
    }
//  for(int i=1;i<=n;i++){
//      for(int j=1;j<=m;j++){
//          if(pri[i][j]==-1){
//              cout<<'@';
//              continue;
//          }
//          cout<<pri[i][j];
//      }
//      cout<<endl;
//  }
    while(rfl!=ans) {
        if(cls)system("cls");
        cout<<"placed "<<ans<<" mines\n";
        cout<<n<<' '<<m<<endl;
        for(int i=1; i<=n; i++) {
            for(int j=1; j<=m; j++) {
                if(flip[i][j]==1) {
                    delay(5);
                    cout<<pri[i][j];
                } else if(flip[i][j]==2) {
                    delay(5);
                    cout<<"F";
                } else {
                    delay(5);
                    cout<<"#";
                }
            }
            cout<<endl;
        }
        if(pp=='Y') {
            cout<<ans-rfl<<" mines not flaged yet\n";
        }
        fast_say("to flag(or unflag): F x y \n \
(type in the place you want to flag/unflag in x and y)\n");
        fast_say("to check: C x y (type in the place you want to check in x and y)\n");
        che();
        if(op=='C') {
            if(pri[x][y]==-1) {
                cout<<"GAME OVER\n";
                for(int i=1; i<=n; i++) {
                    for(int j=1; j<=m; j++) {
                        if(pri[i][j]==-1) {
                            cout<<'@';
                            delay(5);
                            continue;
                        }
                        cout<<pri[i][j];
                        delay(5);
                    }
                    cout<<endl;
                }
                return 0;
            }
            if(pri[x][y]==8&&sp1=="Y"){
                cout<<"8 finded\n";
                break;
            }
            flood(x,y);
        } else {
            if(flip[x][y]==2) {
                flip[x][y]=0;
                if(pri[x][y]==-1) {
                    rfl--;
                }
            } else {
                flip[x][y]=2;
                if(pri[x][y]==-1) {
                    rfl++;
                }
            }
        }
    }
    cout<<"YOU WIN!\n";
    for(int i=1; i<=n; i++) {
        for(int j=1; j<=m; j++) {
            if(pri[i][j]==-1) {
                cout<<'@';
                delay(5);
                continue;
            }
            cout<<pri[i][j];
            delay(5);
        }
        cout<<endl;
    }
}