这是大一的时候写的,代码质量不是很高,仅供学习。关于扫雷怎么玩,代码思想,网上很多,要我写也写不出来他们那么好的,所有这方面的百度下
源码地址http://pan.baidu.com/share/link?shareid=1925887460&uk=2065228996
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
|
#include<windows.h>
#include<stdlib.h>
#include"resource.h"
int
XUNIT = GetSystemMetrics(SM_CXICON);
int
YUNIT = GetSystemMetrics(SM_CYICON);
int
NUM = 80;
int
CXNUM = 20;
int
CYNUM = 20;
int
Dead = 0 ;
//只能设置成全局变量
static
int
minestate[100][100], mineflag[100][100] ;
HDC
hdc, hdcmem3;
/*hdcmem1,hdcmem2, hdcmem4;*/
int
x,y;
int
cxmine , cymine;
static
int
cySource3, cxSource3;
// cxSource1,cxSource2,cxSource3,cxSource4,
// cySource1,cySource2,cySource3,cySource4;
int
width = CXNUM*XUNIT;
int
hight = CYNUM*(YUNIT) + +13+GetSystemMetrics(SM_CYBORDER);
LRESULT
CALLBACK MineProc (
HWND
,
UINT
,
WPARAM
,
LPARAM
);
int
WINAPI WinMain(
HINSTANCE
hInstance,
HINSTANCE
hPrevInstance,
PSTR
szCmdLine,
int
iCmdShow)
{
static
TCHAR
szMineName [] = TEXT (
"mineswap"
);
HWND
hwnd;
MSG msg;
WNDCLASS wc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hbrBackground = (
HBRUSH
)GetStockObject (GRAY_BRUSH);
wc.hCursor = LoadCursor (NULL, IDC_ARROW);
wc.hIcon = LoadIcon (hInstance, MAKEINTRESOURCE(10));
wc.hInstance = hInstance;
wc.lpfnWndProc = MineProc;
wc.lpszClassName = szMineName ;
wc.lpszMenuName = szMineName;
wc.style = CS_HREDRAW | CS_VREDRAW;
// RegisterClass(&wc);
if
(!RegisterClass (&wc))
{
MessageBox (NULL, TEXT (
"Program requires Windows NT!"
),
szMineName, MB_ICONERROR) ;
return
0 ;
}
hwnd = CreateWindow(szMineName,
TEXT(
"MINESWAP"
),
WS_OVERLAPPED |
WS_DLGFRAME | WS_SYSMENU | WS_MINIMIZEBOX,
CW_USEDEFAULT,CW_USEDEFAULT,
width, hight,
//XUNIT*CXNUM,YUNIT*(CYNUM + 1)+13+GetSystemMetrics(SM_CYBORDER),
NULL,NULL,hInstance,NULL);
ShowWindow(hwnd,iCmdShow);
UpdateWindow(hwnd);
while
(GetMessage(&msg,NULL,0,0))
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
return
msg.wParam;
}
int
IsInMineArea(
int
row,
int
col)
//查看有没超出范围
{
return
(row >= 0 && row < CXNUM && col >= 0 && col < CYNUM);
}
int
GetAroundNum(
int
row,
int
col)
//得到周围个数,
{
int
i, j;
int
around = 0;
int
minRow = ( row == 0 ) ? 0 : row - 1;
int
maxRow = row + 2;
int
minCol = ( col == 0 ) ? 0 : col - 1 ;
int
maxCol = col + 2;
for
(i = minRow; i < maxRow; i++)
{
for
(j = minCol; j < maxCol; j++)
{
if
(minestate[i][j] == 5) around++;
}
}
return
around;
}
void
ExpandMines(
int
row,
int
col)
//扫描,如果是已经被按下且雷数为0,显示它周围的八个格,c参见程序一
{
int
i, j;
int
minRow = (row == 0) ? 0 : row - 1;
int
maxRow = row + 2;
int
minCol = (col == 0) ? 0 : col - 1;
int
maxCol = col + 2;
int
around = GetAroundNum(row, col);
if
(mineflag[row][col] == 1)
//标志是1才赋值
{
minestate[row][col] = 15 - around;
}
//对周围一个雷都没有的空白区域
if
(around == 0)
{
for
(i = minRow; i < maxRow; i++)
{
for
(j = minCol; j < maxCol; j++)
{
//对于周围可以拓展的区域进行的规拓展
if
((!(i == row && j == col) )&& (minestate[i][j] == 0) && mineflag[x][y] == 1)
{
if
(!IsInMineArea (i, j) )
continue
;
ExpandMines(i, j);
}
}
}
}
}
LRESULT
CALLBACK MineProc (
HWND
hwnd,
UINT
message,
WPARAM
wParam,
LPARAM
lParam)
{
PAINTSTRUCT ps;
HINSTANCE
hInstance;
static
HBITMAP
hBm3;
//hBm1,hBm2,hBm3,hBm4;
BITMAP bitmap3;
// bimap1,bitmap2,bitmap3,bitmap4;
//若设置此函数的全局变量,没个WM_结束后不会值改变,其实是局部变量
//因为该函数其实就是下面的变形
/*
int fun ()
{
int a, b , c;
int num ;
switch()
{
}
return NUM ;
}
*/
int
j;
HMENU
hmenu ;
static
int
Select = ID_FIRST;
switch
(message)
{
case
WM_CREATE:
hInstance = ((LPCREATESTRUCT)lParam)->hInstance;
//hBm1 = LoadBitmap (hInstance, MAKEINTRESOURCE(1));
//hBm2 = LoadBitmap (hInstance, MAKEINTRESOURCE(2));
hBm3 = LoadBitmap (hInstance, MAKEINTRESOURCE(3));
//hBm4 = LoadBitmap (hInstance, MAKEINTRESOURCE(4));
//GetObject(hBm1,sizeof(BITMAP),&bitmap1);
//GetObject(hBm2,sizeof(BITMAP),&bitmap2);
GetObject(hBm3,
sizeof
(BITMAP),&bitmap3);
cxSource3 = bitmap3.bmWidth;
cySource3 = bitmap3.bmHeight/16;
for
(x = 0 ; x < CXNUM ; x ++)
for
(y = 0 ; y < CXNUM ; y ++)
{
minestate[x][y] = 0 ;
//初始化雷区状态
mineflag[x][y] = 1;
// 初始化标志
}
//随机布雷
srand
((unsigned)GetCurrentTime());
for
( j = 1; j <= NUM; )
{
cxmine =
abs
(
rand
()%(400));
cymine =
abs
(
rand
()%(400));
if
( cxmine < CXNUM && cymine < CYNUM && minestate[cxmine][cymine] != 5 )
{
minestate[cxmine][cymine] = 5 ;
//属性置雷
j++;
}
else
continue
;
}
//GetObject(hBm4,sizeof(BITMAP),&bitmap4);
return
0 ;
case
WM_PAINT:
hdc = BeginPaint (hwnd, &ps) ;
hdcmem3 = CreateCompatibleDC (hdc) ;
SelectObject (hdcmem3, hBm3) ;
//画初始化的雷和重画的雷
for
(x = 0 ; x < CXNUM ; x ++)
{
for
(y = 0 ; y < CYNUM ; y ++)
{
switch
(minestate[x][y])
{
case
0 :
StretchBlt (hdc, x*XUNIT, y*YUNIT, XUNIT, YUNIT, hdcmem3, 0, (mineflag[x][y] - 1)*cySource3,cxSource3, cySource3, MERGECOPY);
continue
;
case
2 :
case
4 :
case
3 :
case
6 :
case
7 :
case
8 :
case
9 :
case
10 :
case
11:
case
12:
case
13 :
case
14:
case
15:
StretchBlt (hdc, x*XUNIT, y*YUNIT, XUNIT, YUNIT, hdcmem3, 0, minestate[x][y] * cySource3,cxSource3, cySource3, MERGECOPY);
continue
;
case
5 :
switch
(Dead)
{
case
0 :
StretchBlt (hdc, x*XUNIT, y*YUNIT, XUNIT, YUNIT, hdcmem3, 0, (mineflag[x][y] - 1)*cySource3,cxSource3, cySource3, MERGECOPY);
continue
;
case
1 :
StretchBlt (hdc, x*XUNIT, y*YUNIT, XUNIT, YUNIT, hdcmem3, 0, 5*cySource3,cxSource3, cySource3, MERGECOPY);
continue
;
}
}
}
}
DeleteDC (hdcmem3) ;
EndPaint (hwnd, &ps) ;
return
0 ;
case
WM_LBUTTONDOWN:
x = LOWORD(lParam)/XUNIT;
y = HIWORD(lParam)/YUNIT;
//雷属性赋值
if
(Dead == 1)
break
;
//结束置dead为一
else
{
hdc = GetDC(hwnd);
hdcmem3 = CreateCompatibleDC (hdc) ;
SelectObject (hdcmem3, hBm3) ;
if
(mineflag[x][y] == 1)
{
switch
(minestate[x][y] )
{
case
0 :
if
(GetAroundNum (x, y) == 0 )
{
ExpandMines(x, y);
//周围无雷时扩展无雷区
}
else
{
minestate[x][y] = 15 - GetAroundNum (x, y);
}
break
;
case
5:
//失败的时候
Dead = 1;
minestate[x][y] = 4 ;
break
;
case
1:
case
2:
case
4:
case
3:
case
6:
case
7:
case
8:
case
9:
case
10:
case
11:
case
12:
case
13:
case
14:
case
15:
break
;
}
}
//画雷
for
(x = 0 ; x < CXNUM ; x ++)
{
for
(y = 0 ; y < CYNUM ; y ++)
{
switch
(minestate[x][y])
{
case
0 :
StretchBlt (hdc, x*XUNIT, y*YUNIT, XUNIT, YUNIT, hdcmem3, 0, (mineflag[x][y] - 1)*cySource3,cxSource3, cySource3, MERGECOPY);
continue
;
case
3 :
case
2 :
case
4 :
case
6 :
case
7 :
case
8 :
case
9 :
case
10 :
case
11:
case
12:
case
13 :
case
14:
case
15:
StretchBlt (hdc, x*XUNIT, y*YUNIT, XUNIT, YUNIT, hdcmem3, 0, minestate[x][y] * cySource3,cxSource3, cySource3, MERGECOPY);
continue
;
case
5 :
switch
(Dead)
{
case
0 :
StretchBlt (hdc, x*XUNIT, y*YUNIT, XUNIT, YUNIT, hdcmem3, 0, (mineflag[x][y] - 1) * cySource3,cxSource3, cySource3, MERGECOPY);
break
;
case
1:
if
(mineflag[x][y]== 1)
StretchBlt (hdc, x*XUNIT, y*YUNIT, XUNIT, YUNIT, hdcmem3, 0, 5 * cxSource3,cxSource3, cySource3, MERGECOPY);
else
StretchBlt (hdc, x*XUNIT, y*YUNIT, XUNIT, YUNIT, hdcmem3, 0, (mineflag[x][y] - 1) * cxSource3,cxSource3, cySource3, MERGECOPY);
break
;
return
0;
}
}
}
}
//此处必须这样,放外面会使重画时失效
DeleteDC (hdcmem3) ;
ReleaseDC(hwnd, hdc);
}
return
0;
case
WM_RBUTTONDOWN:
x = LOWORD(lParam)/XUNIT;
y = HIWORD(lParam)/YUNIT;
hdc = GetDC(hwnd);
hdcmem3 = CreateCompatibleDC (hdc) ;
SelectObject (hdcmem3, hBm3) ;
//标志赋值
if
((IsInMineArea(x, y) && minestate[x][y] == 0 || minestate[x][y] == 5 )&& Dead == 0)
{
mineflag[x][y] = mineflag[x][y] % 3 + 1;
//实现循环
StretchBlt (hdc, x*XUNIT, y*YUNIT, XUNIT, YUNIT, hdcmem3, 0, (mineflag[x][y] - 1)*cySource3,cxSource3, cySource3, MERGECOPY);
}
else
MessageBeep(0);
DeleteDC (hdcmem3) ;
ReleaseDC(hwnd, hdc);
return
0;
case
WM_COMMAND:
//command非commend
hmenu = GetMenu (hwnd);
switch
(LOWORD (wParam))
{
case
ID_FIRST:
//初级时
CheckMenuItem(hmenu,Select, MF_UNCHECKED);
Select = LOWORD(wParam);
CheckMenuItem(hmenu,Select, MF_CHECKED);
NUM = 60;
for
(x = 0 ; x < CXNUM ; x ++)
for
(y = 0 ; y < CXNUM ; y ++)
{
minestate[x][y] = 0 ;
//初始化雷区状态
mineflag[x][y] = 1;
}
//随机布雷
srand
((unsigned)GetCurrentTime());
for
( j = 1; j <= NUM; )
{
cxmine =
abs
(
rand
()%(400));
cymine =
abs
(
rand
()%(400));
if
( cxmine < CXNUM && cymine < CYNUM && minestate[cxmine][cymine] != 5 )
{
minestate[cxmine][cymine] = 5 ;
j++;
Dead = 0;
//重生
}
else
continue
;
}
InvalidateRect(hwnd, NULL,TRUE);
break
;
case
ID_SECOND:
//中级
CheckMenuItem(hmenu,Select, MF_UNCHECKED);
Select = LOWORD(wParam) ;
// 对于前后要用的值得一用
CheckMenuItem(hmenu, Select, MF_CHECKED);
NUM = 100;
for
(x = 0 ; x < CXNUM ; x ++)
for
(y = 0 ; y < CXNUM ; y ++)
{
minestate[x][y] = 0 ;
//初始化雷区状态
mineflag[x][y] = 1;
}
//随机布雷
srand
((unsigned)GetCurrentTime());
for
( j = 1; j <= NUM; )
{
cxmine =
abs
(
rand
()%(400));
cymine =
abs
(
rand
()%(400));
if
( cxmine < CXNUM && cymine < CYNUM && minestate[cxmine][cymine] != 5)
{
minestate[cxmine][cymine] = 5 ;
j++;
Dead = 0 ;
}
else
continue
;
}
InvalidateRect(hwnd, NULL,TRUE);
//重画
break
;
case
ID_MAX:
//高级
CheckMenuItem(hmenu,Select, MF_UNCHECKED);
Select = LOWORD(wParam) ;
// 对于前后要用的值得一用
CheckMenuItem(hmenu, Select, MF_CHECKED);
NUM = 140;
for
(x = 0 ; x < CXNUM ; x ++)
for
(y = 0 ; y < CXNUM ; y ++)
{
minestate[x][y] = 0 ;
//初始化雷区状态
mineflag[x][y] = 1;
}
//随机布雷
srand
((unsigned)GetCurrentTime());
for
( j = 1; j <= NUM; )
{
cxmine =
abs
(
rand
()%(400));
cymine =
abs
(
rand
()%(400));
if
( cxmine < CXNUM && cymine < CYNUM && minestate[cxmine][cymine] != 5 )
{
minestate[cxmine][cymine] = 5 ;
j++;
Dead = 0;
}
else
continue
;
}
InvalidateRect(hwnd, NULL,TRUE);
break
;
case
ID_EXIT:
//退出
SendMessage (hwnd, WM_CLOSE, 0, 0) ;
break
;
case
ID_START:
//重开始
for
(x = 0 ; x < CXNUM ; x ++)
for
(y = 0 ; y < CXNUM ; y ++)
{
minestate[x][y] = 0 ;
//初始化雷区状态
mineflag[x][y] = 1;
}
//随机布雷
srand
((unsigned)GetCurrentTime());
for
( j = 1; j <= NUM; )
{
cxmine =
abs
(
rand
()%(400));
cymine =
abs
(
rand
()%(400));
if
( cxmine < CXNUM && cymine < CYNUM && minestate[cxmine][cymine] != 5 )
{
minestate[cxmine][cymine] = 5 ;
j++;
Dead = 0;
}
else
continue
;
}
InvalidateRect(hwnd, NULL,TRUE);
break
;
}
return
0 ;
case
WM_DESTROY:
// DeleteObject(hBm1);
// DeleteObject(hBm2);
PostQuitMessage(0);
DeleteObject(hBm3);
// DeleteObject(hBm4);
return
0;
}
return
DefWindowProc(hwnd,message, wParam, lParam);
}
|
本文转自lilin9105 51CTO博客,原文链接:http://blog.51cto.com/7071976/1220400,如需转载请自行联系原作者