自制作简易扫雷 | 轻流扇
0%

自制作简易扫雷

基于Borland C3.1开发的简单扫雷

preface: 暑假让我弟学这个爆烂的C,让他自己学学C语言,编出个扫雷的基本功能就可以,没想到,编写了大概有一周的时间,才画好了界面?所以昨天一晚上,加上今天一上午,下午一点时间,便自己用爆烂的C (凭借着死去的C课设记忆)编出了一个堪称能玩的扫雷代码。当然,考虑到我弟的水平,只用了一个main.c文件,和祖传的鼠标库,就完成了这个简单的游戏。下面分享一下,当然功能还不够完善,而且思路不是特别好,(用的一维数组,大意了)。有很多可以简化改进之处,但是好歹能跑,也几乎没有任何的bug

游戏界面如下:

代码如下:(这里鼠标库就不放了,源码可以直接到C课设拯救计划里面找,还要下一个BC3.1,然后就能畅玩扫雷了!)

main.c

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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
#include<graphics.h>
#include<stdio.h>
#include<stdlib.h>
#include<bios.h>
#include<string.h>
#include<dos.h>
#include<math.h>
#include<time.h>
#include <conio.h>
#include"mouse.h"
void drawbackground(void);
void bomb(int bombxy[10]);
int checkbomb(int bombxy[10]);
void inputbox(int(*temp)[4]);
int checkifin(int now, int box[10]);
void showbomb(int out[10], int box[81][4]);
int showshuzi(int i, int out[10], int box[81][4]);
void setzhanai(int box[81][4], int cankao[81]);
void clearzero(int i, int out[10], int box[81][4]);//0
void judgeifwin(int box[81][4]);
void main(void)
{
int *a ;
int i,j;
int out[10];
int gd=VGA;//graphdriver
int gm=VGAHI;//graphmode
int box[81][4];//储存81个格子的坐标,左上和右下。
initgraph(&gd,&gm,"C:\\BORLANDC\\BGI");//这里后面的C:什么的看放的BGI文件路径
mouseinit();
*a = 1;
while (1)
{
int all;
int saveclick[81] = {-1};
int cankao[81] = { 0};

if (*a != 1)
{
break;
}
if (*a == 1)
{
drawbackground();
bomb(out);
inputbox(box);
setfillstyle(1, WHITE);
bar(420, 350,601,409);
*a = 2;
}
MouseX = 640;//防止鼠标留痕,挡住扫雷界面;因为不加就会
MouseY = 0;
while (1)
{
newmouse(&MouseX, &MouseY, &press);

setzhanai(box, cankao);
judgeifwin(box);
for (i = 0; i < 81; i++)
{

if (mouse_press(box[i][0], box[i][1], box[i][2], box[i][3]) == 1)
{
if (checkifin(i,out) == 0)//点击的区域不是雷
{
clrmous(MouseX, MouseY);
setfillstyle(1, LIGHTGRAY);
bar(box[i][0], box[i][1], box[i][2], box[i][3]);
clearzero(i, out, box);
}
else if (checkifin(i,out) == 1)//game over
{
showbomb(out, box);

outtextxy(420, 350, "GAME OVER");
while (1)
{
newmouse(&MouseX, &MouseY, &press);
if (mouse_press(418, 41, 600, 103) == 1)
{
break;//退出游戏
}
if (mouse_press(422, 237, 630, 297) == 1)
{
*a = 1;
clrmous(MouseX, MouseY);
break;//再来一局游戏
}
}
}
}
}



if (mouse_press(418, 41, 600, 103) == 1)
{
break;//退出游戏
}
if (mouse_press(422, 237, 630, 297) == 1)
{
*a=1;
clrmous(MouseX, MouseY);
break;//再来一局游戏
}
}
}
delay(100);//程序结束运行
closegraph();
}

void judgeifwin(int box[81][4])
{
int i;
int count = 0;
for (i = 0; i < 81; i++)
{
if (getpixel(box[i][0]+5, box[i][1]+5 ) == DARKGRAY)
{
count++;
}
}
if (count == 10)
{
outtextxy(420, 350, "YOU WIN!");
}
}

