C + + to achieve a small mine clearance game

Time:2021-6-22

This example for you to share the C + + implementation of Minesweeper game specific code, for your reference, the specific content is as follows

The current version is version 0.98, you can add new features

The code is as follows:

#include<bits/stdc++.h>
#include<windows.h>
using namespace std;
long long int c,dev,m,k,cnt,d,e,jie=10,z,abc,n,b[1000][1000],a[1000][1000],cc,cd,ce,def; // C is a random row and K is a random column 
bool flag=0; 
bool check_ Lei () {// used to stop the game 
 for(int i=0;i<n;i++){
 for(int j=0;j<m;j++){
  if(b[i][j]==999||b[i][j]==5000) def++;
 }
 }
 return (def==abc)?false:true;
}
Void check (int d, int E) {// ray number near 
 if(d<0||d>=n||e<0||e>=m){
 ce++;
 return;
 } 
 if(a[d][e]==10000){
 ce++;
 return;
 }
 if(b[d][e]!=999&&b[d][e]!=5000) return;
 b[d][e]=a[d][e];
 if(b[d][e]!=0){
 for(int i=-1;i<=1;i++){
  for(int j=-1;j<=1;j++){
  if(d+i<0||d+i>=n||e+i<0||e+i>=m){
   ce++;
   continue;
  }
  if(b[d+i][e+i]==5000) continue;
  if(b[d+i][e+j]!=999) continue;
  if(a[d+i][e+j]==10000){
   ce++;
   continue;
  }
  if(ce<=5){
   b[d+i][e+j]=a[d+i][e+j];
   ce++; 
  }
  } 
 }
 return ; 
 }
 if(cd>n/2) return;
 for(int i=-1;i<=1;i++){
 for(int j=-1;j<=1;j++){
  check(d+i,e+j);
 }
 }
 cd++;
}
void buZhi(){
 system("cls"); // Clear screen 
 srand(time(NULL)); // Time seed 
 for(int i=0;i<jie;i++){
 c=rand()%n;
 k=rand()%m;
 Sleep(1); // Prevent random number repetition as much as possible 
 if(a[c][k]!=10000){
  a[c][k]=10000;
 }
 else jie++;
 }// randomly assign thunder 
 if(a[0][0]!= 10000) {// start the numerical distribution of the four corners, top left 
 if(a[0][1]==10000) cnt++;
 if(a[1][1]==10000) cnt++;
 if(a[1][0]==10000) cnt++;
 a[0][0]=cnt;
 }
 cnt=0;
 if(a[n-1][m-1]!= 10000) {// bottom right 
 if(a[n-1][m-2]==10000) cnt++;
 if(a[n-2][m-2]==10000) cnt++;
 if(a[n-2][m-1]==10000) cnt++;
 a[n-1][m-1]=cnt;
 }
 cnt=0;
 if(a[0][m-1]!= 10000) {// top right 
 if(a[1][m-1]==10000) cnt++;
 if(a[1][m-2]==10000) cnt++;
 if(a[0][m-2]==10000) cnt++;
 a[0][m-1]=cnt;
 }
 cnt=0;
 if(a[n-1][0]!= 10000) {// bottom left 
 if(a[n-2][1]==10000) cnt++;
 if(a[n-1][1]==10000) cnt++;
 if(a[n-2][0]==10000) cnt++;
 a[n-1][0]=cnt;
 }
 cnt=0; // The assignment of the four corners here is over 
 for(int i=1; i<n; I + +) {// start the allocation of four edges, top edge 
 if(a[0][i]!=10000){
  if(a[1][i]==10000) cnt++;
  if(a[1][i+1]==10000) cnt++;
  if(a[1][i-1]==10000) cnt++;
  if(a[0][i+1]==10000) cnt++;
  if(a[0][i-1]==10000) cnt++;
  a[0][i]=cnt;
 }
 cnt=0;
 }
 for(int i=1; i<m; I + +) {// left 
 if(a[i][0]!=10000){
  if(a[i][1]==10000) cnt++;
  if(a[i+1][1]==10000) cnt++;
  if(a[i-1][1]==10000) cnt++;
  if(a[i+1][0]==10000) cnt++;
  if(a[i-1][0]==10000) cnt++;
  a[i][0]=cnt;
 }
 cnt=0;
 }
 for(int i=1; i<m; I + +) {// right 
 if(a[i][n-1]!=10000){
  if(a[i-1][n-1]==10000) cnt++;
  if(a[i+1][n-1]==10000) cnt++;
  if(a[i][n-2]==10000) cnt++;
  if(a[i-1][n-2]==10000) cnt++;
  if(a[i+1][n-2]==10000) cnt++;
  a[i][n-1]=cnt;
 }
 cnt=0;
 }
 for(int i=1; i<n; I + +) {// below 
 if(a[m-1][i]!=10000){
  if(a[m-2][i]==10000) cnt++;
  if(a[m-2][i+1]==10000) cnt++;
  if(a[m-2][i-1]==10000) cnt++;
  if(a[m-1][i+1]==10000) cnt++;
  if(a[m-1][i-1]==10000) cnt++;
  a[m-1][i]=cnt;
 }
 cnt=0;
 }// end the assignment of the four edges 
 for(int i=1;i<n-1;i++){
 for(int j=1;j<m-1;j++){
  if(a[i][j]!=10000){
  if(a[i-1][j]==10000) cnt++;
  if(a[i-1][j-1]==10000) cnt++;
  if(a[i-1][j+1]==10000) cnt++;
  if(a[i][j-1]==10000) cnt++;
  if(a[i][j+1]==10000) cnt++;
  if(a[i+1][j]==10000) cnt++;
  if(a[i+1][j-1]==10000) cnt++;
  if(a[i+1][j+1]==10000) cnt++;
  a[i][j]=cnt;
  }
  cnt=0;
 }
 }// end of basic quantity allocation 
} 
void qianYan(){
 Cout < "welcome to the minesweeping game. The rules of the game are as follows:;
 Cout < "1. The game specifications can be selected, and the number of thunder can be customized;
 Cout < "2. In this game - represents the place that has not been swept, the numbers of 0-9 represent the numbers in normal mine sweeping (0 is special, which means there is nothing in mine sweeping), * represents the marked mine, which will be automatically unmarked when the marked place is swept;
 Cout < "3. Please input the row and column to represent the normal click in the game; 
 system("pause");
 system("cls");
 Cout < "please enter length (> 2):";
 cin>>n;
 while(n>1000||n<=2){
 Cout < "illegal length, please re-enter";
 cin>>n;
 }
 Cout < "please enter width (> 2):";
 cin>>m;
 while(m>1000||m<=2){
 Cout < "illegal width, please re-enter";
 cin>>m;
 }
 Cout < "please input the thunder number (recommended thunder number calculation: round the specification length * width / 10 to achieve better effect)";
 cin>>abc;
 while(abc>n*m||abc<=0){
 Cout < "input is illegal, please re-enter";
 cin>>abc;
 }
 jie=abc; 
 buZhi();
}
void saoLei(){
 for(int i=0;i<n;i++)
 for(int j=0;j<m;j++)
  b[i][j]=999;
 while(check_lei()){
 def=0;
 dev=1;
 ce=0;
 cd=0;
 cout<<"  ";
 for(int i=0;i<m;i++)
  cout<<i+1<<' ';
 cout<<'\n';
 for(int i=0;i<n;i++){
  if(dev>9)
  cout<<dev<<' ';
  else 
  cout<<dev<<" ";
  for(int j=0;j<m;j++){
  if(b[i][j]!=999&&b[i][j]!=5000)
   cout<<b[i][j]<<' ';
  else if(b[i][j]==999)
   cout<<"- ";
  else
   cout<<"* ";
  }
  cout<<'\n';
  dev++;
 }
 def=0;
 if(!check_lei()) break;
 Cout < "please enter row and column:;";
 cin>>d>>e;
 while(d>n||e>m||d<0||e<0){
  Cout < "input is illegal, please re-enter";
  cin>>d>>e;
 }
 if(a[d-1][e-1]!=10000) check(d-1,e-1);//b[d-1][e-1]=a[d-1][e-1]; 
 else {
  Cout < "failed";
  flag=1; 
  return;
 }
 Cout < "need to mark ray (input 1, not input 2)"; 
 cin>>z; 
 while(z==1){
  Cout < "please enter row and column:;";
  cin>>d>>e;
  while(d>n||e>m||d<0||e<0){
  Cout < "input is illegal, please re-enter";
  cin>>d>>e;
  }
  if(b[d-1][e-1]==999){
  b[d-1][e-1]=5000;
  }
  Else cout < "mark error: mark the grid that has been swept that is not Ray's'";
  Cout < "need to mark ray (input 1, not input 2)";
  cin>>z;
 }
 system("pause");
 system("cls");
 Cout < loading;
 Sleep(300);
 cout<<".";
 Sleep(300);
 cout<<".";
 Sleep(300);
 cout<<"."; 
 system("cls"); 
 def=0;
 }
}
int main(){
 Cout < "new features in version 2.2.0":;
 Cout < "display the number of rows and columns, which is easy to find;
 Cout < "fixed the problem that 2.1.0 games can't win";
 system("pause"); 
 system("cls");
 qianYan();
 saoLei();
 If (flag = = 0) cout < "great;
 Else cout < "I'm sorry about it.";
 system("pause");
 Cout < "leitu is as follows:;
 for(int i=0;i<n;i++){
 for(int j=0;j<m;j++){
  if(a[i][j]==10000)
  cout<<"*"<<' ';
  else cout<<a[i][j]<<' ';
 }
 cout<<'\n';
 }
 cout<<'\n';
 for(int i=0;i<n;i++){
 for(int j=0;j<m;j++){
  if (b[i][j]==5000&&a[i][j]!= 10000) cout < < I + 1 < < I + 1 < < j + 1 < < column tag error; 
 } 
 } 
 system("pause");
 return 0;
}
}

The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.