-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
754 lines (614 loc) · 14 KB
/
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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
#include "stdafx.h"
#include "surface.h"
extern surfacePage_t currentSurfacePage;
char *hashStatusToText[] = {
" ",
" 1 ",
" 2 ",
" 3 ",
" 4 ",
" 5 ",
" 6 ",
" 7 ",
" 8 ",
" ? ",
" X ",
" E "
};
posStatus_t hashUint32ToStatus[] = {
POS_EMPTY,
POS_1,
POS_2,
POS_3,
POS_4,
POS_5,
POS_6,
POS_UNKNOWN,
POS_MINE
};
mineMap_t *pMineMap = NULL;
score_t glScore = {0};
uint32_t mineNum = 0;
uint32_t glIsOutRoundCleaned = 0;
uint32_t initialize_score(score_t *pScore)
{
if (NULL == pScore)
{
exit(-1);
}
pScore -> clickCount = 0;
pScore -> beginTick64 = 0;
pScore -> endTick64 = 0;
}
/**
@fn 刷新棋盘
@return 始终返回1
**/
uint32_t update_mine_map()
{
// 清屏
system("cls");
const uint32_t width = pMineMap -> pMapRect -> width;
const uint32_t heigh = pMineMap -> pMapRect -> width;
const uint32_t col = width + 1;
const uint32_t row = heigh + 1;
for (uint32_t i = 0; i < width; i++)
{
printf("%3d", i + 1);
}
fputs("\n", stdout);
fputs("+", stdout);
for (uint32_t i = 0; i < width; i++)
{
fputs("---", stdout);
}
fputs("+\n", stdout);
for (uint32_t yPos = 1; yPos < col; yPos++)
{
fputs("|", stdout);
for (uint32_t xPos = 1; xPos < row; xPos++)
{
fputs(hashStatusToText[get_pos_status(pMineMap -> pMapRect, pMineMap -> pUserBoard, xPos, yPos)], stdout);
}
printf("|%d\n", yPos);
}
fputs("+", stdout);
for (uint32_t i = 0; i < width; i++)
{
fputs("---", stdout);
}
fputs("+\n", stdout);
fputs("这是MineBoard状态:\n", stdout);
fputs("+", stdout);
for (uint32_t i = 0; i < width; i++)
{
fputs("---", stdout);
}
fputs("+\n", stdout);
for (uint32_t yPos = 1; yPos < col; yPos++)
{
fputs("|", stdout);
for (uint32_t xPos= 1; xPos < row; xPos++)
{
fputs(hashStatusToText[get_pos_status(pMineMap -> pMapRect, pMineMap -> pMineBoard, xPos, yPos)], stdout);
}
printf("|%d\n", yPos);
}
fputs("+", stdout);
for (uint32_t i = 0; i < width; i++)
{
fputs("---", stdout);
}
fputs("+\n", stdout);
fputs("提示>11 4代表清理第11行第4列的位置,5 14则代表清理第5行第14列的位置\n", stdout);
printf("提示>本局雷数: %d枚\n", (0 == glScore.clickCount)?COL:mineNum);
printf("提示>当前耗时: %lld秒\n", (0 == glScore.clickCount)?0:(GetTickCount64() - (glScore.beginTick64)) / 1000 + 1);
printf("提示>当前点击次数: %d次\n", glScore.clickCount);
return 1;
}
/**
@fn 分配Board的内存并进行初始化
@param pRect 层的尺寸,如果传入空指针将会中断运行
#param defaultValue 用于填充层的值
@return 返回一个指向Board_t结构的指针,如果失败返回NULL
**/
uint32_t *create_board(const rect_t *pRect, const uint32_t defaultValue)
{
if (NULL == pRect)
{
exit(-1);
}
uint32_t width = pRect -> width;
uint32_t heigh = pRect -> heigh;
// 分配内存
uint32_t *pBoard;
pBoard = calloc(heigh * width, sizeof(uint32_t));
if(NULL == pBoard)
{
return NULL;
}
// 填充
for (uint32_t i = 0; i < width; i++)
{
for (uint32_t j = 0; j < heigh; j++)
{
(pBoard + (width * i))[j] = defaultValue;
}
}
return pBoard;
}
/**
@fn 分配棋盘结构的内存
@param pRect 指定棋盘结构的形状,传入空指针将中断运行
@return 返回一个指向mineMap_t结构的指针,如果失败返回NULL
**/
mineMap_t *create_map(const rect_t *pRect)
{
mineMap_t *pMineMap = NULL;
// 分配MineMap内存
pMineMap = malloc(sizeof(mineMap_t));
if(NULL == pMineMap)
{
// MineMap内存分配失败
exit(-1);
}
// 分配Rect内存
pMineMap -> pMapRect = malloc(sizeof(rect_t));
if(NULL == pMineMap -> pMapRect)
{
// Rect内存分配失败
return NULL;
}
// 生成pRect指向矩形的副本
memcpy(pMineMap -> pMapRect, pRect, sizeof(rect_t));
pMineMap -> pMineBoard = create_board(pMineMap -> pMapRect, POS_EMPTY);
if(NULL == pMineMap -> pMineBoard)
{
return NULL;
}
pMineMap -> pUserBoard = create_board(pMineMap -> pMapRect, POS_UNKNOWN);
if (NULL == pMineMap -> pUserBoard)
{
return NULL;
}
return pMineMap;
}
/**
@fn 随机埋雷
@return 函数始终返回1
**/
uint32_t bury_mine(const rect_t *const pRect, uint32_t *const pBoard, const uint32_t xClickPos, const uint32_t yClickPos)
{
// 检查指针合法性
if(NULL == pRect || NULL == pBoard)
{
exit(-1);
}
uint32_t width = pRect -> width;
uint32_t heigh = pRect -> heigh;
// 设置随机数种子
srand((unsigned int)time(NULL));
#ifdef NO_REGULARLY_RANDOM
struct {
uint32_t xPos;
uint32_t yPos;
} minePosArray[MINE_NUM] = { 0 };
for (uint32_t i = 0; i < MINE_NUM; i ++)
{
uint32_t res = 0;
while (res)
{
uint32_t xPos = (rand() % width) + 1;
uint32_t yPos = (rand() % heigh) + 1;
if(xPos == xClickPos && yPos == yClickPos)
{
// 与用户点击位置重复,重新生成
continue;
}
uint32_t isRepeat = 0;
for (uint32_t j = 0; j < i; j++)
{
if (xPos == minePosArray[j].xPos || yPos == minePosArray[j].yPos)
{
isRepeat = !isRepeat;
break;
}
}
if (!isRepeat)
{
set_pos_status(pRect, pBoard, xPos, yPos, POS_MINE);
mineNum += 1;
res = !res;
}
}
}
#else
for (uint32_t xPos = 1; xPos < width + 1; xPos++)
{
// i * sizeof(uint32_t) 代表访问pArray这个二维数组的第i行
// rand() % (pRect -> width + 1) 代表的则是二维数组的随机列
// 所以这句话的意思是访问第i行的随机一个列并赋值
uint32_t yPos = (rand() % heigh) + 1;
// 如果用户点击的坐标与生成的地雷的坐标相同则重新生成地雷的位置
if (xClickPos == xPos)
{
while (yClickPos == yPos)
{
yPos = (rand() % heigh) + 1;
}
}
set_pos_status(pRect, pBoard, xPos, yPos, POS_MINE);
mineNum += 1;
}
#endif
return 1;
}
/**
@fn 初始化用户层(O(n^2), n = 16 * 16)
**/
uint32_t initialize_user_board(const mineMap_t *pMineMap)
{
if (NULL == pMineMap)
{
exit(-1);
}
uint32_t mineNum = 0;
for (uint32_t xPos = pMineMap -> pMapRect -> width; xPos > 0; xPos--)
{
for (uint32_t yPos = pMineMap -> pMapRect -> heigh; yPos > 0; yPos--)
{
if(get_pos_status(pMineMap -> pMapRect, pMineMap -> pMineBoard, xPos, yPos) != POS_MINE)
{
mineNum = get_around_mine_num(pMineMap, xPos, yPos);
set_pos_status(pMineMap -> pMapRect, pMineMap -> pMineBoard, xPos, yPos, hashUint32ToStatus[mineNum]);
}
}
}
return 1;
}
/**
@fn 判断坐标是否越界
**/
int32_t is_out_of_range(const rect_t *pRect, uint32_t xPos, uint32_t yPos)
{
if (NULL == pRect)
{
exit(-1);
}
if (xPos < 1 || xPos > pRect -> width || yPos < 1 || yPos > pRect -> heigh)
{
// 坐标越界
return 1;
}
return 0;
}
uint32_t set_pos_status(const rect_t *const pRect, uint32_t *const pBoard, const uint32_t xPos, const uint32_t yPos, const posStatus_t posStatus)
{
if (NULL == pRect || NULL == pBoard)
{
exit(-1);
}
if (is_out_of_range(pRect, xPos, yPos))
{
// 坐标越界
exit(-1);
}
(pBoard + (yPos - 1) * (pRect -> width))[xPos - 1] = posStatus;
return 1;
}
posStatus_t get_pos_status(const rect_t *pRect, const uint32_t *const pBoard, const uint32_t xPos, const uint32_t yPos)
{
if (NULL == pRect || NULL == pBoard)
{
exit(-1);
}
if (is_out_of_range(pRect, xPos, yPos))
{
// 坐标越界
#ifdef _DEBUG
printf("[警告]越界的坐标无法获取状态\n");
#endif
return POS_INVALID;
}
return (pBoard + (yPos - 1) * (pRect -> width))[xPos - 1];
}
uint32_t get_around_mine_num(const mineMap_t *const pMineMap, const uint32_t xCenterPos, const uint32_t yCenterPos)
{
if (NULL == pMineMap)
{
return -1;
}
uint32_t count = 0;
uint32_t posList[8][2];
posList[0][0] = xCenterPos - 1;
posList[0][1] = yCenterPos - 1;
posList[1][0] = xCenterPos - 1;
posList[1][1] = yCenterPos;
posList[2][0] = xCenterPos - 1;
posList[2][1] = yCenterPos + 1;
posList[3][0] = xCenterPos;
posList[3][1] = yCenterPos - 1;
posList[4][0] = xCenterPos;
posList[4][1] = yCenterPos + 1;
posList[5][0] = xCenterPos + 1;
posList[5][1] = yCenterPos - 1;
posList[6][0] = xCenterPos + 1;
posList[6][1] = yCenterPos;
posList[7][0] = xCenterPos + 1;
posList[7][1] = yCenterPos + 1;
uint32_t mineNumCount = 0;
for (uint32_t i = 0; i < 8; i++)
{
if(POS_MINE == get_pos_status(pMineMap -> pMapRect, pMineMap -> pMineBoard, posList[i][0], posList[i][1]))
{
mineNumCount += 1;
}
}
return mineNumCount;
}
uint32_t clean_mine(const mineMap_t *pMineMap, uint32_t xPos, uint32_t yPos)
{
if (NULL == pMineMap)
{
exit(-1);
}
if (POS_MINE == get_pos_status(pMineMap -> pMapRect, pMineMap -> pMineBoard, xPos, yPos))
{
// 踩到雷了(悲
return 0;
}
#ifdef CLEAN_OUT_ROUND
if (!glIsOutRoundCleaned)
{
clean_out_round(pMineMap);
}
#endif
clean_empty(pMineMap, xPos, yPos, DIRECTION_RIGHT | DIRECTION_UP | DIRECTION_LEFT | DIRECTION_DOWN);
return 1;
}
inline void clean_pos(const mineMap_t* const pMineMap, const uint32_t xPos, const uint32_t yPos)
{
posStatus_t posStatus = get_pos_status(pMineMap->pMapRect, pMineMap->pMineBoard, xPos, yPos);
if (posStatus != POS_MINE)
{
if (POS_UNKNOWN == get_pos_status(pMineMap->pMapRect, pMineMap->pUserBoard, xPos, yPos))
{
set_pos_status(pMineMap->pMapRect, pMineMap->pUserBoard, xPos, yPos, posStatus);
}
}
}
uint32_t clean_out_round(const mineMap_t* const pMineMap)
{
if (NULL == pMineMap)
{
exit(-1);
}
const uint32_t width = pMineMap -> pMapRect -> width;
const uint32_t heigh = pMineMap -> pMapRect -> width;
uint32_t x = 1;
uint32_t y = 1;
for (; x < width; x++)
{
clean_pos(pMineMap, x, y);
}
for (; y < heigh; y++)
{
clean_pos(pMineMap, x, y);
}
for (; x > 1; x--)
{
clean_pos(pMineMap, x, y);
}
for (; y > 1; y--)
{
clean_pos(pMineMap, x, y);
}
glIsOutRoundCleaned = 1;
return 1;
}
uint32_t clean_empty(const mineMap_t *const pMineMap, const uint32_t xPos, const uint32_t yPos, const direction_t direction)
{
if (is_out_of_range(pMineMap -> pMapRect, xPos, yPos))
{
// 触碰到边界后停止递归
return 1;
}
if (POS_UNKNOWN != get_pos_status(pMineMap -> pMapRect, pMineMap -> pUserBoard, xPos, yPos))
{
// 触碰到已经清扫过的鸽子
return 1;
}
posStatus_t posStatus = get_pos_status(pMineMap -> pMapRect, pMineMap -> pMineBoard, xPos, yPos);
// 将mineBoard层指定坐标显示到userBoard层
set_pos_status(pMineMap -> pMapRect, pMineMap -> pUserBoard, xPos, yPos, posStatus);
if (posStatus != POS_EMPTY)
{
// 触碰到附近有雷的格子后停止递归
return 1;
}
if (direction & DIRECTION_RIGHT)
{
clean_empty(pMineMap, xPos + 1, yPos, DIRECTION_RIGHT);
clean_empty(pMineMap, xPos, yPos - 1, DIRECTION_UP);
clean_empty(pMineMap, xPos, yPos + 1, DIRECTION_DOWN);
}
if (direction & DIRECTION_LEFT)
{
clean_empty(pMineMap, xPos - 1, yPos, DIRECTION_LEFT);
clean_empty(pMineMap, xPos, yPos - 1, DIRECTION_UP);
clean_empty(pMineMap, xPos, yPos + 1, DIRECTION_DOWN);
}
if (direction & DIRECTION_UP)
{
clean_empty(pMineMap, xPos, yPos - 1, DIRECTION_UP);
clean_empty(pMineMap, xPos - 1, yPos, DIRECTION_LEFT);
clean_empty(pMineMap, xPos + 1, yPos, DIRECTION_RIGHT);
}
if (direction & DIRECTION_DOWN)
{
clean_empty(pMineMap, xPos, yPos + 1, DIRECTION_DOWN);
clean_empty(pMineMap, xPos - 1, yPos, DIRECTION_LEFT);
clean_empty(pMineMap, xPos + 1, yPos, DIRECTION_RIGHT);
}
return 1;
}
/**
@fn 初始化一些资源
@return 始终返回1
**/
int initialize_resource()
{
rect_t boardRect = {COL, ROW};
pMineMap = create_map(&boardRect);
if (NULL == pMineMap)
{
pause_and_exit("[错误]内存初始化失败\n", -1);
}
return 1;
}
/**
@fn 检查是否清理完所有地雷
@return 如果地雷清理干净返回1,否则返回0
**/
uint32_t is_mine_all_cleaned(const mineMap_t *const pMineMap)
{
const uint32_t width = pMineMap -> pMapRect -> width;
const uint32_t heigh = pMineMap -> pMapRect -> heigh;
uint32_t unknownNum = 0;
for (uint32_t yPos = 1; yPos <= heigh; yPos++)
{
for (uint32_t xPos = 1; xPos <= width; xPos++)
{
if (POS_UNKNOWN == get_pos_status(pMineMap -> pMapRect, pMineMap -> pUserBoard, xPos, yPos))
{
/*
if(POS_MINE != get_pos_status(pMineMap -> pMapRect, pMineMap -> pMineBoard, xPos, yPos))
{
return 0;
}
*/
unknownNum += 1;
if (unknownNum > mineNum)
{
return 0;
}
}
}
}
return 1;
}
/**
@fn 游戏主体循环
@return 如果游戏通关返回1,
中途失败则返回0
**/
uint32_t game_process()
{
uint32_t xPos, yPos = 0;
update_mine_map();
// 获取玩家的点击,并据此随机埋雷
while (!get_click(&xPos, &yPos, &glScore))
{
show_illegal_input();
update_mine_map();
}
bury_mine(pMineMap -> pMapRect, pMineMap -> pMineBoard, xPos, yPos);
update_mine_map();
// 在随机埋雷后初始化雷周围的数字
initialize_user_board(pMineMap);
// 扫雷
uint32_t isFailed = clean_mine(pMineMap, xPos, yPos);
// 刷新棋盘
update_mine_map();
uint32_t isAllCleaned = is_mine_all_cleaned(pMineMap);
glScore.beginTick64 = GetTickCount64();
while (!isAllCleaned)
{
if (get_click(&xPos, &yPos, &glScore))
{
if (is_out_of_range(pMineMap -> pMapRect, xPos, yPos))
{
show_invalid_click();
}
else if (POS_UNKNOWN != get_pos_status(pMineMap -> pMapRect, pMineMap -> pUserBoard, xPos, yPos))
{
show_already_cleaned();
}
else
{
isFailed = clean_mine(pMineMap, xPos, yPos);
if (0 == isFailed)
{
return 0;
}
isAllCleaned = is_mine_all_cleaned(pMineMap);
}
}
else
{
// 刷新棋盘
show_illegal_input();
}
// 刷新棋盘
update_mine_map();
}
glScore.endTick64 = GetTickCount64();
// 通关
// 地雷全部清理完毕,游戏结束
return 1;
}
uint32_t is_invalid_choice(menuChoice_t choice)
{
return choice == CHOICE_INVALID;
}
menuChoice_t get_choice_until_no_invalid(const surfacePage_t page)
{
menuChoice_t choice = get_choice();
while (is_invalid_choice(choice))
{
show_illegal_input();
show_page(page);
choice = get_choice();
}
return choice;
}
/**
@fn 入口函数
**/
int main(int argc, char **argv)
{
show_first_play_menu();
//menuChoice_t choice = get_choice();
menuChoice_t choice = get_choice_until_no_invalid(currentSurfacePage);
while (choice != CHOICE_EXIT)
{
switch(choice)
{
case CHOICE_FIRST_PLAY:
case CHOICE_NEXT_PLAY:
initialize_resource();
initialize_score(&glScore);
if (game_process())
{
show_win(&glScore);
}
else
{
show_gameover(&glScore);
}
break;
/*
case CHOICE_INVALID:
show_illegal_input();
break;
default:
show_illegal_input();
break;
*/
}
show_next_play_menu();
choice = get_choice_until_no_invalid(currentSurfacePage);
}
show_bye();
return 0;
}