码迷,mamicode.com
首页 > 其他好文 > 详细

扫雷搞着玩

时间:2018-03-12 23:03:14      阅读:401      评论:0      收藏:0      [点我收藏+]

标签:sleep   hit   rect   fill   大小   number   red   iostream   map   

 

技术分享图片

代码:

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<ctime>
  4 #include<graphics.h>
  5 using namespace std;
  6 void number(int x,char s[]) {
  7     int num = 0, pre = 0;
  8     if (x < 0) x *= -1, pre++, s[num++] = -;
  9     if (x == 0) {
 10         s[num++] = 0;
 11         s[num] = 0;
 12         return;
 13     }
 14     while (x > 0) {
 15         s[num++] = x % 10+48;
 16         x /= 10;
 17     }
 18     for (int i = pre; i <= (pre / 2) - 1; i++) swap(s[i],s[num-1-i]);
 19     s[num] = 0;
 20 }
 21 class Map {
 22 public:
 23     int X = 1300, Y = 650;
 24     int d[8][2] = { { 1,1 },{ 1,0 },{ 1,-1 },{ 0,-1 },{ -1,-1 },{ -1,0 },{ -1,1 },{ 0,1 } };
 25     int n, m, p;
 26     int dx, dy;
 27     int mapp[200][200];
 28     int show[200][200];
 29     int zx[200][200], zy[200][200];
 30     Map(int nn, int mm, int pp) :n(nn), m(mm), p(pp) { init(); }
 31     void init() {
 32         memset(mapp, 0, sizeof(mapp));
 33         if (n >= 200) n = 199;
 34         if (m >= 200) m = 199;
 35         if (p > n*m) p = n*m;
 36         for (int i = 0; i < p; i++) {
 37             int a = rand() % n;
 38             int b = rand() % m;
 39             if (mapp[a][b]) {
 40                 i--;
 41             }
 42             mapp[a][b] = -1;
 43         }
 44         memset(show, 0, sizeof(show));
 45         initgraph(X, Y);
 46         dx = X/n;
 47         dy = Y/m;
 48         setlinecolor(WHITE);
 49         for (int i = dx; i <= X; i+=dx) line(i,0,i,Y);
 50         for (int i = dy; i <= Y; i += dy) line(0,i,X,i);
 51         int ansx = dx/2, ansy = dy/2;
 52         for (int i = 0; i < n; i++) {
 53             ansy = dy / 2;
 54             for (int j = 0; j < m; j++) {
 55                 zx[i][j] = ansx;
 56                 zy[i][j] = ansy;
 57                 ansy += dy;
 58             }
 59             ansx += dx;
 60         }
 61     }
 62     int Show() {
 63         int t = 0;
 64         for (int i = 0; i < n; i++) {
 65             for (int j = 0; j < m; j++) {
 66                 if (show[i][j] == 0) {
 67                     setfillcolor(BLUE);
 68                     fillrectangle(zx[i][j] - dx / 2, zy[i][j] - dy / 2, zx[i][j] + dx / 2, zy[i][j] + dy / 2);
 69                 }
 70                 else {
 71                     settextcolor(WHITE);
 72                     setfillcolor(GREEN);
 73                     fillrectangle(zx[i][j] - dx / 2, zy[i][j] - dy / 2, zx[i][j] + dx / 2, zy[i][j] + dy / 2);
 74                     settextstyle(dy/3,dx/3,"宋体");
 75                     char s[100];
 76                     number(mapp[i][j],s);
 77                     outtextxy(zx[i][j]-dx/4,zy[i][j]-dy/4,s);
 78                     t++;
 79                 }
 80             }
 81         }
 82         if (t == n*m - p) {
 83             return 2;
 84         }
 85         return 0;
 86     }
 87     int isBoom(int x, int y,int &cc) {
 88         if (show[x][y] == 1) {
 89             return 0;
 90         }
 91         cc++;
 92         if (mapp[x][y] == -1) {
 93             //printf("Boom!");
 94             int i = x, j = y;
 95             setfillcolor(RED);
 96             clearrectangle(zx[i][j] - dx / 2, zy[i][j] - dy / 2, zx[i][j] + dx / 2, zy[i][j] + dy / 2);
 97             fillrectangle(zx[i][j] - dx / 2, zy[i][j] - dy / 2, zx[i][j] + dx / 2, zy[i][j] + dy / 2);
 98             int gs = 0;
 99             for (int i = 0; i < n; i++) {
100                 for (int j = 0; j < m; j++) {
101                     if (mapp[i][j] == -1) {
102                         gs++;
103                     }
104                 }
105             }
106             if (gs == 0) gs++;
107             for (int i = 0; i < n; i++) {
108                 for (int j = 0; j < m; j++) {
109                     if (mapp[i][j] == -1) {
110                         clearrectangle(zx[i][j] - dx / 2, zy[i][j] - dy / 2, zx[i][j] + dx / 2, zy[i][j] + dy / 2);
111                         fillrectangle(zx[i][j] - dx / 2, zy[i][j] - dy / 2, zx[i][j] + dx / 2, zy[i][j] + dy / 2);
112                         Sleep(3500 / gs);
113                     }
114                 }
115             }
116             return 1;
117         }
118         else {
119             mapp[x][y] = 0;
120             for (int i = 0; i < 8; i++) {
121                 int nx = x + d[i][0];
122                 int ny = y + d[i][1];
123                 if (nx < 0 || nx >= n || ny < 0 || ny >= m) {
124                     continue;
125                 }
126                 if (mapp[nx][ny] == -1) {
127                     mapp[x][y]++;
128                 }
129             }
130             show[x][y] = 1;
131             if (mapp[x][y] == 0) {
132                 for (int i = 0; i < 8; i++) {
133                     int nx = x + d[i][0];
134                     int ny = y + d[i][1];
135                     if (nx < 0 || nx >= n || ny < 0 || ny >= m) {
136                         continue;
137                     }
138                     int nc = cc + 1;
139                     isBoom(nx, ny, nc);
140                 }
141             }
142         }
143         //system("cls");
144         if(cc==1)
145         return Show();
146         else return 0;
147     }
148     bool getxy(int &x, int &y) {
149         for (int i = 0; i < n; i++) {
150             for (int j = 0; j < m; j++) {
151                 if (x <= zx[i][j] + dx / 2 && x >= zx[i][j] - dx / 2 && y <= zy[i][j] + dy / 2 && y >= zy[i][j] - dy / 2) {
152                     x = i, y = j;
153                     return true;
154                 }
155             }
156         }
157         return false;
158     }
159 };
160 int main() {
161     srand(time(0));
162     int n, m, p, x, y;
163     printf("请输入地图大小X,Y,和雷的数量共3个数字\n");
164     scanf("%d%d%d", &n, &m, &p);
165     Map mapp = Map(n, m, p);
166     mapp.Show();
167     MOUSEMSG mess;
168     while (true) {
169         if (MouseHit == false) continue;
170         mess = GetMouseMsg();
171         FlushMouseMsgBuffer();
172         if (mess.uMsg != WM_LBUTTONDOWN) continue;
173         x = mess.x, y = mess.y;
174         if (mapp.getxy(x, y) == false) continue;
175         int c = 0;
176         int pt = mapp.isBoom(x, y,c);
177         if (pt==2) {
178             settextcolor(GREEN);
179             settextstyle(mapp.X/10,mapp.Y/10,"楷体");
180             outtextxy(0,mapp.Y/2,"全部清空!");
181             break;
182         }
183         else if(pt==1){
184             settextcolor(RED);
185             settextstyle(mapp.X / 5, mapp.Y / 5, "楷体");
186             outtextxy(0, mapp.Y / 2, "GG了!");
187             break;
188         }
189     }
190     while (1) continue;
191     return 0;
192 }

 

扫雷搞着玩

标签:sleep   hit   rect   fill   大小   number   red   iostream   map   

原文地址:https://www.cnblogs.com/xfww/p/8552068.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!