Read a day in the day to see the book tired, Night saw minesweeper game, he thought to do a chant. Think for a while, also did not see what others do, about 1 hours to complete this simple version of the minesweeper game, because did not learn C #, interface things may be a few days later to do, tomorrow to go home, haha!
First of all, the idea is quite simple.
(1) Randomly generate 10 lei, labeled into a two-dimensional array, and then calculate the total number of mines in eight directions recorded, this is the preprocessing phase.
(2) Enter the position to turn the coordinates, if the number is directly displayed, is blank, where the BFS is the width of the first search solution, search to the outermost layer is the number (only one layer) when the end, detailed in the code.
Minesweeper #include <iostream> #include <string> #include <queue> #include <cstring> #include < Ctime>using namespace Std;int grid[100][100];int Randmark[100][100];char showus[100][100];int vis[100][100];int dir[8][2]={{-1,0},{1,0},{0,1},{0,-1},{-1,-1},{-1,1},{1,-1},{1,1}};//direction array int res; struct node{int x;int y;}; void BFs (int nx,int NY)//Processing click Blank {queue <node> q;node temp;node T;t.x=nx,t.y=ny;q.push (t); Vis[nx][ny]=1;while (! Q.empty ()) {Res++;temp=q.front (); showus[temp.x][temp.y]=grid[temp.x][temp.y]+ ' 0 '; Q.pop (); for (int i=0;i<8;i++) {int Xx=temp.x+dir[i][0];int yy=temp.y+dir[i][1];if (xx>=0&&xx<10&&yy>=0&&yy< {if (!vis[xx][yy]&&grid[xx][yy]==0) {t.x=xx,t.y=yy; Vis[xx][yy]=1; showus[xx][yy]=grid[xx][yy]+ ' 0 '; Q.push (t); }if (!vis[xx][yy]&&grid[xx][yy]>0&&grid[temp.x][temp.y]==0) {t.x=xx,t.y=yy; Vis[xx][yy]=1; showus[xx][yy]=grid[xx][yy]+ ' 0 '; Q.push (t); }}}}}int Main () {memset (grid,0,sizeof (grid)); Memset (Randmark,0,sizeof (Randmark)); memset (vis,0,sizeof (VIS)); for (int i=0;i<10;i++) for (int j= 0;j<10;j++) showus[i][j]= ' * '; Srand (unsigned (Time (NULL))); int Sum=0;while (1) {int X=rand ()%10;int Y=rand ()%10;if ( Randmark[x][y]!=1) {randmark[x][y]=1;//has thunder sum++;} if (sum==10) break; } res=0;for (int i=0;i<10;i++) for (int. j=0;j<10;j++) {if (randmark[i][j]) grid[i][j]=-1;} for (int i=0;i<10;i++) for (int j=0;j<10;j++) {if (grid[i][j]!=-1) {for (int k=0;k<8;k++) {int X=i+dir[k][0];int y=j+dir[k][1];if (x>=0 &&x<10&&y>=0&&y<10&&grid[x][y]==-1) {grid[i][j]++;}}} for (int i=0;i<10;i++) {for (int j=0;j<10;j++) cout<<showus[i][j]<< ""; Cout<<endl;} cout<< "Please enter the coordinates of the position to be opened:" <<endl;int x,y;while (1) {if (res==90) cout<< "Congratulations on your victory, mine success!" "<<endl; Cin>>x>>y;if (grid[x][y]==-1) {cout<< "You are in thunder!" "<<endl;break;} else if (!vis[x][y]&&grid[x][y]>0) {res++;vis[x][y]=1;showus[x][y]=grid[x][y]+ ' 0 '; System ("CLS"); for (int. i=0;i<10;i++) {for (int j=0;j<10;j++) cout<<showus[i][j]<< " "; cout<<endl; } cout<< "Please enter the coordinates of the position to be opened:" <<ENDL;} else if (!vis[x][y]&&grid[x][y]==0) {BFS (x, y); System ("CLS"); for (int. i=0;i<10;i++) {for (int j=0;j&l t;10;j++) cout<<showus[i][j]<< ""; cout<<endl; } cout<< "Please enter the coordinates of the position to be opened:" <<endl;}} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
C + + console version Minesweeper game