void clearzero(int i, int out[10], int box[81][4])//0
{
int judge;
int j;
int a[8];
a[0] = i - 10;
a[1] = i - 9;
a[2] = i - 8;
a[3] = i - 1;
a[4] = i + 1;
a[5] = i + 8;
a[6] = i + 9;
a[7] = i + 10;
judge = showshuzi(i, out, box);
if (judge == 0)
{
if (box[i][0] > 40 && box[i][1] > 120 + 40 && box[i][2] < 360 - 40 && box[i][3] < 480 - 40)//周围有八个格子
{
for (j = 0; j < 8; j++)
{
showshuzi(a[j], out, box);
}
}
else if (i == 0) // || i == 8 || i == 80 || i = -72)
{
showshuzi(a[4], out, box);
showshuzi(a[6], out, box);
showshuzi(a[7], out, box);
}
else if (i == 8) // || i == 8 || i == 80 || i = -72)
{
showshuzi(a[3], out, box);
showshuzi(a[5], out, box);
showshuzi(a[6], out, box);
}
else if (i == 72) // || i == 8 || i == 80 || i = -72)
{
showshuzi(a[1], out, box);
showshuzi(a[2], out, box);
showshuzi(a[4], out, box);
}
else if (i == 80) // || i == 8 || i == 80 || i = -72)
{
showshuzi(a[0], out, box);
showshuzi(a[1], out, box);
showshuzi(a[3], out, box);
}

else if (i > 0 && i < 9)
{
for (j = 3; j < 8; j++)
{
showshuzi(a[j], out, box);

}
}
else if (i > 72 && i < 81)
{
for (j = 0; j < 5; j++)
{
showshuzi(a[j], out, box);
}
}
else if ((i / 9) > 0 && (i / 9) < 8 && ((i % 9) == 0))
{
for (j = 0; j < 8; j++)
{
if (j == 0 || j == 3 || j == 5)
{
;
}
else
{
showshuzi(a[j], out, box);
}
}
}
else
{
for (j = 0; j < 8; j++)
{
if (j == 2 || j == 4 || j == 7)
{
;
}
else
{
showshuzi(a[j], out, box);
}
}
}
}
}


int showshuzi(int i,int out[10],int box[81][4])//显示第i-1个方块的数字
{
int ji;
int j;
int a[8];
char buffer[10];
ji = 0;
a[0] = i - 10;
a[1] = i - 9;
a[2] = i - 8;
a[3] = i - 1;
a[4] = i + 1;
a[5] = i + 8;
a[6] = i + 9;
a[7] = i + 10;
if (box[i][0] > 40 && box[i][1] > 120 + 40 && box[i][2] < 360 - 40 && box[i][3] < 480 - 40)//周围有八个格子
{
for (j = 0; j < 8; j++)
{
if (checkifin(a[j], out) == 1)
{
ji++;
}
}
}
else if (i == 0) // || i == 8 || i == 80 || i = -72)
{
if (checkifin(a[4], out) == 1)
{
ji++;
}
if (checkifin(a[6], out) == 1)
{
ji++;
}
if (checkifin(a[7], out) == 1)
{
ji++;
}
}
else if (i == 8) // || i == 8 || i == 80 || i = -72)
{
if (checkifin(a[3], out) == 1)
{
ji++;
}
if (checkifin(a[5], out) == 1)
{
ji++;
}
if (checkifin(a[6], out) == 1)
{
ji++;
}
}
else if (i == 72) // || i == 8 || i == 80 || i = -72)
{
if (checkifin(a[1], out) == 1)
{
ji++;
}
if (checkifin(a[2], out) == 1)
{
ji++;
}
if (checkifin(a[4], out) == 1)
{
ji++;
}
}
else if (i == 80) // || i == 8 || i == 80 || i = -72)
{
if (checkifin(a[1], out) == 1)
{
ji++;
}
if (checkifin(a[0], out) == 1)
{
ji++;
}
if (checkifin(a[3], out) == 1)
{
ji++;
}
}
else if (i > 0 && i < 9)
{
for (j = 3; j < 8; j++)
{
if (checkifin(a[j], out) == 1)
{
ji++;
}
}
}
else if (i > 72 && i < 81)
{
for (j = 0; j < 5; j++)
{
if (checkifin(a[j], out) == 1)
{
ji++;
}
}
}
else if ((i/9)>0&&(i/9)<8&&((i%9)==0))
{
for (j = 0; j < 8; j++)
{
if(j==0||j==3||j==5)
{
;
}
else if (checkifin(a[j], out) == 1)
{
ji++;
}
}
}
else
{
for (j = 0; j < 8; j++)
{
if (j == 2 || j == 4 || j == 7)
{
;
}
else if (checkifin(a[j], out) == 1)
{
ji++;
}
}
}

//ji代表周围有多少个雷;
itoa(ji, buffer, 10);
if (ji != 0)
{
setfillstyle(1, LIGHTGRAY);
clrmous(MouseX, MouseY);
bar(box[i][0], box[i][1], box[i][2], box[i][3]);

setcolor(BLUE);
outtextxy(box[i][0]+10, box[i][1]+10, buffer);
return 1;
}
else
{
setfillstyle(1, LIGHTGRAY);
clrmous(MouseX, MouseY);
bar(box[i][0], box[i][1], box[i][2], box[i][3]);
}


return 0;
}


int checkifin(int now, int box[10])
{
int i;
for (i = 0; i < 10; i++)
{
if (now == box[i])
{
return 1;
}
}
return 0;
}
void setzhanai(int box[81][4],int cankao[81])
{
int i;
for (i = 0; i < 81; i++)
{

if (mouse_press(box[i][0], box[i][1], box[i][2], box[i][3]) == 3)//右键点击
{
if (cankao[i] == 0&& (getpixel(box[i][0]+10, box[i][1]+10) == DARKGRAY))//
{
setfillstyle(1, GREEN);
clrmous(MouseX, MouseY);
bar(box[i][0]+10, box[i][1]+10, box[i][2]-10, box[i][3]-10);
cankao[i] = 1;
delay(200);
}
else if (cankao[i] == 1&&(getpixel(box[i][0]+10,box[i][1]+10) == DARKGRAY)|| (getpixel(box[i][0] + 10, box[i][1] + 10)==GREEN))
{

setfillstyle(1, DARKGRAY);
clrmous(MouseX, MouseY);
bar(box[i][0], box[i][1], box[i][2], box[i][3]);
cankao[i] = 0;
delay(200);
}

}

}
}

void showbomb(int out[10], int box[81][4])
{
int i;
for (i = 0; i < 10; i++)
{
setfillstyle(1, RED);
clrmous(MouseX, MouseY);
bar(box[out[i]][0], box[out[i]][1], box[out[i]][2], box[out[i]][3] );

}

}
void inputbox(int(*temp)[4])
{
int i, j;

for (j = 0; j < 9;j++)//i确定列数,j确定行数。
{
for (i = 0; i < 9; i++)
{
temp[i + 9 * j][0] = 1 + 40 * i;
temp[i + 9 * j][1] = 1 + 120 + 40 * j;
temp[i + 9 * j][2] = 40 + 40 * i - 1;
temp[i + 9 * j][3] = 40 + 120 + 40 * j - 1;;

}

}
}

void bomb(int bombxy[10])//利用返回指针,或者直接传入数组更好;
{

int i;
//总共分成81份;
srand(time(NULL));
for (i = 0; i < 10; i++)
{
bombxy[i] = rand() % 81 + 1;//生成1-81的随机数
}
checkbomb(bombxy);


}

int checkbomb(int bombxy[10])
{
int i, j;
for (i = 0; i < 10; i++)
{
for (j = i+1; j < 10; j++)
{
if (bombxy[i] == bombxy[j])
{
bombxy[i] = rand() % 81 + 1;
i = -1;//重新检查
break;
}

}

}
return 0;
}

void drawbackground(void)
{
int i;
setbkcolor(WHITE);
setcolor(GREEN);
settextstyle(1, HORIZ_DIR, 2);//HOR...表示横向显示,第二个1表是字体大小。1为小,0默认。2,3..大
outtextxy(432, 60, "exit the game");
outtextxy(437, 256, "restart the game");
rectangle(418, 41, 600, 103);
rectangle(422, 237, 630, 297);
//==============
setfillstyle(1, DARKGRAY);
bar(0, 120, 360, 480);
setfillstyle(1, YELLOW);
bar(0, 0, 360, 120);
setcolor(GREEN);
rectangle(25, 40, 100, 80);
rectangle(150, 40, 220, 80);
rectangle(345-75, 40, 345, 80);
setcolor(RED);
rectangle(0, 120, 360, 480);
for (i = 0; i < 10; i++)//画十次
{
line(0, 120 + 40 * i, 360, 120 + 40 * i);
}

for (i = 0; i < 10; i++)//画十次
{
line(40 * i, 120, 40 * i, 480);
}

}

**其实代码量也不多,有蛮多有点重复,工作量也就大概400行右吧。个人认为编程这种东西,我们不是主攻的(计算机专业啥的),刚开始学学思路就差不多。谁还死记函数什么啊?我编的时候都经常问gpt,或者借鉴之前写的代码。**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

就这样吧,是时候和祖传的鼠标库说再见了。愿世上再无C语言

​ ——8.1.2024

留下万分之一点,采得孤人所笑言